text/plain
•
1.43 KB
•
55 lines
#include "audio.h"
#include "driver/i2s_std.h"
#include "freertos/FreeRTOS.h"
#include "esp_log.h"
#include "../src/moon.h"
static const char *TAG = "audio";
#define PIN_BCK 13
#define PIN_WS 14
#define PIN_DOUT 21
static i2s_chan_handle_t tx_chan;
void audio_init(void) {
i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, I2S_ROLE_MASTER);
ESP_ERROR_CHECK(i2s_new_channel(&chan_cfg, &tx_chan, NULL));
i2s_std_config_t std_cfg = {
.clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(44100),
.slot_cfg = I2S_STD_PHILIPS_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO),
.gpio_cfg = {
.mclk = I2S_GPIO_UNUSED,
.bclk = PIN_BCK,
.ws = PIN_WS,
.dout = PIN_DOUT,
.din = I2S_GPIO_UNUSED,
.invert_flags = {
.mclk_inv = false,
.bclk_inv = false,
.ws_inv = false,
},
},
};
ESP_ERROR_CHECK(i2s_channel_init_std_mode(tx_chan, &std_cfg));
ESP_ERROR_CHECK(i2s_channel_enable(tx_chan));
ESP_LOGI(TAG, "I2S ready");
}
void audio_update(void) {
static int16_t buf[1024 * 2];
static int cnt = 0;
size_t written;
for (int i = 0; i < 1024; i++) {
int16_t s = (cnt++ % 100 < 50) ? 32 : -32;
buf[i * 2] = s;
buf[i * 2 + 1] = s;
}
i2s_channel_write(tx_chan, buf, sizeof(buf), &written, portMAX_DELAY);
}