Jump to content
  • Advertisement
Sign in to follow this  
squicklid

Outputting a frequency from c++

This topic is 5006 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!