Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MacProg

macrumors newbie
Original poster
Jun 17, 2013
4
0
Apologies if this is an obvious question, but I'm a new C++ programmer for Mac OS and I'm trying to figure out a simple way to play a tone of a set frequency. Any help would be greatly appreciated! Many thanks in advance.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
If you want to create the tone your self you need to use Core Audio. You have to create a function that generates the tone at a given frequency then provide that function as a render callback to an output AudioUnit.

Core Audio is a low level C framework that provides pro level performance on OS X. You may be able to find some 3rd party framework wrapped around Core Audio that let's you do this at a higher level if you search for it.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,764
8,463
A sea of green
Apologies if this is an obvious question, but I'm a new C++ programmer for Mac OS ...

New to C++, or new to Mac OS X, or both?

New to programming in general? I.e. is C++ on Mac OS X your first programming language? If not, what other languages do you know?

and I'm trying to figure out a simple way to play a tone of a set frequency.

http://www.mikeash.com/getting_answers.html

What have you tried? Be specific.

If you used a search engine, what did you search for? What did you find?

When I google this: os x c++ audio library, I see the two top results are irrKlang and PortAudio.

If you're learning from a book, online tutorial, or video tutorial, which one? Title, author, and edition for a book; URL(s) for online resources.
 

MacProg

macrumors newbie
Original poster
Jun 17, 2013
4
0
New to C++, or new to Mac OS X, or both?

New to programming in general? I.e. is C++ on Mac OS X your first programming language? If not, what other languages do you know?



http://www.mikeash.com/getting_answers.html

What have you tried? Be specific.

If you used a search engine, what did you search for? What did you find?

When I google this: os x c++ audio library, I see the two top results are irrKlang and PortAudio.

If you're learning from a book, online tutorial, or video tutorial, which one? Title, author, and edition for a book; URL(s) for online resources.

Used to program in C++ but it has been many years, trying to relearn. Actually using C++ for dummies. Totally new to OS X. Just looking for a simple way to make a tone- on windows would use the beep(freq,duration) command but googled extensively and couldn't find a mac equivalent.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Just looking for a simple way to make a tone- on windows would use the beep(freq,duration) command but googled extensively and couldn't find a mac equivalent.

A bit pedantic, but in that case you don't make the tone, someone else made it, you just play it. ;)

As far as I know there's not anything equivalent readily available, the closest is probably:

Code:
printf("%c", 7);

:D

But you may be able to find something similar in a library if you use your favorite search engine.
 

firewood

macrumors G3
Jul 29, 2003
8,113
1,353
Silicon Valley
Tons of ways in Mac OS X. Audio Queue API, Audio Unit API, CoreMIDI, 3rd party libs such as portaudio, build a WAV file and sent it to QuickTime, some sound or music apps (Garage Band?) might be AppleScript aware, output a ctrl-G to the Terminal app, etc.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
Audio Queue API, Audio Unit API, CoreMIDI

Non of them will actually let you create a tone by them selves the way the OP requested though. Only the Audio Unit API is related, and it's part of Core Audio.

Edit: to clarify, something similar to: beep(freq,duration), although IMO the use of such a function is very limited. Core Audio on the other hand is very powerful.
 
Last edited:

MacProg

macrumors newbie
Original poster
Jun 17, 2013
4
0
Non of them will actually let you create a tone by them selves the way the OP requested though. Only the Audio Unit API is related, and it's part of Core Audio.

Edit: to clarify, something similar to: beep(freq,duration), although IMO the use of such a function is very limited. Core Audio on the other hand is very powerful.

OK, I looked up Core Audio on the apple developers sight and I see that it in theory gives you access to systems sounds, which is good, but in practice the rest of it was way over my head. Could someone give me some tips on how you would actually code something to make a tone of a given frequency using this? Many thanks in advance for any help.
 

subsonix

macrumors 68040
Feb 2, 2008
3,551
79
OK, I looked up Core Audio on the apple developers sight and I see that it in theory gives you access to systems sounds, which is good, but in practice the rest of it was way over my head. Could someone give me some tips on how you would actually code something to make a tone of a given frequency using this? Many thanks in advance for any help.

If you are interested in Core Audio I recommend that you get the book Learning Core Audio. http://www.amazon.com/Learning-Core...TF8&qid=1371684699&sr=8-1&keywords=core+audio

But as I mentioned previously it's a low level framework, you really build things from atoms, but as a consequence of that it gives you a lot of control and offers world class performance, like low latency out of the box unlike any other platform.

It's built around a pull model, so the previous example I gave you would create a waveform yourself with for example the sin() function and fill a buffer. You then provide that functions as a render callback to an output AudioUnit that calls you function when it needs more data to play. You can connect AudioUnits with a graph, the API for that is called AUGraph which is part of the AudioToolbox framework, there are many different AudioUnits to choose from. Typically you will not hear anything from your speakers before you have written 2 pages of code.

For playing, mixing and recording sound files there is a higher level framework called AVFoundation you can also check out.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.