15 #include "audio_hal/driver/es7148/es7148.h"
16 #include "audio_hal/driver/es7210/es7210.h"
17 #include "audio_hal/driver/es7243/es7243.h"
18 #include "audio_hal/driver/es8311/es8311.h"
19 #include "audio_hal/driver/es8374/es8374.h"
20 #include "audio_hal/driver/es8388/es8388.h"
21 #include "audio_hal/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 =
false;
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:
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 };
208 result.
codec_mode = AUDIO_HAL_CODEC_MODE_DECODE;
210 result.
codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE;
212 result.
codec_mode = AUDIO_HAL_CODEC_MODE_BOTH;
219 KIT_LOGI(LOG_METHOD);
221 KIT_LOGI(
"Selected board: %d", AUDIOKIT_BOARD);
226 audio_hal_conf.adc_input = cfg.
adc_input;
230 audio_hal_conf.i2s_iface.fmt = cfg.
fmt;
231 audio_hal_conf.i2s_iface.samples = cfg.
sample_rate;
235 if (hal_handle == 0) {
236 hal_handle = audio_hal_init(&audio_hal_conf, &AUDIO_DRIVER);
237 if (hal_handle == 0) {
238 KIT_LOGE(
"audio_hal_init");
247 i2s_config_t i2s_config = cfg.i2sConfig();
248 if (i2s_driver_install(cfg.i2s_num, &i2s_config, 0, NULL) != ESP_OK) {
249 KIT_LOGE(
"i2s_driver_install");
254 i2s_pin_config_t pin_config = cfg.i2sPins();
255 KIT_LOGI(
"i2s_set_pin");
256 if (i2s_set_pin(cfg.i2s_num, &pin_config) != ESP_OK) {
257 KIT_LOGE(
"i2s_set_pin");
262 if (i2s_mclk_gpio_select(cfg.i2s_num, cfg.mclk_gpio) != ESP_OK) {
263 KIT_LOGE(
"i2s_mclk_gpio_select");
272 KIT_LOGE(
"setActive");
281 KIT_LOGI(LOG_METHOD);
285 i2s_driver_uninstall(cfg.i2s_num);
288 audio_hal_ctrl_codec(hal_handle, cfg.
codec_mode, AUDIO_HAL_CTRL_STOP);
290 audio_hal_deinit(hal_handle);
299 return audio_hal_ctrl_codec( hal_handle, cfg.
codec_mode, active ? AUDIO_HAL_CTRL_START : AUDIO_HAL_CTRL_STOP) == ESP_OK;
304 return audio_hal_set_mute(hal_handle, mute) == ESP_OK;
309 return (vol > 0) ? audio_hal_set_volume(hal_handle, vol) == ESP_OK :
false;
315 if (audio_hal_get_volume(hal_handle, &
volume) != ESP_OK) {
324 size_t write(
const void *src,
size_t size,
325 TickType_t ticks_to_wait = portMAX_DELAY) {
326 KIT_LOGD(
"write: %zu", size);
327 size_t bytes_written = 0;
328 if (i2s_write(cfg.i2s_num, src, size, &bytes_written, ticks_to_wait) !=
330 KIT_LOGE(
"i2s_write");
332 return bytes_written;
336 size_t read(
void *dest,
size_t size,
337 TickType_t ticks_to_wait = portMAX_DELAY) {
338 KIT_LOGD(
"read: %zu", size);
339 size_t bytes_read = 0;
340 if (i2s_read(cfg.i2s_num, dest, size, &bytes_read, ticks_to_wait) !=
342 KIT_LOGE(
"i2s_read");
355 int8_t
pinAuxin() {
return get_auxin_detect_gpio(); }
484 int paPin = get_pa_enable_gpio();
486 digitalWrite(paPin, active);
488 KIT_LOGW(
"setSpeakerActive not supported");
498 if (selfAudioKit->headphonePin>0){
502 if (selfAudioKit->headphoneIsConnected != isConnected) {
503 KIT_LOGD(
"actionHeadphoneDetection -> %s", isConnected ?
"inserted" :
"removed");
504 selfAudioKit->speakerChangeTimeout = millis()+600;
505 selfAudioKit->headphoneIsConnected = isConnected;
509 if (millis()>selfAudioKit->speakerChangeTimeout){
512 bool powerActive = !isConnected;
513 KIT_LOGW(
"Headphone jack has been %s", isConnected ?
"inserted" :
"removed");
515 selfAudioKit->speakerChangeTimeout = millis()+1000*60*50*30;
529 return headphonePin>0 ? !digitalRead(headphonePin) :
false;
534 audio_hal_codec_config_t audio_hal_conf;
535 audio_hal_handle_t hal_handle = 0;
536 audio_hal_codec_i2s_iface_t iface;
538 bool headphoneIsConnected =
false;
539 unsigned long speakerChangeTimeout = 0;
540 int8_t headphonePin = -1;
550 pinMode(headphonePin, INPUT_PULLUP);
551 pinMode(paPin, OUTPUT);
552 KIT_LOGI(
"headphone detection is active");
554 KIT_LOGI(
"headphone detection not supported");
557 KIT_LOGI(
"headphone detection: PA not supported");
565 KIT_LOGD(LOG_METHOD);
567 #if AUDIOKIT_SETUP_SD==1
569 spi_cs_pin = PIN_AUDIO_KIT_SD_CARD_CS;
570 pinMode(spi_cs_pin, OUTPUT);
571 digitalWrite(spi_cs_pin, HIGH);
573 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);
574 KIT_LOGW(
"setupSPI for SD");
577 #warning "SPI initialization for the SD drive not supported - you might need to take care of this yourself"
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 DSP reset gpio number.
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 DSP reset gpio number.
void setupHeadphoneDetection()
Setup the headphone detection.
bool setVolume(int vol)
Defines the Volume.
int8_t pinResetCodec()
Get green led 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 green led gpio number.
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