Skip to content

Commit 5668ac0

Browse files
committed
OutputMixer: resize
1 parent afa6609 commit 5668ac0

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

src/AudioTools/AudioOutput.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ template <typename T> class OutputMixer : public Print {
323323
size_bytes = copy_buffer_size_bytes;
324324
stream_idx = 0;
325325
memory_type = memoryType;
326-
allocate_buffers();
326+
allocate_buffers(size_bytes);
327327
return true;
328328
}
329329

@@ -414,6 +414,14 @@ template <typename T> class OutputMixer : public Print {
414414
return;
415415
}
416416

417+
/// Resizes the buffer to the indicated number of bytes
418+
void resize(int size){
419+
if(size != size_bytes){
420+
allocate_buffers(size);
421+
}
422+
size_bytes = size;
423+
}
424+
417425
protected:
418426
Vector<RingBuffer<T> *> buffers{0};
419427
Vector<T> output{0};
@@ -434,27 +442,27 @@ template <typename T> class OutputMixer : public Print {
434442
}
435443
}
436444

437-
void allocate_buffers() {
445+
void allocate_buffers(int size) {
438446
// allocate ringbuffers for each output
439447
for (int j = 0; j < output_count; j++) {
440448
if (buffers[j] != nullptr) {
441449
delete buffers[j];
442450
}
443451
#if defined(ESP32) && defined(ARDUINO)
444-
if (memory_type == PS_RAM && ESP.getFreePsram() >= size_bytes) {
445-
p_memory = ps_malloc(size_bytes);
446-
LOGI("Buffer %d allocated %d bytes in PS_RAM", j, size_bytes);
452+
if (memory_type == PS_RAM && ESP.getFreePsram() >= size) {
453+
p_memory = ps_malloc(size);
454+
LOGI("Buffer %d allocated %d bytes in PS_RAM", j, size);
447455
} else {
448-
p_memory = malloc(size_bytes);
449-
LOGI("Buffer %d allocated %d bytes in RAM", j, size_bytes);
456+
p_memory = malloc(size);
457+
LOGI("Buffer %d allocated %d bytes in RAM", j, size);
450458
}
451459
if (p_memory != nullptr) {
452-
buffers[j] = new (p_memory) RingBuffer<T>(size_bytes / sizeof(T));
460+
buffers[j] = new (p_memory) RingBuffer<T>(size / sizeof(T));
453461
} else {
454-
LOGE("Not enough memory to allocate %d bytes", size_bytes);
462+
LOGE("Not enough memory to allocate %d bytes", size);
455463
}
456464
#else
457-
buffers[j] = new RingBuffer<T>(size_bytes / sizeof(T));
465+
buffers[j] = new RingBuffer<T>(size / sizeof(T));
458466
#endif
459467
}
460468
}

0 commit comments

Comments
 (0)