How to adjust audio panning with RtAudio

非 Y 不嫁゛ 提交于 2019-12-01 09:08:08

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;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!