15#include "audio_driver/es7148/es7148.h"
16#include "audio_driver/es7210/es7210.h"
17#include "audio_driver/es7243/es7243.h"
18#include "audio_driver/es8311/es8311.h"
19#include "audio_driver/es8374/es8374.h"
20#include "audio_driver/es8388/es8388.h"
21#include "audio_driver/tas5805m/tas5805m.h"
22#include "audio_hal/audiokit_board.h"
23#include "audio_hal/audiokit_logger.h"
28#include "audio_hal/audio_system.h"
29#include "audio_hal/audio_version.h"
30#include "driver/i2s.h"
31#include "audio_hal/audio_type_def.h"
32#if !defined(ARDUINO_ESP32S3_DEV) && !defined(ARDUINO_ESP32S2_DEV) && !defined(ARDUINO_ESP32C3_DEV)
33SPIClass SPI_VSPI(VSPI);
38#if ESP_IDF_VERSION_MAJOR < 4 && !defined(I2S_COMM_FORMAT_STAND_I2S)
39#define I2S_COMM_FORMAT_STAND_I2S \
40 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
41#define I2S_COMM_FORMAT_STAND_MSB \
42 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
43#define I2S_COMM_FORMAT_STAND_PCM_LONG \
44 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
45#define I2S_COMM_FORMAT_STAND_PCM_SHORT \
46 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_SHORT)
47typedef int eps32_i2s_audio_sample_rate_type;
49typedef uint32_t eps32_i2s_audio_sample_rate_type;
53class AudioKit* selfAudioKit =
nullptr;
61 i2s_port_t i2s_num = (i2s_port_t)0;
62 gpio_num_t mclk_gpio = (gpio_num_t)0;
63 bool sd_active =
true;
64 bool auto_clear =
true;
67 int buffer_size = 512;
70 audio_hal_adc_input_t
adc_input = AUDIOKIT_DEFAULT_INPUT;
71 audio_hal_dac_output_t
dac_output =AUDIOKIT_DEFAULT_OUTPUT;
74 audio_hal_iface_format_t
fmt = AUDIOKIT_DEFAULT_I2S_FMT;
75 audio_hal_iface_samples_t
sample_rate = AUDIOKIT_DEFAULT_RATE;
84 case AUDIO_HAL_BIT_LENGTH_16BITS:
86 case AUDIO_HAL_BIT_LENGTH_24BITS:
88 case AUDIO_HAL_BIT_LENGTH_32BITS:
98 case AUDIO_HAL_08K_SAMPLES:
100 case AUDIO_HAL_11K_SAMPLES:
102 case AUDIO_HAL_16K_SAMPLES:
104 case AUDIO_HAL_22K_SAMPLES:
106 case AUDIO_HAL_24K_SAMPLES:
108 case AUDIO_HAL_32K_SAMPLES:
110 case AUDIO_HAL_44K_SAMPLES:
112 case AUDIO_HAL_48K_SAMPLES:
115 KIT_LOGE(
"sample rate not supported: %d",
sample_rate);
121 i2s_config_t i2sConfig() {
123 const i2s_config_t i2s_config = {
127 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
128 .communication_format = (i2s_comm_format_t)i2sFormat(),
129 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM,
130 .dma_buf_count = buffer_count,
131 .dma_buf_len = buffer_size,
132 .use_apll = use_apll,
133 .tx_desc_auto_clear = auto_clear,
139 i2s_pin_config_t i2sPins() {
140 i2s_pin_config_t result;
141 get_i2s_pins(i2s_num, &result);
142 #if ESP_IDF_VERSION_MAJOR >= 4
143 result.mck_io_num = I2S_PIN_NO_CHANGE;
149 i2s_comm_format_t i2sFormat(){
150 i2s_comm_format_t its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_I2S;
151 if (
fmt==AUDIO_HAL_I2S_LEFT){
152 its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
153 }
else if(
fmt==AUDIO_HAL_I2S_RIGHT){
154 its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
155 }
else if(
fmt==AUDIO_HAL_I2S_DSP){
156 its_com_fmt = (i2s_comm_format_t )I2S_COMM_FORMAT_STAND_PCM_SHORT;
161 i2s_mode_t i2sMode() {
162 int mode =
isMaster() ? I2S_MODE_SLAVE : I2S_MODE_MASTER;
164 if (
fmt == AUDIO_HAL_I2S_DSP){
165 #if AUDIOKIT_BOARD==4 || defined(ARDUINO_ESP32S3_DEV) || defined(ARDUINO_ESP32S2_DEV)|| defined(ARDUINO_ESP32C3_DEV)
166 KIT_LOGE(
"AUDIO_HAL_I2S_DSP not supported");
168 if (
codec_mode == AUDIO_HAL_CODEC_MODE_DECODE) {
169 mode = mode | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN;
170 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_ENCODE) {
171 mode = mode | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN;
172 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_BOTH) {
173 mode = mode | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_ADC_BUILT_IN | I2S_MODE_DAC_BUILT_IN;
178 if (
codec_mode == AUDIO_HAL_CODEC_MODE_DECODE) {
179 mode = mode | I2S_MODE_TX;
180 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_ENCODE) {
181 mode = mode | I2S_MODE_RX;
182 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_BOTH) {
183 mode = mode | I2S_MODE_RX | I2S_MODE_TX;
186 return (i2s_mode_t) mode;
196enum AudioKitInOut {AudioOutput, AudioInput, AudioInputOutput };
220 result.
codec_mode = AUDIO_HAL_CODEC_MODE_DECODE;
223 result.
codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE;
226 result.
codec_mode = AUDIO_HAL_CODEC_MODE_BOTH;
234 KIT_LOGI(LOG_METHOD);
236 KIT_LOGI(
"Selected board: %d", AUDIOKIT_BOARD);
245 if (AUDIOKIT_SETUP_SD && !cfg.sd_active){
252 audio_hal_conf.adc_input = cfg.
adc_input;
256 audio_hal_conf.i2s_iface.fmt = cfg.
fmt;
257 audio_hal_conf.i2s_iface.samples = cfg.
sample_rate;
261 if (hal_handle == 0) {
262 hal_handle = audio_hal_init(&audio_hal_conf, &AUDIO_DRIVER);
263 if (hal_handle == 0) {
264 KIT_LOGE(
"audio_hal_init");
273 i2s_config_t i2s_config = cfg.i2sConfig();
274 if (i2s_driver_install(cfg.i2s_num, &i2s_config, 0, NULL) != ESP_OK) {
275 KIT_LOGE(
"i2s_driver_install");
280 i2s_pin_config_t pin_config = cfg.i2sPins();
281 KIT_LOGI(
"i2s_set_pin");
282 if (i2s_set_pin(cfg.i2s_num, &pin_config) != ESP_OK) {
283 KIT_LOGE(
"i2s_set_pin");
288 if (i2s_mclk_gpio_select(cfg.i2s_num, cfg.mclk_gpio) != ESP_OK) {
289 KIT_LOGE(
"i2s_mclk_gpio_select");
298 KIT_LOGE(
"setActive");
308 KIT_LOGI(LOG_METHOD);
312 i2s_driver_uninstall(cfg.i2s_num);
315 audio_hal_ctrl_codec(hal_handle, cfg.
codec_mode, AUDIO_HAL_CTRL_STOP);
317 audio_hal_deinit(hal_handle);
329 return audio_hal_ctrl_codec( hal_handle, cfg.
codec_mode, active ? AUDIO_HAL_CTRL_START : AUDIO_HAL_CTRL_STOP) == ESP_OK;
334 return audio_hal_set_mute(hal_handle, mute) == ESP_OK;
339 return (vol > 0) ? audio_hal_set_volume(hal_handle, vol) == ESP_OK :
false;
345 if (audio_hal_get_volume(hal_handle, &
volume) != ESP_OK) {
354 size_t write(
const void *src,
size_t size,
355 TickType_t ticks_to_wait = portMAX_DELAY) {
356 KIT_LOGD(
"write: %zu", size);
357 size_t bytes_written = 0;
358 if (i2s_write(cfg.i2s_num, src, size, &bytes_written, ticks_to_wait) !=
360 KIT_LOGE(
"i2s_write");
362 return bytes_written;
366 size_t read(
void *dest,
size_t size,
367 TickType_t ticks_to_wait = portMAX_DELAY) {
368 KIT_LOGD(
"read: %zu", size);
369 size_t bytes_read = 0;
370 if (i2s_read(cfg.i2s_num, dest, size, &bytes_read, ticks_to_wait) !=
372 KIT_LOGE(
"i2s_read");
384 audio_hal_conf.i2s_iface.samples = cfg.
sample_rate = sample_rate;
389 if (audio_hal_codec_iface_config(hal_handle, audio_hal_conf.codec_mode, &audio_hal_conf.i2s_iface) != ESP_OK) {
390 KIT_LOGE(
"audio_hal_ctrl_codec");
395 if (i2s_set_sample_rates(cfg.i2s_num, cfg.
sampleRate()) != ESP_OK) {
396 KIT_LOGE(
"i2s_set_sample_rates");
412 int8_t
pinAuxin() {
return get_auxin_detect_gpio(); }
541 int paPin = get_pa_enable_gpio();
543 digitalWrite(paPin, active);
545 KIT_LOGW(
"setSpeakerActive not supported");
555 if (selfAudioKit->headphonePin>0){
559 if (selfAudioKit->headphoneIsConnected != isConnected) {
560 selfAudioKit->headphoneIsConnected = isConnected;
563 bool powerActive = !isConnected;
564 KIT_LOGW(
"Headphone jack has been %s", isConnected ?
"inserted" :
"removed");
579 return headphonePin>0 ? !digitalRead(headphonePin) :
false;
584 return cfg.sd_active;
588 bool is_active =
false;
590 audio_hal_codec_config_t audio_hal_conf;
591 audio_hal_handle_t hal_handle = 0;
592 audio_hal_codec_i2s_iface_t iface;
594 bool headphoneIsConnected =
false;
595 unsigned long speakerChangeTimeout = 0;
596 int8_t headphonePin = -1;
598 SPIClass *p_spi = &AUDIOKIT_SD_SPI;
608 pinMode(headphonePin, INPUT_PULLUP);
609 pinMode(paPin, OUTPUT);
610 KIT_LOGI(
"headphone detection is active");
612 KIT_LOGI(
"headphone detection not supported");
615 KIT_LOGI(
"headphone detection: PA not supported");
625#if AUDIOKIT_SETUP_SD==1
626 KIT_LOGI(LOG_METHOD);
627 spi_cs_pin = PIN_AUDIO_KIT_SD_CARD_CS;
628 pinMode(spi_cs_pin, OUTPUT);
629 digitalWrite(spi_cs_pin, HIGH);
631 p_spi->begin(PIN_AUDIO_KIT_SD_CARD_CLK, PIN_AUDIO_KIT_SD_CARD_MISO, PIN_AUDIO_KIT_SD_CARD_MOSI, PIN_AUDIO_KIT_SD_CARD_CS);
633 #warning "SPI initialization for the SD drive not supported - you might need to take care of this yourself"
634 cfg.sd_active =
false;
AUDIOKIT_BOARD selects a specic board: 1) lyrat_v4_3 2) lyrat_v4_2 3) lyrat_mini_v1_1 4) esp32_s2_kal...
AudioKit API using the audio_hal.
int8_t pinHeadphoneDetect()
Get the gpio number for headphone detection.
int8_t pinGreenLed()
Get gpio number for green led.
bool headphoneStatus()
Returns true if the headphone was detected.
int8_t pinAdcDetect()
Get the gpio number for adc detection.
int8_t pinVolumeUp()
number for volume up function
int8_t pinSpiCs()
SPI CS Pin for SD Drive.
int8_t pinPaEnable()
Get the gpio number for PA enable.
int8_t pinInputRec()
Get the record-button id for adc-button.
bool setActive(bool active)
Sets the codec active / inactive.
int8_t pinInputMode()
Get the number for mode-button.
int8_t pinEs7243Mclk()
Get the mclk gpio number of es7243.
bool setSampleRate(audio_hal_iface_samples_t sample_rate)
Just update the sample rate.
static void actionHeadphoneDetection()
Switch off the PA if the headphone in plugged in and switch it on again if the headphone is unplugged...
int volume()
Determines the volume %.
int8_t pinAuxin()
Get the gpio number for auxin detection.
bool begin(AudioKitConfig cnfg)
Starts the codec.
bool setMute(bool mute)
Mutes the output.
int8_t pinVolumeDown()
Get number for volume down function.
bool end()
Stops the CODEC.
int8_t pinResetBoard()
Get gpio number to reset the board.
void setupHeadphoneDetection()
Setup the headphone detection.
bool setVolume(int vol)
Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
int8_t pinResetCodec()
Get reset codec gpio number.
int8_t pinInputSet()
Get number for set function.
AudioKitConfig defaultConfig(AudioKitInOut inout=AudioInputOutput)
Provides the default configuration for input or output.
int8_t pinBlueLed()
Get blue led gpio number.
bool isSDActive()
checks if the SD is active
int8_t pinInputPlay()
Get number for play function.
AudioKitConfig config()
Provides the actual configuration.
void setSpeakerActive(bool active)
Activates/deactivates the speaker amplifier output This is working only if the driver is supporting t...
Configuation for AudioKit.
audio_hal_iface_bits_t bits_per_sample
uint32_t sampleRate()
Provides the sample rate in samples per second.
int bitsPerSample()
provides the bits per sample
audio_hal_dac_output_t dac_output
bool isMaster()
Returns true if the CODEC is the master.
audio_hal_adc_input_t adc_input
audio_hal_iface_format_t fmt
audio_hal_iface_mode_t master_slave_mode
audio_hal_iface_samples_t sample_rate
audio_hal_codec_mode_t codec_mode