C Programming Column
by Al Stevens


Listing One
for (int i = 0; i < numSamples; i+= 2)  {
    short int s = *(buffer+i);
    *(buffer+i) -= *(buffer+i+1);
    *(buffer+i+1) -= s;
}

Listing Two
for (int i = 0; i < numSamples; i+= 2)  {
    short int s = *(buffer+i);
    *(buffer+i) -= *(buffer+i+1);
    *(buffer+i+1) -= s;
    *(buffer+i+1) *= -1;
}

Listing Thre
// percentage of pan expressed as an int (e.g. 10 = 10%) increase left side 
// by pan, decrease right side by pan pan is positive for rightward panning,
// and negative for leftward panning
  for (int i = 0; i < numSamples; i+= 2)  {
    int lf = *(buffer+i);
    int rt = *(buffer+i+1);
    lf = (lf * (100+pan))/100;
    rt = (rt * (100-pan))/100;
    *(buffer+i) = min(max((rt-lf), -32768), 32768);
    *(buffer+i+1) = (min(max((lf-rt), -32768), 32768)) * -1;
  }

Listing Four 
for (int i = 0; i < numSamples; i+= 2)  {
    int lf1 = *(buf1+i);
    int rt1 = *(buf1+i+1);
    int lf2 = *(buf2+i);
    int rt2 = *(buf2+i+1);
    // do the channel elimination
    // ...
    // mix left and right channels
    int mixleft  = lf1 + lf2;
    int mixright = rt1 + rt2;
    // build the audio waveform with a center channel
    const int balance = 4;  // experiment for best balance
    *(audiobuf+i) = mixleft + mixright / balance;
    *(audiobuf+i+1) = mixright + mixleft / balance;
}



1

