I use Rtaudio library and I would like to implement an audio program where I can control the panning (e.g. shifting the sound from the left channel to the right channel).
In my specific case, I use a duplex mode (you can find an example here: duplex mode). It means that I link the microphone input to the speaker output.
Should I apply a filter on the output buffer? What kind of filter?
Can anyone help me?
To reduce the signal on the left, simply multiply every sample on the left by a number less than or equal to 1, let's call it l. Similarly for the right, we'll call that number r. Generally speaking you don't want to multiply by a number greater than 1, or you might cause the signal to distort.
l and r are both functions of the "pan position". How you get from pan position to your numbers is a matter of some discussion. If this is for something simple, you can just ramp the values down linearly, using these values at the extremes:
Hard Left:
l=1; r=0
Center:
l=1; r=1
Hard Right:
l=0; r=1;
If this is something fancier, you should google for "pan law". Here's an example that looks like a good start:
http://www.kvraudio.com/forum/viewtopic.php?p=4264576
UPDATE: I've never used RT audio (I usually use PortAudio, which is similar), but I think code to pan would look something like this, with l and r as defined above (assuming a type int32_t - signed 32 bit integer):
int inout( void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status, void *data )
{
if ( status ) std::cout << "Stream over/underflow detected." << std::endl;
int32_t *ob = (int32_t *)outputBuffer;
int32_t *in = (int32_t *)inputBuffer;
unsigned long *bytes = (unsigned long *) data;
int i =0;
while( i < bytes / 4 ) {
ob[i] = (int32_t) ( ib[i] * l + .5 );
++i;
ob[i] = (int32_t) ( ib[i] * r + .5 );
++i;
}
return 0;
}