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

B74A

macrumors member
Original poster
Mar 31, 2008
96
3
Asia and Europe
(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
 
sine

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.
 
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.
 
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?
 
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?
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.
 
Just one simple call to make one simple sound. Computers like VIC 20, Commodore 64, ZX81 ... they all had this!

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. ; )
 
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! :)
 
Can it really be so complicated?

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.

ymmv.
 
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.
 
sound gen

b74a,

I am looking for the same thing...

I saw some guys pulling off live sound gen from accelerometer data here:
http://www.creativeapplications.net/2009/01/13/10-creative-ways-to-use-the-accelerometer-iphone/

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!

Luke

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.
 
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.

y.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.