arduino-audio-tools
All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends Modules Pages
CodecOpus.h
1#pragma once
2
3#include "AudioTools/AudioCodecs/AudioCodecsBase.h"
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
15
16namespace audio_tools {
17
23struct OpusSettings : public AudioInfo {
26 sample_rate = 48000;
28 channels = 2;
30 bits_per_sample = 16;
31 }
32 int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
33 int max_buffer_write_size = 512;
34
35};
36
71 max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
72 }
75 int application = OPUS_APPLICATION_AUDIO;
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
116 public:
120 OpusAudioDecoder() = default;
121
127 OpusAudioDecoder(Print &out_stream) {
128 TRACED();
129 setOutput(out_stream);
130 }
131
133 void setOutput(Print &out_stream) override { p_print = &out_stream; }
134
135 AudioInfo audioInfo() override { return cfg; }
136
138 OpusSettings &config() { return cfg; }
139 OpusSettings &defaultConfig() { return cfg; }
140
141 bool begin(OpusSettings settings) {
142 TRACED();
144 cfg = settings;
145 notifyAudioChange(cfg);
146 return begin();
147 }
148
149 bool begin() override {
150 TRACED();
151 if (!isValidRate(cfg.sample_rate)){
152 LOGE("Sample rate not supported: %d", cfg.sample_rate);
153 return false;
154 }
155 outbuf.resize(cfg.max_buffer_size);
156 assert(outbuf.data() != nullptr);
157
158 // int err;
159 // dec = opus_decoder_create(cfg.sample_rate, cfg.channels, &err);
160
161 size_t size = opus_decoder_get_size(cfg.channels);
162 decbuf.resize(size);
163 assert(decbuf.data() != nullptr);
164 dec = (OpusDecoder*)decbuf.data();
165 int err = opus_decoder_init(dec, cfg.sample_rate, cfg.channels);
166
167
168 if (err != OPUS_OK) {
169 LOGE("opus_decoder_create: %s for sample_rate: %d, channels:%d",
170 opus_strerror(err), cfg.sample_rate, cfg.channels);
171 return false;
172 }
173 active = true;
174 return true;
175 }
176
177 void end() override {
178 TRACED();
179 dec = nullptr;
180 outbuf.resize(0);
181 decbuf.resize(0);
182 active = false;
183 }
184
185 void setAudioInfo(AudioInfo from) override {
187 info = from;
188 cfg.sample_rate = from.sample_rate;
189 cfg.channels = from.channels;
191 }
192
193 size_t write(const uint8_t *data, size_t len) override {
194 if (!active || p_print == nullptr) return 0;
195 // decode data
196 LOGD("OpusAudioDecoder::write: %d", (int)len);
197 int in_band_forward_error_correction = 0;
198 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);
202 if (out_samples < 0) {
203 LOGW("opus-decode: %s", opus_strerror(out_samples));
204 } else if (out_samples > 0) {
205 // write data to final destination
206 int out_bytes = out_samples * cfg.channels * sizeof(int16_t);
207 LOGD("opus-decode: %d", out_bytes);
208 int open = out_bytes;
209 int processed = 0;
210 while(open>0){
211 int to_write = std::min(open, cfg.max_buffer_write_size);
212 int written = p_print->write(outbuf.data()+processed, to_write);
213 open -= written;
214 processed += written;
215 }
216 }
217 return len;
218 }
219
220 operator bool() override { return active; }
221
222 protected:
223 Print *p_print = nullptr;
224 OpusDecoder *dec = nullptr;
225 OpusSettings cfg;
226 bool active = false;
227 Vector<uint8_t> outbuf{0};
228 Vector<uint8_t> decbuf{0};
229 const uint32_t valid_rates[5] = {8000, 12000, 16000, 24000, 48000};
230
231 bool isValidRate(int rate){
232 for (auto &valid : valid_rates){
233 if (valid==rate) return true;
234 }
235 return false;
236 }
237};
238
247 public:
248 // Empty Constructor - the output stream must be provided with begin()
249 OpusAudioEncoder() = default;
250
251 // Constructor providing the output stream
252 OpusAudioEncoder(Print &out) { setOutput(out); }
253
255 void setOutput(Print &out_stream) override { p_print = &out_stream; }
256
258 const char *mime() override { return "audio/opus"; }
259
261 void setAudioInfo(AudioInfo from) override {
263 cfg.sample_rate = from.sample_rate;
264 cfg.channels = from.channels;
265 cfg.bits_per_sample = from.bits_per_sample;
266 }
267
269 bool begin() override {
270 int err;
271 int size = getFrameSizeSamples(cfg.sample_rate) * 2;
272 frame.resize(size);
273 assert(frame.data() != nullptr);
274 enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
275 if (err != OPUS_OK) {
276 LOGE("opus_encoder_create: %s for sample_rate: %d, channels:%d",
277 opus_strerror(err), cfg.sample_rate, cfg.channels);
278 return false;
279 }
280 is_open = settings();
281 return true;
282 }
283
285 OpusEncoderSettings &config() { return cfg; }
286
287 OpusEncoderSettings &defaultConfig() { return cfg; }
288
289 bool begin(OpusEncoderSettings settings) {
290 cfg = settings;
291 return begin();
292 }
293
295 void end() override {
296 // flush buffered data
297 encodeFrame();
298 // release memory
299 opus_encoder_destroy(enc);
300 is_open = false;
301 }
302
304 size_t write(const uint8_t *data, size_t len) override {
305 if (!is_open || p_print == nullptr) return 0;
306 LOGD("OpusAudioEncoder::write: %d", (int)len);
307
308 // fill frame
309 for (int j = 0; j < len; j++) {
310 encodeByte(data[j]);
311 }
312 return len;
313 }
314
315 operator bool() override { return is_open; }
316
317 bool isOpen() { return is_open; }
318
319 protected:
320 Print *p_print = nullptr;
321 OpusEncoder *enc = nullptr;
322 OpusEncoderSettings cfg;
323 bool is_open = false;
324 Vector<uint8_t> frame{0};
325 int frame_pos = 0;
326
327 void encodeByte(uint8_t data) {
328 // add byte to frame
329 frame[frame_pos++] = data;
330
331 // if frame is complete -> encode
332 if (frame_pos >= frame.size()) {
333 encodeFrame();
334 frame_pos = 0;
335 }
336 }
337
338 void encodeFrame() {
339 if (frame.size() > 0) {
340 // allocate temp buffer on stack
341 int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
342 uint8_t packet[packet_len];
343
344 int frames = frame.size() / cfg.channels / sizeof(int16_t);
345 LOGD("opus_encode - frame_size: %d", frames);
346 int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
347 packet, packet_len);
348 if (len < 0) {
349 LOGE("opus_encode: %s", opus_strerror(len));
350 } else if (len > 0) {
351 LOGD("opus-encode: %d", len);
352 int eff = p_print->write(packet, len);
353 if (eff!=len){
354 LOGE("encodeFrame data lost: %d->%d", len, eff);
355 }
356 }
357 }
358 }
359
361 int getFrameSizeSamples(int sampling_rate) {
362 switch (cfg.frame_sizes_ms_x2) {
363 case OPUS_FRAMESIZE_2_5_MS:
364 return sampling_rate / 400;
365 case OPUS_FRAMESIZE_5_MS:
366 return sampling_rate / 200;
367 case OPUS_FRAMESIZE_10_MS:
368 return sampling_rate / 100;
369 case OPUS_FRAMESIZE_20_MS:
370 return sampling_rate / 50;
371 case OPUS_FRAMESIZE_40_MS:
372 return sampling_rate / 25;
373 case OPUS_FRAMESIZE_60_MS:
374 return 3 * sampling_rate / 50;
375 case OPUS_FRAMESIZE_80_MS:
376 return 4 * sampling_rate / 50;
377 case OPUS_FRAMESIZE_100_MS:
378 return 5 * sampling_rate / 50;
379 case OPUS_FRAMESIZE_120_MS:
380 return 6 * sampling_rate / 50;
381 }
382 return sampling_rate / 100;
383 }
384
385 bool settings() {
386 bool ok = true;
387 if (cfg.bitrate >= 0 &&
388 opus_encoder_ctl(enc, OPUS_SET_BITRATE(cfg.bitrate)) != OPUS_OK) {
389 LOGE("invalid bitrate: %d", cfg.bitrate);
390 ok = false;
391 }
392 if (cfg.force_channel >= 0 &&
393 opus_encoder_ctl(enc, OPUS_SET_FORCE_CHANNELS(cfg.force_channel)) !=
394 OPUS_OK) {
395 LOGE("invalid force_channel: %d", cfg.force_channel);
396 ok = false;
397 };
398 if (cfg.vbr >= 0 &&
399 opus_encoder_ctl(enc, OPUS_SET_VBR(cfg.vbr)) != OPUS_OK) {
400 LOGE("invalid vbr: %d", cfg.vbr);
401 ok = false;
402 }
403 if (cfg.vbr_constraint >= 0 &&
404 opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(cfg.vbr_constraint)) !=
405 OPUS_OK) {
406 LOGE("invalid vbr_constraint: %d", cfg.vbr_constraint);
407 ok = false;
408 }
409 if (cfg.complexity >= 0 &&
410 opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(cfg.complexity)) != OPUS_OK) {
411 LOGE("invalid complexity: %d", cfg.complexity);
412 ok = false;
413 }
414 if (cfg.max_bandwidth >= 0 &&
415 opus_encoder_ctl(enc, OPUS_SET_MAX_BANDWIDTH(cfg.max_bandwidth)) !=
416 OPUS_OK) {
417 LOGE("invalid max_bandwidth: %d", cfg.max_bandwidth);
418 ok = false;
419 }
420 if (cfg.signal >= 0 &&
421 opus_encoder_ctl(enc, OPUS_SET_SIGNAL(cfg.signal)) != OPUS_OK) {
422 LOGE("invalid signal: %d", cfg.signal);
423 ok = false;
424 }
425 if (cfg.inband_fec >= 0 &&
426 opus_encoder_ctl(enc, OPUS_SET_INBAND_FEC(cfg.inband_fec)) != OPUS_OK) {
427 LOGE("invalid inband_fec: %d", cfg.inband_fec);
428 ok = false;
429 }
430 if (cfg.packet_loss_perc >= 0 &&
431 opus_encoder_ctl(
432 enc, OPUS_SET_PACKET_LOSS_PERC(cfg.packet_loss_perc)) != OPUS_OK) {
433 LOGE("invalid pkt_loss: %d", cfg.packet_loss_perc);
434 ok = false;
435 }
436 if (cfg.lsb_depth >= 0 &&
437 opus_encoder_ctl(enc, OPUS_SET_LSB_DEPTH(cfg.lsb_depth)) != OPUS_OK) {
438 LOGE("invalid lsb_depth: %d", cfg.lsb_depth);
439 ok = false;
440 }
441 if (cfg.prediction_disabled >= 0 &&
442 opus_encoder_ctl(enc, OPUS_SET_PREDICTION_DISABLED(
443 cfg.prediction_disabled)) != OPUS_OK) {
444 LOGE("invalid pred_disabled: %d", cfg.prediction_disabled);
445 ok = false;
446 }
447 if (cfg.use_dtx >= 0 &&
448 opus_encoder_ctl(enc, OPUS_SET_DTX(cfg.use_dtx)) != OPUS_OK) {
449 LOGE("invalid use_dtx: %d", cfg.use_dtx);
450 ok = false;
451 }
452 if (cfg.frame_sizes_ms_x2 > 0 &&
453 opus_encoder_ctl(enc, OPUS_SET_EXPERT_FRAME_DURATION(
454 cfg.frame_sizes_ms_x2)) != OPUS_OK) {
455 LOGE("invalid frame_sizes_ms_x2: %d", cfg.frame_sizes_ms_x2);
456 ok = false;
457 }
458 return ok;
459 }
460};
461
462} // namespace audio_tools
Decoding of encoded audio into PCM data.
Definition AudioCodecsBase.h:18
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition AudioCodecsBase.h:28
Encoding of PCM data.
Definition AudioCodecsBase.h:90
void setAudioInfo(AudioInfo from) override
Defines the sample rate, number of channels and bits per sample.
Definition AudioCodecsBase.h:99
OpusAudioDecoder: Depends on https://github.com/pschatzmann/arduino-libopus.git.
Definition CodecOpus.h:115
OpusAudioDecoder(Print &out_stream)
Construct a new OpusDecoder object.
Definition CodecOpus.h:127
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecOpus.h:133
void setAudioInfo(AudioInfo from) override
for most decoders this is not needed
Definition CodecOpus.h:185
OpusSettings & config()
Provides access to the configuration.
Definition CodecOpus.h:138
OpusAudioDecoder()=default
Construct a new OpusDecoder object.
AudioInfo audioInfo() override
provides the actual input AudioInfo
Definition CodecOpus.h:135
OpusAudioEncoder: Dependens on https://github.com/pschatzmann/arduino-libopus.git.
Definition CodecOpus.h:246
void setOutput(Print &out_stream) override
Defines the output Stream.
Definition CodecOpus.h:255
void setAudioInfo(AudioInfo from) override
We actually do nothing with this.
Definition CodecOpus.h:261
void end() override
stops the processing
Definition CodecOpus.h:295
size_t write(const uint8_t *data, size_t len) override
Writes PCM data to be encoded as Opus.
Definition CodecOpus.h:304
OpusEncoderSettings & config()
Provides access to the configuration.
Definition CodecOpus.h:285
const char * mime() override
Provides "audio/pcm".
Definition CodecOpus.h:258
bool begin() override
starts the processing using the actual OpusAudioInfo
Definition CodecOpus.h:269
int getFrameSizeSamples(int sampling_rate)
Returns the frame size in samples.
Definition CodecOpus.h:361
Definition NoArduino.h:62
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition AudioCodecsBase.h:10
Basic Audio information which drives e.g. I2S.
Definition AudioTypes.h:53
sample_rate_t sample_rate
Sample Rate: e.g 44100.
Definition AudioTypes.h:55
uint16_t channels
Number of channels: 2=stereo, 1=mono.
Definition AudioTypes.h:57
uint8_t bits_per_sample
Number of bits per sample (int16_t = 16 bits)
Definition AudioTypes.h:59
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:23
OpusSettings()
Definition CodecOpus.h:24