Skip to content

Commit 1d72090

Browse files
committed
Opus Documentaion
1 parent 72d7edc commit 1d72090

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

src/AudioTools/AudioCodecs/CodecOpus.h

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,14 @@
44
#include "Print.h"
55
#include "opus.h"
66

7-
#ifndef OPUS_ENC_MAX_BUFFER_SIZE
7+
#ifndef OPUS_ENC_MAX_BUFFER_SIZE
88
#define OPUS_ENC_MAX_BUFFER_SIZE 2048
99
#endif
1010

1111
#ifndef OPUS_DEC_MAX_BUFFER_SIZE
12-
#define OPUS_DEC_MAX_BUFFER_SIZE 4*1024
12+
#define OPUS_DEC_MAX_BUFFER_SIZE 4 * 1024
1313
#endif
1414

15-
1615
namespace audio_tools {
1716

1817
/**
@@ -31,7 +30,6 @@ struct OpusSettings : public AudioInfo {
3130
}
3231
int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
3332
int max_buffer_write_size = 512;
34-
3533
};
3634

3735
/**
@@ -60,14 +58,16 @@ setting the value.
6058
OPUS_SIGNAL_MUSIC};<br> int inband_fecs[3] = {0, 0, 1};<br> int
6159
packet_loss_perc[4] = {0, 1, 2, 5};<br> int lsb_depths[2] = {8, 24};<br> int
6260
prediction_disabled[3] = {0, 0, 1};<br> int use_dtx[2] = {0, 1};<br> int
63-
frame_sizes_ms_x2[9] = {OPUS_FRAMESIZE_2_5_MS,OPUS_FRAMESIZE_5_MS,OPUS_FRAMESIZE_10_MS,OPUS_FRAMESIZE_20_MS,OPUS_FRAMESIZE_40_MS,OPUS_FRAMESIZE_60_MS,OPUS_FRAMESIZE_80_MS,OPUS_FRAMESIZE_100_MS,OPUS_FRAMESIZE_120_MS} x2 to avoid 2.5 ms <br>
61+
frame_sizes_ms_x2[9] =
62+
{OPUS_FRAMESIZE_2_5_MS,OPUS_FRAMESIZE_5_MS,OPUS_FRAMESIZE_10_MS,OPUS_FRAMESIZE_20_MS,OPUS_FRAMESIZE_40_MS,OPUS_FRAMESIZE_60_MS,OPUS_FRAMESIZE_80_MS,OPUS_FRAMESIZE_100_MS,OPUS_FRAMESIZE_120_MS}
63+
x2 to avoid 2.5 ms <br>
6464
* @author Phil Schatzmann
6565
* @copyright GPLv3
6666
**/
6767

6868
struct OpusEncoderSettings : public OpusSettings {
6969
OpusEncoderSettings() : OpusSettings() {
70-
/// Default is 5760
70+
/// Default is 5760
7171
max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
7272
}
7373
/// OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
@@ -106,7 +106,13 @@ struct OpusEncoderSettings : public OpusSettings {
106106
};
107107

108108
/**
109-
* @brief OpusAudioDecoder: Depends on https://github.com/pschatzmann/arduino-libopus.git
109+
* @brief Decoder for the Opus audio format.
110+
* Each Opus frame must be provided with one write() call. Therefore, Opus
111+
* is usually encapsulated in a container format (e.g., Ogg) that splits
112+
* the stream into frames.
113+
*
114+
* Depends on https://github.com/pschatzmann/arduino-libopus.git
115+
*
110116
* @author Phil Schatzmann
111117
* @ingroup codecs
112118
* @ingroup decoder
@@ -148,22 +154,21 @@ class OpusAudioDecoder : public AudioDecoder {
148154

149155
bool begin() override {
150156
TRACED();
151-
if (!isValidRate(cfg.sample_rate)){
157+
if (!isValidRate(cfg.sample_rate)) {
152158
LOGE("Sample rate not supported: %d", cfg.sample_rate);
153159
return false;
154160
}
155161
outbuf.resize(cfg.max_buffer_size);
156162
assert(outbuf.data() != nullptr);
157-
163+
158164
// int err;
159165
// dec = opus_decoder_create(cfg.sample_rate, cfg.channels, &err);
160-
166+
161167
size_t size = opus_decoder_get_size(cfg.channels);
162168
decbuf.resize(size);
163169
assert(decbuf.data() != nullptr);
164-
dec = (OpusDecoder*)decbuf.data();
170+
dec = (OpusDecoder *)decbuf.data();
165171
int err = opus_decoder_init(dec, cfg.sample_rate, cfg.channels);
166-
167172

168173
if (err != OPUS_OK) {
169174
LOGE("opus_decoder_create: %s for sample_rate: %d, channels:%d",
@@ -190,15 +195,16 @@ class OpusAudioDecoder : public AudioDecoder {
190195
cfg.bits_per_sample = from.bits_per_sample;
191196
}
192197

198+
/// write one full opus frame
193199
size_t write(const uint8_t *data, size_t len) override {
194200
if (!active || p_print == nullptr) return 0;
195201
// decode data
196202
LOGD("OpusAudioDecoder::write: %d", (int)len);
197203
int in_band_forward_error_correction = 0;
198204
int frame_count = cfg.max_buffer_size / cfg.channels / sizeof(opus_int16);
199-
int out_samples = opus_decode(
200-
dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
201-
frame_count, in_band_forward_error_correction);
205+
int out_samples =
206+
opus_decode(dec, (uint8_t *)data, len, (opus_int16 *)outbuf.data(),
207+
frame_count, in_band_forward_error_correction);
202208
if (out_samples < 0) {
203209
LOGW("opus-decode: %s", opus_strerror(out_samples));
204210
} else if (out_samples > 0) {
@@ -207,9 +213,9 @@ class OpusAudioDecoder : public AudioDecoder {
207213
LOGD("opus-decode: %d", out_bytes);
208214
int open = out_bytes;
209215
int processed = 0;
210-
while(open>0){
216+
while (open > 0) {
211217
int to_write = std::min(open, cfg.max_buffer_write_size);
212-
int written = p_print->write(outbuf.data()+processed, to_write);
218+
int written = p_print->write(outbuf.data() + processed, to_write);
213219
open -= written;
214220
processed += written;
215221
}
@@ -226,18 +232,21 @@ class OpusAudioDecoder : public AudioDecoder {
226232
bool active = false;
227233
Vector<uint8_t> outbuf{0};
228234
Vector<uint8_t> decbuf{0};
229-
const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
235+
const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
230236

231-
bool isValidRate(int rate){
232-
for (auto &valid : valid_rates){
233-
if (valid==rate) return true;
237+
bool isValidRate(int rate) {
238+
for (auto &valid : valid_rates) {
239+
if (valid == rate) return true;
234240
}
235241
return false;
236242
}
237243
};
238244

239245
/**
240-
* @brief OpusAudioEncoder: Dependens on https://github.com/pschatzmann/arduino-libopus.git
246+
* @brief Encode for Opus audio.
247+
*
248+
* Depends on https://github.com/pschatzmann/arduino-libopus.git
249+
* Please note that each fully encoded frame is written to the output stream.
241250
* @ingroup codecs
242251
* @ingroup encoder
243252
* @author Phil Schatzmann
@@ -271,7 +280,8 @@ class OpusAudioEncoder : public AudioEncoder {
271280
int size = getFrameSizeSamples(cfg.sample_rate) * 2;
272281
frame.resize(size);
273282
assert(frame.data() != nullptr);
274-
enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
283+
enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application,
284+
&err);
275285
if (err != OPUS_OK) {
276286
LOGE("opus_encoder_create: %s for sample_rate: %d, channels:%d",
277287
opus_strerror(err), cfg.sample_rate, cfg.channels);
@@ -338,19 +348,20 @@ class OpusAudioEncoder : public AudioEncoder {
338348
void encodeFrame() {
339349
if (frame.size() > 0) {
340350
// allocate temp buffer on stack
341-
int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
351+
int packet_len =
352+
OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
342353
uint8_t packet[packet_len];
343354

344355
int frames = frame.size() / cfg.channels / sizeof(int16_t);
345356
LOGD("opus_encode - frame_size: %d", frames);
346-
int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
347-
packet, packet_len);
357+
int len = opus_encode(enc, (opus_int16 *)frame.data(), frames, packet,
358+
packet_len);
348359
if (len < 0) {
349360
LOGE("opus_encode: %s", opus_strerror(len));
350361
} else if (len > 0) {
351362
LOGD("opus-encode: %d", len);
352363
int eff = p_print->write(packet, len);
353-
if (eff!=len){
364+
if (eff != len) {
354365
LOGE("encodeFrame data lost: %d->%d", len, eff);
355366
}
356367
}

0 commit comments

Comments
 (0)