Skip to content

Commit adc7fd9

Browse files
committed
opus-ogg
1 parent b644345 commit adc7fd9

File tree

5 files changed

+56
-65
lines changed

5 files changed

+56
-65
lines changed

examples/tests/codecs/test-codec-opus/test-codec-opus.ino

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
#include "AudioCodecs/CodecOpus.h"
1414

1515
int sample_rate = 24000;
16-
int channels = 1; // The stream will have 2 channels
16+
int channels = 1;
1717

1818
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
1919
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
@@ -42,16 +42,14 @@ void setup() {
4242
cfgs.bits_per_sample = 16;
4343
sineWave.begin(cfgs, N_B4);
4444

45-
// Opus decoder needs to know the audio info
45+
// Opus encoder and decoder need to know the audio info
4646
decoder.begin(cfgs);
47+
encoder.begin(cfgs);
4748

48-
// configure and start encoder
49-
auto encfg = enc.config();
50-
encfg.copyFrom(cfgs); // use sample rate, channels ...
51-
//encfg.application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
52-
//encfg.frame_sizes_ms_x2 = OPUS_FRAMESIZE_20_MS;
53-
//encfg.complexity = 5;
54-
encoder.begin(encfg);
49+
// configure additinal parameters
50+
//enc.config().application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
51+
//enc.config().frame_sizes_ms_x2 = OPUS_FRAMESIZE_20_MS;
52+
//enc.config().complexity = 5;
5553

5654
Serial.println("Test started...");
5755
}

examples/tests/codecs/test-codec-opusogg/test-codec-opusogg.ino

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,36 @@
1313
#include "AudioCodecs/CodecOpusOgg.h"
1414

1515
int sample_rate = 24000;
16-
int channels = 2; // The stream will have 2 channels
17-
int application = OPUS_APPLICATION_AUDIO; // Opus application
16+
int channels = 1;
1817

1918
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
2019
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
21-
AudioKitStream out;
20+
CsvStream<int16_t> out(Serial,channels);
2221
OpusOggEncoder enc;
23-
EncodedAudioStream decoder(&out, new OpusOggDecoder()); // encode and write
22+
OpusOggDecoder dec;
23+
EncodedAudioStream decoder(&out, &dec); // encode and write
2424
EncodedAudioStream encoder(&decoder, &enc); // encode and write
2525
StreamCopy copier(encoder, sound);
2626

2727
void setup() {
2828
Serial.begin(115200);
2929
AudioLogger::instance().begin(Serial, AudioLogger::Warning);
3030

31-
// start I2S
32-
Serial.println("starting I2S...");
33-
auto cfgi = out.defaultConfig(TX_MODE);
34-
cfgi.sample_rate = sample_rate;
35-
cfgi.channels = channels;
36-
cfgi.bits_per_sample = 16;
37-
out.begin(cfgi);
38-
3931
// Setup sine wave
4032
auto cfgs = sineWave.defaultConfig();
4133
cfgs.sample_rate = sample_rate;
4234
cfgs.channels = channels;
4335
cfgs.bits_per_sample = 16;
4436
sineWave.begin(cfgs, N_B4);
4537

46-
// Opus decoder needs to know the audio info
47-
decoder.begin(cfgs);
48-
49-
// configure and start encoder
50-
enc.config().application = application;
38+
// Opus encoder needs to know the audio info
5139
encoder.begin(cfgs);
40+
decoder.begin();
41+
42+
// configure additinal parameters
43+
//enc.config().application = OPUS_APPLICATION_RESTRICTED_LOWDELAY;
44+
//enc.config().frame_sizes_ms_x2 = OPUS_FRAMESIZE_20_MS;
45+
//enc.config().complexity = 5;
5246

5347
Serial.println("Test started...");
5448
}

src/AudioCodecs/CodecOpus.h

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44
#include "Print.h"
55
#include "opus.h"
66

7-
#ifndef OPUS_MAX_BUFFER_SIZE
8-
#define OPUS_MAX_BUFFER_SIZE (5760)
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 1024
913
#endif
1014

1115
namespace audio_tools {
@@ -24,7 +28,7 @@ struct OpusSettings : public AudioBaseInfo {
2428
/// must be 16!
2529
bits_per_sample = 16;
2630
}
27-
int max_buffer_size = OPUS_MAX_BUFFER_SIZE;
31+
int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE;
2832
};
2933

3034
/**
@@ -59,9 +63,10 @@ frame_sizes_ms_x2[9] = {OPUS_FRAMESIZE_2_5_MS,OPUS_FRAMESIZE_5_MS,OPUS_FRAMESIZE
5963
**/
6064

6165
struct OpusEncoderSettings : public OpusSettings {
62-
OpusEncoderSettings() : OpusSettings() {}
66+
OpusEncoderSettings() : OpusSettings() {
6367
/// Default is 5760
64-
int max_buffer_size = OPUS_MAX_BUFFER_SIZE;
68+
max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
69+
}
6570
/// OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
6671
/// OPUS_APPLICATION_RESTRICTED_LOWDELAY
6772
int application = OPUS_APPLICATION_AUDIO;
@@ -144,6 +149,8 @@ class OpusAudioDecoder : public AudioDecoder {
144149
void begin() override {
145150
LOGD(LOG_METHOD);
146151
outbuf.resize(cfg.max_buffer_size);
152+
assert(outbuf.data() != nullptr);
153+
147154
int err;
148155
dec = opus_decoder_create(cfg.sample_rate, cfg.channels, &err);
149156
if (err != OPUS_OK) {
@@ -193,9 +200,9 @@ class OpusAudioDecoder : public AudioDecoder {
193200
Print *p_print = nullptr;
194201
AudioBaseInfoDependent *bid = nullptr;
195202
OpusSettings cfg;
196-
::OpusDecoder *dec;
203+
OpusDecoder *dec;
197204
bool active;
198-
Vector<uint8_t> outbuf;
205+
Vector<uint8_t> outbuf{0};
199206
};
200207

201208
/**
@@ -228,13 +235,10 @@ class OpusAudioEncoder : public AudioEncoder {
228235
/// starts the processing using the actual OpusAudioInfo
229236
void begin() override {
230237
int err;
231-
packet.resize(cfg.max_buffer_size);
232-
frame.resize(getFrameSizeSamples(cfg.sample_rate) * 2);
238+
int size = getFrameSizeSamples(cfg.sample_rate) * 2;
239+
frame.resize(size);
233240
assert(frame.data() != nullptr);
234-
assert(packet.data() != nullptr);
235-
236-
enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application,
237-
&err);
241+
enc = opus_encoder_create(cfg.sample_rate, cfg.channels, cfg.application, &err);
238242
if (err != OPUS_OK) {
239243
LOGE("opus_encoder_create: %s for sample_rate: %d, channels:%d",
240244
opus_strerror(err), cfg.sample_rate, cfg.channels);
@@ -255,7 +259,7 @@ class OpusAudioEncoder : public AudioEncoder {
255259
/// stops the processing
256260
void end() override {
257261
// flush buffered data
258-
encodeFrame(frame_pos);
262+
encodeFrame();
259263
// release memory
260264
opus_encoder_destroy(enc);
261265
is_open = false;
@@ -265,6 +269,7 @@ class OpusAudioEncoder : public AudioEncoder {
265269
size_t write(const void *in_ptr, size_t in_size) {
266270
if (!is_open || p_print == nullptr) return 0;
267271

272+
// fill frame
268273
uint8_t *p_byte = (uint8_t *)in_ptr;
269274
for (int j = 0; j < in_size; j++) {
270275
encodeByte(p_byte[j]);
@@ -278,35 +283,37 @@ class OpusAudioEncoder : public AudioEncoder {
278283

279284
protected:
280285
Print *p_print = nullptr;
281-
::OpusEncoder *enc = nullptr;
286+
OpusEncoder *enc = nullptr;
282287
OpusEncoderSettings cfg;
283288
bool is_open = false;
284-
Vector<uint8_t> packet;
285-
Vector<uint8_t> frame;
289+
Vector<uint8_t> frame{0};
286290
int frame_pos = 0;
287291

288292
void encodeByte(uint8_t data) {
289293
// add byte to frame
290294
frame[frame_pos++] = data;
291295

292296
// if frame is complete -> encode
293-
int frame_size = frame.size();
294-
if (frame_pos >= frame_size) {
295-
encodeFrame(frame_size);
297+
if (frame_pos >= frame.size()) {
298+
encodeFrame();
296299
frame_pos = 0;
297300
}
298301
}
299302

300-
void encodeFrame(int lenBytes) {
301-
if (lenBytes > 0) {
302-
int frames = lenBytes / cfg.channels / sizeof(int16_t);
303+
void encodeFrame() {
304+
if (frame.size() > 0) {
305+
// allocate temp buffer on stack
306+
int packet_len = OPUS_ENC_MAX_BUFFER_SIZE > 0 ? OPUS_ENC_MAX_BUFFER_SIZE : 512;
307+
uint8_t packet[packet_len];
308+
309+
int frames = frame.size() / cfg.channels / sizeof(int16_t);
303310
LOGD("opus_encode - frame_size: %d", frames);
304311
int len = opus_encode(enc, (opus_int16 *)frame.data(), frames,
305-
packet.data(), cfg.max_buffer_size);
312+
packet, packet_len);
306313
if (len < 0) {
307314
LOGE("opus_encode: %s", opus_strerror(len));
308-
} else if (len > 0 && len <= cfg.max_buffer_size) {
309-
int eff = p_print->write(packet.data(), len);
315+
} else if (len > 0) {
316+
int eff = p_print->write(packet, len);
310317
if (eff!=len){
311318
LOGE("encodeFrame data lost");
312319
}

src/AudioCodecs/ContainerOgg.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
#include "AudioTools/Buffers.h"
66
#include "oggz/oggz.h"
77

8-
#define OGG_DEFAULT_BUFFER_SIZE (2048)
9-
#define OGG_READ_SIZE 1024
8+
#define OGG_DEFAULT_BUFFER_SIZE (246)
9+
#define OGG_READ_SIZE (512)
1010

1111
namespace audio_tools {
1212

@@ -102,7 +102,7 @@ class OggContainerDecoder : public AudioDecoder {
102102
}
103103

104104
virtual size_t write(const void *in_ptr, size_t in_size) override {
105-
LOGI("write: %u", in_size);
105+
LOGD("write: %u", in_size);
106106
if (p_print == nullptr) return 0;
107107

108108
// fill buffer

tests/codec/opus/opus.cpp

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
#include "AudioCodecs/CodecOpus.h"
1313

1414
int sample_rate = 24000;
15-
int channels = 2; // The stream will have 2 channels
16-
int application = OPUS_APPLICATION_AUDIO; // Opus application
15+
int channels = 1; // The stream will have 2 channels
1716

1817
SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with max amplitude of 32000
1918
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
@@ -28,13 +27,6 @@ void setup() {
2827
Serial.begin(115200);
2928
AudioLogger::instance().begin(Serial, AudioLogger::Debug);
3029

31-
// start I2S
32-
// Serial.println("starting I2S...");
33-
// auto cfgi = out.defaultConfig(TX_MODE);
34-
// cfgi.sample_rate = sample_rate;
35-
// cfgi.channels = channels;
36-
// cfgi.bits_per_sample = 16;
37-
// out.begin(cfgi);
3830

3931
// Setup sine wave
4032
auto cfgs = sineWave.defaultConfig();
@@ -47,7 +39,7 @@ void setup() {
4739
decoder.begin(cfgs);
4840

4941
// configure and start encoder
50-
enc.config().application = application;
42+
enc.config().application = OPUS_APPLICATION_AUDIO;
5143
encoder.begin(cfgs);
5244

5345
Serial.println("Test started...");

0 commit comments

Comments
 (0)