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

Thread: When playing multiple wav files it have distortion when overlapping files! teensy 3.6

  1. #1
    Junior Member
    Join Date
    Mar 2018
    Posts
    8

    When playing multiple wav files it have distortion when overlapping files! teensy 3.6

    I have teensy 3.6.
    When i play file and start new one when the first still playing it have distortion at the beginning , what do i have to check to solve this issue?
    thank you.
    Code:
    #include <Audio.h>
    #include <Wire.h>
    #include <SD.h>
    #include <Time.h>
    #include <TimeLib.h>
    #include <TimeAlarms.h>
    
    int sensoreCount = 24;
    
    int boardActive = 1;//Will toggle Board Activation
    #define SDCARD_CS_PIN    BUILTIN_SDCARD
    
    const int knockSensors [24] = {A0, A1, A2, A3, A4, A5, A10, A11, A8, A9, A6, A7, A12, A13, A14, A15, A16, A17, A18, A19, A20, A21, A23, A24};
    const long soundInterval = 300;
    unsigned long currentTiming = millis();
    
    AudioPlaySdWav     Sound[12];
    AudioMixer4              mixer3;
    AudioMixer4              mixer2;
    AudioMixer4              mixer1;
    AudioMixer4              mixer4;
    AudioOutputAnalogStereo  dacs1;
    AudioConnection          patchCord1(Sound[0], 0, mixer1, 0);
    AudioConnection          patchCord2(Sound[1], 0, mixer1, 1);
    AudioConnection          patchCord3(Sound[2], 0, mixer1, 2);
    AudioConnection          patchCord5(Sound[3], 0, mixer1, 3);
    
    AudioConnection          patchCord4(Sound[4], 0, mixer2, 0);
    AudioConnection          patchCord6(Sound[5], 0, mixer2, 1);
    AudioConnection          patchCord7(Sound[6], 0, mixer2, 2);
    AudioConnection          patchCord8(Sound[7], 0, mixer2, 3);
    
    AudioConnection          patchCord9(Sound[8], 0, mixer3, 0);
    AudioConnection          patchCord10(Sound[9], 0, mixer3, 1);
    AudioConnection          patchCord11(Sound[10], 0, mixer3, 2);
    AudioConnection          patchCord12(Sound[11], 0, mixer3, 3);
    
    AudioConnection          patchCord13(mixer3, 0, mixer4, 0);
    AudioConnection          patchCord14(mixer2, 0, mixer4, 1);
    AudioConnection          patchCord15(mixer1, 0, mixer4, 2);
    AudioConnection          patchCord16(mixer4, 0, dacs1, 0);
    AudioConnection          patchCord17(mixer4, 0, dacs1, 1);
    
    
    
    int knockState [24] = { 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 , 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 };
    int sensorReading[24];
    unsigned long timing[24] = { 0 ,   0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 , 0 ,  0 ,  0 , 0 , 0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 ,  0 };
    int sensetivity = 500 ;
    int instrument = 1;
    
    void setup() {
    
      Serial.begin(9600);
      //Set New Instrument
      pinMode(2, INPUT_PULLUP);
      pinMode(3, INPUT_PULLUP);
      pinMode(4, INPUT_PULLUP);
      pinMode(13, OUTPUT);
    
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          Alarm.delay(500);
        }
      }
    
      AudioMemory(25);
      for (int Vol = 0 ; Vol < 4; Vol++) {
        mixer1.gain(Vol, 0.1);
        mixer2.gain(Vol, 0.1);
        mixer3.gain(Vol, 0.1);
        mixer4.gain(Vol, 0.1);
      }
      for (int i = 0; i <= 10; i++) {
        playMusic(i);
        Alarm.delay(500);
      }
      delay(500);
    //increase Volume
      for (int Vol = 0 ; Vol < 4; Vol++) {
        mixer1.gain(Vol, 0.4);
        mixer2.gain(Vol, 0.4);
        mixer3.gain(Vol, 0.4);
        mixer4.gain(Vol, 0.4);
      }
        
      Serial.println("Finish Set UP");
      Alarm.delay(1500);
    }
    
    void playMusic(int note) {
      if (!boardActive) return;
      String directory = "drum";
      char HoldName[15];
      char fullName[15];
      Serial.println(instrument);
      switch (instrument) {
        case 1: directory = "piano";  break;
        case 2: directory = "drum";  break;
        case 3: directory = "droid";  break;
        default: directory = "piano";  break;
      }
      char filename[15] = "piano/1.wav";
      int currentNote = note + 1;
      sprintf(filename, "/%i.wav", currentNote);
      directory.toCharArray(HoldName, 15);
      sprintf(fullName, "%s%s", HoldName, filename);
      Serial.println(fullName);
      Serial.println(note);
      AudioNoInterrupts();
      if (note <= 11)
        Sound[note].play(fullName);
      else
        Sound[note - 12].play(fullName);
      Serial.print("Audio Memory : ");  
      Serial.println(AudioMemoryUsageMax());  
      AudioInterrupts();  
    }
    
    void loop() {
      Alarm.delay(1);
      int dr2 = 0;
      int dr3 = 0;
      int dr4 = 0;
      currentTiming = millis();
      for (int count = 0; count <= sensoreCount - 1 ; count++) {
        sensorReading[count] = analogRead(knockSensors[count]);
        if ((sensorReading[count] > sensetivity) && (knockState[count] == 0) && (currentTiming - timing[count] >= soundInterval)) {
          timing[count] = currentTiming;
          knockState[count] = 1;
          playMusic(count);
          delay(300);
        }
        else {
          if (sensorReading[count] < sensetivity) knockState[count] = 0;
          if (currentTiming < timing[count]) timing[count] = 0;
        }
      }
      dr2 = digitalRead(2);
      dr3 = digitalRead(3);
      dr4 = digitalRead(4);
      if (dr2 == LOW) instrument = 1;
      else if (dr3 == LOW) instrument = 2;
      else if (dr4 == LOW) instrument = 3;
    }

  2. #2
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Any suggestions?

  3. #3
    Senior Member
    Join Date
    Nov 2012
    Posts
    1,766
    Code:
        mixer1.gain(Vol, 0.4);
        mixer2.gain(Vol, 0.4);
        mixer3.gain(Vol, 0.4);
        mixer4.gain(Vol, 0.4);
    I think the sum of the gains should be no more than 1.
    From the notes on the mixer in the audio designer:
    Signal clipping can occur when any channel has gain greater than 1.0, or when multiple signals add together to greater than 1.0.
    Try reducing all the 0.4 to 0.2 or, at most 0.25.

    Pete

  4. #4
    Junior Member
    Join Date
    Mar 2018
    Posts
    8
    Will give it a try ():
    thank you

  5. #5
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    25,221
    Looks like you have 12 possible sound sources. If their combined levels are more than 1.0 amplitude, you get clipping.

    For more info about clipping, check out part 2-2 in the tutorial, starting on page 12 of the PDF:

    https://www.pjrc.com/store/audio_tutorial_kit.html

    Or if you'd prefer to watch, it starts at 9:06 in the tutorial walkthrough video.


Tags for this Thread

Posting Permissions

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