Generating a simple tone (sound) within an app

Discussion in 'iOS Programming' started by B74A, Apr 2, 2009.

  1. B74A macrumors member

    Mar 31, 2008
    (also posted on the new Apple Dev Beta forums, but no reply there)

    I need to generate a tone in an app (or maybe several, at the same time), where the frequency and duration is controlled directly from within the app. In other words, it is not a recorded sound file, I want to generate a file, say 2000 Hz, 1 second duration.

    I searched for this but cannot find it. Can anyone please direct me - or does it really not exist?

    Maybe someone can also tell me how many tones are possible at the same time (polyphonic). One is good, but up to 6 would be great!

    Thank you so much
  2. Boomhowler macrumors 6502

    Feb 23, 2008

    if you have access to some math library you could use some sort of
    sine function like out = amplitude * sine(2000*2*pi*t) where t is the time in seconds. By simply adding several sines together you can generate how many tones you want, processorpower is the limiting factor. Just make sure that the total sum of tones do not get over 1 or you might get digital clipping.
  3. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    More efficient if you precalculate a sine table, and interpolate it at your desired frequency. Use Audio Queues as in the SpeakHere example code, but fill the audio callback buffers with samples from your mixer output instead of from a file. I've gotten over 20 voices this way, with room to spare for more.
  4. B74A thread starter macrumors member

    Mar 31, 2008
    Can it really be so complicated?

    I have used basic, comal and pascal back in the 80's ... and each had some kind of simple function like

    sound(frequency,duration,bla bla);

    Just one simple call to make one simple sound. Computers like VIC 20, Commodore 64, ZX81 ... they all had this!

    There must be some kind of function for this, it is such a basic requirement to make a sound. How about the "play keyboard" apps and so on, how do THEY make sounds?
  5. fishkorp macrumors 68020


    Apr 10, 2006
    Ellicott City, MD
    Those apps can be 10's or 100's of MBs in size, they generally have every single note as an individual sound file included in the app bundle. Try to download one over EDGE or 3G and you'll probably get an error saying it's too big and you need WiFi. Guitarist is almost 57MB in size. Pianist is 7MB in size.
  6. johnnybluejeans macrumors 6502

    Jan 16, 2006
    New York, NY
    Hahahaha, VIC 20! I feel like you had to use POKE statements on the VIC to generate tones... Wish I could find the old programming guide that came with mine right about now. Would be good for a laugh. ; )
  7. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    In the 70s and 80s, all we had was tone generators. Some machines, like Commodore's, even had a special chip to handle this task. Remember ADSR? But you couldn't play an audio file as they are today. As technology progressed to allow such playback, tone generators got left by the wayside. I don't think you'll find anything in the SDK that handles such a task. Something that seems so simple is actually very hard. Good luck, though! :)
  8. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    Welcome to the world of multi-threaded programming, OO UI frameworks and programmers who usually need something more far flexible, capable, non-blocking, better sounding, and media synchronizable (etc.) than beep(pitch).

    The VIC 20 sound call blocked, only output square waves, and couldn't do a dozen+ simultaneous voices of realistic wavetable synthesis (etc.) synchronized with OpenGL 3D animation, with an mp3 playing in the background.

  9. B74A thread starter macrumors member

    Mar 31, 2008
    Thank you for your answers.

    Let me be more specific, although I will hint quite a bit of my idea to an app I will make.

    I need to vary this tone, not just make a constant one. I need tones to sync with some function of a program, so you don't have to look at the screen but can hear the sound instead. Imagine the possibilities in an app, when not just the screen is giving user feedback.

    So, instead of a "meter" or a "graph" I need a "tone" where I will vary the pitch and the length (beeps and pause, a bit morse code kind-of) to give feedback to the user.
  10. kanumber macrumors newbie

    Apr 29, 2009
    sound gen


    I am looking for the same thing...

    I saw some guys pulling off live sound gen from accelerometer data here:

    I can't seem to find any coding / docs in regards to how they are doing this. I believe 3.0 has a framework or API to handle such a task. Perhaps I should have listened to the video more closely... They could have said something about it there but I was too in awe of the sounds being generated!


  11. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    I'll repeat the earlier suggestion. Just fill audio buffers with synthesized samples:

    audioBuffer = amplitude * sin(i * 2.0 * pi * frequency / sampleRate )

    There are far more efficient ways of doing the above, depending on your math and programming skills. With standard synthesis techniques (lots of them well described on the web), you can get over a dozen simultaneous voices with independently controllable frequencies.

    You can initialize and get given the buffers using either the Audio Queues or Audio Unit RemoteIO API, of which there are several examples on the web, including on Apple's dev site.

    Be careful with the details: sample rate, endianess, bits per sample, peak amplitudes/clipping, bytes per sample and per frame. Don't forget to save the phase index between buffers, or you will get discontinuities.

    If the above is obscure, you might need to spend a couple hours in the library looking at the first chapters of some DSP textbooks.

    BTW: There are several accelerometer driven Theremin apps already in the App store. A bunch of Morse Code apps have also appeared in the last few months.


Share This Page