Skip to content

Commit

Permalink
base-i2s-a2dp.ino
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Nov 10, 2023
1 parent 1ec2ec7 commit 01fe16c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "AudioLibs/AudioKit.h"
#include "AudioLibs/AudioA2DP.h"

AudioInfo info(44100, 2, 16);
BluetoothA2DPSource a2dp_source;
AudioKitStream i2s;
const int16_t BYTES_PER_FRAME = 4;
Expand All @@ -27,9 +28,7 @@ void setup(void) {
Serial.println("starting I2S...");
auto cfg = i2s.defaultConfig(RX_MODE);
cfg.i2s_format = I2S_STD_FORMAT; // or try with I2S_LSB_FORMAT
cfg.bits_per_sample = 16;
cfg.channels = 2;
cfg.sample_rate = 44100;
cfg.copyFrom(info);
cfg.input_device = AUDIO_HAL_ADC_INPUT_LINE2; // microphone
i2s.begin(cfg);

Expand Down
55 changes: 23 additions & 32 deletions examples/examples-basic-api/base-i2s-a2dp/base-i2s-a2dp.ino
Original file line number Diff line number Diff line change
@@ -1,59 +1,50 @@
/**
* @file base-i2s-a2dp.ino
* @author Phil Schatzmann
* @brief see https://github.com/pschatzmann/arduino-audio-tools/blob/main/examples/base-i2s-a2dp/README.md
* @copyright GPLv3
*/
* @brief We use a INMP441 I2S microphone as input and send the data to A2DP
* Unfortunatly the data type from the microphone (int32_t) does not match with
* the required data type by A2DP (int16_t), so we need to convert.
* @copyright GPLv3
*/

#include "AudioTools.h"
#include "AudioLibs/AudioA2DP.h"

/**
* @brief We use a INMP441 I2S microphone as input and send the data to A2DP
* Unfortunatly the data type from the microphone (int32_t) does not match with the required data type by A2DP (int16_t),
* so we need to convert.
*/

AudioInfo info32(44100, 2, 32);
AudioInfo info16(44100, 2, 16);
BluetoothA2DPSource a2dp_source;
I2SStream i2s;
ConverterFillLeftAndRight<int16_t> bothChannels(LeftIsEmpty);
const size_t max_buffer_len = 150;
const int channels = 2;
const size_t max_buffer_bytes = max_buffer_len * sizeof(int16_t) * channels;
uint8_t buffer[max_buffer_bytes]={0};

// callback used by A2DP to provide the sound data - usually len is 128 2 channel int16 frames
int32_t get_sound_data(Frame* data, int32_t len) {
size_t req_bytes = min(max_buffer_bytes, len*2*sizeof(int16_t));
// the microphone provides data in int32_t -> we read it into the buffer of int32_t data so *2
size_t result_bytes = i2s.readBytes((uint8_t*)buffer, req_bytes*2);
// we have data only in 1 channel but we want to fill both
return bothChannels.convert((uint8_t*)buffer, result_bytes);
}
FormatConverterStream conv(i2s);
const int BYTES_PER_FRAME = 4;


int32_t get_sound_data(Frame* data, int32_t frameCount) {
return conv.readBytes((uint8_t*)data, frameCount*BYTES_PER_FRAME)/BYTES_PER_FRAME;
}

// Arduino Setup
void setup(void) {
Serial.begin(115200);
AudioLogger::instance().begin(Serial, AudioLogger::Info);

// setup conversion
conv.begin(info32, info16);

// start i2s input with default configuration
Serial.println("starting I2S...");
auto cfg = i2s.defaultConfig(RX_MODE);
cfg.i2s_format = I2S_STD_FORMAT; // or try with I2S_LSB_FORMAT
cfg.bits_per_sample = 16;
cfg.channels = 2;
cfg.sample_rate = 44100;
cfg.copyFrom(info32);
cfg.is_master = true;
i2s.begin(cfg);

// start the bluetooth
Serial.println("starting A2DP...");
a2dp_source.set_auto_reconnect(false);
a2dp_source.start("LEXON MINO L", get_sound_data);
// a2dp_source.set_auto_reconnect(false);
a2dp_source.start("LEXON MINO L", get_sound_data);

Serial.println("A2DP started");
}

// Arduino loop - repeated processing
void loop() {
delay(1000);
}
// Arduino loop - repeated processing
void loop() { delay(1000); }

0 comments on commit 01fe16c

Please sign in to comment.