Skip to content

Commit

Permalink
Limit size for writeSamples
Browse files Browse the repository at this point in the history
  • Loading branch information
pschatzmann committed Oct 8, 2023
1 parent eaee87b commit cf4e067
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
6 changes: 4 additions & 2 deletions examples/sandbox/basic-a2dp-vs1053/basic-a2dp-vs1053.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ BluetoothA2DPSink a2dp_sink;
VS1053Stream out; // output
VS1053Config cfg;

// Write data from A2DP to buffer
// Write data to VS1053
void read_data_stream(const uint8_t *data, uint32_t bytes) {
out.write(data, bytes);
int samples = bytes / sizeof(int16_t);
// split up writes to max 512 samples
writeSamples<int16_t>(out, (int16_t*) data, samples, 512);
}


Expand Down
10 changes: 8 additions & 2 deletions src/AudioTools/AudioTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
// fix compile error for ESP32 C3
#undef HZ

// MIN
#undef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))


namespace audio_tools {

/**
Expand Down Expand Up @@ -317,13 +322,14 @@ size_t readSamples(Stream* p_stream, T* data, int samples){

/// guaranteed to return the requested data
template<typename T>
size_t writeSamples(Print* p_out, T* data, int samples){
size_t writeSamples(Print* p_out, T* data, int samples, int maxSamples=512){
uint8_t *p_result = (uint8_t*) data;
int open = samples*sizeof(T);
int total = 0;
// copy missing data
while (open>0){
int written = p_out->write(p_result+total, open);
int to_write = MIN(open, (int) maxSamples*sizeof(T));
int written = p_out->write(p_result+total, to_write );
open -= written;
total += written;
}
Expand Down
2 changes: 0 additions & 2 deletions src/AudioTools/Buffers.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "AudioTools/AudioLogger.h"
#include <limits.h> /* For INT_MAX */

#undef MIN
#define MIN(A, B) ((A) < (B) ? (A) : (B))

/**
* @defgroup buffers Buffers
Expand Down

0 comments on commit cf4e067

Please sign in to comment.