Subtract two audio channels - How?

Status
Not open for further replies.
Hello guys. I'm trying to build a code to subtract de 2 the left and right channels of a stereo recording from the SD card and output them though the headphones.

Im trying to use the queue class but im having problems with the queue.play() function. Im getting the error "error: invalid conversion from 'byte* {aka unsigned char*}' to 'int16_t {aka short int}' [-fpermissive]

queue2.play(buffer);"


I have tried to change the data types but it didin't work as well.



Would anyone be able to help?

Capture.PNG




Code:
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>


// GUItool: begin automatically generated code
AudioPlaySdWav           playSdWav1;     //xy=286,151
AudioRecordQueue         queue3;         //xy=504,198
AudioRecordQueue         queue1;         //xy=506,140
AudioPlayQueue           queue2;         //xy=651,179
AudioPlayQueue           queue4;         //xy=652,226
AudioOutputI2S           i2s1;           //xy=793,192
AudioConnection          patchCord1(playSdWav1, 0, queue1, 0);
AudioConnection          patchCord2(playSdWav1, 1, queue3, 0);
AudioConnection          patchCord3(queue2, 0, i2s1, 0);
AudioConnection          patchCord4(queue4, 0, i2s1, 1);
AudioControlSGTL5000     sgtl5000_1;     //xy=416,382
// GUItool: end automatically generated code


// Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN    BUILTIN_SDCARD // 254?
#define SDCARD_MOSI_PIN  11  // not actually used
#define SDCARD_SCK_PIN   13  // not actually used

// Remember which mode we're doing
int mode = 0;  // 0=stopped, 1=recording, 2=playing


void setup() {
  // record queue uses this memory to buffer incoming audio.
  AudioMemory(120); // 60

  // Enable the audio shield, select input, and enable output
  sgtl5000_1.enable();
  sgtl5000_1.volume(0.5);



  // Initialize the SD card
  SPI.setMOSI(SDCARD_MOSI_PIN);
  SPI.setSCK(SDCARD_SCK_PIN);
  if (!(SD.begin(SDCARD_CS_PIN))) {
    // stop here if no SD card, but print a message
    while (1) {
      Serial.println("Unable to access the SD card");
      delay(500);
    }
  }

  startRecording();
}


void loop() {

  if (playSdWav1.isPlaying() == false) {
    Serial.println("Start playing 1");
    playSdWav1.play("SDTEST1.WAV");
    delay(10); // wait for library to parse WAV info
  }

  if (millis() > (1*60000) && mode == 1) {
    stopRecording();
  }
  else {
    if (mode == 1) continueRecording();
  }
}







void startRecording() {
 
    queue1.begin();
    queue3.begin();
    mode = 1;
 

}

void continueRecording() {
  
  if (queue1.available() >= 2 && queue3.available() >= 2) {
    byte buffer[256];
    byte bufferL[256];
    byte bufferR[256];
    memcpy(bufferL, queue1.readBuffer(), 256);
    memcpy(bufferR, queue3.readBuffer(), 256);
    queue1.freeBuffer();
    queue3.freeBuffer();
    int b = 0;

    
    for (int i = 0; i < 256; i= i+1) {
      buffer[i] = bufferL[i] - bufferR[i]  ;
      
    }
  
     
     queue2.play(buffer);
     queue4.play(buffer);
     
     //queue2.getBuffer();
     //queue4.getBuffer();
     //queue2.playBuffer();
     //queue4.playBuffer();
 
     
    
  }
}

void stopRecording() {
  Serial.println("StopRecording");
  queue1.end();
  queue3.end();
  // flush buffer
  while (queue1.available() > 0 && queue3.available() > 0) {
    queue1.readBuffer();
    queue1.freeBuffer();
    queue3.readBuffer();
    queue3.freeBuffer();
  }
  mode = 4;
}
 
You can use negative gain on the mixer channels. Just set the gain to -1.0 on the channel you want to subtract, and sit back in comfort while the audio lib mixer does all the work.

For more complex stereo processing, the midside object might be useful.

https://www.pjrc.com/teensy/gui/?info=AudioEffectMidSide

Again, best to let the library do the work. Grabbing the raw samples with the queue objects can be done, but it's much harder and puts the burden of keeping pace with 44100 samples/sec onto your code. The audio lib has lots of very useful features. Best to try using them before rolling your own.
 
Thank you Paul. And if I need to convolute the resulting signal with a FIR filter that I created (512 taps), how could I do it?
 
Status
Not open for further replies.
Back
Top