Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS: Sound generation on iPad given Hz parameter?

Tags:

ios

ipad

audio

Is there an API in one of the iOS layers that I can use to generate a tone by just specifying its Hertz. What I´m looking to do is generate a DTMF tone. This link explains how DTMF tones consists of 2 tones:

http://en.wikipedia.org/wiki/Telephone_keypad

Which basically means that I should need playback of 2 tones at the same time...

So, does something like this exist:

SomeCleverPlayerAPI(697, 1336);

If spent the whole morning searching for this, and have found a number of ways to playback a sound file, but nothing on how to generate a specific tone. Does anyone know, please...

like image 713
ILM Avatar asked Feb 23 '11 11:02

ILM


2 Answers

Check out the AU (AudioUnit) API. It's pretty low-level, but it can do what you want. A good intro (that probably already gives you what you need) can be found here:

http://cocoawithlove.com/2010/10/ios-tone-generator-introduction-to.html

like image 150
elsurudo Avatar answered Oct 22 '22 01:10

elsurudo


There is no iOS API to do this audio synthesis for you.

But you can use the Audio Queue or Audio Unit RemoteIO APIs to play raw audio samples, generate an array of samples of 2 sine waves summed (say 44100 samples for 1 seconds worth), and then copy the results in the audio callback (1024 samples, or whatever the callback requests, at a time).

See Apple's aurioTouch and SpeakHere sample apps for how to use these audio APIs.

The samples can be generated by something as simple as:

sample[i] = (short int)(v1*sinf(2.0*pi*i*f1/sr) + v2*sinf(2.0*pi*i*f2/sr));

where sr is the sample rate, f1 and f1 are the 2 frequencies, and v1 + v2 sum to less than 32767.0. You can add rounding or noise dithering to this for cleaner results.

Beware of clicking if your generated waveforms don't taper to zero at the ends.

like image 38
hotpaw2 Avatar answered Oct 22 '22 00:10

hotpaw2