-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopusdec~.c
266 lines (219 loc) · 7.62 KB
/
opusdec~.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include "m_pd.h"
#include <opus.h>
#include <opus_private.h>
#include <stdlib.h>
#include <string.h>
#define MAX_PACKET_SIZE 1024
static t_class* opusdec_tilde_class;
enum LOG_LEVEL
{
LOG_LEVEL_DEBUG = 0,
LOG_LEVEL_NORMAL
};
typedef struct _opusdec_tilde
{
t_object x_obj;
t_outlet* _audioOutlet;
OpusDecoder* _decoder;
int _sampleRate;
int _opusFrameSizeMs;
int _opusFrameSize;
int _masterFrameSize;
float* _frameBuffer;
int _frameBufferSize;
int _writePosition;
int _readPosition;
int _framesDecoded;
unsigned char* _packet;
int _packetSize;
int _lostPrevious;
} t_opusdec_tilde;
void opusdec_tilde_setup();
void* opusdec_tilde_new(t_floatarg frameSize);
void opusdec_tilde_free(t_opusdec_tilde* x);
void opusdec_tilde_dsp(t_opusdec_tilde* x, t_signal** sp);
t_int* opusdec_tilde_perform(t_int* w);
void opusdec_tilde_reset(t_opusdec_tilde* x);
void opusdec_tilde_packet(t_opusdec_tilde* x, t_symbol* s, int argc, t_atom* argv);
void opusdec_tilde_bang(t_opusdec_tilde* x);
int setOpusSampleRate(t_opusdec_tilde* x, int sampleRate);
int setBufferSizes(t_opusdec_tilde* x, int masterFrameSize, int opusFrameSize);
void opusdec_tilde_setup()
{
opusdec_tilde_class = class_new(gensym("opusdec~"),
(t_newmethod)opusdec_tilde_new,
(t_method)opusdec_tilde_free,
sizeof(t_opusdec_tilde),
CLASS_DEFAULT,
A_FLOAT,
0);
class_addmethod(opusdec_tilde_class, (t_method)opusdec_tilde_dsp, gensym("dsp"), 0);
class_addlist(opusdec_tilde_class, (t_method)opusdec_tilde_packet);
class_addbang(opusdec_tilde_class, (t_method)opusdec_tilde_bang);
}
void* opusdec_tilde_new(t_floatarg frameSize)
{
t_opusdec_tilde* x = (t_opusdec_tilde*)pd_new(opusdec_tilde_class);
if (!x)
return 0;
x->_audioOutlet = outlet_new(&x->x_obj, &s_signal);
x->_sampleRate = (int)sys_getsr();
x->_opusFrameSizeMs = frameSize;
x->_opusFrameSize = 0;
x->_masterFrameSize = 0;
x->_frameBuffer = 0;
x->_frameBufferSize = 0;
x->_writePosition = 0;
x->_readPosition = 0;
x->_framesDecoded = 0;
x->_packet = (unsigned char*)malloc(MAX_PACKET_SIZE);
x->_packetSize = 0;
x->_lostPrevious = 0;
int err = 0;
x->_decoder = opus_decoder_create(x->_sampleRate, 1, &err);
if (err)
{
error("could not create OPUS decoder: %s", opus_strerror(err));
opusdec_tilde_free(x);
return 0;
}
verbose(LOG_LEVEL_NORMAL, "OPUS decoder initialised @%dhz", x->_sampleRate);
setBufferSizes(x, sys_getblksize(), x->_opusFrameSizeMs * x->_sampleRate / 1000);
return x;
}
void opusdec_tilde_free(t_opusdec_tilde* x)
{
if (x->_decoder)
{
opus_decoder_destroy(x->_decoder);
x->_decoder = 0;
}
if (x->_frameBuffer) {
free(x->_frameBuffer);
}
if (x->_packet) {
free(x->_packet);
}
}
int setOpusSampleRate(t_opusdec_tilde* x, int sampleRate)
{
if (x->_sampleRate == sampleRate)
{
return 1;
}
x->_sampleRate = sampleRate;
int err = opus_decoder_init(x->_decoder, sampleRate, 1);
if (err)
{
error("could not initialise OPUS encoder @%dhz: %s", sampleRate, opus_strerror(err));
return 0;
}
verbose(LOG_LEVEL_NORMAL, "OPUS encoder initialised @%dhz", sampleRate);
return setBufferSizes(x, x->_masterFrameSize, x->_opusFrameSizeMs * x->_sampleRate / 1000);
}
int setBufferSizes(t_opusdec_tilde* x, int masterFrameSize, int opusFrameSize)
{
if (x->_masterFrameSize == masterFrameSize && x->_opusFrameSize == opusFrameSize)
return 1;
x->_masterFrameSize = masterFrameSize;
x->_opusFrameSize = opusFrameSize;
if (x->_frameBuffer)
free(x->_frameBuffer);
x->_frameBufferSize = 3 * x->_opusFrameSize;
x->_frameBuffer = (float*)calloc(x->_frameBufferSize, sizeof(float));
opusdec_tilde_reset(x);
verbose(LOG_LEVEL_NORMAL, "pd~ buffer size: %d, OPUS frame size: %d", x->_masterFrameSize, x->_opusFrameSize);
verbose(LOG_LEVEL_NORMAL, "frame buffer of size %d allocated", x->_frameBufferSize);
return 1;
}
void opusdec_tilde_dsp(t_opusdec_tilde* x, t_signal** sp)
{
setOpusSampleRate(x, sp[0]->s_sr);
dsp_add(opusdec_tilde_perform, 3, x, sp[0]->s_vec, sp[0]->s_n);
}
void opusdec_tilde_reset(t_opusdec_tilde* x)
{
x->_writePosition = 0;
x->_readPosition = x->_frameBufferSize - x->_opusFrameSize;
memset(x->_frameBuffer, 0, x->_frameBufferSize * sizeof(float));
x->_framesDecoded = 0;
}
void advanceWritePosition(t_opusdec_tilde* x, int samples) {
if (samples != x->_opusFrameSize)
{
error("decoded samples do not match frameSize");
return;
}
x->_writePosition = (x->_writePosition + samples) % x->_frameBufferSize;
x->_framesDecoded = 1;
}
void opusdec_tilde_bang(t_opusdec_tilde* x)
{
if (x->_lostPrevious) {
int decoded = opus_decode_float(x->_decoder, 0, 0, x->_frameBuffer + x->_writePosition, x->_opusFrameSize, 0);
advanceWritePosition(x, decoded);
verbose(LOG_LEVEL_NORMAL, "generated %d PLC samples", decoded);
}
x->_lostPrevious = 1;
}
void loadPacket(t_opusdec_tilde* x, t_symbol* s, int argc, t_atom* argv)
{
x->_packetSize = 0;
for (int i = 0; i < argc; ++i)
{
if (argv[i].a_type != A_FLOAT || argv[i].a_w.w_float != (int)argv[i].a_w.w_float || argv[i].a_w.w_float < 0 || argv[i].a_w.w_float > 255)
{
error("recieved invalid packet data (%f) at byte index: %d", argv[i].a_w.w_float, i);
return;
}
x->_packet[i] = (int)argv[i].a_w.w_float;
}
x->_packetSize = argc;
}
void opusdec_tilde_packet(t_opusdec_tilde* x, t_symbol* s, int argc, t_atom* argv)
{
verbose(LOG_LEVEL_NORMAL, "packet of size %d received", argc);
loadPacket(x, s, argc, argv);
if (!x->_packetSize) {
opusdec_tilde_bang(x);
return;
}
int decoded = 0;
if (x->_lostPrevious) {
decoded = opus_decode_float(x->_decoder, x->_packet, x->_packetSize, x->_frameBuffer + x->_writePosition, x->_opusFrameSize, 1);
advanceWritePosition(x, decoded);
verbose(LOG_LEVEL_NORMAL, "decoded %d FEC samples", decoded);
}
x->_lostPrevious = 0;
decoded = opus_decode_float(x->_decoder, x->_packet, x->_packetSize, x->_frameBuffer + x->_writePosition, x->_opusFrameSize, 0);
advanceWritePosition(x, decoded);
verbose(LOG_LEVEL_NORMAL, "decoded %d samples from a packet of size %d", decoded, argc);
}
void readFrameBuffer(t_opusdec_tilde* x, float* out)
{
if (!x->_framesDecoded)
{
memset(out, 0, x->_masterFrameSize * sizeof(float));
return;
}
if (x->_readPosition + x->_masterFrameSize > x->_frameBufferSize)
{
int count = x->_frameBufferSize - x->_readPosition;
memcpy(out, x->_frameBuffer + x->_readPosition, count * sizeof(float));
memcpy(out + count, x->_frameBuffer, (x->_masterFrameSize - count) * sizeof(float));
}
else
{
memcpy(out, x->_frameBuffer + x->_readPosition, x->_masterFrameSize * sizeof(float));
}
x->_readPosition = (x->_readPosition + x->_masterFrameSize) % x->_frameBufferSize;
}
t_int* opusdec_tilde_perform(t_int* w)
{
t_opusdec_tilde* x = (t_opusdec_tilde*)(w[1]);
t_sample* out = (t_sample*)(w[2]);
int n = (int)(w[3]);
setBufferSizes(x, n, x->_opusFrameSize);
readFrameBuffer(x, out);
return w + 4;
}