4
4
#include " Print.h"
5
5
#include " opus.h"
6
6
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
9
13
#endif
10
14
11
15
namespace audio_tools {
@@ -24,7 +28,7 @@ struct OpusSettings : public AudioBaseInfo {
24
28
// / must be 16!
25
29
bits_per_sample = 16 ;
26
30
}
27
- int max_buffer_size = OPUS_MAX_BUFFER_SIZE ;
31
+ int max_buffer_size = OPUS_DEC_MAX_BUFFER_SIZE ;
28
32
};
29
33
30
34
/* *
@@ -59,9 +63,10 @@ frame_sizes_ms_x2[9] = {OPUS_FRAMESIZE_2_5_MS,OPUS_FRAMESIZE_5_MS,OPUS_FRAMESIZE
59
63
**/
60
64
61
65
struct OpusEncoderSettings : public OpusSettings {
62
- OpusEncoderSettings () : OpusSettings() {}
66
+ OpusEncoderSettings () : OpusSettings() {
63
67
// / Default is 5760
64
- int max_buffer_size = OPUS_MAX_BUFFER_SIZE;
68
+ max_buffer_size = OPUS_ENC_MAX_BUFFER_SIZE;
69
+ }
65
70
// / OPUS_APPLICATION_AUDIO, OPUS_APPLICATION_VOIP,
66
71
// / OPUS_APPLICATION_RESTRICTED_LOWDELAY
67
72
int application = OPUS_APPLICATION_AUDIO;
@@ -144,6 +149,8 @@ class OpusAudioDecoder : public AudioDecoder {
144
149
void begin () override {
145
150
LOGD (LOG_METHOD);
146
151
outbuf.resize (cfg.max_buffer_size );
152
+ assert (outbuf.data () != nullptr );
153
+
147
154
int err;
148
155
dec = opus_decoder_create (cfg.sample_rate , cfg.channels , &err);
149
156
if (err != OPUS_OK) {
@@ -193,9 +200,9 @@ class OpusAudioDecoder : public AudioDecoder {
193
200
Print *p_print = nullptr ;
194
201
AudioBaseInfoDependent *bid = nullptr ;
195
202
OpusSettings cfg;
196
- :: OpusDecoder *dec;
203
+ OpusDecoder *dec;
197
204
bool active;
198
- Vector<uint8_t > outbuf;
205
+ Vector<uint8_t > outbuf{ 0 } ;
199
206
};
200
207
201
208
/* *
@@ -228,13 +235,10 @@ class OpusAudioEncoder : public AudioEncoder {
228
235
// / starts the processing using the actual OpusAudioInfo
229
236
void begin () override {
230
237
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 );
233
240
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);
238
242
if (err != OPUS_OK) {
239
243
LOGE (" opus_encoder_create: %s for sample_rate: %d, channels:%d" ,
240
244
opus_strerror (err), cfg.sample_rate , cfg.channels );
@@ -255,7 +259,7 @@ class OpusAudioEncoder : public AudioEncoder {
255
259
// / stops the processing
256
260
void end () override {
257
261
// flush buffered data
258
- encodeFrame (frame_pos );
262
+ encodeFrame ();
259
263
// release memory
260
264
opus_encoder_destroy (enc);
261
265
is_open = false ;
@@ -265,6 +269,7 @@ class OpusAudioEncoder : public AudioEncoder {
265
269
size_t write (const void *in_ptr, size_t in_size) {
266
270
if (!is_open || p_print == nullptr ) return 0 ;
267
271
272
+ // fill frame
268
273
uint8_t *p_byte = (uint8_t *)in_ptr;
269
274
for (int j = 0 ; j < in_size; j++) {
270
275
encodeByte (p_byte[j]);
@@ -278,35 +283,37 @@ class OpusAudioEncoder : public AudioEncoder {
278
283
279
284
protected:
280
285
Print *p_print = nullptr ;
281
- :: OpusEncoder *enc = nullptr ;
286
+ OpusEncoder *enc = nullptr ;
282
287
OpusEncoderSettings cfg;
283
288
bool is_open = false ;
284
- Vector<uint8_t > packet;
285
- Vector<uint8_t > frame;
289
+ Vector<uint8_t > frame{0 };
286
290
int frame_pos = 0 ;
287
291
288
292
void encodeByte (uint8_t data) {
289
293
// add byte to frame
290
294
frame[frame_pos++] = data;
291
295
292
296
// 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 ();
296
299
frame_pos = 0 ;
297
300
}
298
301
}
299
302
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 );
303
310
LOGD (" opus_encode - frame_size: %d" , frames);
304
311
int len = opus_encode (enc, (opus_int16 *)frame.data (), frames,
305
- packet. data (), cfg. max_buffer_size );
312
+ packet, packet_len );
306
313
if (len < 0 ) {
307
314
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);
310
317
if (eff!=len){
311
318
LOGE (" encodeFrame data lost" );
312
319
}
0 commit comments