Game Development Reference
In-Depth Information
The constructor sets the sound data parameters and pre-allocates the buffer space:
public:
ToneGenerator()
{
FBufferUsed = 100000;
FBuffer.resize( 100000 );
FChannels = 2;
FSamplesPerSec = 4100;
FBitsPerSample = 16;
FAmplitude = 350.0f;
FFrequency = 440.0f;
}
virtual ~ToneGenerator() {}
The main routine of this class calculates the sine function, keeping track of the current
sample index to make the queue of sound buffers contain all the values:
virtual int StreamWaveData( int Size )
{
if ( Size > static_cast<int>( FBuffer.size() ) )
{
FBuffer.resize( Size );
LastOffset = 0;
}
for ( int i = 0 ; i < Size / 4 ; i++ )
{
The argument t for the sine function is calculated from the local index i and the phase value
named LastOffset :
float t = ( 2.0f * 3.141592654f *
FFrequency * ( i + LastOffset ) ) /
(float) FSamplesPerSec;
float val = FAmplitude * std::sin( t );
The following lines convert a single loating-point value to a signed word. Such conversion is
necessary because the digital audio hardware only works with integer data:
short V = static_cast<short>( val );
FBuffer[i * 4 + 0] = V & 0xFF;
FBuffer[i * 4 + 1] = V >> 8;
FBuffer[i * 4 + 2] = V & 0xFF;
FBuffer[i * 4 + 3] = V >> 8;
}
 
Search WWH ::




Custom Search