arduino-audio-tools
Loading...
Searching...
No Matches
CodecOpus.h
Go to the documentation of this file.
1#pragma once
2
4#include "Print.h"
5#include "opus.h"
6
7#ifndef OPUS_ENC_MAX_BUFFER_SIZE
8#define OPUS_ENC_MAX_BUFFER_SIZE 2048
9#endif
10
11#ifndef OPUS_DEC_MAX_BUFFER_SIZE
12#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
13#endif
14
15namespace audio_tools {
16
34
78 int bitrate = -1;
80 int force_channel = -1;
82 int vbr = -1;
86 int complexity = -1;
91 int max_bandwidth = -1;
93 int signal = -1;
95 int inband_fec = -1;
99 int lsb_depth = -1;
103 int use_dtx = -1;
105 int frame_sizes_ms_x2 = -1; /* x2 to avoid 2.5 ms */
106};
107
122 public:
127
137
140
141 AudioInfo audioInfo() override { return cfg; }
142
144 OpusSettings &config() { return cfg; }
146
147 bool begin(OpusSettings settings) {
148 TRACED();
150 cfg = settings;
152 return begin();
153 }
154
155 bool begin() override {
156 TRACED();
158 LOGE("Sample rate not supported: %d", cfg.sample_rate);
159 return false;
160 }
162 assert(outbuf.data() != nullptr);
163
164 // allocate decoder
165 size_t size = opus_decoder_get_size(cfg.channels);
166 decbuf.resize(size);
167 assert(decbuf.data() != nullptr);
168 dec = (OpusDecoder *)decbuf.data();
170
171 if (err != OPUS_OK) {
172 LOGE("opus_decoder_create: %s for sample_rate: %d, channels:%d",
174 return false;
175 }
176 active = true;
177 return true;
178 }
179
180 void end() override {
181 TRACED();
182 dec = nullptr;
183 if (release_on_end) {
184 outbuf.resize(0);
185 decbuf.resize(0);
186 }
187 active = false;
188 }
189
190 void setAudioInfo(AudioInfo from) override {
192 info = from;
194 cfg.channels = from.channels;
196 }
197
199 size_t write(const uint8_t *data, size_t len) override {
200 if (!active || p_print == nullptr) return 0;
201 // decode data
202 LOGD("OpusAudioDecoder::write: %d", (int)len);
204 int frame_count = cfg.max_buffer_size / cfg.channels / sizeof(opus_int16);
205 int out_samples =
206 opus_decode(dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
208 if (out_samples < 0) {
209 LOGW("opus-decode: %s", opus_strerror(out_samples));
210 } else if (out_samples > 0) {
211 // write data to final destination
212 int out_bytes = out_samples * cfg.channels * sizeof(int16_t);
213 LOGD("opus-decode: %d", out_bytes);
214 int open = out_bytes;
215 int processed = 0;
216 while (open > 0) {
217 int to_write = std::min(open, cfg.max_buffer_write_size);
218 int written = p_print->write(outbuf.data() + processed, to_write);
219 open -= written;
220 processed += written;
221 }
222 }
223 return len;
224 }
225
226 operator bool() override { return active; }
227
231
232 protected:
233 Print *p_print = nullptr;
234 OpusDecoder *dec = nullptr;
236 bool active = false;
239 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
240 bool release_on_end = false;
241
242 bool isValidRate(int rate) {
243 for (auto &valid : valid_rates) {
244 if (valid == rate) return true;
245 }
246 return false;
247 }
248};
249
261 public:
262 // Empty Constructor - the output stream must be provided with begin()
263 OpusAudioEncoder() = default;
264
265 // Constructor providing the output stream
267
270
272 const char *mime() override { return "audio/opus"; }
273
275 void setAudioInfo(AudioInfo from) override {
278 cfg.channels = from.channels;
280 }
281
283 bool begin() override {
284 int err;
286 sizeof(int16_t);
287 frame.resize(size);
288 assert(frame.data() != nullptr);
290 &err);
291 if (err != OPUS_OK) {
292 LOGE("opus_encoder_create: %s for sample_rate: %d, channels:%d",
294 return false;
295 }
296 is_open = settings();
297 return true;
298 }
299
302
304
306 cfg = settings;
307 return begin();
308 }
309
311 void end() override {
312 // Flush a partial frame only when data is pending; the remainder is
313 // padded with silence and must be trimmed by the container metadata.
314 if (frame_pos > 0) {
316 encodeFrame();
317 frame_pos = 0;
318 }
319 // release memory
321 enc = nullptr;
322 is_open = false;
323 }
324
326 size_t write(const uint8_t *data, size_t len) override {
327 if (!is_open || p_print == nullptr) return 0;
328 LOGD("OpusAudioEncoder::write: %d", (int)len);
329
330 // fill frame
331 for (int j = 0; j < len; j++) {
332 encodeByte(data[j]);
333 }
334 return len;
335 }
336
337 operator bool() override { return is_open; }
338
339 bool isOpen() { return is_open; }
340
342 if (enc == nullptr) return 0;
343 opus_int32 samples = 0;
344 if (opus_encoder_ctl(enc, OPUS_GET_LOOKAHEAD(&samples)) != OPUS_OK) {
345 return 0;
346 }
347 return samples > 0 ? samples : 0;
348 }
349
351
353 int bytesPerSample = cfg.channels * (int)sizeof(int16_t);
354 if (bytesPerSample <= 0) return 0;
355 return frame_pos / bytesPerSample;
356 }
357
359
360
361 protected:
362 Print *p_print = nullptr;
363 OpusEncoder *enc = nullptr;
365 bool is_open = false;
367 int frame_pos = 0;
368
369 void encodeByte(uint8_t data) {
370 // add byte to frame
371 frame[frame_pos++] = data;
372
373 // if frame is complete -> encode
374 if (frame_pos >= frame.size()) {
375 encodeFrame();
376 frame_pos = 0;
377 }
378 }
379
380 void encodeFrame() {
381 if (frame.size() > 0) {
382 // allocate temp buffer on stack
383 int packet_len =
385 uint8_t packet[packet_len];
386
387 int frames = frame.size() / cfg.channels / sizeof(int16_t);
388 LOGD("opus_encode - frame_size: %d", frames);
389 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames, packet,
390 packet_len);
391 if (len < 0) {
392 LOGE("opus_encode: %s", opus_strerror(len));
393 } else if (len > 0) {
394 LOGD("opus-encode: %d", len);
395 int eff = p_print->write(packet, len);
396 if (eff != len) {
397 LOGE("encodeFrame data lost: %d->%d", len, eff);
398 }
399 }
400 }
401 }
402
404 int getFrameSizeSamples(int sampling_rate) {
405 switch (cfg.frame_sizes_ms_x2) {
407 return sampling_rate / 400;
409 return sampling_rate / 200;
411 return sampling_rate / 100;
413 return sampling_rate / 50;
415 return sampling_rate / 25;
417 return 3 * sampling_rate / 50;
419 return 4 * sampling_rate / 50;
421 return 5 * sampling_rate / 50;
423 return 6 * sampling_rate / 50;
424 }
425 return sampling_rate / 100;
426 }
427
428 bool settings() {
429 bool ok = true;
430 if (cfg.bitrate >= 0 &&
432 LOGE("invalid bitrate: %d", cfg.bitrate);
433 ok = false;
434 }
435 if (cfg.force_channel >= 0 &&
437 OPUS_OK) {
438 LOGE("invalid force_channel: %d", cfg.force_channel);
439 ok = false;
440 };
441 if (cfg.vbr >= 0 &&
443 LOGE("invalid vbr: %d", cfg.vbr);
444 ok = false;
445 }
446 if (cfg.vbr_constraint >= 0 &&
448 OPUS_OK) {
449 LOGE("invalid vbr_constraint: %d", cfg.vbr_constraint);
450 ok = false;
451 }
452 if (cfg.complexity >= 0 &&
454 LOGE("invalid complexity: %d", cfg.complexity);
455 ok = false;
456 }
457 if (cfg.max_bandwidth >= 0 &&
459 OPUS_OK) {
460 LOGE("invalid max_bandwidth: %d", cfg.max_bandwidth);
461 ok = false;
462 }
463 if (cfg.signal >= 0 &&
465 LOGE("invalid signal: %d", cfg.signal);
466 ok = false;
467 }
468 if (cfg.inband_fec >= 0 &&
470 LOGE("invalid inband_fec: %d", cfg.inband_fec);
471 ok = false;
472 }
473 if (cfg.packet_loss_perc >= 0 &&
476 LOGE("invalid pkt_loss: %d", cfg.packet_loss_perc);
477 ok = false;
478 }
479 if (cfg.lsb_depth >= 0 &&
481 LOGE("invalid lsb_depth: %d", cfg.lsb_depth);
482 ok = false;
483 }
484 if (cfg.prediction_disabled >= 0 &&
487 LOGE("invalid pred_disabled: %d", cfg.prediction_disabled);
488 ok = false;
489 }
490 if (cfg.use_dtx >= 0 &&
492 LOGE("invalid use_dtx: %d", cfg.use_dtx);
493 ok = false;
494 }
495 if (cfg.frame_sizes_ms_x2 > 0 &&
498 LOGE("invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);
499 ok = false;
500 }
501 return ok;
502 }
503};
504
505} // namespace audio_tools
#define LOGW(...)
Definition AudioLoggerIDF.h:29
#define TRACED()
Definition AudioLoggerIDF.h:31
#define LOGD(...)
Definition AudioLoggerIDF.h:27
#define LOGE(...)
Definition AudioLoggerIDF.h:30
#define OPUS_DEC_MAX_BUFFER_SIZE
Definition CodecOpus.h:12
#define OPUS_ENC_MAX_BUFFER_SIZE
Definition CodecOpus.h:8
#define assert(T)
Definition avr.h:10
Definition Arduino.h:56
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
AudioInfo info
Definition AudioCodecsBase.h:76
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition AudioCodecsBase.h:28
Encoding of PCM data.
Definition AudioCodecsBase.h:97
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:106
void notifyAudioChange(AudioInfo info)
Definition AudioTypes.h:174
Decoder for the Opus audio format. Each Opus frame must be provided with one write() call....
Definition CodecOpus.h:121
OpusAudioDecoder(Print &out_stream)
Construct a new OpusDecoder object.
Definition CodecOpus.h:133
bool active
Definition CodecOpus.h:236
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecOpus.h:139
Vector< uint8_t > outbuf
Definition CodecOpus.h:237
bool begin(OpusSettings settings)
Definition CodecOpus.h:147
OpusAudioDecoder(bool releaseOnEnd=false)
Construct a new OpusDecoder object.
Definition CodecOpus.h:126
OpusDecoder * dec
Definition CodecOpus.h:234
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition CodecOpus.h:190
void end() override
Definition CodecOpus.h:180
Vector< uint8_t > decbuf
Definition CodecOpus.h:238
size_t write(const uint8_t *data, size_t len) override
write one full opus frame
Definition CodecOpus.h:199
OpusSettings cfg
Definition CodecOpus.h:235
const uint32_t valid_rates[5]
Definition CodecOpus.h:239
bool release_on_end
Definition CodecOpus.h:240
bool isValidRate(int rate)
Definition CodecOpus.h:242
void setReleaseOnEnd(bool flag)
Definition CodecOpus.h:230
OpusSettings & config()
Provides access to the configuration.
Definition CodecOpus.h:144
bool begin() override
Definition CodecOpus.h:155
Print * p_print
Definition CodecOpus.h:233
OpusSettings & defaultConfig()
Definition CodecOpus.h:145
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecOpus.h:141
Encode for Opus audio.
Definition CodecOpus.h:260
bool is_open
Definition CodecOpus.h:365
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecOpus.h:269
bool isOpen()
Definition CodecOpus.h:339
bool begin(OpusEncoderSettings settings)
Definition CodecOpus.h:305
uint16_t samplesPerFrame()
Optional rtsp function: provide samples per the frame.
Definition CodecOpus.h:358
int frameSizeSamples()
Definition CodecOpus.h:350
void setAudioInfo(AudioInfo from) override
We actually do nothing with this.
Definition CodecOpus.h:275
void end() override
stops the processing
Definition CodecOpus.h:311
OpusAudioEncoder(Print &out)
Definition CodecOpus.h:266
OpusEncoderSettings cfg
Definition CodecOpus.h:364
void encodeFrame()
Definition CodecOpus.h:380
int lookaheadSamples()
Definition CodecOpus.h:341
size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as Opus.
Definition CodecOpus.h:326
OpusEncoderSettings & config()
Provides access to the configuration.
Definition CodecOpus.h:301
int pendingSamples()
Definition CodecOpus.h:352
OpusEncoder * enc
Definition CodecOpus.h:363
bool settings()
Definition CodecOpus.h:428
const char * mime() override
Provides "audio/pcm".
Definition CodecOpus.h:272
int frame_pos
Definition CodecOpus.h:367
bool begin() override
starts the processing using the actual OpusAudioInfo
Definition CodecOpus.h:283
Print * p_print
Definition CodecOpus.h:362
Vector< uint8_t > frame
Definition CodecOpus.h:366
void encodeByte(uint8_t data)
Definition CodecOpus.h:369
int getFrameSizeSamples(int sampling_rate)
Returns the frame size in samples.
Definition CodecOpus.h:404
OpusEncoderSettings & defaultConfig()
Definition CodecOpus.h:303
Vector implementation which provides the most important methods as defined by std::vector....
Definition Vector.h:21
bool resize(size_t newSize, T value)
Definition Vector.h:266
T * data()
Definition Vector.h:316
int size()
Definition Vector.h:178
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
size_t writeData(Print *p_out, T *data, int samples, int maxSamples=512)
Definition AudioTypes.h:508
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:51
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:53
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:55
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:57
Setting for Opus Encoder where the following values are valid: -1 indicates that the default value sh...
Definition CodecOpus.h:68
int vbr
0, 1
Definition CodecOpus.h:82
int vbr_constraint
0, 1
Definition CodecOpus.h:84
int frame_sizes_ms_x2
OPUS_FRAMESIZE_2_5_MS,OPUS_FRAMESIZE_5_MS,OPUS_FRAMESIZE_10_MS,OPUS_FRAMESIZE_20_MS,...
Definition CodecOpus.h:105
int inband_fec
0, 1
Definition CodecOpus.h:95
OpusEncoderSettings()
Definition CodecOpus.h:69
int use_dtx
0, 1
Definition CodecOpus.h:103
int packet_loss_perc
0, 1, 2, 5
Definition CodecOpus.h:97
int complexity
0 to 10
Definition CodecOpus.h:86
int force_channel
OPUS_AUTO, OPUS_AUTO, 1, 2.
Definition CodecOpus.h:80
int signal
OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC.
Definition CodecOpus.h:93
int bitrate
Definition CodecOpus.h:78
int prediction_disabled
0, 1
Definition CodecOpus.h:101
int max_bandwidth
Definition CodecOpus.h:91
int lsb_depth
8, 24
Definition CodecOpus.h:99
int application
Definition CodecOpus.h:75
Setting for Opus Decoder.
Definition CodecOpus.h:22
OpusSettings()
Definition CodecOpus.h:23
int max_buffer_size
Definition CodecOpus.h:31
int max_buffer_write_size
Definition CodecOpus.h:32