Skip to content

Commit de5dfa2

Browse files
committed
CodecOpuse: configurable resize on end
1 parent 4ab06d3 commit de5dfa2

File tree

1 file changed

+14
-9
lines changed

1 file changed

+14
-9
lines changed

src/AudioTools/AudioCodecs/CodecOpus.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ struct OpusEncoderSettings : public OpusSettings {
110110
* Each Opus frame must be provided with one write() call. Therefore, Opus
111111
* is usually encapsulated in a container format (e.g., Ogg) that splits
112112
* the stream into frames.
113-
*
113+
*
114114
* Depends on https://github.com/pschatzmann/arduino-libopus.git
115-
*
115+
*
116116
* @author Phil Schatzmann
117117
* @ingroup codecs
118118
* @ingroup decoder
@@ -123,7 +123,7 @@ class OpusAudioDecoder : public AudioDecoder {
123123
/**
124124
* @brief Construct a new OpusDecoder object
125125
*/
126-
OpusAudioDecoder() = default;
126+
OpusAudioDecoder(bool releaseOnEnd = false) : release_on_end(releaseOnEnd) {}
127127

128128
/**
129129
* @brief Construct a new OpusDecoder object
@@ -160,10 +160,8 @@ class OpusAudioDecoder : public AudioDecoder {
160160
}
161161
outbuf.resize(cfg.max_buffer_size);
162162
assert(outbuf.data() != nullptr);
163-
164-
// int err;
165-
// dec = opus_decoder_create(cfg.sample_rate, cfg.channels, &err);
166-
163+
164+
// allocate decoder
167165
size_t size = opus_decoder_get_size(cfg.channels);
168166
decbuf.resize(size);
169167
assert(decbuf.data() != nullptr);
@@ -182,8 +180,10 @@ class OpusAudioDecoder : public AudioDecoder {
182180
void end() override {
183181
TRACED();
184182
dec = nullptr;
185-
outbuf.resize(0);
186-
decbuf.resize(0);
183+
if (release_on_end) {
184+
outbuf.resize(0);
185+
decbuf.resize(0);
186+
}
187187
active = false;
188188
}
189189

@@ -225,6 +225,10 @@ class OpusAudioDecoder : public AudioDecoder {
225225

226226
operator bool() override { return active; }
227227

228+
/// Defines if the resources should be released when the stream is closed
229+
/// (default: false)
230+
void setReleaseOnEnd(bool flag) { release_on_end = flag; }
231+
228232
protected:
229233
Print *p_print = nullptr;
230234
OpusDecoder *dec = nullptr;
@@ -233,6 +237,7 @@ class OpusAudioDecoder : public AudioDecoder {
233237
Vector<uint8_t> outbuf{0};
234238
Vector<uint8_t> decbuf{0};
235239
const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
240+
bool release_on_end = false;
236241

237242
bool isValidRate(int rate) {
238243
for (auto &valid : valid_rates) {

0 commit comments

Comments
 (0)