diff options
author | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2010-01-05 10:18:13 +0000 |
---|---|---|
committer | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2010-01-05 10:18:13 +0000 |
commit | f3c71d850fdf6fb10c8709d675812106bc97f503 (patch) | |
tree | 35990e349f83515bdf274eeb209154714f1e7483 | |
parent | 3f236730355a7daf22a1d1c8f864e112cf39f6c1 (diff) |
Make the way .wav files are handled closer to the way sockets are
handled, which is conceptually simpler. No behaviour changes.
-rw-r--r-- | usr.bin/aucat/aproc.c | 54 | ||||
-rw-r--r-- | usr.bin/aucat/aproc.h | 18 | ||||
-rw-r--r-- | usr.bin/aucat/aucat.c | 154 | ||||
-rw-r--r-- | usr.bin/aucat/dev.c | 34 | ||||
-rw-r--r-- | usr.bin/aucat/dev.h | 3 | ||||
-rw-r--r-- | usr.bin/aucat/sock.c | 4 | ||||
-rw-r--r-- | usr.bin/aucat/wav.c | 284 | ||||
-rw-r--r-- | usr.bin/aucat/wav.h | 8 |
8 files changed, 373 insertions, 186 deletions
diff --git a/usr.bin/aucat/aproc.c b/usr.bin/aucat/aproc.c index 0f1e6f5b7e9..1e53a8f6c4d 100644 --- a/usr.bin/aucat/aproc.c +++ b/usr.bin/aucat/aproc.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aproc.c,v 1.40 2009/11/15 21:44:09 ratchov Exp $ */ +/* $OpenBSD: aproc.c,v 1.41 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -157,7 +157,7 @@ aproc_depend(struct aproc *p, struct aproc *dep) } int -rpipe_in(struct aproc *p, struct abuf *ibuf_dummy) +rfile_in(struct aproc *p, struct abuf *ibuf_dummy) { struct abuf *obuf = LIST_FIRST(&p->obuflist); struct file *f = p->u.io.file; @@ -177,7 +177,7 @@ rpipe_in(struct aproc *p, struct abuf *ibuf_dummy) } int -rpipe_out(struct aproc *p, struct abuf *obuf) +rfile_out(struct aproc *p, struct abuf *obuf) { struct file *f = p->u.io.file; unsigned char *data; @@ -196,7 +196,7 @@ rpipe_out(struct aproc *p, struct abuf *obuf) } void -rpipe_done(struct aproc *p) +rfile_done(struct aproc *p) { struct file *f = p->u.io.file; struct abuf *obuf; @@ -219,43 +219,43 @@ rpipe_done(struct aproc *p) } void -rpipe_eof(struct aproc *p, struct abuf *ibuf_dummy) +rfile_eof(struct aproc *p, struct abuf *ibuf_dummy) { aproc_del(p); } void -rpipe_hup(struct aproc *p, struct abuf *obuf) +rfile_hup(struct aproc *p, struct abuf *obuf) { aproc_del(p); } -struct aproc_ops rpipe_ops = { +struct aproc_ops rfile_ops = { "rpipe", - rpipe_in, - rpipe_out, - rpipe_eof, - rpipe_hup, + rfile_in, + rfile_out, + rfile_eof, + rfile_hup, NULL, /* newin */ NULL, /* newout */ aproc_ipos, aproc_opos, - rpipe_done + rfile_done }; struct aproc * -rpipe_new(struct file *f) +rfile_new(struct file *f) { struct aproc *p; - p = aproc_new(&rpipe_ops, f->name); + p = aproc_new(&rfile_ops, f->name); p->u.io.file = f; f->rproc = p; return p; } void -wpipe_done(struct aproc *p) +wfile_done(struct aproc *p) { struct file *f = p->u.io.file; struct abuf *ibuf; @@ -278,7 +278,7 @@ wpipe_done(struct aproc *p) } int -wpipe_in(struct aproc *p, struct abuf *ibuf) +wfile_in(struct aproc *p, struct abuf *ibuf) { struct file *f = p->u.io.file; unsigned char *data; @@ -297,7 +297,7 @@ wpipe_in(struct aproc *p, struct abuf *ibuf) } int -wpipe_out(struct aproc *p, struct abuf *obuf_dummy) +wfile_out(struct aproc *p, struct abuf *obuf_dummy) { struct abuf *ibuf = LIST_FIRST(&p->ibuflist); struct file *f = p->u.io.file; @@ -321,36 +321,36 @@ wpipe_out(struct aproc *p, struct abuf *obuf_dummy) } void -wpipe_eof(struct aproc *p, struct abuf *ibuf) +wfile_eof(struct aproc *p, struct abuf *ibuf) { aproc_del(p); } void -wpipe_hup(struct aproc *p, struct abuf *obuf_dummy) +wfile_hup(struct aproc *p, struct abuf *obuf_dummy) { aproc_del(p); } -struct aproc_ops wpipe_ops = { +struct aproc_ops wfile_ops = { "wpipe", - wpipe_in, - wpipe_out, - wpipe_eof, - wpipe_hup, + wfile_in, + wfile_out, + wfile_eof, + wfile_hup, NULL, /* newin */ NULL, /* newout */ aproc_ipos, aproc_opos, - wpipe_done + wfile_done }; struct aproc * -wpipe_new(struct file *f) +wfile_new(struct file *f) { struct aproc *p; - p = aproc_new(&wpipe_ops, f->name); + p = aproc_new(&wfile_ops, f->name); p->u.io.file = f; f->wproc = p; return p; diff --git a/usr.bin/aucat/aproc.h b/usr.bin/aucat/aproc.h index 106bf0c2cce..e5565614e1d 100644 --- a/usr.bin/aucat/aproc.h +++ b/usr.bin/aucat/aproc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: aproc.h,v 1.27 2009/11/03 21:31:37 ratchov Exp $ */ +/* $OpenBSD: aproc.h,v 1.28 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -210,20 +210,8 @@ void aproc_setin(struct aproc *, struct abuf *); void aproc_setout(struct aproc *, struct abuf *); int aproc_depend(struct aproc *, struct aproc *); -struct aproc *rpipe_new(struct file *); -int rpipe_in(struct aproc *, struct abuf *); -int rpipe_out(struct aproc *, struct abuf *); -void rpipe_done(struct aproc *); -void rpipe_eof(struct aproc *, struct abuf *); -void rpipe_hup(struct aproc *, struct abuf *); - -struct aproc *wpipe_new(struct file *); -void wpipe_done(struct aproc *); -int wpipe_in(struct aproc *, struct abuf *); -int wpipe_out(struct aproc *, struct abuf *); -void wpipe_eof(struct aproc *, struct abuf *); -void wpipe_hup(struct aproc *, struct abuf *); - +struct aproc *rfile_new(struct file *); +struct aproc *wfile_new(struct file *); struct aproc *mix_new(char *, int, struct aproc *); struct aproc *sub_new(char *, int, struct aproc *); struct aproc *resamp_new(char *, unsigned, unsigned); diff --git a/usr.bin/aucat/aucat.c b/usr.bin/aucat/aucat.c index 0531412685d..6f0861cdd6a 100644 --- a/usr.bin/aucat/aucat.c +++ b/usr.bin/aucat/aucat.c @@ -1,4 +1,4 @@ -/* $OpenBSD: aucat.c,v 1.76 2009/11/21 14:21:08 ratchov Exp $ */ +/* $OpenBSD: aucat.c,v 1.77 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -37,7 +37,6 @@ #include "dev.h" #include "listen.h" #include "midi.h" -#include "miofile.h" #include "opt.h" #include "wav.h" @@ -168,19 +167,18 @@ SLIST_HEAD(farglist, farg); void farg_add(struct farglist *list, struct aparams *ipar, struct aparams *opar, unsigned vol, - int hdr, int xrun, int mmc, char *optarg) + int hdr, int xrun, int mmc, char *name) { struct farg *fa; size_t namelen; fa = malloc(sizeof(struct farg)); if (fa == NULL) - err(1, "%s", optarg); + err(1, "%s", name); if (hdr == HDR_AUTO) { - namelen = strlen(optarg); - if (namelen >= 4 && - strcasecmp(optarg + namelen - 4, ".wav") == 0) { + if (name != NULL && (namelen = strlen(name)) >= 4 && + strcasecmp(name + namelen - 4, ".wav") == 0) { fa->hdr = HDR_WAV; } else { fa->hdr = HDR_RAW; @@ -191,119 +189,11 @@ farg_add(struct farglist *list, fa->ipar = *ipar; fa->opar = *opar; fa->vol = vol; - fa->name = optarg; + fa->name = name; fa->mmc = mmc; SLIST_INSERT_HEAD(list, fa, entry); } -/* - * Open an input file and setup converter if necessary. - */ -void -newinput(struct farg *fa) -{ - int fd; - struct wav *f; - struct aproc *proc; - struct abuf *buf; - unsigned nfr; - - if (strcmp(fa->name, "-") == 0) { - fd = STDIN_FILENO; - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) - warn("stdin"); - fa->name = "stdin"; - } else { - fd = open(fa->name, O_RDONLY | O_NONBLOCK, 0666); - if (fd < 0) - err(1, "%s", fa->name); - } - /* - * XXX : we should round rate, right ? - */ - f = wav_new_in(&wav_ops, fd, fa->name, &fa->ipar, fa->hdr); - if (f == NULL) { - if (fd != STDIN_FILENO) - close(fd); - return; - } - nfr = dev_bufsz * fa->ipar.rate / dev_rate; - buf = abuf_new(nfr, &fa->ipar); - proc = rpipe_new((struct file *)f); - aproc_setout(proc, buf); - abuf_fill(buf); /* XXX: move this in dev_attach() ? */ - dev_attach(fa->name, buf, &fa->ipar, fa->xrun, - NULL, NULL, 0, ADATA_UNIT); - dev_setvol(buf, MIDI_TO_ADATA(fa->vol)); -} - -/* - * Open an output file and setup converter if necessary. - */ -void -newoutput(struct farg *fa) -{ - int fd; - struct wav *f; - struct aproc *proc; - struct abuf *buf; - unsigned nfr; - - if (strcmp(fa->name, "-") == 0) { - fd = STDOUT_FILENO; - if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) - warn("stdout"); - fa->name = "stdout"; - } else { - fd = open(fa->name, - O_WRONLY | O_TRUNC | O_CREAT | O_NONBLOCK, 0666); - if (fd < 0) - err(1, "%s", fa->name); - } - /* - * XXX : we should round rate, right ? - */ - f = wav_new_out(&wav_ops, fd, fa->name, &fa->opar, fa->hdr); - if (f == NULL) { - if (fd != STDOUT_FILENO) - close(fd); - return; - } - nfr = dev_bufsz * fa->opar.rate / dev_rate; - proc = wpipe_new((struct file *)f); - buf = abuf_new(nfr, &fa->opar); - aproc_setin(proc, buf); - dev_attach(fa->name, NULL, NULL, 0, buf, &fa->opar, fa->xrun, 0); -} - -/* - * Open a MIDI device and connect it to the thru box - */ -void -newmidi(struct farg *fa, int in, int out) -{ - struct file *dev; - struct abuf *rbuf = NULL, *wbuf = NULL; - struct aproc *rproc, *wproc; - - dev = (struct file *)miofile_new(&miofile_ops, fa->name, in, out); - if (dev == NULL) { - errx(1, "%s: can't open device", - fa->name ? fa->name : "<default>"); - } - if (in) { - rproc = rpipe_new(dev); - rbuf = abuf_new(MIDI_BUFSZ, &aparams_none); - aproc_setout(rproc, rbuf); - } - if (out) { - wproc = wpipe_new(dev); - wbuf = abuf_new(MIDI_BUFSZ, &aparams_none); - aproc_setin(wproc, wbuf); - } - dev_midiattach(rbuf, wbuf); -} - void setsig(void) { @@ -372,7 +262,7 @@ aucat_main(int argc, char **argv) struct farg *fa; struct farglist ifiles, ofiles, sfiles; struct aparams ipar, opar, dipar, dopar; - char base[PATH_MAX], path[PATH_MAX]; + char base[PATH_MAX], path[PATH_MAX], *file; unsigned bufsz, round, mode; char *devpath; const char *str; @@ -440,12 +330,18 @@ aucat_main(int argc, char **argv) errx(1, "%s: volume is %s", optarg, str); break; case 'i': + file = optarg; + if (strcmp(file, "-") == 0) + file = NULL; farg_add(&ifiles, &ipar, &opar, volctl, - hdr, xrun, 0, optarg); + hdr, xrun, 0, file); break; case 'o': + file = optarg; + if (strcmp(file, "-") == 0) + file = NULL; farg_add(&ofiles, &ipar, &opar, volctl, - hdr, xrun, 0, optarg); + hdr, xrun, 0, file); break; case 's': farg_add(&sfiles, &ipar, &opar, volctl, @@ -592,13 +488,16 @@ aucat_main(int argc, char **argv) while (!SLIST_EMPTY(&ifiles)) { fa = SLIST_FIRST(&ifiles); SLIST_REMOVE_HEAD(&ifiles, entry); - newinput(fa); + if (!wav_new_in(&wav_ops, fa->name, + fa->hdr, &fa->ipar, fa->xrun, fa->vol)) + exit(1); free(fa); } while (!SLIST_EMPTY(&ofiles)) { fa = SLIST_FIRST(&ofiles); SLIST_REMOVE_HEAD(&ofiles, entry); - newoutput(fa); + if (!wav_new_out(&wav_ops, fa->name, + fa->hdr, &fa->ipar, fa->xrun)) free(fa); } while (!SLIST_EMPTY(&sfiles)) { @@ -752,9 +651,12 @@ midicat_main(int argc, char **argv) while (!SLIST_EMPTY(&dfiles)) { fa = SLIST_FIRST(&dfiles); SLIST_REMOVE_HEAD(&dfiles, entry); - newmidi(fa, - !SLIST_EMPTY(&ofiles) || l_flag, - !SLIST_EMPTY(&ifiles) || l_flag); + if (!dev_thruadd(fa->name, + !SLIST_EMPTY(&ofiles) || l_flag, + !SLIST_EMPTY(&ifiles) || l_flag)) { + errx(1, "%s: can't open device", + fa->name ? fa->name : "<default>"); + } free(fa); } if (l_flag) { @@ -777,7 +679,7 @@ midicat_main(int argc, char **argv) err(1, "%s", fa->name); } stdx = (struct file *)pipe_new(&pipe_ops, fd, fa->name); - p = rpipe_new(stdx); + p = rfile_new(stdx); buf = abuf_new(MIDI_BUFSZ, &aparams_none); aproc_setout(p, buf); dev_midiattach(buf, NULL); @@ -797,7 +699,7 @@ midicat_main(int argc, char **argv) err(1, "%s", fa->name); } stdx = (struct file *)pipe_new(&pipe_ops, fd, fa->name); - p = wpipe_new(stdx); + p = wfile_new(stdx); buf = abuf_new(MIDI_BUFSZ, &aparams_none); aproc_setin(p, buf); dev_midiattach(NULL, buf); diff --git a/usr.bin/aucat/dev.c b/usr.bin/aucat/dev.c index 6cf2cc36e92..bdd6366235a 100644 --- a/usr.bin/aucat/dev.c +++ b/usr.bin/aucat/dev.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dev.c,v 1.37 2009/11/08 00:08:41 ratchov Exp $ */ +/* $OpenBSD: dev.c,v 1.38 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -24,6 +24,7 @@ #include "conf.h" #include "dev.h" #include "pipe.h" +#include "miofile.h" #include "safile.h" #include "midi.h" @@ -43,6 +44,33 @@ dev_thruinit(void) } /* + * Open a MIDI device and connect it to the thru box + */ +int +dev_thruadd(char *name, int in, int out) +{ + struct file *f; + struct abuf *rbuf = NULL, *wbuf = NULL; + struct aproc *rproc, *wproc; + + f = (struct file *)miofile_new(&miofile_ops, name, in, out); + if (f == NULL) + return 0; + if (in) { + rproc = rfile_new(f); + rbuf = abuf_new(MIDI_BUFSZ, &aparams_none); + aproc_setout(rproc, rbuf); + } + if (out) { + wproc = wfile_new(f); + wbuf = abuf_new(MIDI_BUFSZ, &aparams_none); + aproc_setin(wproc, wbuf); + } + dev_midiattach(rbuf, wbuf); + return 1; +} + +/* * Attach a bi-directional MIDI stream to the MIDI device */ void @@ -160,7 +188,7 @@ dev_init(char *devpath, /* * Create the read end. */ - dev_rec = rpipe_new(f); + dev_rec = rfile_new(f); dev_rec->refs++; buf = abuf_new(nfr, dipar); aproc_setout(dev_rec, buf); @@ -198,7 +226,7 @@ dev_init(char *devpath, /* * Create the write end. */ - dev_play = wpipe_new(f); + dev_play = wfile_new(f); dev_play->refs++; buf = abuf_new(nfr, dopar); aproc_setin(dev_play, buf); diff --git a/usr.bin/aucat/dev.h b/usr.bin/aucat/dev.h index 76c9929cf22..ae8eda32164 100644 --- a/usr.bin/aucat/dev.h +++ b/usr.bin/aucat/dev.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dev.h,v 1.15 2009/11/03 21:31:37 ratchov Exp $ */ +/* $OpenBSD: dev.h,v 1.16 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -26,6 +26,7 @@ extern struct aparams dev_ipar, dev_opar; extern struct aproc *dev_mix, *dev_sub, *dev_midi; void dev_thruinit(void); +int dev_thruadd(char *, int, int); void dev_midiattach(struct abuf *, struct abuf *); unsigned dev_roundof(unsigned); void dev_loopinit(struct aparams *, struct aparams *, unsigned); diff --git a/usr.bin/aucat/sock.c b/usr.bin/aucat/sock.c index c49f16dcbe7..2fa277bca53 100644 --- a/usr.bin/aucat/sock.c +++ b/usr.bin/aucat/sock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sock.c,v 1.35 2009/11/03 21:31:37 ratchov Exp $ */ +/* $OpenBSD: sock.c,v 1.36 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -515,7 +515,7 @@ sock_wdata(struct sock *f) unsigned char *data; unsigned count, n; #define ZERO_MAX 0x1000 - static char zero[ZERO_MAX]; + static unsigned char zero[ZERO_MAX]; if (!(f->pipe.file.state & FILE_WOK)) return 0; diff --git a/usr.bin/aucat/wav.c b/usr.bin/aucat/wav.c index f69a17a7ca7..48563ed0ba1 100644 --- a/usr.bin/aucat/wav.c +++ b/usr.bin/aucat/wav.c @@ -15,8 +15,14 @@ */ #include <stdlib.h> +#include <stdio.h> +#include <fcntl.h> +#include <unistd.h> +#include "abuf.h" +#include "aproc.h" #include "conf.h" +#include "dev.h" #include "wav.h" short wav_ulawmap[256] = { @@ -109,33 +115,287 @@ struct fileops wav_ops = { pipe_revents }; +int rwav_in(struct aproc *, struct abuf *); +int rwav_out(struct aproc *, struct abuf *); +void rwav_eof(struct aproc *, struct abuf *); +void rwav_hup(struct aproc *, struct abuf *); +void rwav_done(struct aproc *); + +int wwav_in(struct aproc *, struct abuf *); +int wwav_out(struct aproc *, struct abuf *); +void wwav_eof(struct aproc *, struct abuf *); +void wwav_hup(struct aproc *, struct abuf *); +void wwav_done(struct aproc *); + +struct aproc_ops rwav_ops = { + "rwav", + rwav_in, + rwav_out, + rwav_eof, + rwav_hup, + NULL, /* newin */ + NULL, /* newout */ + NULL, /* ipos */ + NULL, /* opos */ + rwav_done +}; + +struct aproc_ops wwav_ops = { + "wwav", + wwav_in, + wwav_out, + wwav_eof, + wwav_hup, + NULL, /* newin */ + NULL, /* newout */ + NULL, /* ipos */ + NULL, /* opos */ + wwav_done +}; + +struct aproc * +rwav_new(struct file *f) +{ + struct aproc *p; + + p = aproc_new(&rwav_ops, f->name); + p->u.io.file = f; + f->rproc = p; + return p; +} + +int +rwav_in(struct aproc *p, struct abuf *ibuf_dummy) +{ + struct abuf *obuf = LIST_FIRST(&p->obuflist); + struct file *f = p->u.io.file; + unsigned char *data; + unsigned count; + + if (ABUF_FULL(obuf) || !(f->state & FILE_ROK)) + return 0; + data = abuf_wgetblk(obuf, &count, 0); + count = file_read(f, data, count); + if (count == 0) + return 0; + abuf_wcommit(obuf, count); + if (!abuf_flush(obuf)) + return 0; + return 1; +} + +int +rwav_out(struct aproc *p, struct abuf *obuf) +{ + struct file *f = p->u.io.file; + unsigned char *data; + unsigned count; + + if (f->state & FILE_RINUSE) + return 0; + if (ABUF_FULL(obuf) || !(f->state & FILE_ROK)) + return 0; + data = abuf_wgetblk(obuf, &count, 0); + count = file_read(f, data, count); + if (count == 0) + return 0; + abuf_wcommit(obuf, count); + return 1; +} + +void +rwav_done(struct aproc *p) +{ + struct file *f = p->u.io.file; + struct abuf *obuf; + + if (f == NULL) + return; + /* + * all buffers must be detached before deleting f->wproc, + * because otherwise it could trigger this code again + */ + obuf = LIST_FIRST(&p->obuflist); + if (obuf) + abuf_eof(obuf); + if (f->wproc) { + f->rproc = NULL; + aproc_del(f->wproc); + } else + file_del(f); + p->u.io.file = NULL; +} + +void +rwav_eof(struct aproc *p, struct abuf *ibuf_dummy) +{ + aproc_del(p); +} + +void +rwav_hup(struct aproc *p, struct abuf *obuf) +{ + aproc_del(p); +} + +struct aproc * +wwav_new(struct file *f) +{ + struct aproc *p; + + p = aproc_new(&wwav_ops, f->name); + p->u.io.file = f; + f->wproc = p; + return p; +} + +void +wwav_done(struct aproc *p) +{ + struct file *f = p->u.io.file; + struct abuf *ibuf; + + if (f == NULL) + return; + /* + * all buffers must be detached before deleting f->rproc, + * because otherwise it could trigger this code again + */ + ibuf = LIST_FIRST(&p->ibuflist); + if (ibuf) + abuf_hup(ibuf); + if (f->rproc) { + f->wproc = NULL; + aproc_del(f->rproc); + } else + file_del(f); + p->u.io.file = NULL; +} + +int +wwav_in(struct aproc *p, struct abuf *ibuf) +{ + struct file *f = p->u.io.file; + unsigned char *data; + unsigned count; + + if (f->state & FILE_WINUSE) + return 0; + if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK)) + return 0; + data = abuf_rgetblk(ibuf, &count, 0); + count = file_write(f, data, count); + if (count == 0) + return 0; + abuf_rdiscard(ibuf, count); + return 1; +} + +int +wwav_out(struct aproc *p, struct abuf *obuf_dummy) +{ + struct abuf *ibuf = LIST_FIRST(&p->ibuflist); + struct file *f = p->u.io.file; + unsigned char *data; + unsigned count; + + if (!abuf_fill(ibuf)) + return 0; + if (ABUF_EMPTY(ibuf) || !(f->state & FILE_WOK)) + return 0; + data = abuf_rgetblk(ibuf, &count, 0); + if (count == 0) { + /* XXX: this can't happen, right ? */ + return 0; + } + count = file_write(f, data, count); + if (count == 0) + return 0; + abuf_rdiscard(ibuf, count); + return 1; +} + +void +wwav_eof(struct aproc *p, struct abuf *ibuf) +{ + aproc_del(p); +} + +void +wwav_hup(struct aproc *p, struct abuf *obuf_dummy) +{ + aproc_del(p); +} + struct wav * -wav_new_in(struct fileops *ops, int fd, char *name, - struct aparams *par, unsigned hdr) +wav_new_in(struct fileops *ops, char *name, unsigned hdr, + struct aparams *par, unsigned xrun, unsigned volctl) { + int fd; struct wav *f; + struct aproc *p; + struct abuf *buf; + unsigned nfr; + if (name != NULL) { + fd = open(name, O_RDONLY | O_NONBLOCK, 0666); + if (fd < 0) { + perror(name); + return NULL; + } + } else { + name = "stdin"; + fd = STDIN_FILENO; + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) + perror(name); + } f = (struct wav *)pipe_new(ops, fd, name); if (f == NULL) return NULL; if (hdr == HDR_WAV) { - if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, &f->map)) - exit(1); + if (!wav_readhdr(f->pipe.fd, par, &f->rbytes, &f->map)) { + file_del((struct file *)f); + return NULL; + } f->hpar = *par; } else { f->rbytes = -1; f->map = NULL; } f->hdr = 0; + nfr = dev_bufsz * par->rate / dev_rate; + buf = abuf_new(nfr, par); + p = rwav_new((struct file *)f); + aproc_setout(p, buf); + abuf_fill(buf); /* XXX: move this in dev_attach() ? */ + dev_attach(name, buf, par, xrun, NULL, NULL, 0, ADATA_UNIT); + dev_setvol(buf, MIDI_TO_ADATA(volctl)); return f; } struct wav * -wav_new_out(struct fileops *ops, int fd, char *name, - struct aparams *par, unsigned hdr) +wav_new_out(struct fileops *ops, char *name, unsigned hdr, + struct aparams *par, unsigned xrun) { + int fd; struct wav *f; + struct aproc *p; + struct abuf *buf; + unsigned nfr; + if (name == NULL) { + name = "stdout"; + fd = STDOUT_FILENO; + if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) + perror(name); + } else { + fd = open(name, + O_WRONLY | O_TRUNC | O_CREAT | O_NONBLOCK, 0666); + if (fd < 0) { + perror(name); + return NULL; + } + } f = (struct wav *)pipe_new(ops, fd, name); if (f == NULL) return NULL; @@ -143,13 +403,20 @@ wav_new_out(struct fileops *ops, int fd, char *name, par->le = 1; par->sig = (par->bits <= 8) ? 0 : 1; par->bps = (par->bits + 7) / 8; - if (!wav_writehdr(f->pipe.fd, par)) - exit(1); + if (!wav_writehdr(f->pipe.fd, par)) { + file_del((struct file *)f); + return NULL; + } f->hpar = *par; f->wbytes = WAV_DATAMAX; } else f->wbytes = -1; f->hdr = hdr; + nfr = dev_bufsz * par->rate / dev_rate; + p = wwav_new((struct file *)f); + buf = abuf_new(nfr, par); + aproc_setin(p, buf); + dev_attach(name, NULL, NULL, 0, buf, par, xrun, 0); return f; } @@ -224,3 +491,4 @@ wav_close(struct file *file) wav_writehdr(f->pipe.fd, &f->hpar); pipe_close(file); } + diff --git a/usr.bin/aucat/wav.h b/usr.bin/aucat/wav.h index b3fef15a0f8..921637e9e1a 100644 --- a/usr.bin/aucat/wav.h +++ b/usr.bin/aucat/wav.h @@ -1,4 +1,4 @@ -/* $OpenBSD: wav.h,v 1.4 2009/07/25 10:52:19 ratchov Exp $ */ +/* $OpenBSD: wav.h,v 1.5 2010/01/05 10:18:12 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -36,9 +36,9 @@ struct wav { extern struct fileops wav_ops; -struct wav *wav_new_in(struct fileops *, int, char *, - struct aparams *, unsigned); -struct wav *wav_new_out(struct fileops *, int, char *, +struct wav *wav_new_in(struct fileops *, char *, unsigned, + struct aparams *, unsigned, unsigned); +struct wav *wav_new_out(struct fileops *, char *, unsigned, struct aparams *, unsigned); unsigned wav_read(struct file *, unsigned char *, unsigned); unsigned wav_write(struct file *, unsigned char *, unsigned); |