i wish to generate two opposing tone one in the right stereo channel and one in the left stereo channel both at different frequencies i wish to accomplish this in c or objective c
views:
449answers:
4You could use the beep function to generate tones but I am not sure how you would have one tone come from one channel and the other from another. The only thing I could think of is using two different threads each one calling the beep function but both tones would come out of both channels. Good Luck!
You can easily generate the tone with a sine wave: the nth sample is given by A*sin(2*pi*n*f/R)
, where A is the amplitude (volume), f
is the frequency in Hertz, and R
is the sample rate in samples per second. For stereo sound, you generate two independent waves with different parameters.
The hard part is actually playing the sound. You'll have to use some sort of platform-specific audio library, or a third-party library that abstracts away the platform-specific details. Alternatively, you could just write out the sound data to a WAV file, which is not terribly difficult, since WAV files are just raw, uncompressed PCM data with a RIFF header.
The DefaultOutputUnit sample code
/Developer/Examples/CoreAudio/SimpleSDK/DefaultOutputUnit
shows how to play a sine wave, and it would be relatively trivial to adapt it to play two different sine waves on the left and right speakers.