forked from silentlexx/deadbeef_soxr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
soxr.c
427 lines (367 loc) · 12.2 KB
/
soxr.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
DeaDBeeF - The Ultimate Music Player
Copyright (C) 2009-2013 Alexey Yakovenko <[email protected]>
Copyright (C) 2016 Alexey Makhno <[email protected]>
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <soxr.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <deadbeef/deadbeef.h>
//#define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
static DB_functions_t *deadbeef;
enum {
PARAM_SAMPLERATE = 0,
PARAM_QUALITY = 1,
PARAM_STEEPFILTER = 2,
PARAM_PHASE = 3,
PARAM_ALLOW_ALIASING = 4,
PARAM_SAMPLERATE2 = 5,
PARAM_AUTOSAMPLERATE = 6,
PARAM_COUNT
};
#define MIN_RATE 8000
#define MAX_RATE 192000
static DB_dsp_t plugin;
static soxr_t soxr;
static soxr_error_t error;
typedef struct {
ddb_dsp_context_t ctx;
int channels;
int quality;
int phase;
int current_rate;
int samplerate;
int samplerate2;
int steepfilter;
int allow_aliasing;
int autosamplerate;
unsigned need_reset : 1;
} ddb_soxr_opt_t;
int
soxr_get_q (int val){
switch (val) {
case 0:
return SOXR_QQ;
break;
case 1:
return SOXR_LQ;
break;
case 2:
return SOXR_MQ;
break;
case 3:
return SOXR_HQ;
break;
case 4:
return SOXR_VHQ;
break;
case 5:
return SOXR_32_BITQ;
break;
default:
return SOXR_VHQ;
}
}
int
soxr_get_p (int val){
switch (val) {
case 0:
return SOXR_LINEAR_PHASE;
break;
case 1:
return SOXR_INTERMEDIATE_PHASE;
break;
case 2:
return SOXR_MINIMUM_PHASE;
break;
default:
return SOXR_LINEAR_PHASE;
}
}
int
soxr_get_f (int val){
if(val){
return SOXR_FLOAT32_I;
} else {
return SOXR_INT16_I;
}
}
ddb_dsp_context_t*
ddb_soxr_open (void) {
ddb_soxr_opt_t *opt = malloc (sizeof (ddb_soxr_opt_t));
DDB_INIT_DSP_CONTEXT (opt,ddb_soxr_opt_t,&plugin);
opt->samplerate = 48000;
opt->samplerate2 = 44100;
opt->quality = 4;
opt->phase = 0;
opt->steepfilter = 0;
opt->allow_aliasing = 0;
opt->autosamplerate = 0;
opt->channels = -1;
return (ddb_dsp_context_t *)opt;
}
void
ddb_soxr_close (ddb_dsp_context_t *_opt) {
ddb_soxr_opt_t *opt = (ddb_soxr_opt_t*)_opt;
soxr_delete (soxr);
soxr = 0;
soxr_clear(soxr);
free (soxr);
free (opt);
}
void
ddb_soxr_reset (ddb_dsp_context_t *_opt) {
ddb_soxr_opt_t *opt = (ddb_soxr_opt_t*)_opt;
opt->need_reset = 1;
}
int
ddb_soxr_can_bypass (ddb_dsp_context_t *_opt, ddb_waveformat_t *fmt) {
ddb_soxr_opt_t *opt = (ddb_soxr_opt_t*)_opt;
float samplerate = opt->current_rate;
if (opt->autosamplerate) {
DB_output_t *output = deadbeef->get_output ();
samplerate = output->fmt.samplerate;
}
if (fmt->samplerate == samplerate) {
return 1;
}
return 0;
}
int
ddb_soxr_process (ddb_dsp_context_t *_opt, float *samples, int nframes, int maxframes, ddb_waveformat_t *fmt, float *r) {
ddb_soxr_opt_t *opt = (ddb_soxr_opt_t*)_opt;
if (opt->autosamplerate) {
DB_output_t *output = deadbeef->get_output ();
if (output->fmt.samplerate <= 0) {
return -1;
}
// trace ("soxr: autosamplerate=%d\n", output->fmt.samplerate);
opt->current_rate = output->fmt.samplerate;
} else {
if(fmt->samplerate == 11025 || fmt->samplerate == 22050 ||
fmt->samplerate == 44100 || fmt->samplerate == 88200 ||
fmt->samplerate == 176400 || fmt->samplerate == 352800
){
opt->current_rate = opt->samplerate2;
} else {
opt->current_rate = opt->samplerate;
}
}
int new_rate = opt->current_rate;
if (fmt->samplerate == new_rate || opt->quality==6) {
return nframes;
}
double ratio = (double) new_rate / (double) fmt->samplerate;
if ( opt->channels != fmt->channels || opt->need_reset || !soxr ) {
soxr_delete (soxr);
soxr = 0;
unsigned long quality_recipe;
soxr_io_spec_t io_spec;
int io_format = soxr_get_f(fmt->is_float);
io_spec = soxr_io_spec(io_format, io_format);
/* Resample in one thread. Multithreading makes
* performance worse with small chunks of audio. */
soxr_runtime_spec_t runtime_spec;
runtime_spec = soxr_runtime_spec(1);
int qa = soxr_get_q(opt->quality);
int pa = soxr_get_p(opt->phase);
if(opt->steepfilter && opt->allow_aliasing){
quality_recipe = qa | pa | SOXR_STEEP_FILTER | SOXR_ALLOW_ALIASING ;
} else
if(opt->steepfilter && !opt->allow_aliasing){
quality_recipe = qa | pa | SOXR_STEEP_FILTER ;
} else
if(!opt->steepfilter && opt->allow_aliasing){
quality_recipe = qa | pa | SOXR_ALLOW_ALIASING ;
} else {
quality_recipe = qa | pa ;
}
soxr_quality_spec_t q = soxr_quality_spec(quality_recipe, 0);
trace ("soxr: f=%d, q=%lu, ratio=%f, old_r=%d, new_r=%d\n", fmt->is_float, quality_recipe, ratio, fmt->samplerate, new_rate);
soxr = soxr_create(fmt->samplerate, new_rate, fmt->channels, &error, &io_spec, &q, &runtime_spec);
if(!soxr){
trace ("soxr create error!");
return nframes;
}
opt->channels = fmt->channels;
opt->need_reset = 0;
}
// fmt->samplerate = new_rate;
size_t numoutframes = 0;
size_t outsize = (nframes*ratio);
float outbuf[outsize*fmt->channels];
memset (outbuf, 0, sizeof (outbuf));
char *output = (char *)outbuf;
float *input = samples;
size_t inputsize = nframes;
// size_t samplesize = fmt->channels * sizeof (float);
error = soxr_process (soxr, input , inputsize , NULL, output, outsize, &numoutframes);
memcpy (input, outbuf, numoutframes * fmt->channels * sizeof (float));
fmt->samplerate = new_rate;
// trace ("soxr: ratio=%f, in=%d, out=%d\n", ratio, nframes, numoutframes);
return numoutframes;
}
int
ddb_soxr_num_params (void) {
return PARAM_COUNT;
}
const char *
ddb_soxr_get_param_name (int p) {
switch (p) {
case PARAM_QUALITY:
return "Quality";
case PARAM_SAMPLERATE:
return "Samplerate for 48000, 96000, 192000";
case PARAM_SAMPLERATE2:
return "Samplerate for 44100, 88200, 176400";
case PARAM_STEEPFILTER:
return "Steep Filter";
case PARAM_PHASE:
return "Phase";
case PARAM_ALLOW_ALIASING:
return "Allow Aliasing";
case PARAM_AUTOSAMPLERATE:
return "Auto samplerate";
default:
fprintf (stderr, "ddb_soxr_get_param_name: invalid param index (%d)\n", p);
}
return NULL;
}
void
ddb_soxr_set_param (ddb_dsp_context_t *ctx, int p, const char *val) {
switch (p) {
case PARAM_SAMPLERATE:
((ddb_soxr_opt_t*)ctx)->samplerate = atof (val);
if (((ddb_soxr_opt_t*)ctx)->samplerate < MIN_RATE) {
((ddb_soxr_opt_t*)ctx)->samplerate = MIN_RATE;
}
if (((ddb_soxr_opt_t*)ctx)->samplerate > MAX_RATE) {
((ddb_soxr_opt_t*)ctx)->samplerate = MAX_RATE;
}
break;
case PARAM_SAMPLERATE2:
((ddb_soxr_opt_t*)ctx)->samplerate2 = atof (val);
if (((ddb_soxr_opt_t*)ctx)->samplerate2 < MIN_RATE) {
((ddb_soxr_opt_t*)ctx)->samplerate2 = MIN_RATE;
}
if (((ddb_soxr_opt_t*)ctx)->samplerate2 > MAX_RATE) {
((ddb_soxr_opt_t*)ctx)->samplerate2 = MAX_RATE;
}
break;
case PARAM_QUALITY:
((ddb_soxr_opt_t*)ctx)->quality = atoi (val);
((ddb_soxr_opt_t*)ctx)->need_reset = 1;
break;
case PARAM_PHASE:
((ddb_soxr_opt_t*)ctx)->phase = atoi (val);
((ddb_soxr_opt_t*)ctx)->need_reset = 1;
break;
case PARAM_STEEPFILTER:
((ddb_soxr_opt_t*)ctx)->steepfilter = atoi (val);
((ddb_soxr_opt_t*)ctx)->need_reset = 1;
break;
case PARAM_ALLOW_ALIASING:
((ddb_soxr_opt_t*)ctx)->allow_aliasing = atoi (val);
((ddb_soxr_opt_t*)ctx)->need_reset = 1;
break;
case PARAM_AUTOSAMPLERATE:
((ddb_soxr_opt_t*)ctx)->autosamplerate = atoi (val);
break;
default:
fprintf (stderr, "ddb_soxr_set_param: invalid param index (%d)\n", p);
}
}
void
ddb_soxr_get_param (ddb_dsp_context_t *ctx, int p, char *val, int sz) {
switch (p) {
case PARAM_SAMPLERATE:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->samplerate);
break;
case PARAM_SAMPLERATE2:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->samplerate2);
break;
case PARAM_QUALITY:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->quality);
break;
case PARAM_PHASE:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->phase);
break;
case PARAM_STEEPFILTER:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->steepfilter);
break;
case PARAM_ALLOW_ALIASING:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->allow_aliasing);
break;
case PARAM_AUTOSAMPLERATE:
snprintf (val, sz, "%d", ((ddb_soxr_opt_t*)ctx)->autosamplerate);
break;
default:
fprintf (stderr, "ddb_soxr_get_param: invalid param index (%d)\n", p);
}
}
static const char settings_dlg[] =
"property \"Automatic Samplerate (overrides Target Samplerate)\" checkbox 6 0;\n"
"property \"Target Samplerate for 48000, 96000, 192000\" spinbtn[8000,192000,1] 0 48000;\n"
"property \"Target Samplerate for 44100, 88200, 176400\" spinbtn[8000,192000,1] 5 44100;\n"
"property \"Quality / Algorithm\" select[7] 1 4 QQ LQ MQ HQ VHQ 32BIT DISABLE;\n"
"property \"Phase\" select[3] 3 0 LINEAR INTERMEDIATE MINIMUM;\n"
"property \"Steep Filter\" checkbox 2 0;\n"
"property \"Allow Aliasing (Reserved for future use)\" checkbox 4 0;\n"
;
static DB_dsp_t plugin = {
// need 1.1 api for pass_through
.plugin.api_vmajor = 1,
.plugin.api_vminor = 1,
.open = ddb_soxr_open,
.close = ddb_soxr_close,
.process = ddb_soxr_process,
.plugin.version_major = 1,
.plugin.version_minor = 0,
.plugin.type = DB_PLUGIN_DSP,
.plugin.id = "SOXR",
.plugin.name = "SoX Resampler",
.plugin.descr = "High quality samplerate converter using SoX Resampler, http://sourceforge.net/projects/soxr/",
.plugin.copyright =
"Copyright (C) 2016 Alexey Makhno <[email protected]>\n"
"\n"
"This program is free software; you can redistribute it and/or\n"
"modify it under the terms of the GNU General Public License\n"
"as published by the Free Software Foundation; either version 2\n"
"of the License, or (at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program; if not, write to the Free Software\n"
"Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\n"
,
.plugin.website = "http://deadbeef.sf.net",
.num_params = ddb_soxr_num_params,
.get_param_name = ddb_soxr_get_param_name,
.set_param = ddb_soxr_set_param,
.get_param = ddb_soxr_get_param,
.reset = ddb_soxr_reset,
.configdialog = settings_dlg,
.can_bypass = ddb_soxr_can_bypass,
};
DB_plugin_t *
ddb_soxr_dsp_load (DB_functions_t *f) {
deadbeef = f;
return &plugin.plugin;
}