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"
27 #include "esp_a2dp_api.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"
36 #if ESP_IDF_VERSION_MAJOR < 4 && !defined(I2S_COMM_FORMAT_STAND_I2S)
37 #define I2S_COMM_FORMAT_STAND_I2S \
38 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_MSB)
39 #define I2S_COMM_FORMAT_STAND_MSB \
40 (I2S_COMM_FORMAT_I2S | I2S_COMM_FORMAT_I2S_LSB)
41 #define I2S_COMM_FORMAT_STAND_PCM_LONG \
42 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_LONG)
43 #define I2S_COMM_FORMAT_STAND_PCM_SHORT \
44 (I2S_COMM_FORMAT_PCM | I2S_COMM_FORMAT_PCM_SHORT)
45 typedef int eps32_i2s_audio_sample_rate_type;
47 typedef uint32_t eps32_i2s_audio_sample_rate_type;
51 class AudioKit* selfAudioKit =
nullptr;
58 i2s_port_t i2s_num = (i2s_port_t)0;
59 gpio_num_t mclk_gpio = (gpio_num_t)0;
60 bool sd_active =
true;
62 audio_hal_adc_input_t
adc_input = AUDIOKIT_DEFAULT_INPUT;
63 audio_hal_dac_output_t
dac_output =AUDIOKIT_DEFAULT_OUTPUT;
66 audio_hal_iface_format_t
fmt = AUDIOKIT_DEFAULT_I2S_FMT;
67 audio_hal_iface_samples_t
sample_rate = AUDIOKIT_DEFAULT_RATE;
76 case AUDIO_HAL_BIT_LENGTH_16BITS:
78 case AUDIO_HAL_BIT_LENGTH_24BITS:
80 case AUDIO_HAL_BIT_LENGTH_32BITS:
90 case AUDIO_HAL_08K_SAMPLES:
92 case AUDIO_HAL_11K_SAMPLES:
94 case AUDIO_HAL_16K_SAMPLES:
96 case AUDIO_HAL_22K_SAMPLES:
98 case AUDIO_HAL_24K_SAMPLES:
100 case AUDIO_HAL_32K_SAMPLES:
102 case AUDIO_HAL_44K_SAMPLES:
104 case AUDIO_HAL_48K_SAMPLES:
107 KIT_LOGE(
"sample rate not supported: %d",
sample_rate);
113 i2s_config_t i2sConfig() {
115 const i2s_config_t i2s_config = {
119 .channel_format = I2S_CHANNEL_FMT_RIGHT_LEFT,
120 .communication_format = (i2s_comm_format_t)i2sFormat(),
121 .intr_alloc_flags = ESP_INTR_FLAG_LEVEL2 | ESP_INTR_FLAG_IRAM,
125 .tx_desc_auto_clear =
true,
134 i2s_pin_config_t i2sPins() {
135 i2s_pin_config_t result;
136 get_i2s_pins(i2s_num, &result);
137 #if ESP_IDF_VERSION_MAJOR >= 4
138 result.mck_io_num = I2S_PIN_NO_CHANGE;
144 i2s_comm_format_t i2sFormat(){
145 i2s_comm_format_t its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_I2S;
146 if (
fmt==AUDIO_HAL_I2S_LEFT){
147 its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
148 }
else if(
fmt==AUDIO_HAL_I2S_RIGHT){
149 its_com_fmt = (i2s_comm_format_t) I2S_COMM_FORMAT_STAND_MSB;
150 }
else if(
fmt==AUDIO_HAL_I2S_DSP){
151 its_com_fmt = (i2s_comm_format_t )I2S_COMM_FORMAT_STAND_PCM_SHORT;
156 i2s_mode_t i2sMode() {
157 int mode =
isMaster() ? I2S_MODE_SLAVE : I2S_MODE_MASTER;
159 if (
fmt == AUDIO_HAL_I2S_DSP){
160 if (
codec_mode == AUDIO_HAL_CODEC_MODE_DECODE) {
161 mode = mode | I2S_MODE_TX | I2S_MODE_DAC_BUILT_IN;
162 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_ENCODE) {
163 mode = mode | I2S_MODE_RX | I2S_MODE_ADC_BUILT_IN;
164 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_BOTH) {
165 mode = mode | I2S_MODE_RX | I2S_MODE_TX | I2S_MODE_ADC_BUILT_IN | I2S_MODE_DAC_BUILT_IN;
169 if (
codec_mode == AUDIO_HAL_CODEC_MODE_DECODE) {
170 mode = mode | I2S_MODE_TX;
171 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_ENCODE) {
172 mode = mode | I2S_MODE_RX;
173 }
else if (
codec_mode == AUDIO_HAL_CODEC_MODE_BOTH) {
174 mode = mode | I2S_MODE_RX | I2S_MODE_TX;
177 return (i2s_mode_t) mode;
187 enum AudioKitInOut {AudioOutput, AudioInput, AudioInputOutput };
209 result.
codec_mode = AUDIO_HAL_CODEC_MODE_DECODE;
211 result.
codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE;
213 result.
codec_mode = AUDIO_HAL_CODEC_MODE_BOTH;
220 KIT_LOGI(LOG_METHOD);
222 KIT_LOGI(
"Selected board: %d", AUDIOKIT_BOARD);
230 audio_hal_conf.adc_input = cfg.
adc_input;
234 audio_hal_conf.i2s_iface.fmt = cfg.
fmt;
235 audio_hal_conf.i2s_iface.samples = cfg.
sample_rate;
239 if (hal_handle == 0) {
240 hal_handle = audio_hal_init(&audio_hal_conf, &AUDIO_DRIVER);
241 if (hal_handle == 0) {
242 KIT_LOGE(
"audio_hal_init");
251 i2s_config_t i2s_config = cfg.i2sConfig();
252 if (i2s_driver_install(cfg.i2s_num, &i2s_config, 0, NULL) != ESP_OK) {
253 KIT_LOGE(
"i2s_driver_install");
258 i2s_pin_config_t pin_config = cfg.i2sPins();
259 KIT_LOGI(
"i2s_set_pin");
260 if (i2s_set_pin(cfg.i2s_num, &pin_config) != ESP_OK) {
261 KIT_LOGE(
"i2s_set_pin");
266 if (i2s_mclk_gpio_select(cfg.i2s_num, cfg.mclk_gpio) != ESP_OK) {
267 KIT_LOGE(
"i2s_mclk_gpio_select");
276 KIT_LOGE(
"setActive");
285 KIT_LOGI(LOG_METHOD);
289 i2s_driver_uninstall(cfg.i2s_num);
292 audio_hal_ctrl_codec(hal_handle, cfg.
codec_mode, AUDIO_HAL_CTRL_STOP);
294 audio_hal_deinit(hal_handle);
303 return audio_hal_ctrl_codec( hal_handle, cfg.
codec_mode, active ? AUDIO_HAL_CTRL_START : AUDIO_HAL_CTRL_STOP) == ESP_OK;
308 return audio_hal_set_mute(hal_handle, mute) == ESP_OK;
313 return (vol > 0) ? audio_hal_set_volume(hal_handle, vol) == ESP_OK :
false;
319 if (audio_hal_get_volume(hal_handle, &
volume) != ESP_OK) {
328 size_t write(
const void *src,
size_t size,
329 TickType_t ticks_to_wait = portMAX_DELAY) {
330 KIT_LOGD(
"write: %zu", size);
331 size_t bytes_written = 0;
332 if (i2s_write(cfg.i2s_num, src, size, &bytes_written, ticks_to_wait) !=
334 KIT_LOGE(
"i2s_write");
336 return bytes_written;
340 size_t read(
void *dest,
size_t size,
341 TickType_t ticks_to_wait = portMAX_DELAY) {
342 KIT_LOGD(
"read: %zu", size);
343 size_t bytes_read = 0;
344 if (i2s_read(cfg.i2s_num, dest, size, &bytes_read, ticks_to_wait) !=
346 KIT_LOGE(
"i2s_read");
359 int8_t
pinAuxin() {
return get_auxin_detect_gpio(); }
488 int paPin = get_pa_enable_gpio();
490 digitalWrite(paPin, active);
492 KIT_LOGW(
"setSpeakerActive not supported");
502 if (selfAudioKit->headphonePin>0){
506 if (selfAudioKit->headphoneIsConnected != isConnected) {
507 selfAudioKit->headphoneIsConnected = isConnected;
510 bool powerActive = !isConnected;
511 KIT_LOGW(
"Headphone jack has been %s", isConnected ?
"inserted" :
"removed");
526 return headphonePin>0 ? !digitalRead(headphonePin) :
false;
531 return cfg.sd_active;
536 audio_hal_codec_config_t audio_hal_conf;
537 audio_hal_handle_t hal_handle = 0;
538 audio_hal_codec_i2s_iface_t iface;
540 bool headphoneIsConnected =
false;
541 unsigned long speakerChangeTimeout = 0;
542 int8_t headphonePin = -1;
543 bool setup_sd_spi =
true;
553 pinMode(headphonePin, INPUT_PULLUP);
554 pinMode(paPin, OUTPUT);
555 KIT_LOGI(
"headphone detection is active");
557 KIT_LOGI(
"headphone detection not supported");
560 KIT_LOGI(
"headphone detection: PA not supported");
569 #if AUDIOKIT_SETUP_SD==1
570 if (cfg.sd_active && setup_sd_spi){
571 KIT_LOGI(LOG_METHOD);
572 spi_cs_pin = PIN_AUDIO_KIT_SD_CARD_CS;
573 pinMode(spi_cs_pin, OUTPUT);
574 digitalWrite(spi_cs_pin, HIGH);
576 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);
577 setup_sd_spi =
false;
580 #warning "SPI initialization for the SD drive not supported - you might need to take care of this yourself"
581 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.
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.
void setupSPI()
Setup the SPI so that we can access the SD Drive.
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.
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