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) {
350 ReformatBaseStream::begin();
359 int availableForWrite() {
return enc_out.availableForWrite(); }
361 size_t write(
const uint8_t *data,
size_t len) {
363 return enc_out.
write(data, len);
366 size_t readBytes(uint8_t *data,
size_t len) {
367 return reader.readBytes(data, len);
376 void setByteFactor(
float factor) {byte_factor = factor;}
378 #if USE_AUDIO_LOGGING
380 void setLogLevel(AudioLogger::LogLevel level) { enc_out.setLogLevel(level); }
388 float byte_factor = 2.0f;
401 void setWriter(
AudioWriter *writer) { p_writer = writer; }
402 size_t write(
const uint8_t *data,
size_t len) {
403 return p_writer->write(data, len);
419 virtual bool begin() = 0;
420 virtual void end() = 0;
421 virtual void setAudioInfo(
AudioInfo info) {
424 if (p_writer1 !=
nullptr) p_writer1->
setAudioInfo(info);
425 if (p_writer2 !=
nullptr) p_writer2->
setAudioInfo(info);
428 virtual size_t write(uint8_t *data,
size_t size) = 0;
444 print2.setWriter(p_writer2);
452 virtual bool begin() {
455 if (p_writer2 !=
nullptr) {
456 p_writer1->setOutput(print2);
457 p_writer2->setOutput(*p_print);
461 p_writer1->setOutput(*p_print);
469 if (p_writer1 !=
nullptr) p_writer1->end();
470 if (p_writer2 !=
nullptr) p_writer2->end();
474 virtual size_t write(uint8_t *data,
size_t size) {
476 return p_writer1->write(data, size);
480 Print *p_print =
nullptr;