Sign in to follow this  
squicklid

Outputting a frequency from c++

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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));

}

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this