-
Notifications
You must be signed in to change notification settings - Fork 0
/
track.c
354 lines (260 loc) · 7.88 KB
/
track.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
/*
* 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.
*
*/
#define _BSD_SOURCE /* vfork() */
#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/poll.h>
#include <sys/types.h>
#include <sys/wait.h>
#include "track.h"
#define SAMPLE 4 /* bytes per sample (all channels) */
#define LOCK(tr) pthread_mutex_lock(&(tr)->mx)
#define UNLOCK(tr) pthread_mutex_unlock(&(tr)->mx)
/* Start the importer process. On completion, pid and fd are set */
static int start_import(struct track_t *tr, const char *path)
{
int pstdout[2];
if (pipe(pstdout) == -1) {
perror("pipe");
return -1;
}
if (fcntl(pstdout[0], F_SETFL, O_NONBLOCK) == -1) {
perror("fcntl");
return -1;
}
tr->pid = vfork();
if (tr->pid == -1) {
perror("vfork");
return -1;
} else if (tr->pid == 0) { /* child */
/* Reconnect stdout to this process, leave stderr to terminal */
if (close(pstdout[0]) != 0) {
perror("close");
abort();
}
if (dup2(pstdout[1], STDOUT_FILENO) == -1) {
perror("dup2");
_exit(EXIT_FAILURE); /* vfork() was used */
}
if (close(pstdout[1]) != 0) {
perror("close");
abort();
}
if (execl(tr->importer, "import", path, NULL) == -1) {
perror("execl");
fprintf(stderr, "Failed to launch importer %s\n", tr->importer);
_exit(EXIT_FAILURE); /* vfork() was used */
}
abort(); /* execl() never returns */
}
if (close(pstdout[1]) != 0) {
perror("close");
abort();
}
tr->fd = pstdout[0];
tr->bytes = 0;
tr->length = 0;
tr->ppm = 0;
tr->overview = 0;
tr->rate = TRACK_RATE;
return 0;
}
/* Conclude the importer process. To be called whether the importer
* was aborted or completed successfully */
static void stop_import(struct track_t *tr)
{
int status;
assert(tr->pid != 0);
if (close(tr->fd) != 0) {
perror("close");
abort();
}
if (waitpid(tr->pid, &status, 0) == -1) {
perror("waitpid");
abort();
}
tr->pid = 0;
if (WIFEXITED(status) && WEXITSTATUS(status) == EXIT_SUCCESS) {
fputs("Track import completed.\n", stderr);
} else {
fputs("Track import did not complete successfully.\n", stderr);
}
}
/* Prematurely abort the import process */
static void abort_import(struct track_t *tr)
{
if (kill(tr->pid, SIGTERM) == -1) {
perror("kill");
abort();
}
stop_import(tr);
}
/* Read the next block of data from the file. Return -1 when an error
* occurs and requires our attention, 1 if there is no more data to be
* read, otherwise zero. */
static int read_from_pipe(struct track_t *tr)
{
int r, ls, used;
size_t m;
unsigned short v;
unsigned int s, w;
struct track_block_t *block;
/* Check whether we need to allocate a new block */
if (tr->bytes >= (size_t)tr->blocks * TRACK_BLOCK_SAMPLES * SAMPLE) {
if (tr->blocks >= TRACK_MAX_BLOCKS) {
fprintf(stderr, "Maximum track length reached; aborting...\n");
return -1;
}
block = malloc(sizeof(struct track_block_t));
if (!block) {
perror("malloc");
return -1;
}
tr->block[tr->blocks++] = block;
fprintf(stderr, "Allocated new track block (%d at %zu bytes).\n",
tr->blocks, tr->bytes);
}
/* Load in audio to the end of the current block. We've just
* allocated a new one if needed, so no possibility of read()
* returning zero, except for EOF */
block = tr->block[tr->bytes / (TRACK_BLOCK_SAMPLES * SAMPLE)];
used = tr->bytes % (TRACK_BLOCK_SAMPLES * SAMPLE);
r = read(tr->fd, (char*)block->pcm + used,
TRACK_BLOCK_SAMPLES * SAMPLE - used);
if (r == -1) {
if (errno == EAGAIN)
return 0;
perror("read");
return -1;
} else if (r == 0) {
m = TRACK_BLOCK_SAMPLES * SAMPLE * tr->blocks / 1024;
fprintf(stderr, "Track memory %zuKb PCM, %zuKb PPM, %zuKb overview.\n",
m, m / TRACK_PPM_RES, m / TRACK_OVERVIEW_RES);
return 1;
}
tr->bytes += r;
/* Meter the audio which has just been read */
for (s = tr->length; s < tr->bytes / SAMPLE; s++) {
ls = s % TRACK_BLOCK_SAMPLES;
v = (abs(block->pcm[ls * TRACK_CHANNELS])
+ abs(block->pcm[ls * TRACK_CHANNELS + 1]));
/* PPM-style fast meter approximation */
if (v > tr->ppm)
tr->ppm += (v - tr->ppm) >> 3;
else
tr->ppm -= (tr->ppm - v) >> 9;
block->ppm[ls / TRACK_PPM_RES] = tr->ppm >> 8;
/* Update the slow-metering overview. Fixed point arithmetic
* going on here */
w = v << 16;
if (w > tr->overview)
tr->overview += (w - tr->overview) >> 8;
else
tr->overview -= (tr->overview - w) >> 17;
block->overview[ls / TRACK_OVERVIEW_RES] = tr->overview >> 24;
}
tr->length = s;
return 0;
}
void track_init(struct track_t *tr, const char *importer)
{
tr->importer = importer;
tr->pid = 0;
tr->artist = NULL;
tr->title = NULL;
tr->blocks = 0;
tr->bytes = 0;
tr->length = 0;
tr->rate = TRACK_RATE;
if (pthread_mutex_init(&tr->mx, 0) != 0)
abort();
}
/* Destroy this track from memory, and any child process */
void track_clear(struct track_t *tr)
{
int n;
/* Force a cleanup of whichever state we are in */
if (tr->pid != 0)
abort_import(tr);
for (n = 0; n < tr->blocks; n++)
free(tr->block[n]);
if (pthread_mutex_destroy(&tr->mx) != 0)
abort();
}
/* Return the number of file descriptors which should be watched for
* this track, and fill pe */
int track_pollfd(struct track_t *tr, struct pollfd *pe)
{
int r;
LOCK(tr);
if (tr->pid != 0) {
pe->fd = tr->fd;
pe->revents = 0;
pe->events = POLLIN | POLLHUP | POLLERR;
tr->pe = pe;
r = 1;
} else {
tr->pe = NULL;
r = 0;
}
UNLOCK(tr);
return r;
}
/* Handle any activity on this track, whatever the current state */
int track_handle(struct track_t *tr)
{
int r;
/* Only one thread is allowed to call this function, and it owns
* the poll entry */
if (!tr->pe || !tr->pe->revents)
return 0;
LOCK(tr);
if (tr->pid != 0) {
r = read_from_pipe(tr);
if (r != 0)
stop_import(tr);
}
UNLOCK(tr);
return 0;
}
/* A request to begin importing a new track. Can be called when the
* track is in any state */
int track_import(struct track_t *tr, const char *path)
{
int r;
LOCK(tr);
/* Abort any running import process */
if (tr->pid != 0)
abort_import(tr);
/* Start the new import process */
r = start_import(tr, path);
if (r < 0) {
UNLOCK(tr);
return -1;
}
UNLOCK(tr);
rig_awaken(tr->rig);
return 0;
}