@@ -296,7 +296,8 @@ class HexDumpOutput : public AudioOutput {
296
296
297
297
298
298
/* *
299
- * @brief Mixing of multiple outputs to one final output
299
+ * @brief Mixing of multiple outputs to one final output.
300
+ * By default a RingBuffer is used as buffer type.
300
301
* @ingroup transform
301
302
* @author Phil Schatzmann
302
303
* @copyright GPLv3
@@ -340,12 +341,10 @@ class OutputMixer : public Print {
340
341
}
341
342
342
343
// / Starts the processing.
343
- bool begin (int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE,
344
- MemoryType memoryType = PS_RAM) {
344
+ bool begin (int copy_buffer_size_bytes = DEFAULT_BUFFER_SIZE) {
345
345
is_active = true ;
346
346
size_bytes = copy_buffer_size_bytes;
347
347
stream_idx = 0 ;
348
- memory_type = memoryType;
349
348
allocate_buffers (size_bytes);
350
349
return true ;
351
350
}
@@ -381,7 +380,7 @@ class OutputMixer : public Print {
381
380
size_t write (int idx, const uint8_t *buffer_c, size_t bytes) {
382
381
LOGD (" write idx %d: %d" , idx, bytes);
383
382
size_t result = 0 ;
384
- RingBuffer <T> *p_buffer = idx < output_count ? buffers[idx] : nullptr ;
383
+ BaseBuffer <T> *p_buffer = idx < output_count ? buffers[idx] : nullptr ;
385
384
assert (p_buffer != nullptr );
386
385
size_t samples = bytes / sizeof (T);
387
386
if (p_buffer->availableForWrite () >= samples) {
@@ -401,20 +400,25 @@ class OutputMixer : public Print {
401
400
402
401
// / Provides the bytes available to write for the indicated stream index
403
402
int availableForWrite (int idx) {
404
- RingBuffer <T> *p_buffer = buffers[idx];
403
+ BaseBuffer <T> *p_buffer = buffers[idx];
405
404
if (p_buffer == nullptr )
406
405
return 0 ;
407
406
return p_buffer->availableForWrite () * sizeof (T);
408
407
}
409
408
410
409
// / Provides the available bytes in the buffer
411
410
int available (int idx){
412
- RingBuffer <T> *p_buffer = buffers[idx];
411
+ BaseBuffer <T> *p_buffer = buffers[idx];
413
412
if (p_buffer == nullptr )
414
413
return 0 ;
415
414
return p_buffer->available () * sizeof (T);
416
415
}
417
416
417
+ // / Provides the % fill level of the buffer for the indicated index
418
+ int availablePercent (int idx){
419
+ return 100.0 * available (idx) / size_bytes;
420
+ }
421
+
418
422
// / Force output to final destination
419
423
void flushMixer () {
420
424
LOGD (" flush" );
@@ -465,7 +469,6 @@ class OutputMixer : public Print {
465
469
size_bytes = size;
466
470
}
467
471
468
-
469
472
size_t writeSilence (size_t bytes) {
470
473
if (bytes == 0 ) return 0 ;
471
474
uint8_t silence[bytes];
@@ -495,8 +498,18 @@ class OutputMixer : public Print {
495
498
stream_idx++;
496
499
}
497
500
501
+ // / Define callback to allocate custum buffer types
502
+ void setCreateBufferCallback (BaseBuffer<T>* (*cb)(int size) ){
503
+ create_buffer_cb = cb;
504
+ }
505
+
506
+ // / Provides the write buffer for the indicated index
507
+ BaseBuffer<T>* getBuffer (int idx){
508
+ return idx < output_count ? buffers[idx] : nullptr ;
509
+ }
510
+
498
511
protected:
499
- Vector<RingBuffer <T> *> buffers{0 };
512
+ Vector<BaseBuffer <T> *> buffers{0 };
500
513
Vector<T> output{0 };
501
514
Vector<float > weights{0 };
502
515
Print *p_final_output = nullptr ;
@@ -505,9 +518,13 @@ class OutputMixer : public Print {
505
518
int stream_idx = 0 ;
506
519
int size_bytes = 0 ;
507
520
int output_count = 0 ;
508
- MemoryType memory_type;
509
521
void *p_memory = nullptr ;
510
522
bool is_auto_index = true ;
523
+ BaseBuffer<T>* (*create_buffer_cb)(int size) = create_buffer;
524
+
525
+ static BaseBuffer<T>* create_buffer (int size) {
526
+ return new RingBuffer<T>(size / sizeof (T));
527
+ }
511
528
512
529
void update_total_weights () {
513
530
total_weights = 0.0 ;
@@ -522,22 +539,7 @@ class OutputMixer : public Print {
522
539
if (buffers[j] != nullptr ) {
523
540
delete buffers[j];
524
541
}
525
- #if defined(ESP32) && defined(ARDUINO)
526
- if (memory_type == PS_RAM && ESP.getFreePsram () >= size) {
527
- p_memory = ps_malloc (size);
528
- LOGI (" Buffer %d allocated %d bytes in PS_RAM" , j, size);
529
- } else {
530
- p_memory = malloc (size);
531
- LOGI (" Buffer %d allocated %d bytes in RAM" , j, size);
532
- }
533
- if (p_memory != nullptr ) {
534
- buffers[j] = new (p_memory) RingBuffer<T>(size / sizeof (T));
535
- } else {
536
- LOGE (" Not enough memory to allocate %d bytes" , size);
537
- }
538
- #else
539
- buffers[j] = new RingBuffer<T>(size / sizeof (T));
540
- #endif
542
+ buffers[j] = create_buffer (size);
541
543
}
542
544
}
543
545
@@ -546,11 +548,6 @@ class OutputMixer : public Print {
546
548
for (int j = 0 ; j < output_count; j++) {
547
549
if (buffers[j] != nullptr ) {
548
550
delete buffers[j];
549
- #ifdef ESP32
550
- if (p_memory != nullptr ) {
551
- free (p_memory);
552
- }
553
- #endif
554
551
buffers[j] = nullptr ;
555
552
}
556
553
}
0 commit comments