Arduino ESP32 WROOM Pam8403 SPIFFS WAV

Hello

I've been able to play successfully my wav file using this method (wav converted to hex).

I'm now trying to play a 8 bits mono file (22KHz, unsigned 8bits pcm) :
image
I've tried different method using chatgpt4 to help me out but the arduino keeps playing a "bee" noise instead of my wav file (that is playing perfectly using a hex transformation / but not on spiffs).

I've tried many different codes with no sucess - I tried to insist with I2S until I realized it was no sense for my equipment.

Could you give me some advices and eventually guide towards another way than this one that is giving me a weird "sound" rather than a song :

#include <Arduino.h>
#include <FS.h>
#include <SPIFFS.h>

// Broche DAC
#define DAC_PIN 26

File wavFile;

void setup() {
  Serial.begin(115200);

  if (!SPIFFS.begin(true)) {
    Serial.println("Erreur lors de l'initialisation de SPIFFS");
    return;
  }

  wavFile = SPIFFS.open("/son/backswing.wav", "r"); // Remplacez "/son/backswing.wav" par le chemin de votre fichier WAV

  if (!wavFile) {
    Serial.println("Impossible d'ouvrir le fichier WAV");
    return;
  }

  pinMode(DAC_PIN, OUTPUT);
}

void loop() {
  int16_t sample;
  while (wavFile.read((byte *)&sample, 2)) {
    // Lire un échantillon audio depuis le fichier WAV
    sample = sample / 2; // Réduire la puissance du son à la moitié
    dacWrite(DAC_PIN, sample >> 4); // Écrire l'échantillon audio sur la broche DAC (bitshifting pour ajuster la plage)
  }

  wavFile.close(); // Fermer le fichier WAV
  Serial.println("Fichier backswing.wav joué."); // Message de débogage
  delay(1000); // Attendez 1 seconde avant de rejouer le fichier
  wavFile = SPIFFS.open("/son/backswing.wav", "r"); // Réouvrez le fichier WAV
}

What has ChatGPT got to say about your problem ?

ChatGPT was not of a great help - it was for the rest of the code.

I've just tried this code :

#include <Arduino.h>
#include <WiFi.h>
#include "SPIFFS.h"
#include "AudioFileSourceSPIFFS.h"
#include "AudioFileSourceID3.h"
#include "AudioGeneratorMP3.h"
#include "AudioOutputI2SNoDAC.h"


// pno_cs from https://ccrma.stanford.edu/~jos/pasp/Sound_Examples.html

AudioGeneratorMP3 *mp3;
AudioFileSourceSPIFFS *file;
AudioOutputI2SNoDAC *out;
AudioFileSourceID3 *id3;


// Called when a metadata event occurs (i.e. an ID3 tag, an ICY block, etc.
void MDCallback(void *cbData, const char *type, bool isUnicode, const char *string)
{
  (void)cbData;
  Serial.printf("ID3 callback for: %s = '", type);

  if (isUnicode) {
    string += 2;
  }
  
  while (*string) {
    char a = *(string++);
    if (isUnicode) {
      string++;
    }
    Serial.printf("%c", a);
  }
  Serial.printf("'\n");
  Serial.flush();
}


void setup()
{
  WiFi.mode(WIFI_OFF); 
  Serial.begin(115200);
  delay(1000);
  SPIFFS.begin();
  Serial.printf("Sample MP3 playback begins...\n");

  audioLogger = &Serial;
  file = new AudioFileSourceSPIFFS("/son/backswing.mp3");
  id3 = new AudioFileSourceID3(file);
  id3->RegisterMetadataCB(MDCallback, (void*)"ID3TAG");
  out = new AudioOutputI2SNoDAC();
  mp3 = new AudioGeneratorMP3();
  mp3->begin(id3, out);
}

void loop()
{
  if (mp3->isRunning()) {
    if (!mp3->loop()) mp3->stop();
  } else {
    Serial.printf("MP3 done\n");
    delay(1000);
  }
}

PAM8403 is connected to my PIN 26 - the output is a very acute sound.
based on this link

I'm stuck - I don't really understand why the HEX code of the WAV file is OK but as soon as I try to play the same WAV file from SPIFFS, it does not work....

thanks for your help
pat

hello

I've found the code using chatgpt - since I don't have any I2S hardware (ie, max98357a), I have to use the following librairies :
#include <AudioGeneratorWAV.h>
#include <AudioOutputI2SNoDAC.h>
As for the wav (very important), no more than 44100hz (I use 22050hz) - mono and PCM unsigned 8bits using the Audacity software.
Here is the code on DAC 26 (there are two DAC pins on the ESP32; 25&26) :

#include <Arduino.h>
#include <SPIFFS.h>
#include <AudioFileSourceSPIFFS.h>
#include <AudioGeneratorWAV.h>
#include <AudioOutputI2SNoDAC.h>

#define I2S_SPEAKER_SERIAL_CLOCK GPIO_NUM_4 // BCLK
#define I2S_SPEAKER_LEFT_RIGHT_CLOCK GPIO_NUM_5 // WSEL
#define I2S_SPEAKER_SERIAL_DATA GPIO_NUM_26

AudioGeneratorWAV *wav;
AudioFileSourceSPIFFS *file;
AudioOutputI2SNoDAC *out;

void setup() {
  Serial.begin(115200);

  if (!SPIFFS.begin(true)) {
    Serial.println("Erreur lors de l'initialisation de SPIFFS");
    return;
  }

  file = new AudioFileSourceSPIFFS("/son/test.wav");
  out = new AudioOutputI2SNoDAC();
  out->SetPinout(I2S_SPEAKER_SERIAL_CLOCK, I2S_SPEAKER_LEFT_RIGHT_CLOCK, I2S_SPEAKER_SERIAL_DATA);
  wav = new AudioGeneratorWAV();
  wav->begin(file, out);
}

void loop() {
  if (wav->isRunning()) {
    if (wav->loop()) {
      // Le fichier WAV est toujours en lecture
    } else {
      Serial.println("Lecture du fichier WAV terminée.");
      wav->stop();
      file->close();
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.