Outputting a frequency from c++
I want to make a multitrack mixer, or at least attempt. And therefore, I must know how to output a frequency of a specified duration. Does anyone know how to do this?
Or, should I just mix it into a single file, and then use sndPlaySound or something.
PS: The world is a dangerous place and I am a dangerous monkey.
November 30, 2004 05:59 PM
Look into the sound functions w/winapi, or directsound might help you a bit. Both can accomplish basic sound tasks. You might have to get a bit better apis if you want to do anything really advanced, about which I have no clue, I don't work with sound much.
When you play sounds, you don't "play a frequency of a specified duration". You generate sample data, and you push that data at the sample card, at whatever your sampling rate is.
If you're on Windows, it's probably easiest to start with DirectSound. Create a streaming 2D secondary DirectSound buffer, fill it with zeros, start it playing, and then repeatedly poll the play position, lock the area between where you previously wrote and the new play position, and fill in that data. Unlock, repeat.
A good site for sound synthesis is music-dsp.org.
On Linux, use /dev/dsp instead of DirectSound. Or use something like OpenAL that serves as a portability layer, although the support for streaming (generated) audio is not always great (depending on which portability API you use).
If you're on Windows, it's probably easiest to start with DirectSound. Create a streaming 2D secondary DirectSound buffer, fill it with zeros, start it playing, and then repeatedly poll the play position, lock the area between where you previously wrote and the new play position, and fill in that data. Unlock, repeat.
A good site for sound synthesis is music-dsp.org.
On Linux, use /dev/dsp instead of DirectSound. Or use something like OpenAL that serves as a portability layer, although the support for streaming (generated) audio is not always great (depending on which portability API you use).
enum Bool { True, False, FileNotFound };
Here's my crack at C++
// generate sample data for wave output at given frequency
// for one second, but can be looped. Assumes 16-bit samples
long numberOfSamples;
long counter;
long sampleRate = 44100;
float frequency = 440.0;
int myBuffer[44100];
numberOfSamples = sampleRate;
for(counter = 0;counter<numberOfSamples;counter++) {
myBuffer[counter]=(short)(32768*sin(frequency*2*3.14*counter/sampleRate));
}
// generate sample data for wave output at given frequency
// for one second, but can be looped. Assumes 16-bit samples
long numberOfSamples;
long counter;
long sampleRate = 44100;
float frequency = 440.0;
int myBuffer[44100];
numberOfSamples = sampleRate;
for(counter = 0;counter<numberOfSamples;counter++) {
myBuffer[counter]=(short)(32768*sin(frequency*2*3.14*counter/sampleRate));
}
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement