See this code of encoder works flawlessly with RAM loading. All files loading to ram takes 1 second only.
When I write this line
code is with affected running Speed :
very very slow speed of " LOADING FILES to PSRAM" in
However, the loading files to PSRAM speed is quite good. if i don't use of
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.
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 ");
}
}
void setup() you can see in this VideoHowever, 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: