I am working on a Port Audio application and having some issue understanding the writedata command which is a blocking I/O method.
I want to output a sine wave for 2 seconds on my 2 channel headphones, so I create a buffer with sine wave values. Below is snippet of my code:
#define NUM_SECONDS (2)
#define SAMPLE_RATE (44100)
#define FRAMES_PER_BUFFER (256)
#define INTERVAL (NUM_SECONDS*SAMPLE_RATE/FRAMES_PER_BUFFER)
float writeData[INTERVAL][FRAMES_PER_BUFFER]; // bufferfor storing sine values
// store sine values in the buffer
for(j=0;j<INTERVAL;j++)
{
//I am creating an interleaved array as this is a stereo output
for( i=0; i<FRAMES_PER_BUFFER/2; i++ )
{
i *= 2;
//left channel
writeData[j][i] = (float) sin( ((double)i/((double)FRAMES_PER_BUFFER)/2) * M_PI * 2. );
k=i+1;
//write left channel value to right channel
writeData[j][k] = writeData[j][i];
}
}
// code to put sine values to writestream
for( i=0; i<INTERVAL; i++ ){
err=Pa_WriteStream(stream,writeData[i],FRAMES_PER_BUFFER);
}
The above code works fine and it plays the sound for 2 seconds on my output device on both the channels. My question is the number of samples that send to writestream = NUM_SECONDS * SAMPLE_RATE = 88200. but these are for 2 channels, so 44100 for 1 channel. Shouldn't the sound just play for 1 sec and not 2 secs?
Aucun commentaire:
Enregistrer un commentaire