-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
486 lines (377 loc) · 11.6 KB
/
player.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
/*
* Copyright (C) 2021 Mark Hills <mark@xwax.org>
*
* This file is part of "xwax".
*
* "xwax" is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License, version 3 as
* published by the Free Software Foundation.
*
* "xwax" 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, see <https://www.gnu.org/licenses/>.
*
*/
#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "device.h"
#include "player.h"
#include "track.h"
#include "timecoder.h"
/* Bend playback speed to compensate for the difference between our
* current position and that given by the timecode */
#define SYNC_TIME (1.0 / 2) /* time taken to reach sync */
#define SYNC_PITCH 0.05 /* don't sync at low pitches */
#define SYNC_RC 0.05 /* filter to 1.0 when no timecodes available */
/* If the difference between our current position and that given by
* the timecode is greater than this value, recover by jumping
* straight to the position given by the timecode. */
#define SKIP_THRESHOLD (1.0 / 8) /* before dropping audio */
/* The base volume level. A value of 1.0 leaves no headroom to play
* louder when the record is going faster than 1.0. */
#define VOLUME (7.0/8)
#define SQ(x) ((x)*(x))
#define TARGET_UNKNOWN INFINITY
/*
* Return: the cubic interpolation of the sample at position 2 + mu
*/
static inline double cubic_interpolate(signed short y[4], double mu)
{
signed long a0, a1, a2, a3;
double mu2;
mu2 = SQ(mu);
a0 = y[3] - y[2] - y[0] + y[1];
a1 = y[0] - y[1] - a0;
a2 = y[2] - y[0];
a3 = y[1];
return (mu * mu2 * a0) + (mu2 * a1) + (mu * a2) + a3;
}
/*
* Return: Random dither, between -0.5 and 0.5
*/
static double dither(void)
{
unsigned int bit, v;
static unsigned int x = 0xbeefface;
/* Maximum length LFSR sequence with 32-bit state */
bit = (x ^ (x >> 1) ^ (x >> 21) ^ (x >> 31)) & 1;
x = x << 1 | bit;
/* We can adjust the balance between randomness and performance
* by our chosen bit permutation; here we use a 12 bit subset
* of the state */
v = (x & 0x0000000f)
| ((x & 0x000f0000) >> 12)
| ((x & 0x0f000000) >> 16);
return (double)v / 4096 - 0.5; /* not quite whole range */
}
/*
* Build a block of PCM audio, resampled from the track
*
* This is just a basic resampler which has a small amount of aliasing
* where pitch > 1.0.
*
* Return: number of seconds advanced in the source audio track
* Post: buffer at pcm is filled with the given number of samples
*/
static double build_pcm(signed short *pcm, unsigned samples, double sample_dt,
struct track *tr, double position, double pitch,
double start_vol, double end_vol)
{
int s;
double sample, step, vol, gradient;
sample = position * tr->rate;
step = sample_dt * pitch * tr->rate;
vol = start_vol;
gradient = (end_vol - start_vol) / samples;
for (s = 0; s < samples; s++) {
int c, sa, q;
double f;
signed short i[PLAYER_CHANNELS][4];
/* 4-sample window for interpolation */
sa = (int)sample;
if (sample < 0.0)
sa--;
f = sample - sa;
sa--;
for (q = 0; q < 4; q++, sa++) {
if (sa < 0 || sa >= tr->length) {
for (c = 0; c < PLAYER_CHANNELS; c++)
i[c][q] = 0;
} else {
signed short *ts;
int c;
ts = track_get_sample(tr, sa);
for (c = 0; c < PLAYER_CHANNELS; c++)
i[c][q] = ts[c];
}
}
for (c = 0; c < PLAYER_CHANNELS; c++) {
double v;
v = vol * cubic_interpolate(i[c], f) + dither();
if (v > SHRT_MAX) {
*pcm++ = SHRT_MAX;
} else if (v < SHRT_MIN) {
*pcm++ = SHRT_MIN;
} else {
*pcm++ = (signed short)v;
}
}
sample += step;
vol += gradient;
}
return sample_dt * pitch * samples;
}
/*
* Equivalent to build_pcm, but for use when the track is
* not available
*
* Return: number of seconds advanced in the audio track
* Post: buffer at pcm is filled with silence
*/
static double build_silence(signed short *pcm, unsigned samples,
double sample_dt, double pitch)
{
memset(pcm, '\0', sizeof(*pcm) * PLAYER_CHANNELS * samples);
return sample_dt * pitch * samples;
}
/*
* Change the timecoder used by this playback
*/
void player_set_timecoder(struct player *pl, struct timecoder *tc)
{
assert(tc != NULL);
pl->timecoder = tc;
pl->recalibrate = true;
pl->timecode_control = true;
}
/*
* Post: player is initialised
*/
void player_init(struct player *pl, unsigned int sample_rate,
struct track *track, struct timecoder *tc)
{
assert(track != NULL);
assert(sample_rate != 0);
spin_init(&pl->lock);
pl->sample_dt = 1.0 / sample_rate;
pl->track = track;
player_set_timecoder(pl, tc);
pl->position = 0.0;
pl->offset = 0.0;
pl->target_position = TARGET_UNKNOWN;
pl->last_difference = 0.0;
pl->pitch = 0.0;
pl->sync_pitch = 1.0;
pl->volume = 0.0;
}
/*
* Pre: player is initialised
* Post: no resources are allocated by the player
*/
void player_clear(struct player *pl)
{
spin_clear(&pl->lock);
track_release(pl->track);
}
/*
* Enable or disable timecode control
*/
void player_set_timecode_control(struct player *pl, bool on)
{
if (on && !pl->timecode_control)
pl->recalibrate = true;
pl->timecode_control = on;
}
/*
* Toggle timecode control
*
* Return: the new state of timecode control
*/
bool player_toggle_timecode_control(struct player *pl)
{
pl->timecode_control = !pl->timecode_control;
if (pl->timecode_control)
pl->recalibrate = true;
return pl->timecode_control;
}
void player_set_internal_playback(struct player *pl)
{
pl->timecode_control = false;
pl->pitch = 1.0;
}
double player_get_position(struct player *pl)
{
return pl->position;
}
double player_get_elapsed(struct player *pl)
{
return pl->position - pl->offset;
}
double player_get_remain(struct player *pl)
{
return (double)pl->track->length / pl->track->rate
+ pl->offset - pl->position;
}
bool player_is_active(const struct player *pl)
{
return (fabs(pl->pitch) > 0.01);
}
/*
* Cue to the zero position of the track
*/
void player_recue(struct player *pl)
{
pl->offset = pl->position;
}
/*
* Set the track used for the playback
*
* Pre: caller holds reference on track
* Post: caller does not hold reference on track
*/
void player_set_track(struct player *pl, struct track *track)
{
struct track *x;
assert(track != NULL);
assert(track->refcount > 0);
spin_lock(&pl->lock); /* Synchronise with the playback thread */
x = pl->track;
pl->track = track;
spin_unlock(&pl->lock);
track_release(x); /* discard the old track */
}
/*
* Set the playback of one player to match another, used
* for "instant doubles" and beat juggling
*/
void player_clone(struct player *pl, const struct player *from)
{
double elapsed;
struct track *x, *t;
elapsed = from->position - from->offset;
pl->offset = pl->position - elapsed;
t = from->track;
track_acquire(t);
spin_lock(&pl->lock);
x = pl->track;
pl->track = t;
spin_unlock(&pl->lock);
track_release(x);
}
/*
* Synchronise to the position and speed given by the timecoder
*
* Return: 0 on success or -1 if the timecoder is not currently valid
*/
static int sync_to_timecode(struct player *pl)
{
double when, tcpos;
signed int timecode;
timecode = timecoder_get_position(pl->timecoder, &when);
/* Instruct the caller to disconnect the timecoder if the needle
* is outside the 'safe' zone of the record */
if (timecode != -1 && timecode > timecoder_get_safe(pl->timecoder))
return -1;
/* If the timecoder is alive, use the pitch from the sine wave */
pl->pitch = timecoder_get_pitch(pl->timecoder);
/* If we can read an absolute time from the timecode, then use it */
if (timecode == -1) {
pl->target_position = TARGET_UNKNOWN;
} else {
tcpos = (double)timecode / timecoder_get_resolution(pl->timecoder);
pl->target_position = tcpos + pl->pitch * when;
}
return 0;
}
/*
* Synchronise to the position given by the timecoder without
* affecting the audio playback position
*/
static void calibrate_to_timecode_position(struct player *pl)
{
assert(pl->target_position != TARGET_UNKNOWN);
pl->offset += pl->target_position - pl->position;
pl->position = pl->target_position;
}
void retarget(struct player *pl)
{
double diff;
if (pl->recalibrate) {
calibrate_to_timecode_position(pl);
pl->recalibrate = false;
}
/* Calculate the pitch compensation required to get us back on
* track with the absolute timecode position */
diff = pl->position - pl->target_position;
pl->last_difference = diff; /* to print in user interface */
if (fabs(diff) > SKIP_THRESHOLD) {
/* Jump the track to the time */
pl->position = pl->target_position;
fprintf(stderr, "Seek to new position %.2lfs.\n", pl->position);
} else if (fabs(pl->pitch) > SYNC_PITCH) {
/* Re-calculate the drift between the timecoder pitch from
* the sine wave and the timecode values */
pl->sync_pitch = pl->pitch / (diff / SYNC_TIME + pl->pitch);
}
}
/*
* Seek to the given position
*/
void player_seek_to(struct player *pl, double seconds)
{
pl->offset = pl->position - seconds;
}
/*
* Get a block of PCM audio data to send to the soundcard
*
* This is the main function which retrieves audio for playback. The
* clock of playback is decoupled from the clock of the timecode
* signal.
*
* Post: buffer at pcm is filled with the given number of samples
*/
void player_collect(struct player *pl, signed short *pcm, unsigned samples)
{
double r, pitch, dt, target_volume;
dt = pl->sample_dt * samples;
if (pl->timecode_control) {
if (sync_to_timecode(pl) == -1)
pl->timecode_control = false;
}
if (pl->target_position != TARGET_UNKNOWN) {
/* Bias the pitch towards a known target, and acknowledge that
* we did so */
retarget(pl);
pl->target_position = TARGET_UNKNOWN;
} else {
/* Without a known target, tend sync_pitch towards 1.0, to
* avoid using outlier values from scratching for too long */
pl->sync_pitch += dt / (SYNC_RC + dt) * (1.0 - pl->sync_pitch);
}
target_volume = fabs(pl->pitch) * VOLUME;
if (target_volume > 1.0)
target_volume = 1.0;
/* Sync pitch is applied post-filtering */
pitch = pl->pitch * pl->sync_pitch;
/* We must return audio immediately to stay realtime. A spin
* lock protects us from changes to the audio source */
if (!spin_try_lock(&pl->lock)) {
r = build_silence(pcm, samples, pl->sample_dt, pitch);
} else {
r = build_pcm(pcm, samples, pl->sample_dt, pl->track,
pl->position - pl->offset, pitch,
pl->volume, target_volume);
spin_unlock(&pl->lock);
}
pl->position += r;
pl->volume = target_volume;
}