Skip to content

Instantly share code, notes, and snippets.

@Airrr17
Created April 26, 2019 20:17
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Airrr17/bc0f01b39e3948598023b614efe96597 to your computer and use it in GitHub Desktop.
Save Airrr17/bc0f01b39e3948598023b614efe96597 to your computer and use it in GitHub Desktop.
Random MOD player for ESP32 with OLED and button.
// Airrr's i2s random MOD player with "skipnext" button and OLED
//main library: https://github.com/earlephilhower/ESP8266Audio
//DAC (PCM5102):
//3.3V -> VCC, 33V, XMT, (SCK)
//GND -> GND, FLT, DMP, FMT, SCL
//BCLK->BCK to Pin D26 ESP32
//I2SO->DIN to Pin D22
//LRCLK(WS)->LCK to Pin D25
//OLED SSD1306:
//SDA -> D21, SCL -> D4
//SD card:
//CS -> D5, MOSI -> D23, CLK -> D18, MISO -> D19
//Next track -> VP pin to VCC (pull down req.)
#include <Arduino.h>
#include "AudioGeneratorMOD.h"
#include "AudioOutputI2S.h"
#include "AudioFileSourceSD.h"
//#include <WiFi.h> //ESP32 now!!!
#include <SPI.h>
#include <SD.h>
#include <Wire.h>
#include "SSD1306Wire.h"
SSD1306Wire display(0x3c, 21, 4, GEOMETRY_128_32);
AudioGeneratorMOD *mod;
AudioFileSourceSD *fileO;
AudioOutputI2S *out;
uint16_t FileCount; //in root
unsigned long seed;
void setup() { ////////////////////////////setup start
// WiFi.mode(WIFI_OFF); // WiFi.forceSleepBegin();
Serial.begin(115200);
display.init();
display.setContrast(255);
display.setFont(ArialMT_Plain_10);
display.clear();
seed = esp_random(); /// oherennaya funkciya
randomSeed(seed);
Serial.print("Seed = ");
Serial.println(seed);
Serial.printf("MOD start\n");
delay(500);
if (!SD.begin(5, SPI, 8000000)) {
Serial.println("SD initialization failed!");
display.drawString(0, 0, "SD init. failed!");
display.display();
while (1);
}
File root = SD.open("/");
printDirectory(root);
PlayRandom();
} //////////////////////////////setup end
void loop() { /////////////loop start
if (mod->isRunning()) {
if (!mod->loop()) mod->stop();
} else {
Serial.printf("MOD done\n");
delay(1000);
PlayRandom();
}
if (analogRead(A0) > 3000) { //skip next (VP)
mod->stop();
fileO->close();
delete fileO;
delete out;
delete mod;
delay(750);
PlayRandom(); /// skip to next
}
} //////////////////loop end
void PlayRandom() { /////////////////PLAY
int file2open = random(0, FileCount); //-1
display.clear();
display.drawString(0, 0, "File#:");
char buf[4];
sprintf (buf, "%03d", file2open + 1);
display.drawString(40, 0, buf);
display.drawString(62, 0, "of");
sprintf (buf, "%03d", FileCount);
display.drawString(77, 0, buf);
File root = SD.open("/");
root.rewindDirectory();
while (true) {
File entry = root.openNextFile();
if (! entry) {
Serial.println("no more files");// no more files
break;
}
if (!strcasecmp(entry.name() + strlen(entry.name()) - 4, ".mod")) {
if (file2open <= 0) {
Serial.printf("Vibrali:");
Serial.println(entry.name());
display.drawString(-3, 10, entry.name()); // -3 chtob "/" uehal za ekran // display selected filename
byte BufName[20];
char buf[20];
entry.read(BufName, 20);
strncpy(buf, (char*)BufName, sizeof(buf));
// Serial.println(buf);
display.drawString(0, 20, buf); //display in string first 20 bytes of the selected file (20 char Song title)
display.display();
fileO = new AudioFileSourceSD(entry.name());
out = new AudioOutputI2S();
mod = new AudioGeneratorMOD();
mod->SetBufferSize(1 * 1024); ///3
mod->SetSampleRate(44100);
mod->SetStereoSeparation(16); // upto64 64-no separation! 0-max and ugly!
mod->begin(fileO, out); //out
break;
}
file2open--;
}
}
} ///////////////////////PLAY
void printDirectory(File dir) { ////////////////print dir, count mods
while (true) {
File entry = dir.openNextFile();
if (! entry) {
// no more files
break;
}
Serial.print(entry.name());
if (entry.isDirectory()) {
} else {
if (!strcasecmp(entry.name() + strlen(entry.name()) - 4, ".mod")) {
FileCount++;
}
Serial.println(entry.size(), DEC);
}
entry.close();
}
Serial.print("MODs found:");
Serial.println(FileCount);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment