3 #include "AudioConfig.h"
4 #include "AudioLogger.h"
5 #include "AudioTools/CoreAudio/AudioIO.h"
6 #include "AudioTools/CoreAudio/AudioOutput.h"
7 #include "AudioTools/CoreAudio/AudioStreams.h"
8 #include "AudioTools/CoreAudio/AudioTypes.h"
9 #include "AudioCodecsBase.h"
100 ptr_out = outputStream;
101 if (decoder_ptr !=
nullptr) {
104 if (encoder_ptr !=
nullptr) {
105 encoder_ptr->setOutput(*ptr_out);
111 encoder = CodecNOP::instance();
115 if (ptr_out !=
nullptr) {
116 encoder_ptr->setOutput(*ptr_out);
120 AudioEncoder *getEncoder() {
return encoder_ptr; }
122 void setDecoder(AudioDecoder *
decoder) {
124 decoder = CodecNOP::instance();
128 if (ptr_out !=
nullptr) {
133 AudioDecoder *getDecoder() {
return decoder_ptr; }
137 #if USE_AUDIO_LOGGING
138 custom_log_level.set();
143 const CodecNOP *nop = CodecNOP::instance();
144 if (decoder_ptr != nop || encoder_ptr != nop) {
146 if (!decoder_ptr->begin(cfg)) active =
false;
147 if (!encoder_ptr->begin(cfg)) active =
false;
149 LOGW(
"no decoder or encoder defined");
152 #if USE_AUDIO_LOGGING
153 custom_log_level.reset();
166 #if USE_AUDIO_LOGGING
167 custom_log_level.set();
173 #if USE_AUDIO_LOGGING
174 custom_log_level.reset();
179 virtual size_t write(
const uint8_t *data,
size_t len)
override {
184 #if USE_AUDIO_LOGGING
185 custom_log_level.set();
187 LOGD(
"EncodedAudioOutput::write: %d", (
int)len);
189 if (writer_ptr ==
nullptr || data ==
nullptr) {
194 if (check_available_for_write && availableForWrite() == 0) {
198 size_t result = writer_ptr->write(data, len);
199 LOGD(
"EncodedAudioOutput::write: %d -> %d", (
int)len, (
int)result);
200 #if USE_AUDIO_LOGGING
201 custom_log_level.reset();
206 int availableForWrite()
override {
207 if (!check_available_for_write)
return frame_size;
208 return min(ptr_out->availableForWrite(), frame_size);
212 operator bool() {
return active; }
220 #if USE_AUDIO_LOGGING
222 void setLogLevel(AudioLogger::LogLevel level) { custom_log_level.set(level); }
235 Print *ptr_out =
nullptr;
237 bool check_available_for_write =
false;
238 #if USE_AUDIO_LOGGING
239 CustomLogLevel custom_log_level;
241 int frame_size = DEFAULT_BUFFER_SIZE;
245 using EncodedAudioPrint = EncodedAudioOutput;
260 setStream(*ioStream);
265 setStream(*ioStream);
269 setOutput(*outputStream);
274 setOutput(*outputStream);
279 setOutput(*outputStream);
291 AudioEncoder *getEncoder() {
return enc_out.getEncoder(); }
293 AudioDecoder *getDecoder() {
return enc_out.getDecoder(); }
301 void setStream(
Stream *stream) {
305 void setStream(AudioStream *stream) {
309 void setOutput(AudioOutput *stream) {
313 void setOutput(Print *stream) {
317 void setStream(AudioStream &stream) {
328 ReformatBaseStream::setOutput(stream);
333 ReformatBaseStream::setOutput(out);
342 bool begin(AudioInfo info) {
349 reader.setByteCountFactor(10);
351 ReformatBaseStream::begin();
360 int availableForWrite() {
return enc_out.availableForWrite(); }
362 size_t write(
const uint8_t *data,
size_t len) {
364 return enc_out.
write(data, len);
367 size_t readBytes(uint8_t *data,
size_t len) {
368 return reader.readBytes(data, len);
375 float getByteFactor() {
return 1.0f; }
377 #if USE_AUDIO_LOGGING
379 void setLogLevel(AudioLogger::LogLevel level) { enc_out.setLogLevel(level); }
399 void setWriter(
AudioWriter *writer) { p_writer = writer; }
400 size_t write(
const uint8_t *data,
size_t len) {
401 return p_writer->write(data, len);
417 virtual bool begin() = 0;
418 virtual void end() = 0;
419 virtual void setAudioInfo(
AudioInfo info) {
422 if (p_writer1 !=
nullptr) p_writer1->
setAudioInfo(info);
423 if (p_writer2 !=
nullptr) p_writer2->
setAudioInfo(info);
426 virtual size_t write(uint8_t *data,
size_t size) = 0;
442 print2.setWriter(p_writer2);
450 virtual bool begin() {
453 if (p_writer2 !=
nullptr) {
454 p_writer1->setOutput(print2);
455 p_writer2->setOutput(*p_print);
459 p_writer1->setOutput(*p_print);
467 if (p_writer1 !=
nullptr) p_writer1->end();
468 if (p_writer2 !=
nullptr) p_writer2->end();
472 virtual size_t write(uint8_t *data,
size_t size) {
474 return p_writer1->write(data, size);
478 Print *p_print =
nullptr;