I have ESP32-WROOM-32D board and MAX98357A I2S board which i need to play 8Khz 8bit PCM audio. The pcm audio buffer is declared as a constant buffer in header wavedata.h like below
const uint8_t rawData[49428] = {0x52, 0x49, 0x46, 0x46,.......};
Below is my test code for playing this buffer audio through I2S
#include "wavedata.h"
#include <Arduino.h>
#include <I2S.h>
#define LRC_PIN 26
#define BCLK_PIN 27
#define DOUT_PIN 25
void setup()
{
I2S.begin(I2S_PHILIPS_MODE, 8000, 8);
I2S.setSckPin(BCLK_PIN);
I2S.setFsPin(LRC_PIN);
I2S.setDataPin(DOUT_PIN);
}
void loop()
{
I2S.write_blocking(rawData,49428);
delay(1000);
}
But I am not hearing any sound from the speaker.
I tried like below also
#include "driver/i2s.h"
#include "wavedata.h"
#define I2S_SAMPLE_RATE (8000)
#define I2S_SAMPLE_BITS (I2S_BITS_PER_SAMPLE_8BIT)
#define I2S_CHANNEL_NUM (1)
#define I2S_BUFFER_SIZE (512)
#define PIN_LRC 25
#define PIN_BCLK 14
#define PIN_DIN I2S_PIN_NO_CHANGE
#define PIN_DOUT 25
#define MCK I2S_PIN_NO_CHANGE
void setup()
{
i2s_config_t i2s_config = {
.mode = (i2s_mode_t)(I2S_MODE_MASTER),
.sample_rate = I2S_SAMPLE_RATE,
.bits_per_sample = I2S_SAMPLE_BITS,
.channel_format = I2S_CHANNEL_FMT_ALL_LEFT,
.communication_format = (i2s_comm_format_t)(I2S_COMM_FORMAT_STAND_I2S),
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
.dma_buf_count = 16,
.dma_buf_len = I2S_BUFFER_SIZE,
.use_apll = false, // must be disabled in V2.0.1-RC1
.tx_desc_auto_clear = true, // new in V1.0.1
.fixed_mclk = I2S_PIN_NO_CHANGE,
};
i2s_pin_config_t pin_config = {
.bck_io_num = PIN_BCLK,
.ws_io_num = PIN_LRC,
.data_out_num = PIN_DOUT,
.data_in_num = PIN_DIN,
};
i2s_driver_install(I2S_NUM_0, &i2s_config, 0, NULL);
i2s_set_pin(I2S_NUM_0, &pin_config);
i2s_set_sample_rates(I2S_NUM_0, I2S_SAMPLE_RATE);
size_t written_bytes = 0;
while (written_bytes < sizeof(rawData)) {
size_t bytes_to_write = sizeof(rawData) - written_bytes;
if (bytes_to_write > I2S_BUFFER_SIZE) {
bytes_to_write = I2S_BUFFER_SIZE;
}
i2s_write(I2S_NUM_0, &rawData[written_bytes], bytes_to_write, &written_bytes, portMAX_DELAY);
}
i2s_driver_uninstall(I2S_NUM_0);
}
void loop()
{
}
Above code also not working