Arduino AudioKit HAL
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
AudioKit Class Reference

AudioKit API using the audio_hal. More...

#include <AudioKitHAL.h>

Public Member Functions

AudioKitConfig defaultConfig (AudioKitInOut inout=AudioInputOutput)
 Provides the default configuration for input or output.
 
bool begin (AudioKitConfig cnfg)
 Starts the codec.
 
bool end ()
 Stops the CODEC.
 
AudioKitConfig config ()
 Provides the actual configuration.
 
bool setActive (bool active)
 Sets the codec active / inactive.
 
bool setMute (bool mute)
 Mutes the output.
 
bool setVolume (int vol)
 Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.
 
int volume ()
 Determines the volume %.
 
bool setSampleRate (audio_hal_iface_samples_t sample_rate)
 Just update the sample rate.
 
int8_t pinAuxin ()
 Get the gpio number for auxin detection.
 
int8_t pinHeadphoneDetect ()
 Get the gpio number for headphone detection.
 
int8_t pinPaEnable ()
 Get the gpio number for PA enable.
 
int8_t pinAdcDetect ()
 Get the gpio number for adc detection.
 
int8_t pinEs7243Mclk ()
 Get the mclk gpio number of es7243.
 
int8_t pinInputRec ()
 Get the record-button id for adc-button.
 
int8_t pinInputMode ()
 Get the number for mode-button.
 
int8_t pinInputSet ()
 Get number for set function.
 
int8_t pinInputPlay ()
 Get number for play function.
 
int8_t pinVolumeUp ()
 number for volume up function
 
int8_t pinVolumeDown ()
 Get number for volume down function.
 
int8_t pinResetCodec ()
 Get reset codec gpio number.
 
int8_t pinResetBoard ()
 Get gpio number to reset the board.
 
int8_t pinGreenLed ()
 Get gpio number for green led.
 
int8_t pinBlueLed ()
 Get blue led gpio number.
 
int8_t pinSpiCs ()
 SPI CS Pin for SD Drive.
 
void setSpeakerActive (bool active)
 Activates/deactivates the speaker amplifier output This is working only if the driver is supporting the functionality.
 
bool headphoneStatus ()
 Returns true if the headphone was detected.
 
bool isSDActive ()
 checks if the SD is active
 

Static Public Member Functions

static void actionHeadphoneDetection ()
 Switch off the PA if the headphone in plugged in and switch it on again if the headphone is unplugged.
 

Protected Member Functions

void setupHeadphoneDetection ()
 Setup the headphone detection.
 

Protected Attributes

bool is_active = false
 
AudioKitConfig cfg
 
audio_hal_codec_config_t audio_hal_conf
 
audio_hal_handle_t hal_handle = 0
 
audio_hal_codec_i2s_iface_t iface
 
int8_t spi_cs_pin
 
bool headphoneIsConnected = false
 
unsigned long speakerChangeTimeout = 0
 
int8_t headphonePin = -1
 

Detailed Description

AudioKit API using the audio_hal.

Definition at line 216 of file AudioKitHAL.h.

Constructor & Destructor Documentation

◆ AudioKit()

AudioKit::AudioKit ( )
inline

Definition at line 219 of file AudioKitHAL.h.

219 {
220 // setup SPI for SD drives
221 selfAudioKit = this;
222 // added to constructor so that SPI is setup as part of global variable setup
223#if SETUP_ESP32_I2S && defined(ESP32)
224 setupSPI();
225#endif
226 }

Member Function Documentation

◆ actionHeadphoneDetection()

static void AudioKit::actionHeadphoneDetection ( )
inlinestatic

Switch off the PA if the headphone in plugged in and switch it on again if the headphone is unplugged.

Definition at line 582 of file AudioKitHAL.h.

582 {
583 if (selfAudioKit->headphonePin>0){
584
585 // detect changes
586 bool isConnected = selfAudioKit->headphoneStatus();
587 if (selfAudioKit->headphoneIsConnected != isConnected) {
588 selfAudioKit->headphoneIsConnected = isConnected;
589
590 // update if things have stabilized
591 bool powerActive = !isConnected;
592 KIT_LOGW("Headphone jack has been %s", isConnected ? "inserted" : "removed");
593 selfAudioKit->setSpeakerActive(powerActive);
594 }
595 }
596 yield();
597 }
bool headphoneStatus()
Returns true if the headphone was detected.
Definition: AudioKitHAL.h:606
void setSpeakerActive(bool active)
Activates/deactivates the speaker amplifier output This is working only if the driver is supporting t...
Definition: AudioKitHAL.h:568

◆ begin()

bool AudioKit::begin ( AudioKitConfig  cnfg)
inline

Starts the codec.

Definition at line 246 of file AudioKitHAL.h.

246 {
247 KIT_LOGI(LOG_METHOD);
248 cfg = cnfg;
249 KIT_LOGI("Selected board: %d", AUDIOKIT_BOARD);
250
251 // if already active we stop first
252 if (is_active){
253 end();
254 }
255
256#if SETUP_ESP32_I2S && defined(ESP32)
257 // release SPI for SD card if it is not necessary
258 if (AUDIOKIT_SETUP_SD && !cfg.sd_active){
259 p_spi->end();
260 }
261#endif
262 // setup headphone if necessary
264
265 audio_hal_conf.adc_input = cfg.adc_input;
266 audio_hal_conf.dac_output = cfg.dac_output;
267 audio_hal_conf.codec_mode = cfg.codec_mode;
268 audio_hal_conf.i2s_iface.mode = cfg.master_slave_mode;
269 audio_hal_conf.i2s_iface.fmt = cfg.fmt;
270 audio_hal_conf.i2s_iface.samples = cfg.sample_rate;
271 audio_hal_conf.i2s_iface.bits = cfg.bits_per_sample;
272
273 // init HAL
274 if (hal_handle == 0) {
275 hal_handle = audio_hal_init(&audio_hal_conf, &AUDIO_DRIVER);
276 if (hal_handle == 0) {
277 KIT_LOGE("audio_hal_init");
278 return false;
279 }
280 }
281
282 if (cfg.is_i2s_active){
283
284#if SETUP_ESP32_I2S && defined(ESP32)
285 // setup i2s driver - with no queue
286 i2s_config_t i2s_config = cfg.i2sConfig();
287 if (i2s_driver_install(cfg.i2s_num, &i2s_config, 0, NULL) != ESP_OK) {
288 KIT_LOGE("i2s_driver_install");
289 return false;
290 }
291
292 // define i2s pins
293 i2s_pin_config_t pin_config = cfg.i2sPins();
294 KIT_LOGI("i2s_set_pin");
295
296 KIT_LOGI("- bck_io_num: %d", pin_config.bck_io_num);
297 KIT_LOGI("- ws_io_num: %d", pin_config.ws_io_num);
298 KIT_LOGI("- data_out_num: %d", pin_config.data_out_num);
299 KIT_LOGI("- data_in_num: %d", pin_config.data_in_num);
300 KIT_LOGI("- mck_io_num: %d", pin_config.mck_io_num);
301
302 if (i2s_set_pin(cfg.i2s_num, &pin_config) != ESP_OK) {
303 KIT_LOGE("i2s_set_pin");
304 return false;
305 }
306
307#if ESP_IDF_VERSION_MAJOR < 4
308 if (i2s_mclk_gpio_select(cfg.i2s_num,(gpio_num_t)cfg.mclk_gpio) != ESP_OK) {
309 KIT_LOGE("i2s_mclk_gpio_select");
310 return false;
311 }
312#endif
313
314#endif
315 }
316
317 // call start
318 if (!setActive(true)) {
319 KIT_LOGE("setActive");
320 return false;
321 }
322
323 // activate speaker when output not earphones only
324 setSpeakerActive(cfg.dac_output!=AUDIO_HAL_DAC_OUTPUT_LINE1);
325
326 is_active = true;
327 return true;
328 }
bool setActive(bool active)
Sets the codec active / inactive.
Definition: AudioKitHAL.h:354
bool end()
Stops the CODEC.
Definition: AudioKitHAL.h:331
void setupHeadphoneDetection()
Setup the headphone detection.
Definition: AudioKitHAL.h:631
audio_hal_iface_bits_t bits_per_sample
Definition: AudioKitHAL.h:86
audio_hal_dac_output_t dac_output
Definition: AudioKitHAL.h:81
audio_hal_adc_input_t adc_input
Definition: AudioKitHAL.h:80
audio_hal_iface_format_t fmt
Definition: AudioKitHAL.h:84
audio_hal_iface_mode_t master_slave_mode
Definition: AudioKitHAL.h:83
audio_hal_iface_samples_t sample_rate
Definition: AudioKitHAL.h:85
audio_hal_codec_mode_t codec_mode
Definition: AudioKitHAL.h:82

◆ config()

AudioKitConfig AudioKit::config ( )
inline

Provides the actual configuration.

Definition at line 351 of file AudioKitHAL.h.

351{ return cfg; }

◆ defaultConfig()

AudioKitConfig AudioKit::defaultConfig ( AudioKitInOut  inout = AudioInputOutput)
inline

Provides the default configuration for input or output.

Definition at line 229 of file AudioKitHAL.h.

229 {
230 AudioKitConfig result;
231 switch(inout){
232 case AudioOutput:
233 result.codec_mode = AUDIO_HAL_CODEC_MODE_DECODE; // dac
234 break;
235 case AudioInput:
236 result.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; // adc
237 break;
238 default:
239 result.codec_mode = AUDIO_HAL_CODEC_MODE_BOTH;
240 break;
241 }
242 return result;
243 }
Configuation for AudioKit.
Definition: AudioKitHAL.h:66

◆ end()

bool AudioKit::end ( )
inline

Stops the CODEC.

Definition at line 331 of file AudioKitHAL.h.

331 {
332 KIT_LOGI(LOG_METHOD);
333
334#if SETUP_ESP32_I2S && defined(ESP32)
335 if (cfg.is_i2s_active){
336 // uninstall i2s driver
337 i2s_driver_uninstall(cfg.i2s_num);
338 }
339#endif
340 // stop codec driver
341 audio_hal_ctrl_codec(hal_handle, cfg.codec_mode, AUDIO_HAL_CTRL_STOP);
342 // deinit
343 audio_hal_deinit(hal_handle);
344
345 is_active = false;
346 hal_handle = 0;
347 return true;
348 }

◆ headphoneStatus()

bool AudioKit::headphoneStatus ( )
inline

Returns true if the headphone was detected.

Returns
true
false

Definition at line 606 of file AudioKitHAL.h.

606 {
607 return headphonePin>0 ? !digitalRead(headphonePin) : false;
608 }

◆ isSDActive()

bool AudioKit::isSDActive ( )
inline

checks if the SD is active

Definition at line 611 of file AudioKitHAL.h.

611 {
612 return cfg.sd_active;
613 }

◆ pinAdcDetect()

int8_t AudioKit::pinAdcDetect ( )
inline

Get the gpio number for adc detection.

Returns
-1 non-existent Others gpio number

Definition at line 464 of file AudioKitHAL.h.

464{ return get_adc_detect_gpio(); }

◆ pinAuxin()

int8_t AudioKit::pinAuxin ( )
inline

Get the gpio number for auxin detection.

Returns
-1 non-existent Others gpio number

Definition at line 440 of file AudioKitHAL.h.

440{ return get_auxin_detect_gpio(); }

◆ pinBlueLed()

int8_t AudioKit::pinBlueLed ( )
inline

Get blue led gpio number.

Returns
-1 non-existent Others gpio number

Definition at line 552 of file AudioKitHAL.h.

552{ return get_blue_led_gpio(); }

◆ pinEs7243Mclk()

int8_t AudioKit::pinEs7243Mclk ( )
inline

Get the mclk gpio number of es7243.

Returns
-1 non-existent Others gpio number

Definition at line 472 of file AudioKitHAL.h.

472{ return get_es7243_mclk_gpio(); }

◆ pinGreenLed()

int8_t AudioKit::pinGreenLed ( )
inline

Get gpio number for green led.

Returns
-1 non-existent Others gpio number

Definition at line 544 of file AudioKitHAL.h.

544{ return get_green_led_gpio(); }

◆ pinHeadphoneDetect()

int8_t AudioKit::pinHeadphoneDetect ( )
inline

Get the gpio number for headphone detection.

Returns
-1 non-existent Others gpio number

Definition at line 448 of file AudioKitHAL.h.

448{ return get_headphone_detect_gpio(); }

◆ pinInputMode()

int8_t AudioKit::pinInputMode ( )
inline

Get the number for mode-button.

Returns
-1 non-existent Others number

Definition at line 488 of file AudioKitHAL.h.

488{ return get_input_mode_id(); }

◆ pinInputPlay()

int8_t AudioKit::pinInputPlay ( )
inline

Get number for play function.

Returns
-1 non-existent Others number

Definition at line 504 of file AudioKitHAL.h.

504{ return get_input_play_id(); }

◆ pinInputRec()

int8_t AudioKit::pinInputRec ( )
inline

Get the record-button id for adc-button.

Returns
-1 non-existent Others button id

Definition at line 480 of file AudioKitHAL.h.

480{ return get_input_rec_id(); }

◆ pinInputSet()

int8_t AudioKit::pinInputSet ( )
inline

Get number for set function.

Returns
-1 non-existent Others number

Definition at line 496 of file AudioKitHAL.h.

496{ return get_input_set_id(); };

◆ pinPaEnable()

int8_t AudioKit::pinPaEnable ( )
inline

Get the gpio number for PA enable.

Returns
-1 non-existent Others gpio number

Definition at line 456 of file AudioKitHAL.h.

456{ return get_pa_enable_gpio(); }

◆ pinResetBoard()

int8_t AudioKit::pinResetBoard ( )
inline

Get gpio number to reset the board.

Returns
-1 non-existent Others gpio number

Definition at line 536 of file AudioKitHAL.h.

536{ return get_reset_board_gpio(); }

◆ pinResetCodec()

int8_t AudioKit::pinResetCodec ( )
inline

Get reset codec gpio number.

Returns
-1 non-existent Others gpio number

Definition at line 528 of file AudioKitHAL.h.

528{ return get_reset_codec_gpio(); }

◆ pinSpiCs()

int8_t AudioKit::pinSpiCs ( )
inline

SPI CS Pin for SD Drive.

Returns
int8_t

Definition at line 559 of file AudioKitHAL.h.

559 {
560 return spi_cs_pin;
561 }

◆ pinVolumeDown()

int8_t AudioKit::pinVolumeDown ( )
inline

Get number for volume down function.

Returns
-1 non-existent Others number

Definition at line 520 of file AudioKitHAL.h.

520{ return get_input_voldown_id(); }

◆ pinVolumeUp()

int8_t AudioKit::pinVolumeUp ( )
inline

number for volume up function

Returns
-1 non-existent Others number

Definition at line 512 of file AudioKitHAL.h.

512{ return get_input_volup_id(); }

◆ setActive()

bool AudioKit::setActive ( bool  active)
inline

Sets the codec active / inactive.

Definition at line 354 of file AudioKitHAL.h.

354 {
355 return audio_hal_ctrl_codec( hal_handle, cfg.codec_mode, active ? AUDIO_HAL_CTRL_START : AUDIO_HAL_CTRL_STOP) == ESP_OK;
356 }

◆ setMute()

bool AudioKit::setMute ( bool  mute)
inline

Mutes the output.

Definition at line 359 of file AudioKitHAL.h.

359 {
360 return audio_hal_set_mute(hal_handle, mute) == ESP_OK;
361 }

◆ setSampleRate()

bool AudioKit::setSampleRate ( audio_hal_iface_samples_t  sample_rate)
inline

Just update the sample rate.

Definition at line 407 of file AudioKitHAL.h.

407 {
408 bool result = true;
409 // update values
410 audio_hal_conf.i2s_iface.samples = cfg.sample_rate = sample_rate;
411
412 // apply new value
413 if (is_active){
414 // esp_err_t audio_hal_codec_iface_config(audio_hal_handle_t audio_hal, audio_hal_codec_mode_t mode, audio_hal_codec_i2s_iface_t *iface)
415 if (audio_hal_codec_iface_config(hal_handle, audio_hal_conf.codec_mode, &audio_hal_conf.i2s_iface) != ESP_OK) {
416 KIT_LOGE("audio_hal_ctrl_codec");
417 result = false;
418 }
419#if SETUP_ESP32_I2S && defined(ESP32)
420 // update I2S
421 if (cfg.is_i2s_active){
422 if (i2s_set_sample_rates(cfg.i2s_num, cfg.sampleRate()) != ESP_OK) {
423 KIT_LOGE("i2s_set_sample_rates");
424 result = false;
425 }
426 }
427#endif
428 }
429
430 return result;
431 }
uint32_t sampleRate()
Provides the sample rate in samples per second.
Definition: AudioKitHAL.h:106

◆ setSpeakerActive()

void AudioKit::setSpeakerActive ( bool  active)
inline

Activates/deactivates the speaker amplifier output This is working only if the driver is supporting the functionality.

Parameters
active

Definition at line 568 of file AudioKitHAL.h.

568 {
569 int paPin = get_pa_enable_gpio();
570 if (paPin>0){
571 digitalWrite(paPin, active);
572 } else {
573 KIT_LOGW("setSpeakerActive not supported");
574 }
575 }

◆ setupHeadphoneDetection()

void AudioKit::setupHeadphoneDetection ( )
inlineprotected

Setup the headphone detection.

Definition at line 631 of file AudioKitHAL.h.

631 {
632 int8_t paPin = pinPaEnable();
633 if (paPin>0){
634 headphonePin = pinHeadphoneDetect();
635 if (headphonePin>0){
636 pinMode(headphonePin, INPUT_PULLUP);
637 pinMode(paPin, OUTPUT);
638 KIT_LOGI("headphone detection is active");
639 } else {
640 KIT_LOGI("headphone detection not supported");
641 }
642 } else {
643 KIT_LOGI("headphone detection: PA not supported");
644 }
645 }
int8_t pinHeadphoneDetect()
Get the gpio number for headphone detection.
Definition: AudioKitHAL.h:448
int8_t pinPaEnable()
Get the gpio number for PA enable.
Definition: AudioKitHAL.h:456

◆ setVolume()

bool AudioKit::setVolume ( int  vol)
inline

Defines the Volume (in %) if volume is 0, mute is enabled,range is 0-100.

Definition at line 364 of file AudioKitHAL.h.

364 {
365 return (vol > 0) ? audio_hal_set_volume(hal_handle, vol) == ESP_OK : false;
366 }

◆ volume()

int AudioKit::volume ( )
inline

Determines the volume %.

Definition at line 369 of file AudioKitHAL.h.

369 {
370 int volume;
371 if (audio_hal_get_volume(hal_handle, &volume) != ESP_OK) {
372 volume = -1;
373 }
374 return volume;
375 }
int volume()
Determines the volume %.
Definition: AudioKitHAL.h:369

Member Data Documentation

◆ audio_hal_conf

audio_hal_codec_config_t AudioKit::audio_hal_conf
protected

Definition at line 618 of file AudioKitHAL.h.

◆ cfg

AudioKitConfig AudioKit::cfg
protected

Definition at line 617 of file AudioKitHAL.h.

◆ hal_handle

audio_hal_handle_t AudioKit::hal_handle = 0
protected

Definition at line 619 of file AudioKitHAL.h.

◆ headphoneIsConnected

bool AudioKit::headphoneIsConnected = false
protected

Definition at line 622 of file AudioKitHAL.h.

◆ headphonePin

int8_t AudioKit::headphonePin = -1
protected

Definition at line 624 of file AudioKitHAL.h.

◆ iface

audio_hal_codec_i2s_iface_t AudioKit::iface
protected

Definition at line 620 of file AudioKitHAL.h.

◆ is_active

bool AudioKit::is_active = false
protected

Definition at line 616 of file AudioKitHAL.h.

◆ speakerChangeTimeout

unsigned long AudioKit::speakerChangeTimeout = 0
protected

Definition at line 623 of file AudioKitHAL.h.

◆ spi_cs_pin

int8_t AudioKit::spi_cs_pin
protected

Definition at line 621 of file AudioKitHAL.h.


The documentation for this class was generated from the following file: