views:

63

answers:

1

Hi!

I want to develop an application for the iPhone that generates custom DTMF tones. I assume the pure tone will be played from a memory buffer and not from an uploaded file, and I think I should use AVAudioPlayer class and the method "initWithData:error:", but how do I implement it? Whatever I do, I can't make it work!

I saw this code in the forum that is needed in order to calculate the sinus wave:

const int PLAYBACKFREQ = 44100;
const float PI2 = 3.14159265359f * 2;

void generateDTMF(short *buffer, int length, float freq1, float freq2) {
  int i;
  short *dest = buffer;
  for(i=0; i<length; i++) *(dest++) = (sin(i*(PI2*(PLAYBACKFREQ/freq1))) + sin(i(PI2(PLAYBACKFREQ/freq2)))) * 16383;
}

But where should I place this code? How does it help me to fill a designated memory buffer?

Any kind of help would be appreciated!

Sagiftw

+1  A: 

The data which you pass to the AVAudioPlayer instance method initWithData:error: must have a valid audio file header. You can use a WAVE file header, AIFF file header, or a CAF file header. Since your data format is fixed, you can just create a static header for the duration of the audio you're generating.

The alternative is to go lower level and use AudioQueue to directly play audio buffers. This might end up being easier depending on how much control you want over playback, or if you want playback to happen indefinitely.

lucius