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

Thread: Polyphonic playSdRaw

  1. #1
    Junior Member
    Join Date
    Mar 2019
    Posts
    17

    Polyphonic playSdRaw

    Hi forum,

    I'm working with a Capacitive touch keyboard that triggers ".raw" samples from the SD card (using T3.2 +audioShield).

    I wish to have the keyboard as polyphonic as possible (i.e play at least 4 files at once).


    here's the sketch :

    Code:
    
    
    #include <Bounce.h>
    #include "pitches.h"
    
    
    ///////CAP TOUCH///////////////////
    #include <Wire.h>
    #include "Adafruit_MPR121.h"
    #ifndef _BV
    #define _BV(bit) (1 << (bit)) 
    #endif
    
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioPlaySdRaw           playSdRaw1;     //xy=185,103
    AudioPlaySdRaw           playSdRaw2;     //xy=185,145
    AudioPlaySdRaw           playSdRaw3;     //xy=190,197
    AudioPlaySdRaw           playSdRaw4;     //xy=211,244
    AudioMixer4              mixer1;         //xy=476,179
    AudioOutputI2S           i2s1;           //xy=747,191
    AudioConnection          patchCord1(playSdRaw1, 0, mixer1, 0);
    AudioConnection          patchCord2(playSdRaw2, 0, mixer1, 1);
    AudioConnection          patchCord3(playSdRaw3, 0, mixer1, 2);
    AudioConnection          patchCord4(playSdRaw4, 0, mixer1, 3);
    AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=592,392
    // GUItool: end automatically generated code
    
    
    
    
    
    
    //CAP TOUCH Vars//
    Adafruit_MPR121 cap = Adafruit_MPR121();
    uint16_t lasttouched = 0;
    uint16_t currtouched = 0;
    
    int SdRawMode = 0;
    ///////////////////NOTES ARRAY/////////////////////////////
    char* notes[] = {
      "C4.Raw", "D4.Raw", "G4.Raw","A4.Raw", "C5.Raw", "E4.Raw", "F4.Raw", "B4.Raw"
    };
    
    void setup() {
      Serial.begin(9600);
      
      ///////CAP TOUCH STUFF////////////////////////
      // Default address is 0x5A, if tied to 3.3V its 0x5B
      // If tied to SDA its 0x5C and if SCL then 0x5D
      if (!cap.begin(0x5A)) {
    //    Serial.println("MPR121 not found, check wiring?");
        while (1);
      }
    //  Serial.println("MPR121 found!");
    
      AudioMemory(10);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
      
      mixer1.gain(0, 0.6);
      mixer1.gain(1, 0.6);
      mixer1.gain(2, 0.6);
      mixer1.gain(3, 0.6);
    
    
    SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
      delay(1000);
    }
    
    
    void loop() {
    
    //////////////////CAPTOUCH KEYBOARD PLAY////////////////////////
    currtouched = cap.touched();
    
     for (uint8_t i=0; i<8; i++) {
      
    //if key "i" is pressed - play notes[i]
     if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
         Serial.println(SdRawMode);
          if (SdRawMode == 0){
            playSdRaw1.play(notes[i]);
            } else if (SdRawMode == 1){
              playSdRaw2.play(notes[i]);
             } else if (SdRawMode == 2){
              playSdRaw3.play(notes[i]);
              } else if (SdRawMode == 3){
              playSdRaw4.play(notes[i]);
              } 
    
            ///Toggle modes to play the next "playSdRaw" object to make polyphonic 
            SdRawMode ++; 
            
        }
     }
    
     // return to "playSdRaw1"
        if (SdRawMode > 3){
          SdRawMode = 0;
          }
          
      lasttouched = currtouched;
    
    
    }
    My problem is that as soon as I play a bit faster, it either not-play every third key or so, or that it breaks down and gets stuck on static noise (then I have to reboot).

    When I make it monophonic, the keyboard works perfectly.

    Here's the monophonic sketch:
    Code:
    
    
    #include <Bounce.h>
    #include "pitches.h"
    
    
    ///////CAP TOUCH///////////////////
    #include <Wire.h>
    #include "Adafruit_MPR121.h"
    #ifndef _BV
    #define _BV(bit) (1 << (bit)) 
    #endif
    
    #define SDCARD_CS_PIN    10
    #define SDCARD_MOSI_PIN  7
    #define SDCARD_SCK_PIN   14
    
    #include <Audio.h>
    #include <Wire.h>
    #include <SPI.h>
    #include <SD.h>
    #include <SerialFlash.h>
    
    // GUItool: begin automatically generated code
    AudioPlaySdRaw           playSdRaw1;     //xy=185,103
    AudioPlaySdRaw           playSdRaw2;     //xy=185,145
    AudioPlaySdRaw           playSdRaw3;     //xy=190,197
    AudioPlaySdRaw           playSdRaw4;     //xy=211,244
    AudioMixer4              mixer1;         //xy=476,179
    AudioOutputI2S           i2s1;           //xy=747,191
    AudioConnection          patchCord1(playSdRaw1, 0, mixer1, 0);
    AudioConnection          patchCord2(playSdRaw2, 0, mixer1, 1);
    AudioConnection          patchCord3(playSdRaw3, 0, mixer1, 2);
    AudioConnection          patchCord4(playSdRaw4, 0, mixer1, 3);
    AudioConnection          patchCord5(mixer1, 0, i2s1, 0);
    AudioConnection          patchCord6(mixer1, 0, i2s1, 1);
    AudioControlSGTL5000     sgtl5000_1;     //xy=592,392
    // GUItool: end automatically generated code
    
    
    
    
    
    
    //CAP TOUCH Vars//
    Adafruit_MPR121 cap = Adafruit_MPR121();
    uint16_t lasttouched = 0;
    uint16_t currtouched = 0;
    
    int SdRawMode = 0;
    ///////////////////NOTES ARRAY/////////////////////////////
    char* notes[] = {
      "C4.Raw", "D4.Raw", "G4.Raw","A4.Raw", "C5.Raw", "E4.Raw", "F4.Raw", "B4.Raw"
    };
    
    void setup() {
      Serial.begin(9600);
    
    ///////CAP TOUCH STUFF////////////////////////
      // Default address is 0x5A, if tied to 3.3V its 0x5B
      // If tied to SDA its 0x5C and if SCL then 0x5D
      if (!cap.begin(0x5A)) {
    //    Serial.println("MPR121 not found, check wiring?");
        while (1);
      }
    //  Serial.println("MPR121 found!");
    ////////////////////////////////////////////////
      AudioMemory(10);
      sgtl5000_1.enable();
      sgtl5000_1.volume(0.5);
    
      mixer1.gain(0, 0.6);
      mixer1.gain(1, 0.6);
      mixer1.gain(2, 0.6);
      mixer1.gain(3, 0.6);
    
    
    SPI.setMOSI(SDCARD_MOSI_PIN);
      SPI.setSCK(SDCARD_SCK_PIN);
      if (!(SD.begin(SDCARD_CS_PIN))) {
        while (1) {
          Serial.println("Unable to access the SD card");
          delay(500);
        }
      }
      delay(1000);
    }
    
    
    void loop() {
    
    //////////////////CAPTOUCH KEYBOARD PLAY////////////////////////
    currtouched = cap.touched();
    
     for (uint8_t i=0; i<8; i++) {
      
    //if key "i" is pressed - play notes[i]
     if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
         Serial.println(SdRawMode);
            playSdRaw1.play(notes[i]);
            
        }
     } 
       lasttouched = currtouched;
    
    
    }
    Here are my error messages :
    Code:
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
     };
     ^
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    Looper_sampleTest_SD_new_doup_poly:56: warning: ISO C++ forbids converting a string constant to 'char*' 
    /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.cpp: In member function 'boolean Adafruit_MPR121::begin(uint8_t)':
    /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.cpp:67:3: warning: 'void Adafruit_MPR121::setThreshholds(uint8_t, uint8_t)' is deprecated [-Wdeprecated-declarations]
       setThreshholds(12, 6);
       ^
    In file included from /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.cpp:29:0:
    /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.h:101:8: note: declared here
       void setThreshholds(uint8_t touch, uint8_t release) __attribute__((deprecated));
            ^
    /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.cpp:67:23: warning: 'void Adafruit_MPR121::setThreshholds(uint8_t, uint8_t)' is deprecated [-Wdeprecated-declarations]
       setThreshholds(12, 6);
                           ^
    In file included from /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.cpp:29:0:
    /Users/ayalrosenberg/Documents/Arduino/libraries/Adafruit_MPR121/Adafruit_MPR121.h:101:8: note: declared here
       void setThreshholds(uint8_t touch, uint8_t release) __attribute__((deprecated));
            ^
    Sketch uses 31628 bytes (12%) of program storage space. Maximum is 262144 bytes.
    Global variables use 10232 bytes (15%) of dynamic memory, leaving 55304 bytes for local variables. Maximum is 65536 bytes.
    * I tried first with the playSdWav object but it felt more flimsy and tended to break more easily.

    I converted the files from wav to raw with Audacity.
    Click image for larger version. 

Name:	Screen Shot 2019-04-04 at 12.55.42 PM.jpg 
Views:	8 
Size:	65.6 KB 
ID:	16331

    Any suggestions on how to tackle this matter?

    Thank you

  2. #2
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany NRW
    Posts
    5,565
    https://forum.pjrc.com/threads/42401...l=1#post202250

    For polyphony use a SPI Flash chip. The 3rd.
    Last edited by Frank B; 04-04-2019 at 05:21 PM.

Posting Permissions

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