code speed affect on another Code mixing

charnjit

Well-known member
See this code of encoder works flawlessly with RAM loading. All files loading to ram takes 1 second only.

C++:
#include <Audio.h>               // WAV playing will
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Encoder.h>
 
// RAM CODE
#define NUM_WAVS 16 // avoid silly mistakes
 const char *SMP_WAV[NUM_WAVS] = { "A01.WAV", "A02.WAV", "A03.WAV", "A04.WAV", "05.WAV", "06.WAV", "07.WAV", "08.WAV", "09.WAV", "10.WAV", "11.WAV", "12.WAV", "13.WAV", "14.WAV", "15.WAV", "16.WAV" };


File x_File;
int16_t *SMP_addr[NUM_WAVS];
uint32_t sizes[NUM_WAVS];

Encoder myEnc(2, 3);
Encoder myEnc2(31, 32);
void setup() {
    if (!SD.begin(BUILTIN_SDCARD)) {
        Serial.println("SD init failed");
        return;
               }
      SD.begin(BUILTIN_SDCARD);             Serial.println("CARD FOUND");  
     RAM_LOAD ();        Serial.println("RAM loaded........................");
}
        long oldPosition  =   myEnc.read();  // -999;
        long oldPosition2  =   myEnc2.read();  // -999;
 
void loop() {
                              long newPosition = myEnc.read();
                            long newPosition2 = myEnc2.read();

  if (newPosition != oldPosition) {
                                 if  (newPosition < oldPosition)  {
                                                                   Serial.println(" -- E1");
                                                                  }
                            else if  (newPosition > oldPosition)  {
                                                                  Serial.println(" ++ E1");
                                                                  }
                                            oldPosition = newPosition;
                                            Serial.println(myEnc.read());
                                                  }

        if (myEnc2.read() != oldPosition2) {              //  --------------------------------------------------------- //  Enc2 Start
                                if  (myEnc2.read() < oldPosition2)  {
                                                                     Serial.println(" -- E2");                            
                                                                     }
                            else if  (myEnc2.read() > oldPosition2)  {
                                                                      Serial.println(" ++ E2");
                                                                      }
                                         oldPosition2 = myEnc2.read();
                                         Serial.println(myEnc2.read());
                                      }     //   --------------------------------------------------------- //  Enc2   End
              }  //   Loop End

///=============================================================================================================
            void RAM_LOAD ()  {        
             
  int SMP_FILE_NUM=0;
  for (int i = 0; i < NUM_WAVS; i++) {
                              x_File = SD.open(SMP_WAV[i], FILE_READ);
                             if (x_File)
                                         {
      sizes[i] = x_File.size();
      SMP_addr[i] = (int16_t*) extmem_malloc(sizes[i]);
      if (nullptr == SMP_addr[i])
        Serial.printf("Failed to allocate %d in EXTMEM for %s\n", sizes[i], SMP_WAV[i]);
      else     {
        if (sizes[i] != x_File.read(SMP_addr[i], sizes[i]))    
        {
          Serial.printf("Failed to read in %s - wrong length\n", SMP_WAV[i]);
          extmem_free(SMP_addr[i]); // free memory
          SMP_addr[i] = nullptr;    // mark as "not loaded"
        }
        else
          Serial.printf("Read %s into memory at %08X; %d bytes\n", SMP_WAV[i], SMP_addr[i], sizes[i]);
      }
      x_File.close();
    }
    else
      Serial.printf("Failed to open %s\n", SMP_WAV[i]);
  }
}

   void RAM_FREE () {
       for (int i = 0; i < NUM_WAVS; i++) {
        if (nullptr != SMP_addr[i])  {extmem_free(SMP_addr[i]); }  // IF ALREADY LOADED
        Serial.print(i);          Serial.println(" CLEARED ");
        }
   }

When I write this line Encoder myEnc(2, 3); in the below code (RAM LOADING + KEYPAD etc), the RAM loading speed becomes very slow. All files loading to PSRAM take 35+ seconds
code is with affected running Speed :
C++:
 #include <Keypad.h>
#include <Audio.h>               // WAV playing will
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Encoder.h>
#include "effect_phaseVocoder.h"

// KEYPAD
const uint8_t RowS = 7;  // 7 Rows
const uint8_t COLS = 4;  //4 columns    
char keys[RowS][COLS] = {
  { 1, 2, 3, 4 },
  { 5, 6, 7, 8 },
  { 9, 10, 11, 12 },
  { 13, 14, 15, 16 },
  { 17, 18, 19, 20 },
  { 21, 22, 23, 24 },
  { 25, 26, 27, 28 }
};
uint8_t RowPins[RowS] = { 33, 34, 35, 36, 37, 38, 39 };
uint8_t colPins[COLS] = { 40, 41, 14, 16 };
Keypad kpd = Keypad(makeKeymap(keys), RowPins, colPins, RowS, COLS);

AudioEffectPhaseVocoder vocoder;

 AudioOutputI2S       audioOut;
 AudioControlSGTL5000 codec;

AudioConnection patchLeft (vocoder, 0, audioOut, 0);
AudioConnection patchRight(vocoder, 0, audioOut, 1);
 
// RAM CODE
#define NUM_WAVS 16 // avoid silly mistakes
 const char *SMP_WAV[NUM_WAVS] = { "A01.WAV", "A02.WAV", "A03.WAV", "A04.WAV", "05.WAV", "06.WAV", "07.WAV", "08.WAV", "09.WAV", "10.WAV", "11.WAV", "12.WAV", "13.WAV", "14.WAV", "15.WAV", "16.WAV" };


File x_File;
int16_t *SMP_addr[NUM_WAVS];
uint32_t sizes[NUM_WAVS];

  Encoder myEnc(2, 3);  // error LINE
  float SPEEDO = 1.00;

 
 
void setup() {
    AudioMemory(128);
    Serial.begin(57600);
     kpd.setHoldTime(1500);
     codec.enable();
     codec.volume(0.5f);

    if (!SD.begin(BUILTIN_SDCARD)) {
                              Serial.println("SD init failed");
                                 return;
                                  }
      SD.begin(BUILTIN_SDCARD);             Serial.println("CARD FOUND");
     RAM_LOAD ();                           Serial.println("RAM loaded........................");
     vocoder.stop();
     vocoder.setTransientThreshold(4.0f);
      vocoder.setSample(SMP_addr[0], sizes[0]/2);
        vocoder.setLoop(true);
        vocoder.play();
}
 
void loop() {  //  Loop start
 
      if (kpd.getKeys()) {
    for (int i = 0; i < LIST_MAX; i++)
    { int mykey = (kpd.key[i].kchar);
      if (kpd.key[i].stateChanged)  //  -----------------------------------------Key Changed----------------
      { switch (kpd.key[i].kstate) {
          case PRESSED:
            Serial.print(mykey);    Serial.println("   :PRESSED");
            break;
          case RELEASED:
            Serial.print(mykey);    Serial.println("  :RELEASED");
            break;
        }
      }  //--------------------------------------------------------------------------------------------------
       //-----------------------------WRITE HERE  CODE FOR other KEYS---------------------------------------------------------------------
       //-----------------------------WRITE HERE  CODE FOR other KEYS---------------------------------------------------------------------
         }
      }// ----------------------------KEYPAD END
         


 
    if (Serial.available() > 0) {   // ----------------------------------------------SERIAL------------------------------------------
        char key = Serial.read();
        if      (key == ' ')                { vocoder.stop(); vocoder.play(); Serial.println("Playing"); }
        else if (key == 's')                { vocoder.stop(); Serial.println("Stopped"); }
        else if (key == 'p')                {  SPEEDO=SPEEDO+0.1;       vocoder.setStretch(SPEEDO);  }
        else if (key == 'q')                  {   SPEEDO=SPEEDO-0.1;       vocoder.setStretch(SPEEDO);  }
         else if (key == 'L')                  {  RAM_LOAD ();   }
          else if (key == 'F')                  {  RAM_FREE ();  }
    }  // ----------------------------------------------SERIAL-----------------------------------------
              }  //   Loop End

///=========================================
            void RAM_LOAD ()  {        
             
  int SMP_FILE_NUM=0;
  for (int i = 0; i < NUM_WAVS; i++) {
                              x_File = SD.open(SMP_WAV[i], FILE_READ);
                             if (x_File)
                                         {
      sizes[i] = x_File.size();
      SMP_addr[i] = (int16_t*) extmem_malloc(sizes[i]);
      if (nullptr == SMP_addr[i])
        Serial.printf("Failed to allocate %d in EXTMEM for %s\n", sizes[i], SMP_WAV[i]);
      else     {
        if (sizes[i] != x_File.read(SMP_addr[i], sizes[i]))    
        {
          Serial.printf("Failed to read in %s - wrong length\n", SMP_WAV[i]);
          extmem_free(SMP_addr[i]); // free memory
          SMP_addr[i] = nullptr;    // mark as "not loaded"
        }
        else
          Serial.printf("Read %s into memory at %08X; %d bytes\n", SMP_WAV[i], SMP_addr[i], sizes[i]);
      }
      x_File.close();
    }
    else
      Serial.printf("Failed to open %s\n", SMP_WAV[i]);
  }
}

   void RAM_FREE () {
       for (int i = 0; i < NUM_WAVS; i++) {
        if (nullptr != SMP_addr[i])  {extmem_free(SMP_addr[i]); }  // IF ALREADY LOADED
        Serial.print(i);          Serial.println(" CLEARED ");
        }
   }
very very slow speed of " LOADING FILES to PSRAM" in void setup() you can see in this Video
However, the loading files to PSRAM speed is quite good. if i don't use of Encoder myEnc(2, 3); in my code , all files Loading to PSRAM takes 1 second only.
Please tell me under what circumstances this happens.
I want to know the details of it.
Looks like it will bother me if I mix more code like Display etc.
 
Last edited:
I got and fitted some PSRAM. Both these run equally as quickly for me.

I tried the one that is running slowly for you with 6 files -
Code:
CARD FOUND
Read 01.WAV into memory at 7000000C; 199846 bytes
Read 02.WAV into memory at 70030CCC; 199846 bytes
Read 03.WAV into memory at 7006198C; 210988 bytes
Read 04.WAV into memory at 700951D8; 1587644 bytes
Read 05.WAV into memory at 70218BB0; 1416146 bytes
Read 06.WAV into memory at 703727A4; 1296050 bytes
Failed to open 07.WAV
Failed to open 08.WAV
Failed to open 09.WAV
Failed to open 10.WAV
Failed to open 11.WAV
Failed to open 12.WAV
Failed to open 13.WAV
Failed to open 14.WAV
Failed to open 15.WAV
Failed to open 16.WAV
RAM loaded........................
Total load time: 620 ms

A couple of points :
- Can you show me a picture of how you have connected your encoders
- Are you using a decent quality card, do you have another one you can try

Cheers Paul
 
Thankyou....paul

Encoder wiring is this way. I have same this encoder.my sd Card is SanDisk ultra fast 64 gb card is working fine. Problem occurred only when I add rotary encoder's code to my main code ,as you are seeing in Post #1 codes. I think very tinny mistake make this problem. That is out of my search...
 

Attachments

  • IMG_20260426_080224.jpg
    IMG_20260426_080224.jpg
    32.3 KB · Views: 20
Last edited:
Hi,

I can't replicate your problem - for me both sets of code at post #1 work well under 1s.

A couple of things to try:
1. Since the code works fine for me with a clean board, it is likely something in your hardware or environment,
  • can you show me your actual wiring to the encoder.
  • do you have the keypad wired and if so can you show me a picture of the actual connections and wiring to the teensy
2. I don't think it is your code however a notable difference between them is in the second one you initialise the Audio system (with a large memory allocation 128 blocks) before you read the card - try moving AudioMemory, codec.enable(); and codec.volume(0.5); after the call to RAM_LOAD() in setup. If that works it may still be a hardware problem but would explain why one working and not the other


Cheers, Paul
 
I tried changing the sequence of functions in void setup() and also change AudioMemory(40); like this..
C++:
void setup() {
 
    Serial.begin(57600);
     kpd.setHoldTime(1500);
    if (!SD.begin(BUILTIN_SDCARD)) {
                              Serial.println("SD init failed");
                                 return;
                                  }
      SD.begin(BUILTIN_SDCARD);             Serial.println("CARD FOUND");
       RAM_LOAD ();                           Serial.println("RAM loaded........................");
          AudioMemory(40);
     codec.enable();
     codec.volume(0.5f);
     vocoder.stop();
     vocoder.setTransientThreshold(4.0f);
      vocoder.setSample(SMP_addr[0], sizes[0]/2);
        vocoder.setLoop(true);
        vocoder.play();
}
But no change was seen. My circuit might seem unsightly to you, but this is due to space constraints.
IMG_20260426_222136.jpg


Its actual pins diagram is something like this
Keypad ENC Pins connections.jpg


I used the exact same encoder you see in the picture.

Note here ..... the keypad pins(39 and 40 ) are connected to the encoder's Push buttons

. all keys from keypad diode protected.
Please tell me how much AudioMemory(?); should be there according to my audio structure.And Explain why??
Audio Structure.jpg

New Thing when i removed vocoder related code from main code ,it works ok . all keys of kepad work also work rotary encoder. RAM LOADING() speed also fast, what i want.I mean everything works fine.check this code without vocoder
C++:
 #include <Keypad.h>
#include <Audio.h>               
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <Encoder.h>

// KEYPAD
const uint8_t RowS = 7;  // 7 Rows
const uint8_t COLS = 4;  //4 columns   
char keys[RowS][COLS] = {
  { 1, 2, 3, 4 },
  { 5, 6, 7, 8 },
  { 9, 10, 11, 12 },
  { 13, 14, 15, 16 },
  { 17, 18, 19, 20 },
  { 21, 22, 23, 24 },
  { 25, 26, 27, 28 }
};
uint8_t RowPins[RowS] = { 33, 34, 35, 36, 37, 38, 39 };
uint8_t colPins[COLS] = { 40, 41, 14, 16 };
Keypad kpd = Keypad(makeKeymap(keys), RowPins, colPins, RowS, COLS);

 

 AudioOutputI2S       audioOut;
 AudioControlSGTL5000 codec;
 
#define NUM_WAVS 16 // avoid silly mistakes
 const char *SMP_WAV[NUM_WAVS] = { "A01.WAV", "A02.WAV", "A03.WAV", "A04.WAV", "05.WAV", "06.WAV", "07.WAV", "08.WAV", "09.WAV", "10.WAV", "11.WAV", "12.WAV", "13.WAV", "14.WAV", "15.WAV", "16.WAV" };

File x_File;
int16_t *SMP_addr[NUM_WAVS];
uint32_t sizes[NUM_WAVS];

  Encoder myEnc(2, 3);   
  float SPEEDO = 1.00;

 
 
void setup() {
  
    Serial.begin(57600);
     kpd.setHoldTime(1500);
    if (!SD.begin(BUILTIN_SDCARD)) {
                              Serial.println("SD init failed");
                                 return;
                                  }
      SD.begin(BUILTIN_SDCARD);             Serial.println("CARD FOUND");
 
       RAM_LOAD ();                           Serial.println("RAM loaded........................");
     AudioMemory(40);
     codec.enable();
     codec.volume(0.5f);
}
 long oldPosition  =   myEnc.read();  // -999; 


void loop() {  //  Loop start
//=======================================================================================================
  long newPosition = myEnc.read();
  if (newPosition != oldPosition) {
    if  (newPosition < oldPosition)  {
                                      Serial.println(" -- ");
                                      }
     else if  (newPosition > oldPosition)  {
                                             Serial.println(" ++ ");
                                              }
    oldPosition = newPosition;  Serial.println(myEnc.read());
  }
//=========================================================================================================
 
 
      if (kpd.getKeys()) {
    for (int i = 0; i < LIST_MAX; i++)
    { int mykey = (kpd.key[i].kchar);
      if (kpd.key[i].stateChanged)  //  -----------------------------------------Key Changed----------------
      { switch (kpd.key[i].kstate) {
          case PRESSED:
            Serial.print(mykey);    Serial.println("   :PRESSED");
            break;
          case RELEASED:
            Serial.print(mykey);    Serial.println("  :RELEASED");
            break;
        }
      }  //--------------------------------------------------------------------------------------------------
       //-----------------------------WRITE HERE  CODE FOR other KEYS---------------------------------------------------------------------
       //-----------------------------WRITE HERE  CODE FOR other KEYS---------------------------------------------------------------------
         }
      }// ----------------------------KEYPAD END
        


 
    if (Serial.available() > 0) {   // ----------------------------------------------SERIAL------------------------------------------
        char key = Serial.read();
           if (key == 'L')                  {  RAM_LOAD ();   }
          else if (key == 'F')                  {  RAM_FREE ();  }
    }  // ----------------------------------------------SERIAL-----------------------------------------
              }  //   Loop End

///=========================================
            void RAM_LOAD ()  {       
            
  int SMP_FILE_NUM=0;
  for (int i = 0; i < NUM_WAVS; i++) {
                              x_File = SD.open(SMP_WAV[i], FILE_READ);
                             if (x_File)
                                         {
      sizes[i] = x_File.size();
      SMP_addr[i] = (int16_t*) extmem_malloc(sizes[i]);
      if (nullptr == SMP_addr[i])
        Serial.printf("Failed to allocate %d in EXTMEM for %s\n", sizes[i], SMP_WAV[i]);
      else     {
        if (sizes[i] != x_File.read(SMP_addr[i], sizes[i]))   
        {
          Serial.printf("Failed to read in %s - wrong length\n", SMP_WAV[i]);
          extmem_free(SMP_addr[i]); // free memory
          SMP_addr[i] = nullptr;    // mark as "not loaded"
        }
        else
          Serial.printf("Read %s into memory at %08X; %d bytes\n", SMP_WAV[i], SMP_addr[i], sizes[i]);
      }
      x_File.close();
    }
    else
      Serial.printf("Failed to open %s\n", SMP_WAV[i]);
  }
}

   void RAM_FREE () {
       for (int i = 0; i < NUM_WAVS; i++) {
        if (nullptr != SMP_addr[i])  {extmem_free(SMP_addr[i]); }  // IF ALREADY LOADED
        Serial.print(i);          Serial.println(" CLEARED ");
        }
   }
I've checked result by upload 10 times (with vocoder code) and 10 times (without vocoder code) . i upload both code total 20 times
Result: without vocoder code all things working ,,,, with vocoder code RAM_LOAD (); speed gets ultra slow down..

Having trouble with the vocoder code, and working fine without the vocoder code proves there's no problem with my hardware.
Like I need all three in my code. (keypad+Rotary encoder+ vocoder Effect).
The results are coming out something like this. (keypad+Encoder) works ,,, (keypad+ vocoder) works..
I have to choose between a vocoder or an encoder. If you see anything noticeable in this, please reply.
 
Back
Top