Skip to content

Commit 473111d

Browse files
committed
Opus codec
1 parent 7883c17 commit 473111d

File tree

6 files changed

+48
-21
lines changed

6 files changed

+48
-21
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@
1313
#include "AudioCodecs/CodecOpus.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; // The stream will have 2 channels
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
@@ -47,8 +46,12 @@ void setup() {
4746
decoder.begin(cfgs);
4847

4948
// configure and start encoder
50-
enc.config().application = application;
51-
encoder.begin(cfgs);
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);
5255

5356
Serial.println("Test started...");
5457
}

src/AudioCodecs/CodecOpus.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ struct OpusEncoderSettings : public OpusSettings {
8181
/// OPUS_BANDWIDTH_SUPERWIDEBAND, OPUS_BANDWIDTH_FULLBAND,
8282
/// OPUS_BANDWIDTH_FULLBAND
8383
int max_bandwidth = -1;
84-
/// OPUS_AUTO, OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC
84+
/// OPUS_AUTO, OPUS_SIGNAL_VOICE, OPUS_SIGNAL_MUSIC
8585
int singal = -1;
8686
/// 0, 1
8787
int inband_fec = -1;
@@ -93,7 +93,7 @@ struct OpusEncoderSettings : public OpusSettings {
9393
int prediction_disabled = -1;
9494
/// 0, 1
9595
int use_dtx = -1;
96-
/// 5, 10, 20, 40, 80, 120, 160, 200, 240
96+
/// 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
9797
int frame_sizes_ms_x2 = -1; /* x2 to avoid 2.5 ms */
9898
};
9999

@@ -306,7 +306,10 @@ class OpusAudioEncoder : public AudioEncoder {
306306
if (len < 0) {
307307
LOGE("opus_encode: %s", opus_strerror(len));
308308
} else if (len > 0 && len <= cfg.max_buffer_size) {
309-
p_print->write(packet.data(), len);
309+
int eff = p_print->write(packet.data(), len);
310+
if (eff!=len){
311+
LOGE("encodeFrame data lost");
312+
}
310313
}
311314
}
312315
}

src/AudioCodecs/CodecOpusOgg.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,14 @@ class OpusOggDecoder : public OggContainerDecoder {
6464

6565
virtual void beginOfSegment(ogg_packet *op) {
6666
LOGD("bos");
67-
if (strncmp((char *)op->bytes, "OpusHead", 8) == 0) {
67+
if (op->packet==nullptr) return;
68+
if (strncmp("OpusHead", (char *)op->packet, 8) == 0) {
6869
memmove(&header, (char *)op->packet, sizeof(header));
6970
cfg.sample_rate = header.sampleRate;
7071
cfg.channels = header.channelCount;
72+
LOGI("sample rate: %d", cfg.sample_rate);
7173
notify();
72-
} else if (strncmp((char *)op->bytes, "OpusTags", 8) == 0) {
74+
} else if (strncmp("OpusTags",(char *)op->packet , 8) == 0) {
7375
// not processed
7476
}
7577
}

src/AudioCodecs/ContainerOgg.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,21 @@ class OggContainerDecoder : public AudioDecoder {
6666
void begin() override {
6767
LOGD(LOG_METHOD);
6868
if (p_oggz == nullptr) {
69-
p_oggz = oggz_new(OGGZ_READ | OGGZ_NONSTRICT | OGGZ_AUTO);
69+
p_oggz = oggz_new(OGGZ_READ | OGGZ_AUTO); // OGGZ_NONSTRICT
7070
is_open = true;
71+
// Callback to Replace standard IO
72+
if (oggz_io_set_read(p_oggz, ogg_io_read, this)!=0){
73+
LOGE("oggz_io_set_read");
74+
is_open = false;
75+
}
7176
// Callback
7277
if (oggz_set_read_callback(p_oggz, -1, read_packet, this)!=0){
7378
LOGE("oggz_set_read_callback");
7479
is_open = false;
7580
}
76-
// Callback to Replace standard IO
77-
if (oggz_io_set_read(p_oggz, ogg_io_read, this)!=0){
78-
LOGE("oggz_io_set_read");
81+
82+
if (oggz_set_read_page(p_oggz, -1, read_page, this)!=0){
83+
LOGE("oggz_set_read_page");
7984
is_open = false;
8085
}
8186
}
@@ -125,13 +130,14 @@ class OggContainerDecoder : public AudioDecoder {
125130
bool is_open = false;
126131
long pos = 0;
127132

128-
// Final Stream Callback
133+
// Final Stream Callback -> write data to requested output destination
129134
static size_t ogg_io_read(void *user_handle, void *buf, size_t n) {
130135
LOGI("ogg_io_read: %u", n);
131136
OggContainerDecoder *self = (OggContainerDecoder *)user_handle;
132137
int len = self->buffer.readArray((uint8_t *)buf, n);
133138
self->pos += len;
134139
return len;
140+
//return self->p_print->write((uint8_t *)buf, n);
135141
}
136142

137143
// Process full packet
@@ -147,11 +153,20 @@ class OggContainerDecoder : public AudioDecoder {
147153
self->endOfSegment(op);
148154
} else {
149155
LOGI("process audio packet");
150-
self->p_print->write(op->packet, op->bytes);
156+
int eff = self->p_print->write(op->packet, op->bytes);
157+
if (eff!=result){
158+
LOGE("Incomplere write");
159+
}
151160
}
152161
return result;
153162
}
154163

164+
165+
static int read_page(OGGZ *oggz, const ogg_page *og, long serialno, void *user_data){
166+
LOGI("read_page: %u", og->body_len);
167+
return og->body_len;
168+
}
169+
155170
virtual void beginOfSegment(ogg_packet *op) {
156171
LOGD("bos");
157172
if (op->bytes >= sizeof(cfg)) {
@@ -225,7 +240,7 @@ class OggContainerEncoder : public AudioEncoder {
225240
LOGD(LOG_METHOD);
226241
is_open = true;
227242
if (p_oggz == nullptr) {
228-
p_oggz = oggz_new(OGGZ_WRITE | OGGZ_NONSTRICT);
243+
p_oggz = oggz_new(OGGZ_WRITE | OGGZ_NONSTRICT | OGGZ_AUTO);
229244
serialno = oggz_serialno_new(p_oggz);
230245
oggz_io_set_write(p_oggz, ogg_io_write, this);
231246
packetno = 0;
@@ -266,7 +281,7 @@ class OggContainerEncoder : public AudioEncoder {
266281
op.b_o_s = false;
267282
op.e_o_s = false;
268283
op.packetno = packetno++;
269-
if (!writePacket(op)) {
284+
if (!writePacket(op, OGGZ_FLUSH_AFTER)) {
270285
return 0;
271286
}
272287

tests/codec/opus/opus.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with
1919
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
2020
CsvStream<int16_t> out(Serial, 2); // Output of sound on desktop
2121
OpusAudioEncoder enc;
22-
EncodedAudioStream decoder(&out, new OpusAudioDecoder()); // encode and write
23-
EncodedAudioStream encoder(&decoder, &enc); // encode and write
22+
OpusAudioDecoder dec;
23+
EncodedAudioStream decoder(out, dec); // encode and write
24+
EncodedAudioStream encoder(decoder, enc); // encode and write
2425
StreamCopy copier(encoder, sound);
2526

2627
void setup() {

tests/codec/opusogg/opusogg.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@ SineWaveGenerator<int16_t> sineWave( 32000); // subclass of SoundGenerator with
1919
GeneratedSoundStream<int16_t> sound( sineWave); // Stream generated from sine wave
2020
CsvStream<int16_t> out(Serial, channels); // Output of sound on desktop
2121
OpusOggEncoder enc;
22-
EncodedAudioStream decoder(&out, new OpusOggDecoder()); // encode and write
23-
EncodedAudioStream encoder(&decoder, &enc); // encode and write
22+
OpusOggDecoder dec;
23+
EncodedAudioStream decoder(out, dec); // encode and write
24+
HexDumpStream hex(Serial);
25+
EncodedAudioStream encoder(&hex, &enc); // encode and write
26+
//EncodedAudioStream encoder(&decoder, &enc); // encode and write
2427
StreamCopy copier(encoder, sound);
2528

2629
void setup() {

0 commit comments

Comments
 (0)