Skip to content

Commit 9ac7585

Browse files
committed
StreamCopy: do not use availableToWrite info by default
1 parent a66b651 commit 9ac7585

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

src/AudioTools/CoreAudio/AudioStreams.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -450,7 +450,7 @@ class GeneratedSoundStream : public AudioStream {
450450

451451
/// This is unbounded so we just return the buffer size
452452
virtual int available() override {
453-
return active ? DEFAULT_BUFFER_SIZE * 2 : 0;
453+
return active ? buffer_size : 0;
454454
}
455455

456456
/// privide the data as byte stream
@@ -466,9 +466,13 @@ class GeneratedSoundStream : public AudioStream {
466466

467467
void flush() override {}
468468

469+
/// Redefine the buffer size which is reported in available()
470+
void resize(int maxReadSize) { buffer_size = maxReadSize; }
471+
469472
protected:
470473
bool active = true; // support for legacy sketches
471474
SoundGenerator<T> *generator_ptr;
475+
int buffer_size = DEFAULT_BUFFER_SIZE;
472476
const char *source_not_defined_error = "Source not defined";
473477
};
474478

src/AudioTools/CoreAudio/StreamCopy.h

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,29 @@ template <class T>
2424
class StreamCopyT {
2525
public:
2626
StreamCopyT(Allocator &allocator, int bufferSize = DEFAULT_BUFFER_SIZE)
27-
: _allocator(allocator) {
27+
: _allocator(allocator), buffer_size(bufferSize) {
2828
TRACED();
29-
this->buffer_size = bufferSize;
3029
begin();
3130
}
3231
StreamCopyT(Print &to, AudioStream &from,
3332
int bufferSize = DEFAULT_BUFFER_SIZE,
3433
Allocator &allocator = DefaultAllocator)
35-
: _allocator(allocator) {
34+
: _allocator(allocator), buffer_size(bufferSize) {
3635
TRACED();
37-
this->buffer_size = bufferSize;
3836
begin(to, from);
3937
}
4038

4139
StreamCopyT(Print &to, Stream &from, int bufferSize = DEFAULT_BUFFER_SIZE,
4240
Allocator &allocator = DefaultAllocator)
43-
: _allocator(allocator) {
41+
: _allocator(allocator), buffer_size(bufferSize) {
4442
TRACED();
45-
this->buffer_size = bufferSize;
4643
begin(to, from);
4744
}
4845

4946
StreamCopyT(int bufferSize = DEFAULT_BUFFER_SIZE,
5047
Allocator &allocator = DefaultAllocator)
51-
: _allocator(allocator) {
48+
: _allocator(allocator), buffer_size(bufferSize) {
5249
TRACED();
53-
this->buffer_size = bufferSize;
5450
begin();
5551
}
5652

@@ -147,13 +143,13 @@ class StreamCopyT {
147143
if (check_available) {
148144
len = available();
149145
}
150-
size_t bytes_to_read = bytes;
146+
size_t bytes_to_read = len;
151147
size_t bytes_read = 0;
152148

153149
if (len > 0) {
154150
bytes_to_read = min(len, static_cast<size_t>(buffer_size));
155151
// don't overflow buffer
156-
if (to_write > 0) {
152+
if (check_available_for_write && to_write > 0) {
157153
bytes_to_read = min((int)bytes_to_read, to_write);
158154
}
159155

@@ -312,7 +308,7 @@ class StreamCopyT {
312308
/// resizes the copy buffer
313309
void resize(int len) {
314310
buffer_size = len;
315-
buffer.resize(buffer_size);
311+
buffer.resize(len);
316312
}
317313

318314
/// deactivate/activate copy - active by default

0 commit comments

Comments
 (0)