arduino-audio-tools
AudioCodecsBase.h
1 #pragma once
2 
3 #include "AudioConfig.h"
4 #include "AudioLogger.h"
5 #include "AudioTools/CoreAudio/AudioBasic/Collections/Vector.h"
6 #include "AudioTools/CoreAudio/AudioTypes.h"
7 
8 namespace audio_tools {
9 
16 class AudioDecoder : public AudioWriter, public AudioInfoSource {
17  public:
18  AudioDecoder() = default;
19  virtual ~AudioDecoder() = default;
20  AudioDecoder(AudioDecoder const &) = delete;
21  AudioDecoder &operator=(AudioDecoder const &) = delete;
22 
23  virtual AudioInfo audioInfo() { return info; };
24 
26  virtual void setAudioInfo(AudioInfo from) override {
27  TRACED();
28  if (info != from) {
29  notifyAudioChange(from);
30  }
31  info = from;
32  }
34  virtual void setOutput(AudioStream &out_stream) {
35  Print *p_print = &out_stream;
36  setOutput(*p_print);
37  addNotifyAudioChange(out_stream);
38  }
39 
41  virtual void setOutput(AudioOutput &out_stream) {
42  Print *p_print = &out_stream;
43  setOutput(*p_print);
44  addNotifyAudioChange(out_stream);
45  }
46 
48  virtual void setOutput(Print &out_stream) override { p_print = &out_stream; }
49 
51  virtual bool isResultPCM() { return true; }
52  virtual bool begin(AudioInfo info) override {
53  setAudioInfo(info);
54  return begin();
55  }
56  bool begin() override { return true; }
57  void end() override {}
58 
60  int id;
61 
62  protected:
63  Print *p_print = nullptr;
64  AudioInfo info;
65 };
66 
75  bool isResultPCM() override { return true; }
76 };
77 
84 class AudioEncoder : public AudioWriter {
85  public:
86  AudioEncoder() = default;
87  virtual ~AudioEncoder() = default;
88  AudioEncoder(AudioEncoder const &) = delete;
89  AudioEncoder &operator=(AudioEncoder const &) = delete;
91  virtual const char *mime() = 0;
93  void setAudioInfo(AudioInfo from) override { info = from; }
94  AudioInfo audioInfo() { return info; }
95 
96  protected:
97  AudioInfo info;
98 };
99 
101  public:
102  virtual void setBlockSize(int blockSize) = 0;
103 };
104 
106  public:
107  virtual int blockSize() = 0;
108 };
109 
118 class CodecNOP : public AudioDecoder, public AudioEncoder {
119  public:
120  static CodecNOP *instance() {
121  static CodecNOP self;
122  return &self;
123  }
124 
125  virtual bool begin() { return true; }
126  virtual void end() {}
127  virtual void setOutput(Print &out_stream) {}
129  virtual void setAudioInfo(AudioInfo info) {}
130 
131  virtual AudioInfo audioInfo() {
132  AudioInfo info;
133  return info;
134  }
135  virtual operator bool() { return false; }
136  virtual int readStream(Stream &in) { return 0; };
137 
138  // just output silence
139  virtual size_t write(const uint8_t *data, size_t len) {
140  memset((void *)data, 0, len);
141  return len;
142  }
143 
144  virtual const char *mime() { return nullptr; }
145 };
146 
155  public:
157  virtual bool begin() = 0;
158 
160  virtual void end() = 0;
161 
163  virtual void setOutput(Print &out_stream) { p_print = &out_stream; }
164 
166  virtual void setOutput(AudioStream &out_stream) {
167  Print *p_print = &out_stream;
168  setOutput(*p_print);
169  addNotifyAudioChange(out_stream);
170  }
171 
173  virtual void setOutput(AudioOutput &out_stream) {
174  Print *p_print = &out_stream;
175  setOutput(*p_print);
176  addNotifyAudioChange(out_stream);
177  }
178 
182  void setInput(Stream &inStream) { this->p_input = &inStream; }
183 
185  virtual AudioInfo audioInfo() = 0;
186 
188  virtual operator bool() = 0;
189 
191  virtual bool copy() = 0;
192 
193  protected:
194  virtual size_t readBytes(uint8_t *data, size_t len) = 0;
195  Print *p_print = nullptr;
196  Stream *p_input = nullptr;
197 };
198 
206  public:
208  int copySize = DEFAULT_BUFFER_SIZE) {
209  p_decoder = &decoder;
210  if (copySize > 0) resize(copySize);
211  }
213  bool begin() override { return p_input != nullptr && p_decoder->begin(); }
214 
216  void end() override { p_decoder->end(); }
217 
219  void setOutput(Print &out_stream) override {
220  p_decoder->setOutput(out_stream);
221  }
222 
224  AudioInfo audioInfo() override { return p_decoder->audioInfo(); }
225 
227  virtual operator bool() { return *p_decoder; }
228 
230  virtual bool copy() {
231  int read = readBytes(&buffer[0], buffer.size());
232  int written = 0;
233  if (read > 0) written = p_decoder->write(&buffer[0], read);
234  return written > 0;
235  }
236 
238  void resize(int bufferSize) { buffer.resize(bufferSize); }
239 
240  protected:
241  AudioDecoder *p_decoder = nullptr;
242  Vector<uint8_t> buffer{0};
243 
244  size_t readBytes(uint8_t *data, size_t len) override {
245  if (p_input == nullptr) return 0;
246  return p_input->readBytes(data, len);
247  }
248 };
249 
250 } // namespace audio_tools
Definition: AudioCodecsBase.h:100
Docoding of encoded audio into PCM data.
Definition: AudioCodecsBase.h:16
virtual bool isResultPCM()
Returns true to indicate that the decoding result is PCM data.
Definition: AudioCodecsBase.h:51
int id
custom id to be used by application
Definition: AudioCodecsBase.h:60
virtual void setOutput(AudioStream &out_stream)
Defines where the decoded result is written to.
Definition: AudioCodecsBase.h:34
virtual void setOutput(Print &out_stream) override
Defines where the decoded result is written to.
Definition: AudioCodecsBase.h:48
virtual void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition: AudioCodecsBase.h:26
virtual void setOutput(AudioOutput &out_stream)
Defines where the decoded result is written to.
Definition: AudioCodecsBase.h:41
virtual AudioInfo audioInfo()
provides the actual input AudioInfo
Definition: AudioCodecsBase.h:23
Definition: AudioCodecsBase.h:105
Encoding of PCM data.
Definition: AudioCodecsBase.h:84
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition: AudioCodecsBase.h:93
AudioInfo audioInfo()
provides the actual input AudioInfo
Definition: AudioCodecsBase.h:94
virtual const char * mime()=0
Provides the mime type of the encoded result.
Supports the subscription to audio change notifications.
Definition: AudioTypes.h:159
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition: AudioTypes.h:162
Supports changes to the sampling rate, bits and channels.
Definition: AudioTypes.h:139
Abstract Audio Ouptut class.
Definition: AudioOutput.h:22
Base class for all Audio Streams. It support the boolean operator to test if the object is ready with...
Definition: BaseStream.h:109
E.g. used by Encoders and Decoders.
Definition: AudioTypes.h:224
Dummy no implmentation Codec. This is used so that we can initialize some pointers to decoders and en...
Definition: AudioCodecsBase.h:118
virtual void setOutput(Print &out_stream)
Defines where the decoded result is written to.
Definition: AudioCodecsBase.h:127
virtual void addNotifyAudioChange(AudioInfoSupport &bi)
Adds target to be notified about audio changes.
Definition: AudioCodecsBase.h:128
virtual void setAudioInfo(AudioInfo info)
for most decoders this is not needed
Definition: AudioCodecsBase.h:129
virtual AudioInfo audioInfo()
provides the actual input AudioInfo
Definition: AudioCodecsBase.h:131
virtual const char * mime()
Provides the mime type of the encoded result.
Definition: AudioCodecsBase.h:144
Parent class for all container formats.
Definition: AudioCodecsBase.h:74
Definition: NoArduino.h:58
Definition: NoArduino.h:125
Converts any AudioDecoder to a StreamingDecoder.
Definition: AudioCodecsBase.h:205
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition: AudioCodecsBase.h:219
virtual bool copy()
Process a single read operation - to be called in the loop.
Definition: AudioCodecsBase.h:230
void end() override
Releases the reserved memory.
Definition: AudioCodecsBase.h:216
void resize(int bufferSize)
Adjust the buffer size: the existing content of the buffer is lost!
Definition: AudioCodecsBase.h:238
bool begin() override
Starts the processing.
Definition: AudioCodecsBase.h:213
AudioInfo audioInfo() override
Provides the last available MP3FrameInfo.
Definition: AudioCodecsBase.h:224
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 void setOutput(AudioStream &out_stream)
Defines the output streams and register to be notified.
Definition: AudioCodecsBase.h:166
virtual void end()=0
Releases the reserved memory.
virtual void setOutput(AudioOutput &out_stream)
Defines the output streams and register to be notified.
Definition: AudioCodecsBase.h:173
virtual AudioInfo audioInfo()=0
Provides the last available MP3FrameInfo.
virtual bool begin()=0
Starts the processing.
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:823
Basic Audio information which drives e.g. I2S.
Definition: AudioTypes.h:52