@@ -22,38 +22,35 @@ namespace audio_tools {
22
22
template <class T >
23
23
class StreamCopyT {
24
24
public:
25
- StreamCopyT () = default ;
26
25
27
- StreamCopyT (Print &to, AudioStream &from, int buffer_size =DEFAULT_BUFFER_SIZE){
26
+ StreamCopyT (Print &to, AudioStream &from, int bufferSize =DEFAULT_BUFFER_SIZE){
28
27
TRACED ();
28
+ this ->buffer_size = bufferSize;
29
29
begin (to, from);
30
- resize (buffer_size);
31
- if (!buffer){
32
- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
33
- }
34
30
}
35
31
36
- StreamCopyT (Print &to, Stream &from, int buffer_size =DEFAULT_BUFFER_SIZE){
32
+ StreamCopyT (Print &to, Stream &from, int bufferSize =DEFAULT_BUFFER_SIZE){
37
33
TRACED ();
34
+ this ->buffer_size = bufferSize;
38
35
begin (to, from);
39
- resize (buffer_size);
40
- if (!buffer){
41
- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
42
- }
43
36
}
44
37
45
- StreamCopyT (int buffer_size =DEFAULT_BUFFER_SIZE){
38
+ StreamCopyT (int bufferSize =DEFAULT_BUFFER_SIZE){
46
39
TRACED ();
47
- resize (buffer_size);
48
- if (!buffer){
49
- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
50
- }
40
+ this ->buffer_size = bufferSize;
41
+ begin ();
51
42
}
52
43
53
44
// / (Re)starts the processing
54
45
void begin (){
55
- is_first = true ;
56
- LOGI (" buffer_size=%d" ,buffer_size);
46
+ TRACED ();
47
+ is_first = true ;
48
+ resize (buffer_size);
49
+ if (buffer){
50
+ LOGI (" buffer_size=%d" ,buffer_size);
51
+ } else {
52
+ LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
53
+ }
57
54
}
58
55
59
56
// / Ends the processing
@@ -66,16 +63,14 @@ class StreamCopyT {
66
63
void begin (Print &to, Stream &from){
67
64
this ->from = new AudioStreamWrapper (from);
68
65
this ->to = &to;
69
- is_first = true ;
70
- LOGI (" buffer_size=%d" ,buffer_size);
66
+ begin ();
71
67
}
72
68
73
69
// / assign a new output and input stream
74
70
void begin (Print &to, AudioStream &from){
75
71
this ->from = &from;
76
72
this ->to = &to;
77
- is_first = true ;
78
- LOGI (" buffer_size=%d" ,buffer_size);
73
+ begin ();
79
74
}
80
75
81
76
// / Provides a pointer to the copy source. Can be used to check if the source is defined.
@@ -90,6 +85,13 @@ class StreamCopyT {
90
85
91
86
// / copies the data from the source to the destination and returns the processed number of bytes
92
87
inline size_t copy () {
88
+ p_converter = nullptr ;
89
+ return copyBytes (buffer_size);
90
+ }
91
+
92
+ // / copies the data from the source to the destination and applies the converter - the result is the processed number of bytes
93
+ inline size_t copy (BaseConverter &converter) {
94
+ p_converter = &converter;
93
95
return copyBytes (buffer_size);
94
96
}
95
97
@@ -148,6 +150,9 @@ class StreamCopyT {
148
150
// determine mime
149
151
notifyMime (buffer.data (), bytes_to_read);
150
152
153
+ // convert data
154
+ if (p_converter!=nullptr ) p_converter->convert ((uint8_t *)buffer.data (), result );
155
+
151
156
// write data
152
157
result = write (bytes_read, delayCount);
153
158
@@ -176,28 +181,6 @@ class StreamCopyT {
176
181
return result;
177
182
}
178
183
179
-
180
- // / available bytes of the data source
181
- int available () {
182
- int result = 0 ;
183
- if (from!=nullptr ) {
184
- if (availableCallback!=nullptr ){
185
- result = availableCallback ((Stream*)from);
186
- } else {
187
- result = from->available ();
188
- }
189
- } else {
190
- LOGW (" source not defined" );
191
- }
192
- LOGD (" available: %d" , result);
193
- return result;
194
- }
195
-
196
- // / Defines the dealy that is used if no data is available
197
- void setDelayOnNoData (int delayMs){
198
- delay_on_no_data = delayMs;
199
- }
200
-
201
184
// / Copies pages * buffersize samples: returns the processed number of bytes
202
185
size_t copyN (size_t pages){
203
186
if (!active) return 0 ;
@@ -245,6 +228,27 @@ class StreamCopyT {
245
228
return result;
246
229
}
247
230
231
+ // / available bytes of the data source
232
+ int available () {
233
+ int result = 0 ;
234
+ if (from!=nullptr ) {
235
+ if (availableCallback!=nullptr ){
236
+ result = availableCallback ((Stream*)from);
237
+ } else {
238
+ result = from->available ();
239
+ }
240
+ } else {
241
+ LOGW (" source not defined" );
242
+ }
243
+ LOGD (" available: %d" , result);
244
+ return result;
245
+ }
246
+
247
+ // / Defines the dealy that is used if no data is available
248
+ void setDelayOnNoData (int delayMs){
249
+ delay_on_no_data = delayMs;
250
+ }
251
+
248
252
// / Provides the actual mime type, that was determined from the first available data
249
253
const char * mime () {
250
254
return actual_mime;
@@ -347,7 +351,7 @@ class StreamCopyT {
347
351
AudioStream *from = nullptr ;
348
352
Print *to = nullptr ;
349
353
Vector<uint8_t > buffer{0 };
350
- int buffer_size;
354
+ int buffer_size = DEFAULT_BUFFER_SIZE ;
351
355
void (*onWrite)(void *obj, void *buffer, size_t len) = nullptr ;
352
356
void (*notifyMimeCallback)(const char *mime) = nullptr ;
353
357
int (*availableCallback)(Stream*stream)=nullptr ;
@@ -365,6 +369,8 @@ class StreamCopyT {
365
369
int min_copy_size = 1 ;
366
370
bool is_sync_audio_info = false ;
367
371
AudioInfoSupport *p_audio_info_support = nullptr ;
372
+ BaseConverter* p_converter = nullptr ;
373
+
368
374
369
375
void syncAudioInfo (){
370
376
// synchronize audio info
@@ -436,70 +442,12 @@ class StreamCopyT {
436
442
};
437
443
438
444
/* *
439
- * @brief We provide the typeless StreamCopy as a subclass of StreamCopyT
445
+ * @brief We provide the typeless StreamCopy
440
446
* @ingroup tools
441
447
* @author Phil Schatzmann
442
448
* @copyright GPLv3
443
449
*/
444
- class StreamCopy : public StreamCopyT <uint8_t > {
445
- public:
446
- StreamCopy (int buffer_size=DEFAULT_BUFFER_SIZE): StreamCopyT<uint8_t >(buffer_size) {
447
- TRACED ();
448
- }
449
-
450
- StreamCopy (AudioStream &to, AudioStream &from, int buffer_size=DEFAULT_BUFFER_SIZE) : StreamCopyT<uint8_t >(to, from, buffer_size){
451
- TRACED ();
452
- p_audio_info_support = &to;
453
- }
454
-
455
- StreamCopy (AudioOutput &to, AudioStream &from, int buffer_size=DEFAULT_BUFFER_SIZE) : StreamCopyT<uint8_t >(to, from, buffer_size){
456
- TRACED ();
457
- p_audio_info_support = &to;
458
- }
459
-
460
- StreamCopy (Print &to, AudioStream &from, int buffer_size=DEFAULT_BUFFER_SIZE) : StreamCopyT<uint8_t >(to, from, buffer_size){
461
- TRACED ();
462
- }
463
-
464
- StreamCopy (Print &to, Stream &from, int buffer_size=DEFAULT_BUFFER_SIZE) : StreamCopyT<uint8_t >(to, from, buffer_size){
465
- TRACED ();
466
- }
467
-
468
- // / copies the data from the source to the destination and applies the converter - the result is the processed number of bytes
469
- size_t copy (BaseConverter &converter) {
470
- size_t result = available ();
471
- size_t delayCount = 0 ;
472
- syncAudioInfo ();
473
- BaseConverter* coverter_ptr = &converter;
474
- if (result>0 ){
475
- size_t bytes_to_read = min (result, static_cast <size_t >(buffer_size) );
476
- result = from->readBytes ((uint8_t *)&buffer[0 ], bytes_to_read);
477
-
478
- // determine mime
479
- notifyMime (buffer.data (), bytes_to_read);
480
- is_first = false ;
481
-
482
- // callback with unconverted data
483
- if (onWrite!=nullptr ) onWrite (onWriteObj, buffer.data (), result);
450
+ using StreamCopy = StreamCopyT<uint8_t >;
484
451
485
- // convert data
486
- coverter_ptr->convert ((uint8_t *)buffer.data (), result );
487
- write (result, delayCount);
488
- #ifndef COPY_LOG_OFF
489
- LOGI (" StreamCopy::copy %u bytes - in %u hops" , (unsigned int )result,(unsigned int ) delayCount);
490
- #endif
491
- } else {
492
- // give the processor some time
493
- delay (delay_on_no_data);
494
- }
495
- return result;
496
- }
497
-
498
- // / copies the data from the source to the destination and returns the processed number of bytes
499
- size_t copy () {
500
- return StreamCopyT<uint8_t >::copy ();
501
- }
502
-
503
- };
504
452
505
453
} // Namespace
0 commit comments