@@ -22,38 +22,35 @@ namespace audio_tools {
2222template <class T >
2323class StreamCopyT {
2424 public:
25- StreamCopyT () = default ;
2625
27- StreamCopyT (Print &to, AudioStream &from, int buffer_size =DEFAULT_BUFFER_SIZE){
26+ StreamCopyT (Print &to, AudioStream &from, int bufferSize =DEFAULT_BUFFER_SIZE){
2827 TRACED ();
28+ this ->buffer_size = bufferSize;
2929 begin (to, from);
30- resize (buffer_size);
31- if (!buffer){
32- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
33- }
3430 }
3531
36- StreamCopyT (Print &to, Stream &from, int buffer_size =DEFAULT_BUFFER_SIZE){
32+ StreamCopyT (Print &to, Stream &from, int bufferSize =DEFAULT_BUFFER_SIZE){
3733 TRACED ();
34+ this ->buffer_size = bufferSize;
3835 begin (to, from);
39- resize (buffer_size);
40- if (!buffer){
41- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
42- }
4336 }
4437
45- StreamCopyT (int buffer_size =DEFAULT_BUFFER_SIZE){
38+ StreamCopyT (int bufferSize =DEFAULT_BUFFER_SIZE){
4639 TRACED ();
47- resize (buffer_size);
48- if (!buffer){
49- LOGE (NOT_ENOUGH_MEMORY_MSG, buffer_size);
50- }
40+ this ->buffer_size = bufferSize;
41+ begin ();
5142 }
5243
5344 // / (Re)starts the processing
5445 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+ }
5754 }
5855
5956 // / Ends the processing
@@ -66,16 +63,14 @@ class StreamCopyT {
6663 void begin (Print &to, Stream &from){
6764 this ->from = new AudioStreamWrapper (from);
6865 this ->to = &to;
69- is_first = true ;
70- LOGI (" buffer_size=%d" ,buffer_size);
66+ begin ();
7167 }
7268
7369 // / assign a new output and input stream
7470 void begin (Print &to, AudioStream &from){
7571 this ->from = &from;
7672 this ->to = &to;
77- is_first = true ;
78- LOGI (" buffer_size=%d" ,buffer_size);
73+ begin ();
7974 }
8075
8176 // / Provides a pointer to the copy source. Can be used to check if the source is defined.
@@ -90,6 +85,13 @@ class StreamCopyT {
9085
9186 // / copies the data from the source to the destination and returns the processed number of bytes
9287 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;
9395 return copyBytes (buffer_size);
9496 }
9597
@@ -148,6 +150,9 @@ class StreamCopyT {
148150 // determine mime
149151 notifyMime (buffer.data (), bytes_to_read);
150152
153+ // convert data
154+ if (p_converter!=nullptr ) p_converter->convert ((uint8_t *)buffer.data (), result );
155+
151156 // write data
152157 result = write (bytes_read, delayCount);
153158
@@ -176,28 +181,6 @@ class StreamCopyT {
176181 return result;
177182 }
178183
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-
201184 // / Copies pages * buffersize samples: returns the processed number of bytes
202185 size_t copyN (size_t pages){
203186 if (!active) return 0 ;
@@ -245,6 +228,27 @@ class StreamCopyT {
245228 return result;
246229 }
247230
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+
248252 // / Provides the actual mime type, that was determined from the first available data
249253 const char * mime () {
250254 return actual_mime;
@@ -347,7 +351,7 @@ class StreamCopyT {
347351 AudioStream *from = nullptr ;
348352 Print *to = nullptr ;
349353 Vector<uint8_t > buffer{0 };
350- int buffer_size;
354+ int buffer_size = DEFAULT_BUFFER_SIZE ;
351355 void (*onWrite)(void *obj, void *buffer, size_t len) = nullptr ;
352356 void (*notifyMimeCallback)(const char *mime) = nullptr ;
353357 int (*availableCallback)(Stream*stream)=nullptr ;
@@ -365,6 +369,8 @@ class StreamCopyT {
365369 int min_copy_size = 1 ;
366370 bool is_sync_audio_info = false ;
367371 AudioInfoSupport *p_audio_info_support = nullptr ;
372+ BaseConverter* p_converter = nullptr ;
373+
368374
369375 void syncAudioInfo (){
370376 // synchronize audio info
@@ -436,70 +442,12 @@ class StreamCopyT {
436442};
437443
438444/* *
439- * @brief We provide the typeless StreamCopy as a subclass of StreamCopyT
445+ * @brief We provide the typeless StreamCopy
440446 * @ingroup tools
441447 * @author Phil Schatzmann
442448 * @copyright GPLv3
443449 */
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 >;
484451
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- };
504452
505453} // Namespace
0 commit comments