Skip to content

Commit bce751a

Browse files
committed
A2dP
1 parent dde0da6 commit bce751a

File tree

7 files changed

+44
-35
lines changed

7 files changed

+44
-35
lines changed

src/AudioA2DP.h

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,6 @@ int32_t a2dp_stream_source_sound_data(Frame* data, int32_t len) {
8282
result_len = len;
8383
}
8484

85-
86-
// Log result
87-
// if (AudioLogger::instance().level()==AudioLogger::Debug){
88-
// LOGD("a2dp_stream_source_sound_data %d -> %d", len, result_len);
89-
// } else {
90-
// if (result_len<len){
91-
// LOGW("a2dp_stream_source_sound_data underflow: %d -> %d ", len, result_len);
92-
// }
93-
// }
94-
95-
// allow some other task
96-
//yield();
97-
9885
return result_len;
9986
}
10087

@@ -104,7 +91,7 @@ void a2dp_stream_sink_sound_data(const uint8_t* data, uint32_t len) {
10491
uint32_t result_len = a2dp_buffer.writeArray(data, len);
10592
LOGI("a2dp_stream_sink_sound_data %d -> %d", len, result_len);
10693
// allow some other task
107-
yield();
94+
//yield();
10895
}
10996
}
11097

@@ -152,6 +139,7 @@ class A2DPStream : public AudioStream {
152139
void begin(RxTxMode mode, const char* name){
153140
this->mode = mode;
154141
this->name = name;
142+
LOGI("Connecting to %s",name);
155143

156144

157145
switch (mode){
@@ -160,7 +148,7 @@ class A2DPStream : public AudioStream {
160148
source(); // allocate object
161149
a2dp_source->start((char*)name, a2dp_stream_source_sound_data);
162150
a2dp_source->set_on_connection_state_changed(a2dpStateCallback, this);
163-
while(!a2dp_source->isConnected()){
151+
while(!a2dp_source->is_connected()){
164152
delay(1000);
165153
}
166154
LOGI("a2dp_source is connected...");
@@ -173,7 +161,7 @@ class A2DPStream : public AudioStream {
173161
a2dp_sink->set_stream_reader(&a2dp_stream_sink_sound_data, false);
174162
a2dp_sink->start((char*)name);
175163
a2dp_sink->set_on_connection_state_changed(a2dpStateCallback, this);
176-
while(!a2dp_sink->isConnected()){
164+
while(!a2dp_sink->is_connected()){
177165
delay(1000);
178166
}
179167
LOGI("a2dp_sink is connected...");
@@ -185,8 +173,8 @@ class A2DPStream : public AudioStream {
185173
/// checks if we are connected
186174
bool isConnected() {
187175
if (a2dp_source==nullptr && a2dp_sink==nullptr) return false;
188-
if (a2dp_source!=nullptr) return a2dp_source->isConnected();
189-
return a2dp_sink->isConnected();
176+
if (a2dp_source!=nullptr) return a2dp_source->is_connected();
177+
return a2dp_sink->is_connected();
190178
}
191179

192180
/// is ready to process data
@@ -201,11 +189,16 @@ class A2DPStream : public AudioStream {
201189

202190
/// Writes the data into a temporary send buffer - where it can be picked up by the callback
203191
virtual size_t write(const uint8_t* data, size_t len) {
192+
LOGD("%s: %zu", LOG_METHOD, len);
193+
204194
size_t result = 0;
205195
if (is_a2dp_active){
206-
LOGD("write %d", len);
207-
result = a2dp_buffer.writeArray(data,len);
208-
LOGI("write %d -> %d", len,result);
196+
if (a2dp_buffer.availableToWrite()>=len){
197+
result = a2dp_buffer.writeArray(data, len);
198+
LOGD("write %d -> %d", len, result);
199+
} else {
200+
LOGI("write %d - buffer full (available sound: %d, free: %d)", len, a2dp_buffer.available(), a2dp_buffer.availableToWrite() );
201+
}
209202
} else {
210203
LOGW( "write failed because !is_a2dp_active (yet)");
211204
delay(5000);
@@ -274,9 +267,9 @@ class A2DPStream : public AudioStream {
274267
static void a2dpStateCallback(esp_a2d_connection_state_t state, void *caller){
275268
LOGD(LOG_METHOD);
276269
A2DPStream *self = (A2DPStream*)caller;
277-
// if (state==ESP_A2D_CONNECTION_STATE_CONNECTED){
278-
// self->is_a2dp_active = true;
279-
// }
270+
if (state==ESP_A2D_CONNECTION_STATE_CONNECTED){
271+
is_a2dp_active = true;
272+
}
280273
LOGW("==> state: %s", self->a2dp->to_str(state));
281274
}
282275

src/AudioCodecs/CodecMP3Helix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class MP3DecoderHelix : public AudioDecoder {
101101

102102
/// Write mp3 data to decoder
103103
size_t write(const void* mp3Data, size_t len) {
104-
LOGD(LOG_METHOD);
104+
LOGD("%s: %zu", LOG_METHOD, len);
105105
return mp3==nullptr ? 0 : mp3->write(mp3Data, len);
106106
}
107107

src/AudioConfig.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222

2323
#define USE_AUDIO_LOGGING true
24-
#define PRINTF_BUFFER_SIZE 80
24+
#define PRINTF_BUFFER_SIZE 160
2525
#define LOG_LEVEL AudioLogger::Warning
2626
#define LOG_STREAM Serial
2727
#define LOG_METHOD __PRETTY_FUNCTION__
@@ -38,8 +38,8 @@
3838
#define I2S_DEFAULT_PORT 0
3939
#define I2S_BUFFER_SIZE 512
4040
#define I2S_BUFFER_COUNT 5 // 20
41-
#define A2DP_BUFFER_SIZE 512
42-
#define A2DP_BUFFER_COUNT 20
41+
#define A2DP_BUFFER_SIZE 1280
42+
#define A2DP_BUFFER_COUNT 50
4343
#define CODEC_DELAY_MS 10
4444
#define COPY_DELAY_ON_NODATA 10
4545
/**

src/AudioTools/AudioCopy.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,16 @@ class StreamCopyT {
155155
this->onWriteObj = obj;
156156
}
157157

158+
/// Defines the max number of retries
158159
void setRetry(int retry){
159160
retryLimit = retry;
160161
}
161162

163+
/// Provides the buffer size
164+
int bufferSize() {
165+
return buffer_size;
166+
}
167+
162168
protected:
163169
Stream *from = nullptr;
164170
Print *to = nullptr;

src/AudioTools/AudioOutput.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ class EncodedAudioStream : public AudioPrint, public AudioBaseInfoSource {
313313

314314
/// encode the data
315315
virtual size_t write(const uint8_t *data, size_t len){
316-
LOGD(LOG_METHOD);
316+
LOGD("%s: %zu", LOG_METHOD, len);
317317
if(writer_ptr==nullptr || data==nullptr){
318318
LOGE("NPE");
319319
return 0;
@@ -572,11 +572,11 @@ class FormatConverterStream : public AudioPrint {
572572
// copy 1:1
573573
convert.multiply = 1;
574574
convert.step = 1;
575-
} else if (p_info_in->channels = 1 && p_info_out->channels>1){
575+
} else if (p_info_in->channels == 1 && p_info_out->channels>1){
576576
// generate multiple output channels per frame
577577
convert.multiply = p_info_out->channels;
578578
convert.step = 1;
579-
} else if (p_info_out->channels = 1){
579+
} else if (p_info_out->channels == 1){
580580
convert.multiply = 1;
581581
convert.step = p_info_in->channels; // just output 1 channel per frame
582582
} else {

src/AudioTools/AudioPlayer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,9 @@ class AudioPlayer: public AudioBaseInfoDependent {
410410
// get first streem
411411
p_input_stream = p_source->nextStream(1);
412412
if (p_input_stream!=nullptr) {
413-
copier.setCallbackOnWrite(decodeMetaData, this);
413+
if (meta_active){
414+
copier.setCallbackOnWrite(decodeMetaData, this);
415+
}
414416
copier.begin(*p_out_decoding, *p_input_stream);
415417
timeout = millis() + p_source->timeoutMs();
416418
active = isActive;

src/AudioTools/Buffers.h

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,9 @@ class NBuffer : public BaseBuffer<T> {
331331
buffer_size = size;
332332
for (int j=0;j<count;j++){
333333
avaliable_buffers[j] = new SingleBuffer<T>(size);
334+
if (avaliable_buffers[j]==nullptr){
335+
LOGE("Not Enough Memory for buffer %d",j);
336+
}
334337
}
335338
}
336339

@@ -400,7 +403,7 @@ class NBuffer : public BaseBuffer<T> {
400403
return result;
401404
}
402405

403-
// deterMINes the available entries for the current read buffer
406+
// determines the available entries for the current read buffer
404407
int available() {
405408
if (actual_read_buffer==nullptr){
406409
actual_read_buffer = getNextFilledBuffer();
@@ -414,6 +417,9 @@ class NBuffer : public BaseBuffer<T> {
414417
resetCurrent();
415418
result = actual_read_buffer==nullptr? 0 : actual_read_buffer->available();
416419
}
420+
if(actual_read_buffer!=nullptr){
421+
result+=write_buffer_count*buffer_size;
422+
}
417423
return result;
418424
}
419425

@@ -432,7 +438,7 @@ class NBuffer : public BaseBuffer<T> {
432438
addFilledBuffer(actual_write_buffer);
433439
actual_write_buffer = getNextAvailableBuffer();
434440
}
435-
return actual_write_buffer->availableToWrite();
441+
return actual_write_buffer->availableToWrite() + (buffer_count - write_buffer_count) *buffer_size;
436442
}
437443

438444
// resets all buffers
@@ -485,6 +491,7 @@ class NBuffer : public BaseBuffer<T> {
485491
unsigned long start_time = 0;
486492
unsigned long sample_count = 0;
487493

494+
488495
void resetCurrent(){
489496
if (actual_read_buffer!=nullptr){
490497
actual_read_buffer->reset();
@@ -505,7 +512,8 @@ class NBuffer : public BaseBuffer<T> {
505512
}
506513
return result;
507514
}
508-
515+
516+
509517
bool addAvailableBuffer(BaseBuffer<T> *buffer){
510518
bool result = false;
511519
for (int j=0;j<buffer_count;j++){

0 commit comments

Comments
 (0)