-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
285 lines (202 loc) · 7.24 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
/*
* Copyright (C) 2010 Mark Hills <mark@pogo.org.uk>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2, as published by the Free Software Foundation.
*
* 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 version 2 for more details.
*
* You should have received a copy of the GNU General Public License
* version 2 along with this program; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
#include <math.h>
#include <stdio.h>
#include <stdlib.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))
static inline float cubic_interpolate(float y[4], float mu)
{
float a0, a1, a2, a3, 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 (a0 * mu * mu2) + (a1 * mu2) + (a2 * mu) + a3;
}
/* Build a block of PCM audio, resampled from the track. Always builds
* 'frame' samples, and returns the number of seconds to advance the
* track position by. This is just a basic resampler which has
* particular problems where pitch > 1.0. */
static double build_pcm(signed short *pcm, int samples, int rate,
struct track_t *tr, double position, float pitch,
float start_vol, float end_vol)
{
int s;
double sample, step;
float vol, gradient;
sample = position * tr->rate;
step = (double)pitch * tr->rate / rate;
vol = start_vol;
gradient = (end_vol - start_vol) / samples;
for (s = 0; s < samples; s++) {
int c, sa, q;
float f, 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.0;
} else {
signed short *ts;
int c;
ts = track_get_sample(tr, sa);
for (c = 0; c < PLAYER_CHANNELS; c++)
i[c][q] = (float)ts[c];
}
}
for (c = 0; c < PLAYER_CHANNELS; c++) {
*pcm++ = vol * cubic_interpolate(i[c], f)
+ (float)(rand() % 32768) / 32768 - 0.5; /* dither */
}
sample += step;
vol += gradient;
}
return (double)pitch * samples / rate;
}
void player_init(struct player_t *pl)
{
pl->reconnect = 0;
pl->position = 0.0;
pl->offset = 0.0;
pl->target_valid = 0;
pl->last_difference = 0.0;
pl->pitch = 0.0;
pl->sync_pitch = 1.0;
pl->volume = 0.0;
pl->track = NULL;
pl->timecoder = NULL;
}
void player_clear(struct player_t *pl)
{
}
void player_connect_timecoder(struct player_t *pl, struct timecoder_t *tc)
{
pl->timecoder = tc;
pl->reconnect = 1;
}
void player_disconnect_timecoder(struct player_t *pl)
{
pl->timecoder = NULL;
}
static int sync_to_timecode(struct player_t *pl)
{
float when;
double 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_valid = 0;
else {
tcpos = (double)timecode / timecoder_get_resolution(pl->timecoder);
pl->target_position = tcpos + pl->pitch * when;
pl->target_valid = 1;
}
return 0;
}
/* Return to the zero of the track */
int player_recue(struct player_t *pl)
{
pl->offset = pl->position;
return 0;
}
/* Get a block of PCM audio data to send to the soundcard. */
void player_collect(struct player_t *pl, signed short *pcm,
int samples, int rate)
{
double diff;
float dt, target_volume;
dt = (float)samples / rate;
if (pl->timecoder) {
if (sync_to_timecode(pl) == -1)
player_disconnect_timecoder(pl);
}
if (!pl->target_valid) {
/* Without timecode sync, 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);
} else {
/* If reconnection has been requested, move the logical record
* on the vinyl so that the current position is right under
* the needle, and continue */
if (pl->reconnect) {
pl->offset += pl->target_position - pl->position;
pl->position = pl->target_position;
pl->reconnect = 0;
}
/* 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);
}
/* Acknowledge that we've accounted for the target position */
pl->target_valid = 0;
}
target_volume = fabs(pl->pitch) * VOLUME;
if (target_volume > 1.0)
target_volume = 1.0;
/* Sync pitch is applied post-filtering */
pl->position += build_pcm(pcm, samples, rate,
pl->track,
pl->position - pl->offset,
pl->pitch * pl->sync_pitch,
pl->volume, target_volume);
pl->volume = target_volume;
}
void player_connect_track(struct player_t *pl, struct track_t *tr)
{
pl->track = tr;
}