Skip to content

Commit 18ba7e8

Browse files
committed
CodecChain
1 parent c96bed8 commit 18ba7e8

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed

src/AudioTools/AudioCodecs/AudioEncoded.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,18 @@ class EncodedAudioOutput : public ModifyingOutput {
208208
/// defines the size of the decoded frame in bytes
209209
void setFrameSize(int size) { frame_size = size; }
210210

211+
EncodedAudioOutput& operator=(EncodedAudioOutput const& src) {
212+
decoder_ptr = src.decoder_ptr;
213+
encoder_ptr = src.encoder_ptr;
214+
ptr_out = src.ptr_out;
215+
active = src.active;
216+
check_available_for_write = src.check_available_for_write;
217+
frame_size = src.frame_size;
218+
cfg = src.cfg;
219+
is_active = src.is_active;
220+
return *this;
221+
}
222+
211223
protected:
212224
// AudioInfo info;
213225
AudioDecoder *decoder_ptr = CodecNOP::instance(); // decoder
@@ -350,6 +362,15 @@ class EncodedAudioStream : public ReformatBaseStream {
350362
/// defines the size of the decoded frame in bytes
351363
void setFrameSize(int size) { enc_out.setFrameSize(size); }
352364

365+
EncodedAudioStream& operator=(EncodedAudioStream const& src) {
366+
enc_out = src.enc_out;
367+
byte_factor = src.byte_factor;
368+
p_stream = src.p_stream;
369+
p_print = src.p_print;
370+
info = src.info;
371+
return *this;
372+
};
373+
353374
protected:
354375
EncodedAudioOutput enc_out;
355376
float byte_factor = 2.0f;
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#pragma once
2+
#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
3+
#include "AudioTools/AudioCodecs/AudioEncoded.h"
4+
5+
namespace audio_tools {
6+
7+
/**
8+
* @brief CodecChain - allows to chain multiple decoders and encoders together
9+
* @ingroup codecs
10+
* @ingroup decoder
11+
* @ingroup encoder
12+
* @author Phil Schatzmann
13+
* @copyright GPLv3
14+
*/
15+
class CodecChain : public AudioDecoder, AudioEncoder {
16+
public:
17+
CodecChain() = default;
18+
CodecChain(AudioDecoder &decoder) { addDecoder(decoder); }
19+
CodecChain(AudioEncoder &encoder) { addEncoder(encoder); }
20+
21+
/// Adds a decoder to the chain
22+
void addDecoder(AudioDecoder &decoder) {
23+
EncodedAudioStream stream;
24+
stream.setDecoder(&decoder);
25+
streams.push_back(stream);
26+
if (streams.size() > 1) {
27+
streams[streams.size() - 2].setOutput(streams[streams.size() - 1]);
28+
}
29+
}
30+
31+
/// Adds an encoder to the chain
32+
void addEncoder(AudioEncoder &encoder) {
33+
EncodedAudioStream stream;
34+
stream.setEncoder(&encoder);
35+
streams.push_back(stream);
36+
if (streams.size() > 1) {
37+
streams[streams.size() - 2].setOutput(streams[streams.size() - 1]);
38+
}
39+
}
40+
41+
void setOutput(Print &out_stream) override {
42+
p_print = &out_stream;
43+
if (streams.size() > 0) streams[streams.size() - 1].setOutput(out_stream);
44+
}
45+
46+
void setOutput(AudioStream &out_stream) override {
47+
p_print = &out_stream;
48+
if (streams.size() > 0) streams[streams.size() - 1].setOutput(out_stream);
49+
}
50+
51+
void setOutput(AudioOutput &out_stream) override {
52+
p_print = &out_stream;
53+
if (streams.size() > 0) streams[streams.size() - 1].setOutput(out_stream);
54+
}
55+
56+
void setAudioInfo(AudioInfo from) override {
57+
AudioDecoder::setAudioInfo(from);
58+
for (auto &stream : streams) {
59+
stream.setAudioInfo(from);
60+
}
61+
}
62+
63+
size_t write(const uint8_t *data, size_t len) override {
64+
if (streams.size() == 0) return 0;
65+
return streams[0].write(data, len);
66+
}
67+
68+
operator bool() { return is_active; }
69+
70+
bool begin() {
71+
is_active = true;
72+
for (auto &stream : streams) {
73+
stream.begin();
74+
}
75+
return true;
76+
}
77+
78+
void end() override {
79+
is_active = false;
80+
for (auto &stream : streams) {
81+
stream.end();
82+
}
83+
};
84+
85+
/// Returns nullptr
86+
const char *mime() { return nullptr; }
87+
88+
protected:
89+
Vector<EncodedAudioStream> streams;
90+
bool is_active = false;
91+
};
92+
93+
} // namespace audio_tools

0 commit comments

Comments
 (0)