Debian Codegraph @

src/audio/au_oss.c

raw
1 /*************************************************************************/
2 /* */
3 /* Language Technologies Institute */
4 /* Carnegie Mellon University */
5 /* Copyright (c) 2000 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /*************************************************************************/
33 /* Author: Alan W Black ([email protected]) */
34 /* Date: August 2000 */
35 /*************************************************************************/
36 /* */
37 /* Access to "voxware" audio devices (Linux/FreeBSD etc), */
38 /* (sorry my naming convention is showing my age here, basic any */
39 /* generic audio on Linux/BSD such as OSS and ALSA) */
40 /* */
41 /*************************************************************************/
42
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <unistd.h>
46 #include <sys/types.h>
47 #include "cst_string.h"
48 #include "cst_wave.h"
49 #include "cst_audio.h"
50
51 #ifdef CST_AUDIO_LINUX
52 /* Linux/voxware audio specific */
53 #include <sys/ioctl.h>
54 #include <sys/soundcard.h>
55 #include <sys/types.h>
56 #endif
57 #ifdef CST_AUDIO_FREEBSD
58 /* probably Net and Open too */
59 #include <machine/soundcard.h>
60 #endif
61 #include <sys/stat.h>
62 #include <fcntl.h>
63
64 static const char * const oss_audio_device = "/dev/dsp";
65
66 cst_audiodev *audio_open_oss(int sps, int channels, cst_audiofmt fmt)
67 {
68 cst_audiodev *ad;
69 int afmt;
70 int sfmts;
71 int afd;
72 int frag;
73
74 afd = open(oss_audio_device,O_WRONLY);
75 if (afd == -1)
76 {
77 cst_errmsg("oss_audio: failed to open audio device %s\n",
78 oss_audio_device);
79 return NULL;
80 }
81
82 ad = cst_alloc(cst_audiodev, 1);
83 ad->sps = sps;
84 ad->channels = channels;
85 ad->fmt = fmt;
86 ad->platform_data = (void *)afd;
87
88 if (ad->channels == 0)
89 ad->channels = 1;
90
91 ioctl(afd,SNDCTL_DSP_RESET,0);
92
93 ad->real_sps = ad->sps;
94
95 ad->real_channels = ad->channels;
96 ioctl(afd,SNDCTL_DSP_CHANNELS,&ad->real_channels);
97 if (ad->real_channels != ad->channels)
98 {
99 /* Its a stereo only device so it'll play things twice as fast */
100 ad->real_sps /= 2;
101 }
102
103 ioctl(afd,SNDCTL_DSP_SPEED,&ad->real_sps);
104
105 frag = (4 << 16) | 10;
106 ioctl(afd,SNDCTL_DSP_SETFRAGMENT,&frag);
107
108 ioctl(afd,SNDCTL_DSP_GETFMTS,&sfmts);
109 if (fmt == CST_AUDIO_LINEAR8 && (sfmts & AFMT_U8))
110 {
111 ad->real_fmt = CST_AUDIO_LINEAR8;
112 afmt = AFMT_U8;
113 }
114 else if (fmt == CST_AUDIO_MULAW && (sfmts & AFMT_MU_LAW))
115 {
116 ad->real_fmt = CST_AUDIO_MULAW;
117 afmt = AFMT_MU_LAW;
118 }
119 else if (CST_LITTLE_ENDIAN)
120 {
121 if (sfmts & AFMT_S16_LE)
122 {
123 ad->real_fmt = CST_AUDIO_LINEAR16;
124 afmt = AFMT_S16_LE;
125 }
126 else if (sfmts & AFMT_S16_BE) /* not likely */
127 {
128 ad->byteswap = 1;
129 ad->real_fmt = CST_AUDIO_LINEAR16;
130 afmt = AFMT_S16_BE;
131 }
132 else if (sfmts & AFMT_U8)
133 {
134 afmt = AFMT_U8;
135 ad->real_fmt = CST_AUDIO_LINEAR8;
136 }
137 else
138 {
139 cst_free(ad);
140 close(afd);
141 return NULL;
142 }
143 }
144 else
145 {
146 if (sfmts & AFMT_S16_BE)
147 {
148 ad->real_fmt = CST_AUDIO_LINEAR16;
149 afmt = AFMT_S16_BE;
150 }
151 else if (sfmts & AFMT_S16_LE) /* likely */
152 {
153 ad->real_fmt = CST_AUDIO_LINEAR16;
154 ad->byteswap = 1;
155 afmt = AFMT_S16_LE;
156 }
157 else if (sfmts & AFMT_U8)
158 {
159 ad->real_fmt = CST_AUDIO_LINEAR8;
160 afmt = AFMT_U8;
161 }
162 else
163 {
164 cst_free(ad);
165 close(afd);
166 return NULL;
167 }
168 }
169 ioctl(afd, SNDCTL_DSP_SETFMT, &afmt);
170
171 return ad;
172 }
173
174 int audio_close_oss(cst_audiodev *ad)
175 {
176 int rv;
177
178 if (ad == NULL)
179 return 0;
180
181 ioctl((int)ad->platform_data, SNDCTL_DSP_SYNC, NULL);
182 rv = close((int)ad->platform_data);
183 cst_free(ad);
184 return rv;
185 }
186
187 int audio_write_oss(cst_audiodev *ad, void *samples, int num_bytes)
188 {
189 return write((int)ad->platform_data,samples,num_bytes);
190 }
191
192 int audio_flush_oss(cst_audiodev *ad)
193 {
194 return ioctl((int)ad->platform_data, SNDCTL_DSP_SYNC, NULL);
195 }
196
197 int audio_drain_oss(cst_audiodev *ad)
198 {
199 return ioctl((int)ad->platform_data, SNDCTL_DSP_RESET, NULL);
200 }