7#ifndef OPUS_ENC_MAX_BUFFER_SIZE
8#define OPUS_ENC_MAX_BUFFER_SIZE 2048
11#ifndef OPUS_DEC_MAX_BUFFER_SIZE
12#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
171 if (err != OPUS_OK) {
172 LOGE(
"opus_decoder_create: %s for sample_rate: %d, channels:%d",
199 size_t write(
const uint8_t *data,
size_t len)
override {
202 LOGD(
"OpusAudioDecoder::write: %d", (
int)len);
203 int in_band_forward_error_correction = 0;
206 opus_decode(
dec, (uint8_t *)data, len, (opus_int16 *)
outbuf.
data(),
207 frame_count, in_band_forward_error_correction);
208 if (out_samples < 0) {
209 LOGW(
"opus-decode: %s", opus_strerror(out_samples));
210 }
else if (out_samples > 0) {
212 int out_bytes = out_samples *
cfg.
channels *
sizeof(int16_t);
213 LOGD(
"opus-decode: %d", out_bytes);
214 int open = out_bytes;
220 processed += written;
226 operator bool()
override {
return active; }
234 OpusDecoder *
dec =
nullptr;
239 const uint32_t
valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
244 if (valid == rate)
return true;
272 const char *
mime()
override {
return "audio/opus"; }
291 if (err != OPUS_OK) {
292 LOGE(
"opus_encoder_create: %s for sample_rate: %d, channels:%d",
320 opus_encoder_destroy(
enc);
326 size_t write(
const uint8_t *data,
size_t len)
override {
328 LOGD(
"OpusAudioEncoder::write: %d", (
int)len);
331 for (
int j = 0; j < len; j++) {
342 if (
enc ==
nullptr)
return 0;
343 opus_int32 samples = 0;
344 if (opus_encoder_ctl(
enc, OPUS_GET_LOOKAHEAD(&samples)) != OPUS_OK) {
347 return samples > 0 ? samples : 0;
353 int bytesPerSample =
cfg.
channels * (int)
sizeof(int16_t);
354 if (bytesPerSample <= 0)
return 0;
363 OpusEncoder *
enc =
nullptr;
385 uint8_t packet[packet_len];
388 LOGD(
"opus_encode - frame_size: %d", frames);
389 int len = opus_encode(
enc, (opus_int16 *)
frame.
data(), frames, packet,
392 LOGE(
"opus_encode: %s", opus_strerror(len));
393 }
else if (len > 0) {
394 LOGD(
"opus-encode: %d", len);
397 LOGE(
"encodeFrame data lost: %d->%d", len, eff);
406 case OPUS_FRAMESIZE_2_5_MS:
407 return sampling_rate / 400;
408 case OPUS_FRAMESIZE_5_MS:
409 return sampling_rate / 200;
410 case OPUS_FRAMESIZE_10_MS:
411 return sampling_rate / 100;
412 case OPUS_FRAMESIZE_20_MS:
413 return sampling_rate / 50;
414 case OPUS_FRAMESIZE_40_MS:
415 return sampling_rate / 25;
416 case OPUS_FRAMESIZE_60_MS:
417 return 3 * sampling_rate / 50;
418 case OPUS_FRAMESIZE_80_MS:
419 return 4 * sampling_rate / 50;
420 case OPUS_FRAMESIZE_100_MS:
421 return 5 * sampling_rate / 50;
422 case OPUS_FRAMESIZE_120_MS:
423 return 6 * sampling_rate / 50;
425 return sampling_rate / 100;
431 opus_encoder_ctl(
enc, OPUS_SET_BITRATE(
cfg.
bitrate)) != OPUS_OK) {
442 opus_encoder_ctl(
enc, OPUS_SET_VBR(
cfg.
vbr)) != OPUS_OK) {
464 opus_encoder_ctl(
enc, OPUS_SET_SIGNAL(
cfg.
signal)) != OPUS_OK) {
485 opus_encoder_ctl(
enc, OPUS_SET_PREDICTION_DISABLED(
491 opus_encoder_ctl(
enc, OPUS_SET_DTX(
cfg.
use_dtx)) != OPUS_OK) {
496 opus_encoder_ctl(
enc, OPUS_SET_EXPERT_FRAME_DURATION(
#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