arduino-audio-tools
DecoderFromStreaming.h
1 #pragma once
2 
3 #include "AudioTools/AudioCodecs/AudioCodecsBase.h"
4 #include "AudioTools/CoreAudio/Buffers.h"
5 //#include "AudioTools/CoreAudio/AudioStreams.h"
6 
7 namespace audio_tools {
8 
16 class DecoderAdapter : public AudioDecoder {
17 public:
19  DecoderAdapter(StreamingDecoder &dec, int bufferSize) {
20  TRACED();
21  p_dec = &dec;
22  p_dec->setInput(queue);
23  resize(bufferSize);
24  }
25 
27  void setOutput(Print &out) override {
28  p_dec->setOutput(out);
29  }
30 
31  void setInput(Stream& in){
32  p_dec->setInput(in);
33  }
34 
35  bool begin() override {
36  TRACED();
37  active = true;
38  bool rc = p_dec->begin();
39  return rc;
40  }
41 
42  void end() override {
43  TRACED();
44  active = false;
45  }
46 
48  void resize(int size){
49  buffer_size = size;
50  // setup the buffer only if needed
51  if (is_setup) rbuffer.resize(size);
52  }
53 
54  size_t write(const uint8_t *data, size_t len) override {
55  TRACED();
56  setupLazy();
57  size_t result = queue.write((uint8_t *)data, len);
58  // trigger processing - we leave byteCount in the buffer
59  // while(queue.available()>byteCount){
60  // p_dec->copy();
61  // delay(1);
62  // }
63  while(p_dec->copy());
64 
65  return result;
66  }
67 
68  StreamingDecoder* getStreamingDecoder(){
69  return p_dec;
70  }
71 
72  operator bool() override { return active; }
73 
74 protected:
75  bool active = false;
76  bool is_setup = false;
77  int buffer_size;
78  StreamingDecoder *p_dec = nullptr;
79  RingBuffer<uint8_t> rbuffer{0};
80  QueueStream<uint8_t> queue{rbuffer}; // convert Buffer to Stream
81 
82  void setupLazy(){
83  if (!is_setup){
84  rbuffer.resize(buffer_size);
85  queue.begin();
86  is_setup = true;
87  }
88  }
89 };
90 
91 using DecoderFromStreaming = DecoderAdapter;
92 
93 } // namespace audio_tools
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
Adapter class which allows the AudioDecoder API on a StreamingDecoder.
Definition: DecoderFromStreaming.h:16
void setOutput(Print &out) override
Defines the output Stream.
Definition: DecoderFromStreaming.h:27
void resize(int size)
resize the buffer
Definition: DecoderFromStreaming.h:48
DecoderAdapter(StreamingDecoder &dec, int bufferSize)
Default Constructor.
Definition: DecoderFromStreaming.h:19
Definition: NoArduino.h:58
virtual bool begin()
Activates the output.
Definition: BaseStream.h:318
Definition: NoArduino.h:125
A Streaming Decoder where we provide both the input and output as streams.
Definition: AudioCodecsBase.h:154
virtual bool copy()=0
Process a single read operation - to be called in the loop.
virtual void setOutput(Print &out_stream)
Defines the output Stream.
Definition: AudioCodecsBase.h:163
void setInput(Stream &inStream)
Definition: AudioCodecsBase.h:182
virtual bool begin()=0
Starts the processing.
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823