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
|
#include <sys/time.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <sndio.h>
#include "tools.h"
struct buf { /* simple circular fifo */
unsigned start; /* first used byte */
unsigned used; /* number of used bytes */
#define BUF_LEN (240 * 0x1000) /* i/o buffer size */
unsigned char data[BUF_LEN];
};
void cb(void *, int);
void buf_read(struct buf *, int);
void buf_write(struct buf *, int);
unsigned buf_rec(struct buf *, struct sio_hdl *);
unsigned buf_play(struct buf *, struct sio_hdl *);
void usage(void);
char *xstr[] = SIO_XSTRINGS;
struct sio_par par;
struct buf playbuf, recbuf;
unsigned long long hwpos, wrpos, rdpos;
int tick = 0;
int randomize, xruns;
void
cb(void *addr, int delta)
{
hwpos += delta;
tick = 1;
}
/*
* read buffer contents from a file
*/
void
buf_read(struct buf *buf, int fd)
{
unsigned count, end, avail;
int n;
for (;;) {
avail = BUF_LEN - buf->used;
if (avail == 0)
break;
end = buf->start + buf->used;
if (end >= BUF_LEN)
end -= BUF_LEN;
count = BUF_LEN - end;
if (count > avail)
count = avail;
n = read(fd, buf->data + end, count);
if (n < 0) {
perror("buf_read: read");
exit(1);
}
if (n == 0) {
bzero(buf->data + end, count);
n = count;
}
buf->used += n;
}
}
/*
* write buffer contents to file
*/
void
buf_write(struct buf *buf, int fd)
{
unsigned count;
int n;
while (buf->used) {
count = BUF_LEN - buf->start;
if (count > buf->used)
count = buf->used;
n = write(fd, buf->data + buf->start, count);
if (n < 0) {
perror("buf_write: write");
exit(1);
}
buf->used -= n;
buf->start += n;
if (buf->start >= BUF_LEN)
buf->start -= BUF_LEN;
}
}
/*
* read recorded samples from the device, without blocking
*/
unsigned
buf_rec(struct buf *buf, struct sio_hdl *hdl)
{
unsigned count, end, avail, done = 0;
int n;
for (;;) {
avail = BUF_LEN - buf->used;
if (avail == 0)
break;
end = buf->start + buf->used;
if (end >= BUF_LEN)
end -= BUF_LEN;
count = BUF_LEN - end;
if (count > avail)
count = avail;
/* try to confuse the server */
if (randomize)
count = 1 + (rand() % count);
n = sio_read(hdl, buf->data + end, count);
if (n == 0) {
if (sio_eof(hdl)) {
fprintf(stderr, "sio_read() failed\n");
exit(1);
}
break;
}
rdpos += n;
buf->used += n;
done += n;
}
return done;
}
/*
* write samples to the device, without blocking
*/
unsigned
buf_play(struct buf *buf, struct sio_hdl *hdl)
{
unsigned count, done = 0;
int n;
while (buf->used) {
count = BUF_LEN - buf->start;
if (count > buf->used)
count = buf->used;
/* try to confuse the server */
if (randomize)
count = 1 + (rand() % count);
n = sio_write(hdl, buf->data + buf->start, count);
if (n == 0) {
if (sio_eof(hdl)) {
fprintf(stderr, "sio_write() failed\n");
exit(1);
}
break;
}
wrpos += n;
//write(STDOUT_FILENO, buf->data + buf->start, n);
buf->used -= n;
buf->start += n;
if (buf->start >= BUF_LEN)
buf->start -= BUF_LEN;
done += n;
}
return done;
}
void
usage(void)
{
fprintf(stderr,
"usage: fd [-vRX] [-b size] [-c pchan] [-C rchan] [-e enc]\n"
" [-i file] [-o file] [-r rate] [-x mode]\n");
}
int
main(int argc, char **argv)
{
int ch, recfd, playfd, nfds, events, revents;
char *recpath, *playpath;
struct sio_hdl *hdl;
#define NFDS 16
struct pollfd pfd[NFDS];
unsigned mode;
recfd = -1;
recpath = NULL;
playfd = -1;
playpath = NULL;
/*
* defaults parameters
*/
sio_initpar(&par);
par.sig = 1;
par.bits = 16;
par.pchan = par.rchan = 2;
par.rate = 48000;
while ((ch = getopt(argc, argv, "RXr:c:C:e:i:o:b:x:")) != -1) {
switch(ch) {
case 'r':
if (sscanf(optarg, "%u", &par.rate) != 1) {
fprintf(stderr, "%s: bad rate\n", optarg);
exit(1);
}
break;
case 'c':
if (sscanf(optarg, "%u", &par.pchan) != 1) {
fprintf(stderr, "%s: bad play chans\n",
optarg);
exit(1);
}
break;
case 'C':
if (sscanf(optarg, "%u", &par.rchan) != 1) {
fprintf(stderr, "%s: bad rec chans\n",
optarg);
exit(1);
}
break;
case 'e':
if (!strtoenc(&par, optarg)) {
fprintf(stderr, "%s: bad encoding\n", optarg);
exit(1);
}
break;
case 'o':
recpath = optarg;
break;
case 'i':
playpath = optarg;
break;
case 'b':
if (sscanf(optarg, "%u", &par.appbufsz) != 1) {
fprintf(stderr, "%s: bad buf size\n", optarg);
exit(1);
}
break;
case 'x':
for (par.xrun = 0;; par.xrun++) {
if (par.xrun ==
sizeof(xstr) / sizeof(char *)) {
fprintf(stderr,
"%s: bad xrun mode\n", optarg);
exit(1);
}
if (strcmp(xstr[par.xrun], optarg) == 0)
break;
}
break;
case 'R':
randomize = 1;
break;
case 'X':
xruns = 1;
break;
default:
usage();
exit(1);
break;
}
}
mode = 0;
if (recpath)
mode |= SIO_REC;
if (playpath)
mode |= SIO_PLAY;
if (mode == 0) {
fprintf(stderr, "-i or -o option required\n");
exit(0);
}
hdl = sio_open(SIO_DEVANY, mode, 1);
if (hdl == NULL) {
fprintf(stderr, "sio_open() failed\n");
exit(1);
}
if (sio_nfds(hdl) > NFDS) {
fprintf(stderr, "too many descriptors to poll\n");
exit(1);
}
sio_onmove(hdl, cb, NULL);
if (!sio_setpar(hdl, &par)) {
fprintf(stderr, "sio_setpar() failed\n");
exit(1);
}
if (!sio_getpar(hdl, &par)) {
fprintf(stderr, "sio_setpar() failed\n");
exit(1);
}
fprintf(stderr, "using %u%%%u frame buffer\n", par.bufsz, par.round);
if (!sio_start(hdl)) {
fprintf(stderr, "sio_start() failed\n");
exit(1);
}
events = 0;
if (recpath) {
recfd = open(recpath, O_CREAT | O_WRONLY | O_TRUNC, 0666);
if (recfd < 0) {
perror(recpath);
exit(1);
}
events |= POLLIN;
}
if (playpath) {
playfd = open(playpath, O_RDONLY, 0);
if (playfd < 0) {
perror(playpath);
exit(1);
}
events |= POLLOUT;
buf_read(&playbuf, playfd);
buf_play(&playbuf, hdl);
}
for (;;) {
nfds = sio_pollfd(hdl, pfd, events);
while (poll(pfd, nfds, 1000) < 0) {
if (errno == EINTR)
continue;
perror("poll");
exit(1);
}
revents = sio_revents(hdl, pfd);
if (revents & POLLHUP) {
fprintf(stderr, "device hangup\n");
exit(0);
}
if (tick) {
fprintf(stderr, "pos = %+7lld, "
"plat = %+7lld, rlat = %+7lld\n",
hwpos,
wrpos - hwpos * par.pchan * par.bps,
hwpos * par.rchan * par.bps - rdpos);
tick = 0;
if (xruns) {
if (rand() % 20 == 0)
sleep(2);
}
}
if (revents & POLLIN) {
buf_rec(&recbuf, hdl);
buf_write(&recbuf, recfd);
}
if (revents & POLLOUT) {
buf_play(&playbuf, hdl);
buf_read(&playbuf, playfd);
}
}
sio_close(hdl);
return 0;
}
|