4 #include "AudioKitHAL.h"
5 #include "AudioTools/CoreAudio/AudioI2S/I2SConfig.h"
6 #include "AudioTools/CoreAudio/AudioActions.h"
9 #error Upgrade the AudioKit library
15 static AudioKitStream *pt_AudioKitStream =
nullptr;
30 audio_hal_adc_input_t input_device = AUDIOKIT_DEFAULT_INPUT;
32 audio_hal_dac_output_t output_device = AUDIOKIT_DEFAULT_OUTPUT;
33 bool sd_active =
true;
34 bool default_actions_active =
true;
36 audio_hal_func_t driver = AUDIO_DRIVER;
41 audiokit_config.driver = driver;
42 audiokit_config.pins = pins;
43 audiokit_config.i2s_num = (i2s_port_t)port_no;
44 audiokit_config.adc_input = input_device;
45 audiokit_config.dac_output = output_device;
47 audiokit_config.master_slave_mode =
toMode();
50 audiokit_config.bits_per_sample = toBits();
52 audiokit_config.buffer_size = buffer_size;
53 audiokit_config.buffer_count = buffer_count;
56 audiokit_config.i2s_active =
false;
58 audiokit_config.sd_active = sd_active;
61 audiokit_config.sd_active =
false;
63 LOGW(
"sd_active = %s", sd_active ?
"true" :
"false" );
65 return audiokit_config;
70 AudioKitConfig audiokit_config;
77 i2s_pin_config_t i2s_pins = {};
79 board.get_i2s_pins((i2s_port_t)port_no, &i2s_pins);
80 pin_mck = i2s_pins.mck_io_num;
81 pin_bck = i2s_pins.bck_io_num;
82 pin_ws = i2s_pins.ws_io_num;
84 pin_data = i2s_pins.data_in_num;
85 pin_data_rx = I2S_PIN_NO_CHANGE;
87 pin_data = i2s_pins.data_out_num;
88 pin_data_rx = i2s_pins.data_in_num;
93 audio_hal_iface_bits_t toBits() {
95 static const int ia[] = {16, 24, 32};
96 static const audio_hal_iface_bits_t oa[] = {AUDIO_HAL_BIT_LENGTH_16BITS,
97 AUDIO_HAL_BIT_LENGTH_24BITS,
98 AUDIO_HAL_BIT_LENGTH_32BITS};
99 for (
int j = 0; j < 3; j++) {
106 return AUDIO_HAL_BIT_LENGTH_16BITS;
112 static const int ia[] = {8000, 11025, 16000, 22050,
113 24000, 32000, 44100, 48000};
114 static const audio_hal_iface_samples_t oa[] = {
115 AUDIO_HAL_08K_SAMPLES, AUDIO_HAL_11K_SAMPLES, AUDIO_HAL_16K_SAMPLES,
116 AUDIO_HAL_22K_SAMPLES, AUDIO_HAL_24K_SAMPLES, AUDIO_HAL_32K_SAMPLES,
117 AUDIO_HAL_44K_SAMPLES, AUDIO_HAL_48K_SAMPLES};
120 for (
int j = 0; j < 8; j++) {
126 if (new_diff < diff) {
132 LOGE(
"Sample Rate not supported: %d - using %d",
sample_rate, ia[result]);
139 static const int ia[] = {I2S_STD_FORMAT,
143 I2S_RIGHT_JUSTIFIED_FORMAT,
144 I2S_LEFT_JUSTIFIED_FORMAT,
146 static const audio_hal_iface_format_t oa[] = {
147 AUDIO_HAL_I2S_NORMAL, AUDIO_HAL_I2S_LEFT, AUDIO_HAL_I2S_RIGHT,
148 AUDIO_HAL_I2S_NORMAL, AUDIO_HAL_I2S_RIGHT, AUDIO_HAL_I2S_LEFT,
150 for (
int j = 0; j < 8; j++) {
151 if (ia[j] == i2s_format) {
156 LOGE(
"Format not supported: %d", i2s_format);
157 return AUDIO_HAL_I2S_NORMAL;
163 return (is_master) ? AUDIO_HAL_MODE_SLAVE : AUDIO_HAL_MODE_MASTER;
170 LOGD(
"-> %s",
"AUDIO_HAL_CODEC_MODE_DECODE");
171 return AUDIO_HAL_CODEC_MODE_DECODE;
173 LOGD(
"-> %s",
"AUDIO_HAL_CODEC_MODE_ENCODE");
174 return AUDIO_HAL_CODEC_MODE_ENCODE;
176 LOGD(
"-> %s",
"AUDIO_HAL_CODEC_MODE_BOTH");
177 return AUDIO_HAL_CODEC_MODE_BOTH;
207 cfg.logInfo(
"AudioKitStream");
211 if (!kit.begin(kit_cfg)){
212 LOGE(
"begin faild: please verify your AUDIOKIT_BOARD setting: %d", AUDIOKIT_BOARD);
217 i2s_stream.begin(cfg);
220 if (cfg.default_actions_active){
231 bool begin()
override {
245 return cfg.
rx_tx_mode == TX_MODE ? 0 : DEFAULT_BUFFER_SIZE;
248 size_t write(
const uint8_t *data,
size_t len)
override {
249 return i2s_stream.
write(data, len);
279 cfg.logInfo(
"AudioKit");
286 i2s_stream.begin(cfg);
295 bool setActive(
bool active) {
return kit.setActive(active); }
298 bool setMute(
bool mute) {
return kit.setMute(mute); }
302 if (vol>100) LOGW(
"Volume is > 100: %d",vol);
305 return kit.setVolume(vol);
310 if (vol>1.0) LOGW(
"Volume is > 1.0: %f",vol);
312 volume_value = 100.0 * vol;
313 return kit.setVolume(volume_value);
327 kit.setSpeakerActive(active);
333 return kit.headphoneStatus();
354 void addAction(
int pin,
void (*action)(
bool,
int,
void*),
void* ref=
nullptr ) {
358 actions.
add(pin, action, activeLogic, ref);
370 void addAction(
int pin,
void (*action)(
bool,
int,
void*), AudioActions::ActiveLogic activeLogic,
void* ref=
nullptr ) {
372 actions.
add(pin, action, activeLogic, ref);
387 LOGI(
"incrementVolume: %d -> %d",vol, volume_value);
388 kit.setVolume(volume_value);
415 pt_AudioKitStream->active = !pt_AudioKitStream->active;
416 pt_AudioKitStream->
setActive(pt_AudioKitStream->active);
425 pt_AudioKitStream->active =
true;
426 pt_AudioKitStream->
setActive(pt_AudioKitStream->active);
435 pt_AudioKitStream->active =
false;
436 pt_AudioKitStream->
setActive(pt_AudioKitStream->active);
445 AudioKit::actionHeadphoneDetection();
574 int volume_value = 40;
576 bool is_started =
false;
580 #if defined(USE_EXT_BUTTON_LOGIC)
581 input_key_service_info_t input_key_info[] = INPUT_KEY_DEFAULT_INFO();
582 int size =
sizeof(input_key_info) /
sizeof(input_key_info[0]);
583 for (
int j=0; j<size; j++){
584 if (pin == input_key_info[j].act_id){
585 switch(input_key_info[j].type){
586 case PERIPH_ID_ADC_BTN:
587 LOGD(
"getActionLogic for pin %d -> %d", pin, AudioActions::ActiveHigh);
588 return AudioActions::ActiveHigh;
589 case PERIPH_ID_BUTTON:
590 LOGD(
"getActionLogic for pin %d -> %d", pin, AudioActions::ActiveLow);
591 return AudioActions::ActiveLow;
592 case PERIPH_ID_TOUCH:
593 LOGD(
"getActionLogic for pin %d -> %d", pin, AudioActions::ActiveTouch);
594 return AudioActions::ActiveTouch;
598 LOGW(
"Undefined ActionLogic for pin: %d ",pin);
600 return AudioActions::ActiveLow;
608 if (! (cfg.sd_active && (AUDIOKIT_BOARD==5 || AUDIOKIT_BOARD==6))){
609 LOGD(
"actionStartStop")
612 LOGW(
"Mode Button ignored because of conflict: %d ",kit.pinInputMode());
616 if (! (cfg.sd_active && AUDIOKIT_BOARD==6)) {
617 LOGD(
"actionHeadphoneDetection pin:%d",kit.pinHeadphoneDetect())
620 LOGW(
"Headphone detection ignored because of conflict: %d ",kit.pinHeadphoneDetect());
624 if (! (cfg.sd_active && (AUDIOKIT_BOARD==1 || AUDIOKIT_BOARD==7))){
625 LOGD(
"actionVolumeDown")
627 LOGD(
"actionVolumeUp")
630 LOGW(
"Volume Buttons ignored because of conflict: %d ",kit.pinVolumeDown());
void stop()
Public generic methods.
Definition: AudioRuntime.h:27
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28