-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlibretro.cpp
338 lines (294 loc) · 12 KB
/
libretro.cpp
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
/*
DeaDBeeF - The Ultimate Music Player
Copyright (C) 2009-2013 Alexey Yakovenko <[email protected]>
Copyright (C) 2021 Michael Lelli <[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 "sinc_resampler.h"
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <deadbeef/deadbeef.h>
#include "libretro.h"
// #define trace(...) { fprintf(stderr, __VA_ARGS__); }
#define trace(fmt,...)
static DB_functions_t *deadbeef;
#define LIBRETRO_BUFFER 16000
#define LIBRETRO_MAX_CHANNELS 8
typedef struct {
ddb_dsp_context_t ctx;
int channels;
enum resampler_quality quality;
float samplerate;
int autosamplerate;
rarch_sinc_resampler_t *resampler;
int in_samplerate;
int out_samplerate;
int remaining; // number of input samples in the buffer
float *outbuf;
int outsize;
int buffersize;
__attribute__((__aligned__(16))) char in_fbuffer[sizeof(float)*LIBRETRO_BUFFER*LIBRETRO_MAX_CHANNELS];
unsigned quality_changed : 1;
unsigned need_reset : 1;
} ddb_libretro_t;
static void
ddb_libretro_close (ddb_dsp_context_t *_libretro) {
ddb_libretro_t *libretro = (ddb_libretro_t*)_libretro;
if (libretro->resampler) {
resampler_sinc_free (libretro->resampler);
libretro->resampler = NULL;
}
if (libretro->outbuf) {
free (libretro->outbuf);
libretro->outbuf = NULL;
}
free (libretro);
}
static void
ddb_libretro_reset (ddb_dsp_context_t *_libretro) {
ddb_libretro_t *libretro = (ddb_libretro_t*)_libretro;
libretro->need_reset = 1;
}
static int
_get_target_samplerate (ddb_libretro_t *libretro, ddb_waveformat_t *fmt) {
if (libretro->autosamplerate) {
DB_output_t *output = deadbeef->get_output ();
return output->fmt.samplerate;
}
else {
return libretro->samplerate;
}
}
static int
ddb_libretro_can_bypass (ddb_dsp_context_t *_libretro, ddb_waveformat_t *fmt) {
ddb_libretro_t *libretro = (ddb_libretro_t*)_libretro;
int samplerate = _get_target_samplerate(libretro, fmt);
return fmt->samplerate == samplerate;
}
static int
ddb_libretro_process (ddb_dsp_context_t *_libretro, float *samples, int nframes, int maxframes, ddb_waveformat_t *fmt, float *r) {
ddb_libretro_t *libretro = (ddb_libretro_t*)_libretro;
int samplerate = _get_target_samplerate(libretro, fmt);
if (fmt->samplerate == samplerate) {
return nframes;
}
int new_samplerates = libretro->in_samplerate != fmt->samplerate || libretro->out_samplerate != samplerate;
libretro->in_samplerate = fmt->samplerate;
libretro->out_samplerate = samplerate;
fmt->samplerate = samplerate;
if (libretro->need_reset || new_samplerates || libretro->channels != fmt->channels || libretro->quality_changed || !libretro->resampler) {
libretro->quality_changed = 0;
libretro->remaining = 0;
if (libretro->resampler) {
resampler_sinc_free (libretro->resampler);
libretro->resampler = NULL;
}
libretro->channels = fmt->channels;
libretro->resampler = (rarch_sinc_resampler_t *)resampler_sinc_new (libretro->in_samplerate, libretro->out_samplerate, libretro->channels, libretro->quality);
if (libretro->resampler == NULL) {
fprintf (stderr, "libretro_process failed to create resampler\n"
"in_samplerate=%d, out_samplerate=%d, channels=%d, quality=%d\n", libretro->in_samplerate, libretro->out_samplerate, libretro->channels, libretro->quality);
return nframes;
}
libretro->need_reset = 0;
}
int numoutframes = 0;
int outsize = nframes*24;
int buffersize = outsize * fmt->channels * sizeof (float);
if (!libretro->outbuf || libretro->outsize != outsize || libretro->buffersize != buffersize) {
if (libretro->outbuf) {
free (libretro->outbuf);
libretro->outbuf = NULL;
}
libretro->outsize = outsize;
libretro->buffersize = buffersize;
libretro->outbuf = (float*)malloc (buffersize);
}
char *output = (char *)libretro->outbuf;
memset (output, 0, buffersize);
float *input = samples;
int inputsize = nframes;
int samplesize = fmt->channels * sizeof (float);
do {
// add more frames to input buffer
int n = inputsize;
if (n >= LIBRETRO_BUFFER - libretro->remaining) {
n = LIBRETRO_BUFFER - libretro->remaining;
}
if (n > 0) {
memcpy (&libretro->in_fbuffer[libretro->remaining*samplesize], samples, n * samplesize);
libretro->remaining += n;
samples += n * fmt->channels;
}
if (!libretro->remaining) {
trace ("WARNING: LIBRETRO input buffer starved\n");
break;
}
// call resampler
struct resampler_data data = {0};
data.data_in = (float *)libretro->in_fbuffer;
data.data_out = (float *)output;
data.input_frames = libretro->remaining;
trace ("libretro input: %zu, buffersize: %lu\n", data.input_frames, libretro->outsize * fmt->channels * sizeof (float));
libretro->resampler->process(libretro->resampler, &data);
trace ("libretro output: %zu\n", data.output_frames);
inputsize -= n;
output += data.output_frames * samplesize;
numoutframes += data.output_frames;
outsize -= data.output_frames;
// calculate how many unused input samples left
libretro->remaining -= data.input_frames;
// copy spare samples for next update
if (libretro->remaining > 0 && data.input_frames > 0) {
memmove (libretro->in_fbuffer, &libretro->in_fbuffer[data.input_frames*samplesize], libretro->remaining * samplesize);
}
if (data.output_frames == 0) {
trace ("src: output_frames_gen=0, interrupt\n");
break;
}
} while (inputsize > 0 && outsize > 0);
memcpy (input, libretro->outbuf, numoutframes * fmt->channels * sizeof (float));
//static FILE *out = NULL;
//if (!out) {
// out = fopen ("out.raw", "w+b");
//}
//fwrite (input, 1, numoutframes*sizeof(float)*(*nchannels), out);
trace ("libretro: in=%d, out=%d\n", nframes, numoutframes);
return numoutframes;
}
static int
ddb_libretro_num_params (void) {
return LIBRETRO_PARAM_COUNT;
}
static const char *
ddb_libretro_get_param_name (int p) {
switch (p) {
case LIBRETRO_PARAM_QUALITY:
return "Quality";
case LIBRETRO_PARAM_SAMPLERATE:
return "Samplerate";
case LIBRETRO_PARAM_AUTOSAMPLERATE:
return "Auto samplerate";
default:
fprintf (stderr, "ddb_libretro_get_param_name: invalid param index (%d)\n", p);
}
return NULL;
}
static void
ddb_libretro_set_param (ddb_dsp_context_t *ctx, int p, const char *val) {
switch (p) {
case LIBRETRO_PARAM_SAMPLERATE:
((ddb_libretro_t*)ctx)->samplerate = atof (val);
if (((ddb_libretro_t*)ctx)->samplerate < 8000) {
((ddb_libretro_t*)ctx)->samplerate = 8000;
}
if (((ddb_libretro_t*)ctx)->samplerate > 192000) {
((ddb_libretro_t*)ctx)->samplerate = 192000;
}
break;
case LIBRETRO_PARAM_QUALITY:
((ddb_libretro_t*)ctx)->quality = (enum resampler_quality) atoi (val);
((ddb_libretro_t*)ctx)->quality_changed = 1;
break;
case LIBRETRO_PARAM_AUTOSAMPLERATE:
((ddb_libretro_t*)ctx)->autosamplerate = atoi (val);
break;
default:
fprintf (stderr, "ddb_libretro_set_param: invalid param index (%d)\n", p);
}
}
static void
ddb_libretro_get_param (ddb_dsp_context_t *ctx, int p, char *val, int sz) {
switch (p) {
case LIBRETRO_PARAM_SAMPLERATE:
snprintf (val, sz, "%f", ((ddb_libretro_t*)ctx)->samplerate);
break;
case LIBRETRO_PARAM_QUALITY:
snprintf (val, sz, "%d", ((ddb_libretro_t*)ctx)->quality);
break;
case LIBRETRO_PARAM_AUTOSAMPLERATE:
snprintf (val, sz, "%d", ((ddb_libretro_t*)ctx)->autosamplerate);
break;
default:
fprintf (stderr, "ddb_libretro_get_param: invalid param index (%d)\n", p);
}
}
static const char settings_dlg[] =
"property \"Autodetect samplerate from output device\" checkbox 2 0;\n"
"property \"Set samplerate directly\" spinbtn[8000,192000,1] 0 44100;\n"
"property \"Resampler quality\" select[5] 1 2 Lowest Lower Normal Higher Highest;\n"
;
static ddb_dsp_context_t *ddb_libretro_open(void);
static DB_dsp_t plugin = {
.plugin = {
.type = DB_PLUGIN_DSP,
.api_vmajor = DB_API_VERSION_MAJOR,
.api_vminor = DB_API_VERSION_MINOR,
.version_major = 1,
.version_minor = 0,
.id = "LIBRETRO",
.name = "Resampler (Libretro)",
.descr =
"Samplerate converter using the sinc resampler from Libretro/RetroArch.\n"
"\n"
"Based on foobar200 modifications:\n"
"https://www.foobar2000.org/sinc-resampler",
.copyright =
"Copyright (C) 2010-2018 The RetroArch team\n"
"Modified by Janne Hyvärinen and Peter Pawlowski\n"
"\n"
"Permission is hereby granted, free of charge,\n"
"to any person obtaining a copy of this software and associated documentation files (the \"Software\"),\n"
"to deal in the Software without restriction, including without limitation the rights to\n"
"use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,\n"
"and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n"
"\n"
"The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n"
"\n"
"THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,\n"
"INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n"
"FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.\n"
"IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,\n"
"WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n"
"OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE."
,
.website = "https://github.com/ToadKing/ddb_dsp_libretro",
},
.open = ddb_libretro_open,
.close = ddb_libretro_close,
.process = ddb_libretro_process,
.reset = ddb_libretro_reset,
.num_params = ddb_libretro_num_params,
.get_param_name = ddb_libretro_get_param_name,
.set_param = ddb_libretro_set_param,
.get_param = ddb_libretro_get_param,
.configdialog = settings_dlg,
.can_bypass = ddb_libretro_can_bypass,
};
extern "C" DB_plugin_t *ddb_dsp_libretro_load (DB_functions_t *f);
DB_plugin_t *
ddb_dsp_libretro_load (DB_functions_t *f) {
deadbeef = f;
return &plugin.plugin;
}
ddb_dsp_context_t*
ddb_libretro_open (void) {
ddb_libretro_t *libretro = (ddb_libretro_t*)malloc (sizeof (ddb_libretro_t));
DDB_INIT_DSP_CONTEXT (libretro,ddb_libretro_t, &plugin);
libretro->autosamplerate = 0;
libretro->samplerate = 44100;
libretro->quality = RESAMPLER_QUALITY_NORMAL;
libretro->channels = -1;
return (ddb_dsp_context_t *)libretro;
}