Annotation of src/usr.bin/aucat/legacy.c, Revision 1.2
1.2 ! ratchov 1: /* $OpenBSD: legacy.c,v 1.1 2008/05/23 07:15:46 ratchov Exp $ */
1.1 ratchov 2: /*
3: * Copyright (c) 1997 Kenneth Stailey. All rights reserved.
4: *
5: * Redistribution and use in source and binary forms, with or without
6: * modification, are permitted provided that the following conditions
7: * are met:
8: * 1. Redistributions of source code must retain the above copyright
9: * notice, this list of conditions and the following disclaimer.
10: * 2. Redistributions in binary form must reproduce the above copyright
11: * notice, this list of conditions and the following disclaimer in the
12: * documentation and/or other materials provided with the distribution.
13: * 3. All advertising materials mentioning features or use of this software
14: * must display the following acknowledgement:
15: * This product includes software developed by Kenneth Stailey.
16: * 4. The name of the author may not be used to endorse or promote products
17: * derived from this software without specific prior written permission.
18: *
19: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30:
1.2 ! ratchov 31: #include <sys/types.h>
! 32: #include <sys/ioctl.h>
! 33: #include <sys/audioio.h>
! 34:
1.1 ratchov 35: #include <fcntl.h>
1.2 ! ratchov 36: #include <string.h>
1.1 ratchov 37: #include <unistd.h>
38: #include <err.h>
39:
40: #include "file.h"
41: #include "aparams.h"
42: #include "dev.h"
43:
44:
45: /* headerless data files. played at /dev/audio's defaults.
46: */
47: #define FMT_RAW 0
48:
49: /* Sun/NeXT .au files. header is skipped and /dev/audio is configured
50: * for monaural 8-bit ulaw @ 8kHz, the de facto format for .au files,
51: * as well as the historical default configuration for /dev/audio.
52: */
53: #define FMT_AU 1
54:
55: /* RIFF WAV files. header is parsed for format details which are
56: * applied to /dev/audio.
57: */
58: #define FMT_WAV 2
59:
60:
61: int
62: legacy_play(char *dev, char *aufile)
63: {
64: struct audio_prinfo ai;
65: struct audio_info info;
66: struct aparams par;
67: ssize_t rd;
68: off_t datasz;
69: char buf[5120];
70: int afd, fd, fmt = FMT_RAW;
71: u_int32_t pos = 0;
72: char magic[4];
73:
74: if ((fd = open(aufile, O_RDONLY)) < 0) {
75: warn("cannot open %s", aufile);
76: return(1);
77: }
78:
79: if (read(fd, magic, sizeof(magic)) != sizeof(magic)) {
80: /* read() error, or the file is smaller than sizeof(magic).
81: * treat as a raw file, like previous versions of aucat.
82: */
83: } else if (!strncmp(magic, ".snd", 4)) {
84: fmt = FMT_AU;
85: if (read(fd, &pos, sizeof(pos)) == sizeof(pos))
86: pos = ntohl(pos);
87: } else if (!strncmp(magic, "RIFF", 4) &&
88: wav_readhdr(fd, &par, &datasz)) {
89: fmt = FMT_WAV;
90: }
91:
92: /* seek to start of audio data. wav_readhdr already took care
93: * of this for FMT_WAV.
94: */
95: if (fmt == FMT_RAW || fmt == FMT_AU)
96: if (lseek(fd, (off_t)pos, SEEK_SET) == -1)
97: warn("lseek");
98:
99: if ((afd = open(dev, O_WRONLY)) < 0) {
100: warn("can't open %s", dev);
101: return(1);
102: }
103:
104: AUDIO_INITINFO(&info);
105: ai = info.play;
106:
107: switch(fmt) {
108: case FMT_WAV:
109: sun_partoinfo(&ai, &par);
110: break;
111: case FMT_AU:
112: ai.encoding = AUDIO_ENCODING_ULAW;
113: ai.precision = 8;
114: ai.sample_rate = 8000;
115: ai.channels = 1;
116: break;
117: case FMT_RAW:
118: default:
119: break;
120: }
121:
122: info.play = ai;
123: if (ioctl(afd, AUDIO_SETINFO, &info) < 0) {
124: warn("%s", dev);
125: /* only WAV could fail in previous aucat versions (unless
126: * the parameters returned by AUDIO_GETINFO would fail,
127: * which is unlikely)
128: */
129: if (fmt == FMT_WAV)
130: return(1);
131: }
132:
133: /* parameters may be silently modified. see audio(9)'s
134: * description of set_params. for compatability with previous
135: * aucat versions, continue running if something doesn't match.
136: */
137: (void) ioctl(afd, AUDIO_GETINFO, &info);
138: if (info.play.encoding != ai.encoding ||
139: info.play.precision != ai.precision ||
140: info.play.channels != ai.channels ||
141: /* devices may return a very close rate, such as 44099 when
142: * 44100 was requested. the difference is inaudible. allow
143: * 2% deviation as an example of how to cope.
144: */
145: (info.play.sample_rate > ai.sample_rate * 1.02 ||
146: info.play.sample_rate < ai.sample_rate * 0.98)) {
147: warnx("format not supported by %s", dev);
148: }
149:
150: while ((rd = read(fd, buf, sizeof(buf))) > 0)
151: if (write(afd, buf, rd) != rd)
152: warn("write");
153: if (rd == -1)
154: warn("read");
155:
156: (void) close(afd);
157: (void) close(fd);
158:
159: return(0);
160: }