Playing a tone in C++ for Mac OS

Discussion in 'Mac Programming' started by MacProg, Jun 17, 2013.

  1. MacProg macrumors newbie

    Jun 17, 2013
    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.
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    Mac OS hasn't been updated in over a decade. Are you referring to OS X?
  3. MacProg thread starter macrumors newbie

    Jun 17, 2013
    Yes, sorry. I meant OS X Lion using Xcode.
  4. subsonix macrumors 68040

    Feb 2, 2008
    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.
  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    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?

    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.
  6. MacProg thread starter macrumors newbie

    Jun 17, 2013
    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.
  7. subsonix macrumors 68040

    Feb 2, 2008
    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);

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

    Jul 29, 2003
    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.
  9. subsonix, Jun 19, 2013
    Last edited: Jun 19, 2013

    subsonix macrumors 68040

    Feb 2, 2008
    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.
  10. MacProg thread starter macrumors newbie

    Jun 17, 2013
    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.
  11. subsonix macrumors 68040

    Feb 2, 2008
    If you are interested in Core Audio I recommend that you get the book Learning 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.

Share This Page