ok so I've actually found some example code which I've taken from here: Demo 26: How to use Arduino ESP32 I2S (external DAC and built-in DAC) to play wav music file from sdcard and which I've tried to adapt for my purposes:
#include "driver/i2s.h"
#include "freertos/queue.h"
#include <pgmspace.h>
#include "soundsample.h"
//i2s configuration
int i2s_num = 0; // i2s port number
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_TX),
.sample_rate = 16000,
.bits_per_sample = I2S_BITS_PER_SAMPLE_16BIT,
.channel_format = I2S_CHANNEL_FMT_ONLY_RIGHT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1, // high interrupt priority
.dma_buf_count = 8,
.dma_buf_len = 64 //Interrupt level 1
};
i2s_pin_config_t pin_config = {
.bck_io_num = 26, //this is BCK pin
.ws_io_num = 25, // this is LRCK pin
.data_out_num = 22, // this is DATA output pin
.data_in_num = -1 //Not used
};
int i2s_write_sample_nb(uint32_t sample) {
return i2s_write_bytes((i2s_port_t)i2s_num, (const char *)&sample, sizeof(uint32_t), 100);
}
//Main function to play samples from PROGMEM
void playPROGMEMsample(const uint32_t* audioSample) {
uint32_t sampleSize = sizeof(audioSample) * 4;
uint32_t counter = 0;
//initialize i2s with configurations above
i2s_driver_install((i2s_port_t)i2s_num, &i2s_config, 0, NULL);
i2s_set_pin((i2s_port_t)i2s_num, &pin_config);
//set sample rates of i2s to sample rate of wav file
i2s_set_sample_rates((i2s_port_t)i2s_num, 16000);
uint32_t readData;
while (audioSample) {
readData = pgm_read_dword(&audioSample[counter++]);
if (readData == NULL) break;
i2s_write_sample_nb(readData);
}
i2s_driver_uninstall((i2s_port_t)i2s_num); //stop & destroy i2s driver
}
void setup() {
Serial.begin(115200);
Serial.println("Serial connection OK");
playPROGMEMsample(sample1);
}
void loop() {
}
At this point, I'm actually not yet fully grasping what I am really doing here. This code compiles, but all I get on my speaker that is connected to the amp is noise.
I'm using a 16kHz 16-bit wav sample that I've put into PROGMEM (see attached zip file for sketch and .h file) as an array of double word hex values. Within my project, I want to play all sound samples that it will contain from PROGMEM. Maybe I would make my life easier by just putting them on an SD card, but that is not the goal. The finished product must contain them in flash memory.
My pinout:
[ESP32] ----------- [Adafruit 98357A]
GPIO22 ----------- DIN
GPIO25 ----------- LRC
GPIO26 ----------- BCLK
The Adafruit amp with the speaker is connected to an external 3V3 supply.
I have loosely tried to make sense of the i2s.h file within the ESP32 core, but I'm really not quite sure how I can get my setup to work.
Any help is appreciated.
esp32_audio__.zip (472 KB)