I am using this library and examples for some test code to see if I can get the performance or responsiveness I am looking for with the sound effects over I2S:
https://github.com/schreibfaul1/ESP32-audioI2S/wiki
I have two problems:
1) Lag/delay from the time the user presses the button until the sound is heard is delayed (~1 second)
2) If the user presses the button before the sound is finished playing I need to start the sound over again at the beginning.
What I have tried:
I originally started using the DFplayer mp3 module https://www.robotshop.com/en/dfplayer-m ... uTEALw_wcB but I quickly ran into issue #1. Too much delay between the time the user presses the button and when the short sound clip is played. It also took too long to issue a new command over the I2C to start playing the file over again if the user presses the button before the play is over.
Next, I tried to using I2S with a MAX98357A chip combined with WAV files located on an SD card. The lag issue was improved somewhat but still some delay in playing the file initially. However, for issue #2, I can seem to rapidly reset the play to the beginning to give rapid shot sound effects.
https://www.aliexpress.com/item/3304366 ... 85648d69e4
I was going to try just running the sound effect out of RAM but I haven't found a ton of great libraries that support playing WAVs direct from memory and I am not sure it would even help with my two issues.
MY QUESTION:
Based on what I am trying to do. What is the best way to get very low latency sound effects using the ESP32? Essentially, I have a gunshot.wav file and for single shot effect I want to quickly play it when a button is pressed followed by some other sound effects, but I need some pretty low latency for a positive experience. If the user rapidly presses the button I want to just keep restarting the sound effect.
Here is some example code I was using for testing:
Code: Select all
#include "Audio.h"
#include "SD.h"
#include "FS.h"
// Digital I/O used
#define SD_CS 5
#define SPI_MOSI 23
#define SPI_MISO 19
#define SPI_SCK 18
#define I2S_DOUT 25
#define I2S_BCLK 27
#define I2S_LRC 26
Audio audio;
const int ctrl_trigger = 4; // trigger, GND=fire
boolean old_button = false;
void setup(){
pinMode(ctrl_trigger,INPUT_PULLUP);
pinMode(SD_CS, OUTPUT);
digitalWrite(SD_CS, HIGH);
SPI.begin(SPI_SCK, SPI_MISO, SPI_MOSI);
Serial.begin(115200);
SD.begin(SD_CS);
audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
audio.setVolume(8); // 0...21
audio.forceMono(true);
}
void loop(){
audio.loop();
if (old_button != (digitalRead(ctrl_trigger)==LOW)){
if (audio.isRunning()){
audio.setAudioPlayPosition(0);
}
else {audio.connecttoFS(SD, "01_shot_umt.wav");}
}
old_button = (digitalRead(ctrl_trigger)==LOW);
}
// optional
void audio_info(const char *info){
Serial.print("info "); Serial.println(info);
}
void audio_id3data(const char *info){ //id3 metadata
Serial.print("id3data ");Serial.println(info);
}
void audio_eof_mp3(const char *info){ //end of file
Serial.print("eof_mp3 ");Serial.println(info);
}
void audio_showstation(const char *info){
Serial.print("station ");Serial.println(info);
}
void audio_showstreaminfo(const char *info){
Serial.print("streaminfo ");Serial.println(info);
}
void audio_showstreamtitle(const char *info){
Serial.print("streamtitle ");Serial.println(info);
}
void audio_bitrate(const char *info){
Serial.print("bitrate ");Serial.println(info);
}
void audio_commercial(const char *info){ //duration in sec
Serial.print("commercial ");Serial.println(info);
}
void audio_icyurl(const char *info){ //homepage
Serial.print("icyurl ");Serial.println(info);
}
void audio_lasthost(const char *info){ //stream URL played
Serial.print("lasthost ");Serial.println(info);
}
void audio_eof_speech(const char *info){
Serial.print("eof_speech ");Serial.println(info);
}
Code: Select all
info buffers freed, free Heap: 286600 bytes
info Reading file: "/01_shot_umt.wav"
info stream ready
info FormatCode: 1
info DataRate: 44100
info DataBlockSize: 2
info Audio-Length: 41364
info syncword found at pos 0
info Channels: 1
info SampleRate: 22050
info BitsPerSample: 16
info BitRate: 352800
info syncword found at pos 0
info Channels: 1
info SampleRate: 22050
info BitsPerSample: 16
info BitRate: 352800
info syncword found at pos 0
info Channels: 1
info SampleRate: 22050
info BitsPerSample: 16
info BitRate: 352800
info syncword found at pos 0
info Channels: 1
info SampleRate: 22050
info BitsPerSample: 16
info BitRate: 352800
info End of file "/01_shot_umt.wav"
eof_mp3 /01_shot_umt.wav