Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Subtract two audio channels - How?

  1. #1
    Junior Member
    Join Date
    Aug 2017

    Subtract two audio channels - How?

    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 function. Im getting the error "error: invalid conversion from 'byte* {aka unsigned char*}' to 'int16_t {aka short int}' [-fpermissive];"

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

    Would anyone be able to help?

    Click image for larger version. 

Name:	Capture.PNG 
Views:	23 
Size:	10.2 KB 
ID:	13962

    #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
      // Initialize the SD card
      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");
    void loop() {
      if (playSdWav1.isPlaying() == false) {
        Serial.println("Start playing 1");"SDTEST1.WAV");
        delay(10); // wait for library to parse WAV info
      if (millis() > (1*60000) && mode == 1) {
      else {
        if (mode == 1) continueRecording();
    void startRecording() {
        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);
        int b = 0;
        for (int i = 0; i < 256; i= i+1) {
          buffer[i] = bufferL[i] - bufferR[i]  ;
    void stopRecording() {
      // flush buffer
      while (queue1.available() > 0 && queue3.available() > 0) {
      mode = 4;

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    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.

    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.

  3. #3
    Junior Member
    Join Date
    Aug 2017
    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?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts