summaryrefslogtreecommitdiff
path: root/usr.bin/aucat/dev.c
blob: 91df14b062be77ebe8ab29d3fa1ca52da8bde106 (plain)
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
/*	$OpenBSD: dev.c,v 1.11 2008/11/07 21:01:15 ratchov Exp $	*/
/*
 * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "dev.h"
#include "abuf.h"
#include "aproc.h"
#include "pipe.h"
#include "conf.h"
#include "safile.h"

unsigned dev_bufsz, dev_round, dev_rate;
unsigned dev_rate_div, dev_round_div;
struct aparams dev_ipar, dev_opar;
struct aproc *dev_mix, *dev_sub, *dev_rec, *dev_play;
struct file *dev_file;

/*
 * supported rates
 */
#define NRATES (sizeof(dev_rates) / sizeof(dev_rates[0]))
unsigned dev_rates[] = {
	  6400,   7200,   8000,   9600,  11025,  12000,
	 12800,  14400,  16000,  19200,  22050,  24000,
	 25600,  28800,  32000,  38400,  44100,  48000,
	 51200,  57600,  64000,  76800,  88200,  96000,
	102400, 115200, 128000, 153600, 176400, 192000
};

/*
 * factors of supported rates
 */
#define NPRIMES (sizeof(dev_primes) / sizeof(dev_primes[0]))
unsigned dev_primes[] = {2, 3, 5, 7};

int
dev_setrate(unsigned rate)
{
	unsigned i, r, p;

	r = 1000 * rate;
	for (i = 0; i < NRATES; i++) {
		if (i == NRATES) {
			fprintf(stderr, "dev_setrate: %u, unsupported\n", rate);
			return 0;
		}
		if (r > 996 * dev_rates[i] &&
		    r < 1004 * dev_rates[i]) {
			dev_rate = dev_rates[i];
			break;
		}
	}

	dev_rate_div = dev_rate;
	dev_round_div = dev_round;
	for (i = 0; i < NPRIMES; i++) {
		p = dev_primes[i];
		while (dev_rate_div % p == 0 && dev_round_div % p == 0) {
			dev_rate_div /= p;
			dev_round_div /= p;
		}
	}
	return 1;
}

void
dev_roundrate(unsigned *newrate, unsigned *newround)
{
	*newrate += dev_rate_div - 1;
	*newrate -= *newrate % dev_rate_div;
	*newround = *newrate * dev_round_div / dev_rate_div;
}

/*
 * open the device with the given hardware parameters and create a mixer
 * and a multiplexer connected to it with all necessary conversions
 * setup
 */
void
dev_init(char *devpath,
    struct aparams *dipar, struct aparams *dopar,
    unsigned bufsz, int blkio)
{
	struct aparams ipar, opar;
	struct aproc *conv;
	struct abuf *buf;
	unsigned nfr, ibufsz, obufsz;

	/*
	 * ask for 1/4 of the buffer for the kernel ring and
	 * limit the block size to 1/4 of the requested buffer
	 */
	dev_bufsz = (bufsz + 3) / 4;
	dev_round = (bufsz + 3) / 4;
	dev_file = (struct file *)safile_new(&safile_ops, devpath,
	    dipar, dopar, &dev_bufsz, &dev_round, blkio);
	if (!dev_file)
		exit(1);
	if (!dev_setrate(dipar ? dipar->rate : dopar->rate))
		exit(1);
	if (dipar) {
		dipar->rate = dev_rate;
		if (debug_level > 0) {
			fprintf(stderr, "dev_init: hw recording ");
			aparams_print(dipar);
			fprintf(stderr, "\n");
		}
	}
	if (dopar) {
		dopar->rate = dev_rate;
		if (debug_level > 0) {
			fprintf(stderr, "dev_init: hw playing ");
			aparams_print(dopar);
			fprintf(stderr, "\n");
		}
	}
	ibufsz = obufsz = dev_bufsz;
	bufsz = (bufsz > dev_bufsz) ? bufsz - dev_bufsz : 0;

	/*
	 * use 1/8 of the buffer for the mixer/converters.  Since we
	 * already consumed 1/4 for the device, bufsz represents the
	 * remaining 3/4. So 1/8 is 1/6 of 3/4
	 */
	nfr = (bufsz + 5) / 6;
	nfr += dev_round - 1;
	nfr -= nfr % dev_round;
	if (nfr == 0)
		nfr = dev_round;

	/*
	 * create record chain
	 */
	if (dipar) {
		aparams_init(&ipar, dipar->cmin, dipar->cmax, dipar->rate);
		/*
		 * create the read end
		 */
		dev_rec = rpipe_new(dev_file);
		buf = abuf_new(nfr, dipar);
		aproc_setout(dev_rec, buf);
		ibufsz += nfr;

		/*
		 * append a converter, if needed
		 */
		if (!aparams_eqenc(dipar, &ipar)) {
			conv = dec_new("subin", dipar);
			aproc_setin(conv, buf);
			buf = abuf_new(nfr, &ipar);
			aproc_setout(conv, buf);
			ibufsz += nfr;
		}
		dev_ipar = ipar;

		/*
		 * append a "sub" to which clients will connect
		 */
		dev_sub = sub_new("sub", nfr);
		aproc_setin(dev_sub, buf);
	} else {
		dev_rec = NULL;
		dev_sub = NULL;
	}

	/*
	 * create play chain
	 */
	if (dopar) {
		aparams_init(&opar, dopar->cmin, dopar->cmax, dopar->rate);
		/*
		 * create the write end
		 */
		dev_play = wpipe_new(dev_file);
		buf = abuf_new(nfr, dopar);
		aproc_setin(dev_play, buf);
		obufsz += nfr;
		
		/*
		 * append a converter, if needed
		 */
		if (!aparams_eqenc(&opar, dopar)) {
			conv = enc_new("mixout", dopar);
			aproc_setout(conv, buf);
			buf = abuf_new(nfr, &opar);
			aproc_setin(conv, buf);
			obufsz += nfr;
		}
		dev_opar = opar;

		/*
		 * append a "mix" to which clients will connect
		 */
		dev_mix = mix_new("mix", nfr);
		aproc_setout(dev_mix, buf);
	} else {
		dev_play = NULL;
		dev_mix = NULL;
	}
	dev_bufsz = (dopar) ? obufsz : ibufsz;
	DPRINTF("dev_init: using %u fpb\n", dev_bufsz);
	dev_start();
}

/*
 * cleanly stop and drain everything and close the device
 * once both play chain and record chain are gone
 */
void
dev_done(void)
{
	struct file *f;

	if (dev_mix) {
		/*
		 * generate EOF on all inputs (but not the device), and
		 * put the mixer in ``autoquit'' state, so once buffers
		 * are drained the mixer will terminate and shutdown the
		 * write-end of the device
		 *
		 * NOTE: since file_eof() can destroy the file and
		 * reorder the file_list, we have to restart the loop
		 * after each call to file_eof()
		 */
	restart:
		LIST_FOREACH(f, &file_list, entry) {
			if (f != dev_file && f->rproc) {
				file_eof(f);
				goto restart;
			}
		}
		if (dev_mix)
			dev_mix->u.mix.flags |= MIX_AUTOQUIT;

		/*
		 * wait play chain to terminate
		 */
		while (dev_file->wproc != NULL) {
			if (!file_poll())
				break;
		}
		dev_mix = 0;
	}
	if (dev_sub) {
		/*
		 * same as above, but for the record chain: generate eof
		 * on the read-end of the device and wait record buffers
		 * to desappear.  We must stop the device first, because
		 * play-end will underrun (and xrun correction code will
		 * insert silence on the record-end of the device)
		 */
		dev_stop();
		file_eof(dev_file);
		if (dev_sub)
			dev_sub->u.sub.flags |= SUB_AUTOQUIT;
		for (;;) {
			if (!file_poll())
				break;
		}
		dev_sub = NULL;
	}
}

/*
 * start the (paused) device. By default it's paused
 */
void
dev_start(void)
{
	if (dev_mix)
		dev_mix->u.mix.flags |= MIX_DROP;
	if (dev_sub)
		dev_sub->u.sub.flags |= SUB_DROP;
	dev_file->ops->start(dev_file);
}

/*
 * pause the device
 */
void
dev_stop(void)
{
	dev_file->ops->stop(dev_file);
	if (dev_mix)
		dev_mix->u.mix.flags &= ~MIX_DROP;
	if (dev_sub)
		dev_sub->u.sub.flags &= ~SUB_DROP;
}

/*
 * sync play buffer to rec buffer (for instance when one of
 * them underruns/overruns)
 */
void
dev_sync(struct abuf *ibuf, struct abuf *obuf)
{
	struct abuf *pbuf, *rbuf;
	int delta;

	if (!dev_mix || !dev_sub)
		return;
	pbuf = LIST_FIRST(&dev_mix->obuflist);
	if (!pbuf)
		return;
	rbuf = LIST_FIRST(&dev_sub->ibuflist);
	if (!rbuf)
		return;
	for (;;) {
		if (!ibuf || !ibuf->rproc) {
			DPRINTF("dev_sync: reader desappeared\n");
			return;
		}
		if (ibuf->rproc == dev_mix)
			break;
		ibuf = LIST_FIRST(&ibuf->rproc->obuflist);
	}
	for (;;) {
		if (!obuf || !obuf->wproc) {
			DPRINTF("dev_sync: writer desappeared\n");
			return;
		}
		if (obuf->wproc == dev_sub)
			break;
		obuf = LIST_FIRST(&obuf->wproc->ibuflist);
	}

	/*
	 * calculate delta, the number of frames the play chain is ahead
	 * of the record chain. It's necessary to schedule silences (or
	 * drops) in order to start playback and record in sync.
	 */
	delta = 
	    rbuf->bpf * (pbuf->abspos + pbuf->used) - 
	    pbuf->bpf *  rbuf->abspos;
	delta /= pbuf->bpf * rbuf->bpf;
	DPRINTF("dev_sync: delta = %d, ppos = %u, pused = %u, rpos = %u\n",
	    delta, pbuf->abspos, pbuf->used, rbuf->abspos);

	if (delta > 0) {
		/*
		 * if the play chain is ahead (most cases) drop some of
		 * the recorded input, to get both in sync
		 */
		obuf->drop += delta * obuf->bpf;
		abuf_ipos(obuf, -delta);
	} else if (delta < 0) {
		/*
		 * if record chain is ahead (should never happen,
		 * right?) then insert silence to play
		 */
		ibuf->silence += -delta * ibuf->bpf;
		abuf_opos(ibuf, delta);
	} else
		DPRINTF("dev_sync: nothing to do\n");
}

/*
 * attach the given input and output buffers to the mixer and the
 * multiplexer respectively. The operation is done synchronously, so
 * both buffers enter in sync. If buffers do not match play
 * and rec
 */
void
dev_attach(char *name, 
    struct abuf *ibuf, struct aparams *sipar, unsigned underrun, 
    struct abuf *obuf, struct aparams *sopar, unsigned overrun)
{
	struct abuf *pbuf = NULL, *rbuf = NULL;
	struct aparams ipar = *sipar, opar = *sopar;
	struct aproc *conv;
	unsigned nfr;
	
	if (ibuf) {
		pbuf = LIST_FIRST(&dev_mix->obuflist);		
		if (!aparams_eqenc(&ipar, &dev_opar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = dec_new(name, &ipar);
			ipar.bps = dev_opar.bps;
			ipar.bits = dev_opar.bits;
			ipar.sig = dev_opar.sig;
			ipar.le = dev_opar.le;
			ipar.msb = dev_opar.msb;
			aproc_setin(conv, ibuf);			
			ibuf = abuf_new(nfr, &ipar);
			aproc_setout(conv, ibuf);
		}
		if (!aparams_subset(&ipar, &dev_opar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = cmap_new(name, &ipar, &dev_opar);
			ipar.cmin = dev_opar.cmin;
			ipar.cmax = dev_opar.cmax;
			aproc_setin(conv, ibuf);
			ibuf = abuf_new(nfr, &ipar);
			aproc_setout(conv, ibuf);
		}
		if (!aparams_eqrate(&ipar, &dev_opar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = resamp_new(name, &ipar, &dev_opar);
			ipar.rate = dev_opar.rate;
			aproc_setin(conv, ibuf);
			ibuf = abuf_new(nfr, &ipar);
			aproc_setout(conv, ibuf);
		}
		aproc_setin(dev_mix, ibuf);
		abuf_opos(ibuf, -dev_mix->u.mix.lat);
		ibuf->xrun = underrun;
	}
	if (obuf) {
		rbuf = LIST_FIRST(&dev_sub->ibuflist);
		if (!aparams_eqenc(&opar, &dev_ipar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = enc_new(name, &opar);
			opar.bps = dev_ipar.bps;
			opar.bits = dev_ipar.bits;
			opar.sig = dev_ipar.sig;
			opar.le = dev_ipar.le;
			opar.msb = dev_ipar.msb;
			aproc_setout(conv, obuf);
			obuf = abuf_new(nfr, &opar);
			aproc_setin(conv, obuf);
		}
		if (!aparams_subset(&opar, &dev_ipar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = cmap_new(name, &dev_ipar, &opar);
			opar.cmin = dev_ipar.cmin;
			opar.cmax = dev_ipar.cmax;
			aproc_setout(conv, obuf);
			obuf = abuf_new(nfr, &opar);
			aproc_setin(conv, obuf);
		}
		if (!aparams_eqrate(&opar, &dev_ipar)) {
			nfr = (dev_bufsz + 3) / 4 + dev_round - 1;
			nfr -= nfr % dev_round;
			conv = resamp_new(name, &dev_ipar, &opar);
			opar.rate = dev_ipar.rate;
			aproc_setout(conv, obuf);
			obuf = abuf_new(nfr, &opar);
			aproc_setin(conv, obuf);
		}
		aproc_setout(dev_sub, obuf);
		abuf_ipos(obuf, -dev_sub->u.sub.lat);
		obuf->xrun = overrun;
	}

	/*
	 * sync play to record
	 */
	if (ibuf && obuf) {
		ibuf->duplex = obuf;
		obuf->duplex = ibuf;
		dev_sync(ibuf, obuf);
	}
}