3

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?

Artjom B.
  • 61,146
  • 24
  • 125
  • 222

1 Answers1

0

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;
}
Bjorn Roche
  • 11,279
  • 6
  • 36
  • 58
  • Actually, that is not what I need. If you start from the duplex mode example, I have one audio *outputBuffer, in the function inout. How can I modify that buffer to get panning? I mean a simple 2D audio panning – user1801724 Nov 07 '12 at 01:04
  • I added some sample code, not sure if it's right, but it will give you the idea. It might be easier to use floating point data which I think RtAudio supports. Also, FYI, most people say 2D panning, so there's no confusion, but it's really 1D. – Bjorn Roche Nov 07 '12 at 15:04