PDA

View Full Version : Playing a tone in C++ for Mac OS




MacProg
Jun 17, 2013, 06:17 PM
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.



ArtOfWarfare
Jun 17, 2013, 07:02 PM
Mac OS hasn't been updated in over a decade. Are you referring to OS X?

MacProg
Jun 17, 2013, 07:30 PM
Mac OS hasn't been updated in over a decade. Are you referring to OS X?

Yes, sorry. I meant OS X Lion using Xcode.

subsonix
Jun 17, 2013, 07:53 PM
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
Jun 17, 2013, 08:18 PM
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
Jun 17, 2013, 08:56 PM
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
Jun 17, 2013, 09:13 PM
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:

printf("%c", 7);

:D

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

firewood
Jun 19, 2013, 03:12 PM
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
Jun 19, 2013, 03:22 PM
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.

MacProg
Jun 19, 2013, 05:54 PM
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
Jun 19, 2013, 06:43 PM
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-Audio-Hands-On-Programming/dp/0321636848/ref=sr_1_1?ie=UTF8&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.