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

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. More...
 

Protected Member Functions

void setupHeadphoneDetection ()
 Setup the headphone detection. More...
 

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 203 of file AudioKitHAL.h.

Constructor & Destructor Documentation

◆ AudioKit()

AudioKit::AudioKit ( )
inline

Definition at line 206 of file AudioKitHAL.h.

206 {
207 // setup SPI for SD drives
208 selfAudioKit = this;
209 // added to constructor so that SPI is setup as part of global variable setup
210#ifdef ESP32
211 setupSPI();
212#endif
213 }

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 554 of file AudioKitHAL.h.

554 {
555 if (selfAudioKit->headphonePin>0){
556
557 // detect changes
558 bool isConnected = selfAudioKit->headphoneStatus();
559 if (selfAudioKit->headphoneIsConnected != isConnected) {
560 selfAudioKit->headphoneIsConnected = isConnected;
561
562 // update if things have stabilized
563 bool powerActive = !isConnected;
564 KIT_LOGW("Headphone jack has been %s", isConnected ? "inserted" : "removed");
565 selfAudioKit->setSpeakerActive(powerActive);
566 }
567 }
568 yield();
569 }
bool headphoneStatus()
Returns true if the headphone was detected.
Definition: AudioKitHAL.h:578
void setSpeakerActive(bool active)
Activates/deactivates the speaker amplifier output This is working only if the driver is supporting t...
Definition: AudioKitHAL.h:540

◆ begin()

bool AudioKit::begin ( AudioKitConfig  cnfg)
inline

Starts the codec.

Definition at line 233 of file AudioKitHAL.h.

233 {
234 KIT_LOGI(LOG_METHOD);
235 cfg = cnfg;
236 KIT_LOGI("Selected board: %d", AUDIOKIT_BOARD);
237
238 // if already active we stop first
239 if (is_active){
240 end();
241 }
242
243#ifdef ESP32
244 // release SPI for SD card if it is not necessary
245 if (AUDIOKIT_SETUP_SD && !cfg.sd_active){
246 p_spi->end();
247 }
248#endif
249 // setup headphone if necessary
251
252 audio_hal_conf.adc_input = cfg.adc_input;
253 audio_hal_conf.dac_output = cfg.dac_output;
254 audio_hal_conf.codec_mode = cfg.codec_mode;
255 audio_hal_conf.i2s_iface.mode = cfg.master_slave_mode;
256 audio_hal_conf.i2s_iface.fmt = cfg.fmt;
257 audio_hal_conf.i2s_iface.samples = cfg.sample_rate;
258 audio_hal_conf.i2s_iface.bits = cfg.bits_per_sample;
259
260 // init HAL
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");
265 return false;
266 }
267 }
268
269
270#ifdef ESP32
271
272 // setup i2s driver - with no queue
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");
276 return false;
277 }
278
279 // define i2s pins
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");
284 return false;
285 }
286
287//#if ESP_IDF_VERSION_MAJOR < 4
288 if (i2s_mclk_gpio_select(cfg.i2s_num, cfg.mclk_gpio) != ESP_OK) {
289 KIT_LOGE("i2s_mclk_gpio_select");
290 return false;
291 }
292//#endif
293
294#endif
295
296 // call start
297 if (!setActive(true)) {
298 KIT_LOGE("setActive");
299 return false;
300 }
301
302 is_active = true;
303 return true;
304 }
bool setActive(bool active)
Sets the codec active / inactive.
Definition: AudioKitHAL.h:328
bool end()
Stops the CODEC.
Definition: AudioKitHAL.h:307
void setupHeadphoneDetection()
Setup the headphone detection.
Definition: AudioKitHAL.h:603
audio_hal_iface_bits_t bits_per_sample
Definition: AudioKitHAL.h:76
audio_hal_dac_output_t dac_output
Definition: AudioKitHAL.h:71
audio_hal_adc_input_t adc_input
Definition: AudioKitHAL.h:70
audio_hal_iface_format_t fmt
Definition: AudioKitHAL.h:74
audio_hal_iface_mode_t master_slave_mode
Definition: AudioKitHAL.h:73
audio_hal_iface_samples_t sample_rate
Definition: AudioKitHAL.h:75
audio_hal_codec_mode_t codec_mode
Definition: AudioKitHAL.h:72

◆ config()

AudioKitConfig AudioKit::config ( )
inline

Provides the actual configuration.

Definition at line 325 of file AudioKitHAL.h.

325{ return cfg; }

◆ defaultConfig()

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

Provides the default configuration for input or output.

Definition at line 216 of file AudioKitHAL.h.

216 {
217 AudioKitConfig result;
218 switch(inout){
219 case AudioOutput:
220 result.codec_mode = AUDIO_HAL_CODEC_MODE_DECODE; // dac
221 break;
222 case AudioInput:
223 result.codec_mode = AUDIO_HAL_CODEC_MODE_ENCODE; // adc
224 break;
225 default:
226 result.codec_mode = AUDIO_HAL_CODEC_MODE_BOTH;
227 break;
228 }
229 return result;
230 }
Configuation for AudioKit.
Definition: AudioKitHAL.h:60

◆ end()

bool AudioKit::end ( )
inline

Stops the CODEC.

Definition at line 307 of file AudioKitHAL.h.

307 {
308 KIT_LOGI(LOG_METHOD);
309
310#ifdef ESP32
311 // uninstall i2s driver
312 i2s_driver_uninstall(cfg.i2s_num);
313#endif
314 // stop codec driver
315 audio_hal_ctrl_codec(hal_handle, cfg.codec_mode, AUDIO_HAL_CTRL_STOP);
316 // deinit
317 audio_hal_deinit(hal_handle);
318
319 is_active = false;
320 hal_handle = 0;
321 return true;
322 }

◆ headphoneStatus()

bool AudioKit::headphoneStatus ( )
inline

Returns true if the headphone was detected.

Returns
true
false

Definition at line 578 of file AudioKitHAL.h.

578 {
579 return headphonePin>0 ? !digitalRead(headphonePin) : false;
580 }

◆ isSDActive()

bool AudioKit::isSDActive ( )
inline

checks if the SD is active

Definition at line 583 of file AudioKitHAL.h.

583 {
584 return cfg.sd_active;
585 }

◆ pinAdcDetect()

int8_t AudioKit::pinAdcDetect ( )
inline

Get the gpio number for adc detection.

Returns
-1 non-existent Others gpio number

Definition at line 436 of file AudioKitHAL.h.

436{ 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 412 of file AudioKitHAL.h.

412{ 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 524 of file AudioKitHAL.h.

524{ 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 444 of file AudioKitHAL.h.

444{ 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 516 of file AudioKitHAL.h.

516{ 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 420 of file AudioKitHAL.h.

420{ 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 460 of file AudioKitHAL.h.

460{ 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 476 of file AudioKitHAL.h.

476{ 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 452 of file AudioKitHAL.h.

452{ 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 468 of file AudioKitHAL.h.

468{ 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 428 of file AudioKitHAL.h.

428{ 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 508 of file AudioKitHAL.h.

508{ 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 500 of file AudioKitHAL.h.

500{ return get_reset_codec_gpio(); }

◆ pinSpiCs()

int8_t AudioKit::pinSpiCs ( )
inline

SPI CS Pin for SD Drive.

Returns
int8_t

Definition at line 531 of file AudioKitHAL.h.

531 {
532 return spi_cs_pin;
533 }

◆ pinVolumeDown()

int8_t AudioKit::pinVolumeDown ( )
inline

Get number for volume down function.

Returns
-1 non-existent Others number

Definition at line 492 of file AudioKitHAL.h.

492{ 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 484 of file AudioKitHAL.h.

484{ return get_input_volup_id(); }

◆ setActive()

bool AudioKit::setActive ( bool  active)
inline

Sets the codec active / inactive.

Definition at line 328 of file AudioKitHAL.h.

328 {
329 return audio_hal_ctrl_codec( hal_handle, cfg.codec_mode, active ? AUDIO_HAL_CTRL_START : AUDIO_HAL_CTRL_STOP) == ESP_OK;
330 }

◆ setMute()

bool AudioKit::setMute ( bool  mute)
inline

Mutes the output.

Definition at line 333 of file AudioKitHAL.h.

333 {
334 return audio_hal_set_mute(hal_handle, mute) == ESP_OK;
335 }

◆ setSampleRate()

bool AudioKit::setSampleRate ( audio_hal_iface_samples_t  sample_rate)
inline

Just update the sample rate.

Definition at line 381 of file AudioKitHAL.h.

381 {
382 bool result = true;
383 // update values
384 audio_hal_conf.i2s_iface.samples = cfg.sample_rate = sample_rate;
385
386 // apply new value
387 if (is_active){
388 // 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)
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");
391 result = false;
392 }
393#ifdef ESP32
394 // update I2S
395 if (i2s_set_sample_rates(cfg.i2s_num, cfg.sampleRate()) != ESP_OK) {
396 KIT_LOGE("i2s_set_sample_rates");
397 result = false;
398 }
399#endif
400 }
401
402 return result;
403 }
uint32_t sampleRate()
Provides the sample rate in samples per second.
Definition: AudioKitHAL.h:96

◆ 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 540 of file AudioKitHAL.h.

540 {
541 int paPin = get_pa_enable_gpio();
542 if (paPin>0){
543 digitalWrite(paPin, active);
544 } else {
545 KIT_LOGW("setSpeakerActive not supported");
546 }
547 }

◆ setupHeadphoneDetection()

void AudioKit::setupHeadphoneDetection ( )
inlineprotected

Setup the headphone detection.

Definition at line 603 of file AudioKitHAL.h.

603 {
604 int8_t paPin = pinPaEnable();
605 if (paPin>0){
606 headphonePin = pinHeadphoneDetect();
607 if (headphonePin>0){
608 pinMode(headphonePin, INPUT_PULLUP);
609 pinMode(paPin, OUTPUT);
610 KIT_LOGI("headphone detection is active");
611 } else {
612 KIT_LOGI("headphone detection not supported");
613 }
614 } else {
615 KIT_LOGI("headphone detection: PA not supported");
616 }
617 }
int8_t pinHeadphoneDetect()
Get the gpio number for headphone detection.
Definition: AudioKitHAL.h:420
int8_t pinPaEnable()
Get the gpio number for PA enable.
Definition: AudioKitHAL.h:428

◆ 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 338 of file AudioKitHAL.h.

338 {
339 return (vol > 0) ? audio_hal_set_volume(hal_handle, vol) == ESP_OK : false;
340 }

◆ volume()

int AudioKit::volume ( )
inline

Determines the volume %.

Definition at line 343 of file AudioKitHAL.h.

343 {
344 int volume;
345 if (audio_hal_get_volume(hal_handle, &volume) != ESP_OK) {
346 volume = -1;
347 }
348 return volume;
349 }
int volume()
Determines the volume %.
Definition: AudioKitHAL.h:343

Member Data Documentation

◆ audio_hal_conf

audio_hal_codec_config_t AudioKit::audio_hal_conf
protected

Definition at line 590 of file AudioKitHAL.h.

◆ cfg

AudioKitConfig AudioKit::cfg
protected

Definition at line 589 of file AudioKitHAL.h.

◆ hal_handle

audio_hal_handle_t AudioKit::hal_handle = 0
protected

Definition at line 591 of file AudioKitHAL.h.

◆ headphoneIsConnected

bool AudioKit::headphoneIsConnected = false
protected

Definition at line 594 of file AudioKitHAL.h.

◆ headphonePin

int8_t AudioKit::headphonePin = -1
protected

Definition at line 596 of file AudioKitHAL.h.

◆ iface

audio_hal_codec_i2s_iface_t AudioKit::iface
protected

Definition at line 592 of file AudioKitHAL.h.

◆ is_active

bool AudioKit::is_active = false
protected

Definition at line 588 of file AudioKitHAL.h.

◆ speakerChangeTimeout

unsigned long AudioKit::speakerChangeTimeout = 0
protected

Definition at line 595 of file AudioKitHAL.h.

◆ spi_cs_pin

int8_t AudioKit::spi_cs_pin
protected

Definition at line 593 of file AudioKitHAL.h.


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