xref: /openbsd-src/regress/lib/libsndio/rec/rec.c (revision 92516d8ed7dd9561e233d8630bd9ecf852766187)
1f153e440Sratchov #include <errno.h>
2f153e440Sratchov #include <fcntl.h>
3f153e440Sratchov #include <poll.h>
4f153e440Sratchov #include <stdio.h>
5f153e440Sratchov #include <string.h>
6f153e440Sratchov #include <stdlib.h>
7f153e440Sratchov #include <unistd.h>
878ccc913Sratchov #include <sndio.h>
978ccc913Sratchov #include "tools.h"
10f153e440Sratchov 
11f153e440Sratchov #define BUFSZ 0x1000
12*92516d8eSratchov 
13*92516d8eSratchov void cb(void *, int);
14*92516d8eSratchov void usage(void);
15*92516d8eSratchov 
16f153e440Sratchov unsigned char buf[BUFSZ];
17f153e440Sratchov struct sio_par par;
18f153e440Sratchov char *xstr[] = SIO_XSTRINGS;
19f153e440Sratchov 
20f153e440Sratchov long long pos = 0;
21f153e440Sratchov int rlat = 0;
22f153e440Sratchov 
23f153e440Sratchov void
cb(void * addr,int delta)24f153e440Sratchov cb(void *addr, int delta)
25f153e440Sratchov {
26f153e440Sratchov 	pos += delta;
27f153e440Sratchov 	rlat += delta;
28f153e440Sratchov 	fprintf(stderr,
29f153e440Sratchov 	    "cb: delta = %+7d, rlat = %+7d, pos = %+7lld\n",
30f153e440Sratchov 	    delta, rlat, pos);
31f153e440Sratchov }
32f153e440Sratchov 
33f153e440Sratchov void
usage(void)34f153e440Sratchov usage(void) {
35f153e440Sratchov 	fprintf(stderr, "usage: rec [-r rate] [-c nchan] [-e enc]\n");
36f153e440Sratchov }
37f153e440Sratchov 
38f153e440Sratchov int
main(int argc,char ** argv)39f153e440Sratchov main(int argc, char **argv) {
40f153e440Sratchov 	int ch;
41f153e440Sratchov 	struct sio_hdl *hdl;
42f153e440Sratchov 	ssize_t n;
43f153e440Sratchov 
44f153e440Sratchov 	/*
45f153e440Sratchov 	 * defaults parameters
46f153e440Sratchov 	 */
47f153e440Sratchov 	sio_initpar(&par);
48f153e440Sratchov 	par.sig = 1;
49f153e440Sratchov 	par.bits = 16;
50f153e440Sratchov 	par.rchan = 2;
51f153e440Sratchov 	par.rate = 44100;
52f153e440Sratchov 
53f153e440Sratchov 	while ((ch = getopt(argc, argv, "r:c:e:b:x:")) != -1) {
54f153e440Sratchov 		switch(ch) {
55f153e440Sratchov 		case 'r':
56f153e440Sratchov 			if (sscanf(optarg, "%u", &par.rate) != 1) {
57f153e440Sratchov 				fprintf(stderr, "%s: bad rate\n", optarg);
58f153e440Sratchov 				exit(1);
59f153e440Sratchov 			}
60f153e440Sratchov 			break;
61f153e440Sratchov 		case 'c':
62f153e440Sratchov 			if (sscanf(optarg, "%u", &par.rchan) != 1) {
63f153e440Sratchov 				fprintf(stderr, "%s: channels number\n", optarg);
64f153e440Sratchov 				exit(1);
65f153e440Sratchov 			}
66f153e440Sratchov 			break;
67f153e440Sratchov 		case 'e':
68f153e440Sratchov 			if (!sio_strtoenc(&par, optarg)) {
69f153e440Sratchov 				fprintf(stderr, "%s: unknown encoding\n", optarg);
70f153e440Sratchov 				exit(1);
71f153e440Sratchov 			}
72f153e440Sratchov 			break;
73f153e440Sratchov 		case 'x':
74f153e440Sratchov 			for (par.xrun = 0;; par.xrun++) {
75f153e440Sratchov 				if (par.xrun == sizeof(xstr) / sizeof(char *)) {
76f153e440Sratchov 					fprintf(stderr,
77f153e440Sratchov 					    "%s: bad xrun mode\n", optarg);
78f153e440Sratchov 					exit(1);
79f153e440Sratchov 				}
80f153e440Sratchov 				if (strcmp(xstr[par.xrun], optarg) == 0)
81f153e440Sratchov 					break;
82f153e440Sratchov 			}
83f153e440Sratchov 			break;
84f153e440Sratchov 		default:
85f153e440Sratchov 			usage();
86f153e440Sratchov 			exit(1);
87f153e440Sratchov 			break;
88f153e440Sratchov 		}
89f153e440Sratchov 	}
90f153e440Sratchov 
91*92516d8eSratchov 	hdl = sio_open(SIO_DEVANY, SIO_REC, 0);
92f153e440Sratchov 	if (hdl == NULL) {
93f153e440Sratchov 		fprintf(stderr, "sio_open() failed\n");
94f153e440Sratchov 		exit(1);
95f153e440Sratchov 	}
96f153e440Sratchov 	sio_onmove(hdl, cb, NULL);
97f153e440Sratchov 	if (!sio_setpar(hdl, &par)) {
98f153e440Sratchov 		fprintf(stderr, "sio_setpar() failed\n");
99f153e440Sratchov 		exit(1);
100f153e440Sratchov 	}
101f153e440Sratchov 	if (!sio_getpar(hdl, &par)) {
102f153e440Sratchov 		fprintf(stderr, "sio_getpar() failed\n");
103f153e440Sratchov 		exit(1);
104f153e440Sratchov 	}
105f153e440Sratchov 	if (!sio_start(hdl)) {
106f153e440Sratchov 		fprintf(stderr, "sio_start() failed\n");
107f153e440Sratchov 		exit(1);
108f153e440Sratchov 	}
109f153e440Sratchov 	for (;;) {
110f153e440Sratchov 		n = sio_read(hdl, buf, BUFSZ);
111f153e440Sratchov 		if (n == 0) {
112f153e440Sratchov 			fprintf(stderr, "sio_write: failed\n");
113f153e440Sratchov 			exit(1);
114f153e440Sratchov 		}
115f153e440Sratchov 		rlat -= n / (int)(par.bps * par.rchan);
116f153e440Sratchov 		if (write(STDOUT_FILENO, buf, n) < 0) {
117f153e440Sratchov 			perror("stdout");
118f153e440Sratchov 			exit(1);
119f153e440Sratchov 		}
120f153e440Sratchov 	}
121f153e440Sratchov 	sio_close(hdl);
122f153e440Sratchov 	return 0;
123f153e440Sratchov }
124