Debian Codegraph @

rplayd/audio/audio_gnu.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
•1 /* $Id: audio_generic.c,v 1.5 1999/03/10 07:58:10 boyns Exp $ */
2
3 /*
4 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
5 *
6 * This file is part of rplay.
7 *
8 * rplay is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * rplay is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with rplay; see the file COPYING. If not, write to the
20 * Free Software Foundation, Inc.,
21 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22 */
23
24
25
26 #include "rplayd.h"
27
28 /*
29 * System audio include files:
30 */
31 #include <sys/types.h>
32 #include <sys/file.h>
33 #include <sys/ioctl.h>
34 #ifdef ultrix
35 #include <fcntl.h>
36 #else
37 #include <sys/fcntl.h>
38 #endif
39 #include <sys/errno.h>
40
41 /*
42 * External variables:
43 */
44 extern char *rplay_audio_device;
45 extern int rplay_audio_sample_rate;
46 extern int rplay_audio_channels;
47 extern int rplay_audio_precision;
48 extern int rplay_audio_format;
49 extern int rplay_audio_port;
50 extern int optional_sample_rate;
51 extern int optional_precision;
52 extern int optional_channels;
53 extern int optional_format;
54 extern int optional_port;
55
56 /*
57 * Internal variables:
58 */
59 static int rplay_audio_fd = -1;
60
61 static RPLAY_AUDIO_TABLE generic_table[] =
62 {
63 {8000, RPLAY_FORMAT_ULAW, 1, 1},
64 {0, 0, 0, 0}
65 };
66
67 /*
68 * Initialize the audio device.
69 * This routine must set the following external variables:
70 * rplay_audio_sample_rate
71 * rplay_audio_precision
72 * rplay_audio_channels
73 * rplay_audio_format
74 * rplay_audio_port
75 *
76 * and may use the following optional parameters:
77 * optional_sample_rate
78 * optional_precision
79 * optional_channels
80 * optional_format
81 * optional_port
82 *
83 * optional_* variables with values of zero should be ignored.
84 *
85 * Return 0 on success and -1 on error.
86 */
87 int
88 rplay_audio_init()
89 {
90 if (rplay_audio_fd == -1)
91 {
92 rplay_audio_open();
93 if (rplay_audio_fd == -1)
94 {
95 report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n",
96 rplay_audio_device);
97 return -1;
98 }
99 }
100
101 rplay_audio_sample_rate = 8000;
102 rplay_audio_precision = 8;
103 rplay_audio_channels = 1;
104 rplay_audio_format = RPLAY_FORMAT_ULAW;
105 rplay_audio_port = RPLAY_AUDIO_PORT_SPEAKER;
106 rplay_audio_table = generic_table;
107
108 return 0;
109 }
110
111 /*
112 * Open the audio device.
113 *
114 * Return 0 on success and -1 on error.
115 */
116 int
117 rplay_audio_open()
118 {
119 int flags;
120
121 rplay_audio_fd = open(rplay_audio_device, O_WRONLY | O_NDELAY, 0);
122 if (rplay_audio_fd < 0)
123 {
124 return -1;
125 }
126
127 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
128 {
129 report(REPORT_ERROR,
130 "rplay_audio_open: close-on-exec %d\n",
131 sys_err_str(errno));
132 /* return -1; */
133 }
134
135 if (rplay_audio_init() < 0)
136 {
137 return -1;
138 }
139
140 /*
141 * Make sure the audio device writes are non-blocking.
142 */
143 flags = fcntl(rplay_audio_fd, F_GETFL, 0);
144 if (flags < 0)
145 {
146 return -1;
147 }
148 flags |= FNDELAY;
149 if (fcntl(rplay_audio_fd, F_SETFL, flags) < 0)
150 {
151 return -1;
152 }
153
154 return 0;
155 }
156
157 /*
158 * Is the audio device open?
159 *
160 * Return 1 for true and 0 for false.
161 */
162 int
163 rplay_audio_isopen()
164 {
165 return rplay_audio_fd != -1;
166 }
167
168 /*
169 * Flush the audio device.
170 *
171 * Return 0 on success and -1 on error.
172 */
173 int
174 rplay_audio_flush()
175 {
176 return 0;
177 }
178
179 /*
180 * Write nbytes from buf to the audio device.
181 *
182 * Return the number of bytes written on success and -1 on error.
183 */
184 #ifdef __STDC__
185 int
186 rplay_audio_write(char *buf, int nbytes)
187 #else
188 int
189 rplay_audio_write(buf, nbytes)
190 char *buf;
191 int nbytes;
192 #endif
193 {
194 int n, nleft, nwritten;
195 char *p;
196
197 nleft = nbytes;
198 nwritten = 0;
199
200 for (p = buf; nleft > 0; nleft -= n, p += n)
201 {
202 n = write(rplay_audio_fd, p, nleft);
203 if (n < 0)
204 {
205 if (errno == EWOULDBLOCK)
206 {
207 return nwritten;
208 }
209 else if (errno != EINTR)
210 {
211 return -1;
212 }
213 n = 0;
214 }
215 else
216 {
217 nwritten += n;
218 }
219 }
220
221 return nwritten;
222 }
223
224 /*
225 * Close the audio device.
226 *
227 * Return 0 on success and -1 on error.
228 */
229 int
230 rplay_audio_close()
231 {
232 if (rplay_audio_fd != -1)
233 {
234 close(rplay_audio_fd);
235 }
236
237 rplay_audio_fd = -1;
238
239 return 0;
240 }
241
242 /*
243 * Return the volume of the audio device.
244 *
245 * Return 0-255 or -1 on error.
246 */
247 int
248 rplay_audio_get_volume()
249 {
250 #ifdef FAKE_VOLUME
251 return rplay_audio_volume;
252 #else /* not FAKE_VOLUME */
253 return -1;
254 #endif /* not FAKE_VOLUME */
255 }
256
257 /*
258 * Set the volume of the audio device.
259 * Input should be 0-255.
260 *
261 * Return the volume of the audio device 0-255 or -1.
262 */
263 #ifdef __STDC__
264 int
265 rplay_audio_set_volume(int volume)
266 #else
267 int
268 rplay_audio_set_volume(volume)
269 int volume;
270 #endif
271 {
272 #ifdef FAKE_VOLUME
273 if (volume < RPLAY_MIN_VOLUME)
274 {
275 volume = RPLAY_MIN_VOLUME;
276 }
277 else if (volume > RPLAY_MAX_VOLUME)
278 {
279 volume = RPLAY_MAX_VOLUME;
280 }
281 rplay_audio_volume = volume;
282
283 return rplay_audio_volume;
284 #else /* not FAKE_VOLUME */
285 return -1;
286 #endif /* not FAKE_VOLUME */
287 }