We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Please help my code is not playing the audio file
#include "FS.h" #include <SD.h> #include <SPI.h> #include "AudioFileSourceSD.h" #include "AudioOutputI2S.h" #include "AudioGeneratorFLAC.h"
// Define CS pin for the SD card module #define SD_MISO 13 #define SD_MOSI 12 #define SD_SCLK 14 #define SD_CS 27 #define SPEAKER_PIN 25
SPIClass sdSPI(VSPI);
File dir; AudioFileSourceSD *source = NULL; AudioOutputI2S *output = NULL; AudioGeneratorFLAC *flacPlayer = NULL;
void cleanup() { delete source; delete output; delete flacPlayer; }
void setup() { // Start serial communication for debugging purposes Serial.begin(115200);
// Initialize SD card sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS); Serial.println("Trying to initialize SD card...");
delay(5000);
if (!SD.begin(SD_CS, sdSPI)) { Serial.println("Card Mount Failed"); cleanup(); return; }
Serial.println("SD card initialized successfully");
// Check the type of card uint8_t cardType = SD.cardType(); if (cardType == CARD_NONE) { Serial.println("No SD card attached"); cleanup(); return; }
Serial.println("SD Card Type: " + String(SD.cardType())); Serial.println("SD Card Size: " + String(SD.cardSize() / (1024 * 1024)) + " MB");
// List all files on the SD card listFiles("/");
// Set up speaker pin pinMode(SPEAKER_PIN, OUTPUT);
// Set up audio components source = new AudioFileSourceSD(); output = new AudioOutputI2S(); flacPlayer = new AudioGeneratorFLAC();
output->SetPinout(22, 26, 25); // Change the pins as per your ESP32 board configuration
// You can adjust the sample rate and bit depth if needed output->SetRate(44100); output->SetBitsPerSample(16);
// Allow some time to initialize delay(2000); }
void loop() { // Play a tone of 440 Hz for 1 second playTone(440, 1000); delay(1000); // Pause for 1 second
// Check and play FLAC file if ((flacPlayer) && (flacPlayer->isRunning())) { if (!flacPlayer->loop()) flacPlayer->stop(); } else { File file = SD.open("MS-Garage.flac"); if (file) { playFlacFile("MS-Garage.flac"); } else { Serial.println("Error opening FLAC file: MS-Garage.flac"); } } }
void playTone(int frequency, int duration) { for (long i = 0; i < duration * 1000L; i += 2 * frequency) { digitalWrite(SPEAKER_PIN, HIGH); delayMicroseconds(frequency); digitalWrite(SPEAKER_PIN, LOW); delayMicroseconds(frequency); } }
void playFlacFile(const char *filename) { source->close(); if (source->open(filename)) { Serial.println("Playing FLAC file: " + String(filename)); flacPlayer->begin(source, output); } else { Serial.println("Error opening FLAC file: " + String(filename)); } }
void listFiles(const char *dirname) { Serial.printf("Listing directory: %s\n", dirname);
dir = SD.open(dirname); if (!dir) { Serial.println("Failed to open directory"); cleanup(); return; }
while (true) { File file = dir.openNextFile(); if (!file) { // no more files break; }
Serial.print("FILE: "); Serial.println(file.name());
}
dir.close(); }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Please help my code is not playing the audio file
#include "FS.h"
#include <SD.h>
#include <SPI.h>
#include "AudioFileSourceSD.h"
#include "AudioOutputI2S.h"
#include "AudioGeneratorFLAC.h"
// Define CS pin for the SD card module
#define SD_MISO 13
#define SD_MOSI 12
#define SD_SCLK 14
#define SD_CS 27
#define SPEAKER_PIN 25
SPIClass sdSPI(VSPI);
File dir;
AudioFileSourceSD *source = NULL;
AudioOutputI2S *output = NULL;
AudioGeneratorFLAC *flacPlayer = NULL;
void cleanup() {
delete source;
delete output;
delete flacPlayer;
}
void setup() {
// Start serial communication for debugging purposes
Serial.begin(115200);
// Initialize SD card
sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
Serial.println("Trying to initialize SD card...");
delay(5000);
if (!SD.begin(SD_CS, sdSPI)) {
Serial.println("Card Mount Failed");
cleanup();
return;
}
delay(5000);
Serial.println("SD card initialized successfully");
delay(5000);
// Check the type of card
uint8_t cardType = SD.cardType();
if (cardType == CARD_NONE) {
Serial.println("No SD card attached");
cleanup();
return;
}
delay(5000);
Serial.println("SD Card Type: " + String(SD.cardType()));
Serial.println("SD Card Size: " + String(SD.cardSize() / (1024 * 1024)) + " MB");
delay(5000);
// List all files on the SD card
listFiles("/");
// Set up speaker pin
pinMode(SPEAKER_PIN, OUTPUT);
// Set up audio components
source = new AudioFileSourceSD();
output = new AudioOutputI2S();
flacPlayer = new AudioGeneratorFLAC();
output->SetPinout(22, 26, 25); // Change the pins as per your ESP32 board configuration
// You can adjust the sample rate and bit depth if needed
output->SetRate(44100);
output->SetBitsPerSample(16);
// Allow some time to initialize
delay(2000);
}
void loop() {
// Play a tone of 440 Hz for 1 second
playTone(440, 1000);
delay(1000); // Pause for 1 second
// Check and play FLAC file
if ((flacPlayer) && (flacPlayer->isRunning())) {
if (!flacPlayer->loop()) flacPlayer->stop();
} else {
File file = SD.open("MS-Garage.flac");
if (file) {
playFlacFile("MS-Garage.flac");
} else {
Serial.println("Error opening FLAC file: MS-Garage.flac");
}
}
}
void playTone(int frequency, int duration) {
for (long i = 0; i < duration * 1000L; i += 2 * frequency) {
digitalWrite(SPEAKER_PIN, HIGH);
delayMicroseconds(frequency);
digitalWrite(SPEAKER_PIN, LOW);
delayMicroseconds(frequency);
}
}
void playFlacFile(const char *filename) {
source->close();
if (source->open(filename)) {
Serial.println("Playing FLAC file: " + String(filename));
flacPlayer->begin(source, output);
} else {
Serial.println("Error opening FLAC file: " + String(filename));
}
}
void listFiles(const char *dirname) {
Serial.printf("Listing directory: %s\n", dirname);
dir = SD.open(dirname);
if (!dir) {
Serial.println("Failed to open directory");
cleanup();
return;
}
while (true) {
File file = dir.openNextFile();
if (!file) {
// no more files
break;
}
}
dir.close();
}
The text was updated successfully, but these errors were encountered: