6#include "opus/opus_multistream.h"
8#ifndef OPUS_ENC_MAX_BUFFER_SIZE
9#define OPUS_ENC_MAX_BUFFER_SIZE 2048
12#ifndef OPUS_DEC_MAX_BUFFER_SIZE
13#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
168 if (err != OPUS_OK ||
dec ==
nullptr) {
170 "opus_multistream_decoder_create: %s for sample_rate: %d, "
171 "channels:%d, streams:%d, coupled:%d",
182 if (
dec !=
nullptr) {
183 opus_multistream_decoder_destroy(
dec);
206 if (channels_changed || !
active) {
213 size_t write(
const uint8_t* data,
size_t len)
override {
215 LOGD(
"OpusMultiStreamAudioDecoder::write: %d", (
int)len);
216 int in_band_forward_error_correction = 0;
218 int out_samples = opus_multistream_decode(
219 dec, (uint8_t*)data, len, (opus_int16*)
outbuf.
data(), frame_count,
220 in_band_forward_error_correction);
221 if (out_samples < 0) {
222 LOGW(
"opus_multistream_decode: %s", opus_strerror(out_samples));
223 }
else if (out_samples > 0) {
224 int out_bytes = out_samples *
cfg.
channels *
sizeof(int16_t);
225 LOGD(
"opus_multistream_decode: %d", out_bytes);
226 int open = out_bytes;
232 processed += written;
238 operator bool()
override {
return active; }
244 OpusMSDecoder*
dec =
nullptr;
248 const uint32_t
valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
253 if (valid == rate)
return true;
282 const char*
mime()
override {
return "audio/opus"; }
285 LOGD(
"OpusMultiStreamAudioEncoder::setAudioInfo: sample_rate: %d, channels: %d",
292 if (
enc !=
nullptr) {
299 LOGD(
"OpusMultiStreamAudioEncoder::begin: sample_rate: %d, channels: %d",
317 if (err != OPUS_OK ||
enc ==
nullptr) {
319 "opus_multistream_encoder_create: %s for sample_rate: %d, "
320 "channels:%d, streams:%d, coupled:%d",
344 if (
enc !=
nullptr) {
345 opus_multistream_encoder_destroy(
enc);
351 size_t write(
const uint8_t* data,
size_t len)
override {
353 LOGD(
"OpusMultiStreamAudioEncoder::write: %d", (
int)len);
354 for (
int j = 0; j < len; j++) {
365 if (
enc ==
nullptr)
return 0;
366 opus_int32 samples = 0;
367 if (opus_multistream_encoder_ctl(
enc, OPUS_GET_LOOKAHEAD(&samples)) !=
371 return samples > 0 ? samples : 0;
377 int bytesPerSample =
cfg.
channels * (int)
sizeof(int16_t);
378 if (bytesPerSample <= 0)
return 0;
386 OpusMSEncoder*
enc =
nullptr;
404 LOGD(
"opus_multistream_encode - frame_size: %d", frames);
405 int len = opus_multistream_encode(
enc, (opus_int16*)
frame.
data(), frames,
408 LOGE(
"opus_multistream_encode: %s", opus_strerror(len));
409 }
else if (len > 0) {
410 LOGD(
"opus_multistream_encode: %d", len);
413 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
421 case OPUS_FRAMESIZE_2_5_MS:
422 return sampling_rate / 400;
423 case OPUS_FRAMESIZE_5_MS:
424 return sampling_rate / 200;
425 case OPUS_FRAMESIZE_10_MS:
426 return sampling_rate / 100;
427 case OPUS_FRAMESIZE_20_MS:
428 return sampling_rate / 50;
429 case OPUS_FRAMESIZE_40_MS:
430 return sampling_rate / 25;
431 case OPUS_FRAMESIZE_60_MS:
432 return 3 * sampling_rate / 50;
433 case OPUS_FRAMESIZE_80_MS:
434 return 4 * sampling_rate / 50;
435 case OPUS_FRAMESIZE_100_MS:
436 return 5 * sampling_rate / 50;
437 case OPUS_FRAMESIZE_120_MS:
438 return 6 * sampling_rate / 50;
440 return sampling_rate / 100;
446 opus_multistream_encoder_ctl(
enc, OPUS_SET_BITRATE(
cfg.
bitrate)) !=
452 opus_multistream_encoder_ctl(
458 opus_multistream_encoder_ctl(
enc, OPUS_SET_VBR(
cfg.
vbr)) != OPUS_OK) {
463 opus_multistream_encoder_ctl(
469 opus_multistream_encoder_ctl(
475 opus_multistream_encoder_ctl(
480 if (
cfg.
signal >= 0 && opus_multistream_encoder_ctl(
486 opus_multistream_encoder_ctl(
492 opus_multistream_encoder_ctl(
498 opus_multistream_encoder_ctl(
enc, OPUS_SET_LSB_DEPTH(
cfg.
lsb_depth)) !=
504 opus_multistream_encoder_ctl(
510 if (
cfg.
use_dtx >= 0 && opus_multistream_encoder_ctl(
516 opus_multistream_encoder_ctl(
#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
virtual size_t write(const uint8_t *data, size_t len)
Definition Arduino.h:120