2 #include "AudioBoard.h"
3 #include "AudioConfig.h"
4 #include "AudioTools/CoreAudio/AudioI2S/I2SStream.h"
9 #ifndef PIN_AUDIO_KIT_SD_CARD_CS
10 #define PIN_AUDIO_KIT_SD_CARD_CS 13
11 #define PIN_AUDIO_KIT_SD_CARD_MISO 2
12 #define PIN_AUDIO_KIT_SD_CARD_MOSI 15
13 #define PIN_AUDIO_KIT_SD_CARD_CLK 14
25 input_device_t input_device = ADC_INPUT_LINE1;
26 output_device_t output_device = DAC_OUTPUT_ALL;
28 bool sd_active =
true;
30 PinFunction i2s_function = PinFunction::UNDEFINED;
32 return input_device == alt.input_device &&
33 output_device == alt.output_device && *((
AudioInfo *)
this) == alt;
64 memcpy(&cfg, &cfg1,
sizeof(cfg1));
65 cfg.input_device = ADC_INPUT_LINE1;
66 cfg.output_device = DAC_OUTPUT_ALL;
88 if (p_board) p_board->end();
108 if (!is_active || p_board ==
nullptr) {
120 p_board->setConfig(codec_cfg);
124 virtual size_t write(
const uint8_t *data,
size_t len) {
125 LOGD(
"I2SStream::write: %d", len);
126 return i2s.
write(data, len);
130 virtual size_t readBytes(uint8_t *data,
size_t len)
override {
143 if (!is_active || p_board ==
nullptr)
return false;
144 return p_board->setVolume(vol * 100.0);
149 if (p_board ==
nullptr)
return 0.0f;
150 return static_cast<float>(p_board->getVolume()) / 100.0f;
157 if (p_board ==
nullptr)
return false;
158 return p_board->setMute(mute);
162 if (p_board ==
nullptr)
return false;
163 return p_board->setMute(mute, line);
168 if (p_board ==
nullptr)
return false;
169 return p_board->setPAPower(active);
174 if (!is_active || p_board ==
nullptr)
return false;
175 return p_board->setInputVolume(100.0 * vol);
179 AudioBoard &
board() {
return *p_board; }
189 if (p_board ==
nullptr)
return -1;
190 return p_board->getPins().getPinID(
function);
195 if (p_board ==
nullptr)
return -1;
196 return p_board->getPins().getPinID(
function, pos);
203 DriverPins &
getPins() {
return p_board->getPins(); }
211 CodecConfig codec_cfg;
212 AudioBoard *p_board =
nullptr;
213 bool is_active =
false;
219 if (!beginCodec(cfg)) {
224 is_active = i2s.begin(cfg);
228 if (is_active && tobeVol >= 0.0f) {
236 if (cfg.i2s_function == PinFunction::UNDEFINED){
238 auto i2s = p_board->getPins().getI2SPins(PinFunction::CODEC_ADC);
240 cfg.i2s_function = PinFunction::CODEC_ADC;
241 LOGI(
"using i2s_function: CODEC_ADC");
243 cfg.i2s_function = PinFunction::CODEC;
246 cfg.i2s_function = PinFunction::CODEC;
255 auto i2s = getI2SPins();
258 PinsI2S i2s_pins = i2s.value();
259 cfg.pin_bck = i2s_pins.bck;
260 cfg.pin_mck = i2s_pins.mclk;
261 cfg.pin_ws = i2s_pins.ws;
264 cfg.pin_data = i2s_pins.data_in;
267 cfg.pin_data = i2s_pins.data_out;
270 cfg.pin_data = i2s_pins.data_out;
271 cfg.pin_data_rx = i2s_pins.data_in;
277 audio_driver_local::Optional<PinsI2S> getI2SPins(){
279 audio_driver_local::Optional<PinsI2S> i2s;
281 return p_board->getPins().getI2SPins(cfg.i2s_function);
284 bool beginCodec(I2SCodecConfig info) {
288 codec_cfg.input_device = info.input_device;
289 codec_cfg.output_device = DAC_OUTPUT_NONE;
292 codec_cfg.output_device = info.output_device;
293 codec_cfg.input_device = ADC_INPUT_NONE;
296 codec_cfg.input_device = info.input_device;
297 codec_cfg.output_device = info.output_device;
300 codec_cfg.sd_active = info.sd_active;
301 LOGD(
"input: %d", info.input_device);
302 LOGD(
"output: %d", info.output_device);
303 codec_cfg.i2s.bits = toCodecBits(info.bits_per_sample);
304 codec_cfg.i2s.rate = toRate(info.sample_rate);
305 codec_cfg.i2s.fmt = toFormat(info.i2s_format);
307 codec_cfg.i2s.mode = info.is_master ? MODE_SLAVE : MODE_MASTER;
308 if (p_board ==
nullptr)
return false;
311 return p_board->begin(codec_cfg);
314 sample_bits_t toCodecBits(
int bits) {
317 LOGD(
"BIT_LENGTH_16BITS");
318 return BIT_LENGTH_16BITS;
320 LOGD(
"BIT_LENGTH_24BITS");
321 return BIT_LENGTH_24BITS;
323 LOGD(
"BIT_LENGTH_32BITS");
324 return BIT_LENGTH_32BITS;
326 LOGE(
"Unsupported bits: %d", bits);
327 return BIT_LENGTH_16BITS;
329 samplerate_t toRate(
int rate) {
354 if (rate <= 48000 || rate > 48000) {
358 LOGE(
"Invalid rate: %d using 44K", rate);
364 case I2S_PHILIPS_FORMAT:
368 case I2S_LEFT_JUSTIFIED_FORMAT:
372 case I2S_RIGHT_JUSTIFIED_FORMAT:
380 LOGE(
"unsupported mode");
RxTxMode
The Microcontroller is the Audio Source (TX_MODE) or Audio Sink (RX_MODE). RXTX_MODE is Source and Si...
Definition: AudioTypes.h:28