HELP!! Producing a Sinewave
I need some really basic help creating an output of a given sinewave to the speakers using C++. Every avenue and API I explore I seem to hit a brick wall.
Trebs
Does this mean you don''t know how to generate a sinusoidal waveform, or that you don''t know how to implement audio functionality with an API?
Generating a sin wave is fairly simple; there is some sample code at www.harmonycentral.com, or simply do a search for sine generator. There are lots of clever and computationally efficient ways of doing this, but I''d leave them well alone for the time being.
Digital audio works on the principal of Pulse Code Modulation (PCM). Each sample ( being a discreet slice of audio data ) will have a single value that represents the amplitude of the signal at that point. Imagine the waveview in SoundForge, or any such wav editing app. Zooming right in, each individual sample will have a height. This is the amplitude at that point in time. Digital audio can therefore be seen as an array of values.
For reasons of efficiency, any periodic waveform ( as a sinewave is ) only needs a single cycle to be stored; this can be repeated to produce a sustained tone.
Okay, now, we have the array of samples ( called a Wavetable ), now we need to play it back. A good test to see if you grasp this principal is to find the .wav format, and try to store your wavetable in a wav file.
I''m no expert with any particular audio API; but I hear that Fmod is very good for playing back audio date. However, I guess that you would rather learn to do it by hand. The basic procedure under DirectSound is to ;
a ) Get the DirectSound object. You''ll want it exclusively if you want to change the sample / bit rate.
b ) Create the primary buffer, setting the wave format to
something nice n sensible like 16-bit 44100Hz.
c ) Create a looping secondary buffer.
d ) Start the secondary buffer
e ) Poll the playback position of the buffer; When a certain position is reached, copy your wavetable into the secondary buffer.
f ) goto e
This is vastly simplified, and there are a lot better ways of reading playback positions than polling ( although it tends to be the favourite method for synth coders. ) The DirectSound docs are really rather good, I''d recommend going through the accompanying tutorials.
Generating a sin wave is fairly simple; there is some sample code at www.harmonycentral.com, or simply do a search for sine generator. There are lots of clever and computationally efficient ways of doing this, but I''d leave them well alone for the time being.
Digital audio works on the principal of Pulse Code Modulation (PCM). Each sample ( being a discreet slice of audio data ) will have a single value that represents the amplitude of the signal at that point. Imagine the waveview in SoundForge, or any such wav editing app. Zooming right in, each individual sample will have a height. This is the amplitude at that point in time. Digital audio can therefore be seen as an array of values.
For reasons of efficiency, any periodic waveform ( as a sinewave is ) only needs a single cycle to be stored; this can be repeated to produce a sustained tone.
Okay, now, we have the array of samples ( called a Wavetable ), now we need to play it back. A good test to see if you grasp this principal is to find the .wav format, and try to store your wavetable in a wav file.
I''m no expert with any particular audio API; but I hear that Fmod is very good for playing back audio date. However, I guess that you would rather learn to do it by hand. The basic procedure under DirectSound is to ;
a ) Get the DirectSound object. You''ll want it exclusively if you want to change the sample / bit rate.
b ) Create the primary buffer, setting the wave format to
something nice n sensible like 16-bit 44100Hz.
c ) Create a looping secondary buffer.
d ) Start the secondary buffer
e ) Poll the playback position of the buffer; When a certain position is reached, copy your wavetable into the secondary buffer.
f ) goto e
This is vastly simplified, and there are a lot better ways of reading playback positions than polling ( although it tends to be the favourite method for synth coders. ) The DirectSound docs are really rather good, I''d recommend going through the accompanying tutorials.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement