Skip to content

Commit 820359e

Browse files
committed
A2DP basic examples corrections
1 parent 8639e2a commit 820359e

File tree

5 files changed

+45
-27
lines changed

5 files changed

+45
-27
lines changed

examples/examples-basic-api/base-adc-a2dp/base-adc-a2dp.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,13 @@ BluetoothA2DPSource a2dp_source;
2222
ConverterScaler<int16_t> scaler(1.0, -26427, 32700 );
2323

2424
// callback used by A2DP to provide the sound data
25-
int32_t get_sound_data(Frame* frames, int32_t count) {
25+
int32_t get_sound_data(Frame* frames, int32_t frameCount) {
2626
uint8_t *data = (uint8_t*)frames;
27-
int channels = 2;
27+
int frameSize = 4;
2828
size_t len = count * channels * sizeof(int16_t);
29-
// the ADC provides data in 16 bits
30-
size_t result_len = adc.readBytes(data, len);
31-
scaler.convert(data, result_len);
32-
return result_len;
29+
size_t resultBytes = adc.readBytes(data, frameCount*frameSize);
30+
scaler.convert(data, resultBytes);
31+
return resultBytes/frameSize;
3332
}
3433

3534
// Arduino Setup
@@ -42,6 +41,7 @@ void setup(void) {
4241

4342
// start the bluetooth
4443
Serial.println("starting A2DP...");
44+
a2dp_source.set_auto_reconnect(false);
4545
a2dp_source.start("MyMusic", get_sound_data);
4646
}
4747

examples/examples-basic-api/base-i2s-a2dp/base-i2s-a2dp.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ void setup(void) {
4949

5050
// start the bluetooth
5151
Serial.println("starting A2DP...");
52+
a2dp_source.set_auto_reconnect(false);
5253
a2dp_source.start("LEXON MINO L", get_sound_data);
5354
}
5455

examples/sandbox/basic-a2dp-vs1053/basic-a2dp-vs1053.ino

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,44 +15,41 @@
1515

1616
BluetoothA2DPSink a2dp_sink;
1717
VS1053Stream out; // final output
18-
bool active = false;
19-
// Write data to SPDIF in callback
20-
void read_data_stream(const uint8_t *data, uint32_t length) {
21-
if (active) out.write(data, length);
18+
VS1053Config cfg;
19+
CallbackBufferedStream<uint8_t> buffer(1024,10);
20+
StreamCopy copier(out, buffer); // copies sound into i2s
21+
22+
// Write data from A2DP to buffer
23+
void read_data_stream(const uint8_t *data, uint32_t frames) {
24+
int frameSize = 4;
25+
buffer.write(data, frames*frameSize);
2226
}
2327

28+
2429
// for esp_a2d_audio_state_t see https://docs.espressif.com/projects/esp-idf/en/latest/esp32/api-reference/bluetooth/esp_a2dp.html#_CPPv421esp_a2d_audio_state_t
2530
void audio_state_changed(esp_a2d_audio_state_t state, void *ptr){
2631
Serial.println(a2dp_sink.to_str(state));
2732
switch(state){
2833
case ESP_A2D_AUDIO_STATE_STARTED:
29-
out.begin();
30-
active = true;
34+
buffer.begin();
35+
out.begin(cfg);
3136
break;
3237
case ESP_A2D_AUDIO_STATE_STOPPED:
3338
case ESP_A2D_AUDIO_STATE_REMOTE_SUSPEND:
34-
active = false;
3539
out.end();
40+
buffer.end();
41+
buffer.clear();
3642
break;
3743
}
3844
}
3945

40-
4146
void setup() {
4247
Serial.begin(115200);
4348
AudioLogger::instance().begin(Serial, AudioLogger::Info);
4449

45-
// register callbacks
46-
a2dp_sink.set_stream_reader(read_data_stream, false);
47-
a2dp_sink.set_on_audio_state_changed(audio_state_changed);
48-
49-
// Start Bluetooth Audio Receiver
50-
a2dp_sink.set_auto_reconnect(false);
51-
a2dp_sink.start("a2dp-vs1053");
52-
5350
// setup VS1053
54-
auto cfg = out.defaultConfig();
55-
cfg.sample_rate = a2dp_sink.sample_rate();
51+
cfg = out.defaultConfig();
52+
cfg.sample_rate = 44100;
5653
cfg.channels = 2;
5754
cfg.bits_per_sample = 16;
5855
// Use your custom pins or define in AudioCodnfig.h
@@ -61,8 +58,16 @@ void setup() {
6158
//cfg.dreq_pin = VS1053_DREQ;
6259
//cfg.reset_pin = VS1053_RESET;
6360

64-
out.begin(cfg);
65-
out.end();
61+
// register callbacks
62+
a2dp_sink.set_stream_reader(read_data_stream, false);
63+
a2dp_sink.set_on_audio_state_changed(audio_state_changed);
64+
// Start Bluetooth Audio Receiver
65+
a2dp_sink.set_auto_reconnect(false);
66+
a2dp_sink.start("a2dp-vs1053");
67+
68+
6669
}
6770

68-
void loop() { delay(1000); }
71+
void loop() {
72+
if (buffer) copier.copy();
73+
}

src/AudioTools/AudioStreams.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -815,6 +815,18 @@ class CallbackBufferedStream : public AudioStreamX {
815815
;
816816
}
817817

818+
/// Clears the data in the buffer
819+
void clear() {
820+
if (active){
821+
callback_buffer_ptr->reset();
822+
}
823+
}
824+
825+
/// Returns true if active
826+
operator bool(){
827+
return active;
828+
}
829+
818830
protected:
819831
NBuffer<T> *callback_buffer_ptr;
820832
bool active;

0 commit comments

Comments
 (0)