Debian Codegraph @

rplayd/audio/audio_FreeBSD.c

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /* $Id: audio_FreeBSD.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 /* FreeBSD audio code was written by Andreas S. Wetzel <[email protected]>. */
27
28 #include "rplayd.h"
29
30 /*
31 * System audio include files:
32 */
33 #include <sys/types.h>
34 #include <sys/file.h>
35 #include <sys/ioctl.h>
36 #include <sys/errno.h>
37 #include <errno.h>
•38 #include <sys/soundcard.h>
39
40 /*
41 * for the poor folks who don't have a sound card, but use the pc speaker
42 * driver pcsndrv version 0.6 (no SNDCTL_DSP_POST ....yet)
43 */
44 #ifndef SNDCTL_DSP_POST
45 #define SNDCTL_DSP_POST SNDCTL_DSP_SYNC
46 #endif
47
48 /*
49 * External variables:
50 */
51 extern char *rplay_audio_device;
52 extern int rplay_audio_sample_rate;
53 extern int rplay_audio_channels;
54 extern int rplay_audio_precision;
55 extern int rplay_audio_format;
56 extern int rplay_audio_port;
57 extern int optional_sample_rate;
58 extern int optional_precision;
59 extern int optional_channels;
60 extern int optional_format;
61 extern int optional_port;
62
63 extern int max_rplay_audio_bufsize;
64
65 /*
66 * Internal variables:
67 */
68 int rplay_audio_fd = -1;
69
70 /*
71 * Initialize the audio device.
72 * This routine must set the following external variables:
73 * rplay_audio_sample_rate
74 * rplay_audio_precision
75 * rplay_audio_channels
76 * rplay_audio_format
77 *
78 * and may use the following optional parameters:
79 * optional_sample_rate
80 * optional_precision
81 * optional_channels
82 * optional_format
83 *
84 * optional_* variables with values of zero should be ignored.
85 *
86 * Return 0 on success and -1 on error.
87 */
88
89 int
90 rplay_audio_init(void)
91 {
92 int n;
93
94 if (rplay_audio_fd == -1)
95 {
96 if (rplay_audio_open() == -1)
97 {
98 report(REPORT_ERROR, "rplay_audio_init: cannot open %s\n", rplay_audio_device);
99 return -1;
100 }
101 }
102
103 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 44100;
104 rplay_audio_precision = optional_precision ? optional_precision : 16;
105 rplay_audio_channels = optional_channels ? optional_channels : 2;
106 rplay_audio_format = optional_format ? optional_format :
107 rplay_audio_precision == 16 ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_ULINEAR_8;
108
109 /*
110 * Set dsp sample size
111 */
112 n = rplay_audio_precision;
113
114 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SAMPLESIZE, &n) == -1)
115 {
116 report(REPORT_ERROR, "rplay_audio_init: can't set audio precision to %d (%d)\n",
117 rplay_audio_precision, n);
118 return -1;
119 }
120
121 if (n != rplay_audio_precision)
122 {
123 report(REPORT_NOTICE, "rplay_audio_init: audio precision changed from %d to %d\n",
124 rplay_audio_precision, n);
125 rplay_audio_precision = n;
126 }
127
128 /*
129 * Set # of channels
130 */
131 n = rplay_audio_channels;
132
133 if (ioctl(rplay_audio_fd, SOUND_PCM_WRITE_CHANNELS, &n) == -1)
134 {
135 report(REPORT_ERROR, "rplay_audio_init: can't set audio channels to %d (%d)\n",
136 rplay_audio_channels, n);
137 return -1;
138 }
139
140 if (n != rplay_audio_channels)
141 {
142 report(REPORT_NOTICE, "rplay_audio_init: audio channels changed from %d to %d\n",
143 rplay_audio_channels, n);
144 rplay_audio_channels = n;
145 }
146
147 /*
148 * Set the sampling rate
149 */
150
151 n = rplay_audio_sample_rate;
152
153 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SPEED, &n) == -1)
154 {
155 report(REPORT_ERROR, "rplay_audio_init: can't set audio sample rate to %d (%d)\n",
156 rplay_audio_sample_rate, n);
157 return (-1);
158 }
159
160 if (n != rplay_audio_sample_rate)
161 {
162 report(REPORT_NOTICE, "rplay_audio_init: audio sample rate changed from %d to %d\n",
163 rplay_audio_sample_rate, n);
164 rplay_audio_sample_rate = n;
165 }
166
167 /*
168 * Set the data format
169 */
170 switch (rplay_audio_format)
171 {
172 case RPLAY_FORMAT_NONE:
173 break; /* ??? */
174 case RPLAY_FORMAT_LINEAR_8:
175 n = AFMT_S8;
176 break;
177 case RPLAY_FORMAT_ULINEAR_8:
178 n = AFMT_U8;
179 break;
180 case RPLAY_FORMAT_LINEAR_16:
181 n = AFMT_S16_LE;
182 break;
183 case RPLAY_FORMAT_ULINEAR_16:
184 n = AFMT_U16_LE;
185 break;
186 case RPLAY_FORMAT_ULAW:
187 n = AFMT_MU_LAW;
188 break;
189 case RPLAY_FORMAT_G721:
190 case RPLAY_FORMAT_G723_3:
191 case RPLAY_FORMAT_G723_5:
192 n = AFMT_IMA_ADPCM;
193 break;
194 default:
195 report(REPORT_ERROR, "rplay_audio_init: unknown audio format (%d)\n", rplay_audio_format);
196 return (-1);
197 }
198
199 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SETFMT, &n) == -1)
200 {
201 report(REPORT_ERROR, "rplay_audio_init: can't set audio format\n");
202 return (-1);
203 }
204
205 /*
206 * Set the audio blocksize
207 */
208 if (n = curr_bufsize)
209 {
210 if (ioctl(rplay_audio_fd, SNDCTL_DSP_SETBLKSIZE, &n) == -1)
211 {
212 report(REPORT_ERROR, "rplay_audio_init: can't set audio blocksize to %d\n", curr_bufsize);
213 return (-1);
214 }
215 report(REPORT_DEBUG, "rplay_audio_init: device blksize set to %d\n", n);
216 }
217
218 rplay_audio_port = RPLAY_AUDIO_PORT_SPEAKER;
219
220 return 0;
221 }
222
223 /*
224 * Open the audio device.
225 *
226 * Return 0 on success and -1 on error.
227 */
228 int
229 rplay_audio_open(void)
230 {
231 if ((rplay_audio_fd = open(rplay_audio_device, (O_WRONLY | O_NONBLOCK), 0)) < 0)
232 return -1;
233
234 if (fcntl(rplay_audio_fd, F_SETFD, 1) < 0)
235 {
236 report(REPORT_ERROR,
237 "rplay_audio_open: close-on-exec %d\n",
238 sys_err_str(errno));
239 /* return -1; */
240 }
241
242 if (rplay_audio_init() < 0)
243 return -1;
244
245 return 0;
246 }
247
248 /*
249 * Is the audio device open?
250 *
251 * Return 1 for true and 0 for false.
252 */
253 int
254 rplay_audio_isopen(void)
255 {
256 return (rplay_audio_fd != -1);
257 }
258
259 /*
260 * Flush the audio device.
261 *
262 * Return 0 on success and -1 on error.
263 */
264
265 int
266 rplay_audio_flush(void)
267 {
268 if (rplay_audio_fd != -1)
269 return (ioctl(rplay_audio_fd, SNDCTL_DSP_POST, 0));
270
271 return 0;
272 }
273
274 /*
275 * Write nbytes from buf to the audio device.
276 *
277 * Return bytes written on success and -1 on error.
278 */
279 int
280 rplay_audio_write(char *buf, int nbytes)
281 {
282 int remain = nbytes;
283 int xr;
284
285 while (remain > 0)
286 {
287 if ((xr = write(rplay_audio_fd, buf, remain)) == -1)
288 {
289 switch (errno)
290 {
291 case EWOULDBLOCK:
292 continue;
293 default:
294 report(REPORT_ERROR, "Error while writing to audio device (%s)\n", strerror(errno));
295 return (-1);
296 }
297 }
298 else
299 {
300 remain -= xr;
301 }
302 }
303
304 return (nbytes);
305 }
306
307 /*
308 * Close the audio device.
309 *
310 * Return 0 on success and -1 on error.
311 */
312 int
313 rplay_audio_close(void)
314 {
315 if (rplay_audio_fd != -1)
316 {
317 close(rplay_audio_fd);
318 }
319
320 rplay_audio_fd = -1;
321
322 return 0;
323 }
324
325 /*
326 * Return the volume of the audio device.
327 *
328 * Return 0-255 or -1 on error.
329 */
330 int
331 rplay_audio_get_volume(void)
332 {
333 #ifndef FAKE_VOLUME
334
335 int mx;
336 int mxdevmask;
337 int left_vol;
338 int right_vol;
339 int vol;
340
341 vol = left_vol = right_vol = 0;
342
343 if ((mx = open(RPLAY_MIXER_DEVICE, O_RDONLY)) == -1)
344 {
345 report(REPORT_ERROR, "rplay_audio_get_volume: unable to open mixer device\n");
346 return (-1);
347 }
348
349 if (ioctl(mx, SOUND_MIXER_READ_DEVMASK, &mxdevmask) == -1)
350 {
351 report(REPORT_ERROR, "rplay_audio_get_volume: unable to get mixer device mask\n");
352 close(mx);
353 return (-1);
354 }
355
356 if (!(mxdevmask & SOUND_MIXER_PCM))
357 {
358 report(REPORT_ERROR, "rplay_audio_get_volume: pcm mixer device not installed\n");
359 close(mx);
360 return (-1);
361 }
362
363 if (ioctl(mx, SOUND_MIXER_READ_PCM, &vol) == -1)
364 {
365 report(REPORT_ERROR, "rplay_audio_get_volume: unable to get mixer volume\n");
366 close(mx);
367 return (-1);
368 }
369 else
370 {
371 left_vol = (int) ((vol & 0x00ff) * 2.55);
372 right_vol = (int) (((vol & 0xff00) >> 8) * 2.55);
373
374 vol = (int) ((left_vol + right_vol) / 2);
375
376 report(REPORT_DEBUG, "current pcm-volume: %d:%d => %d\n", left_vol, right_vol, vol);
377 }
378
379 close(mx);
380
381 return (vol);
382
383 #else
384 return rplay_audio_volume;
385 #endif
386 }
387
388 /*
389 * Set the volume of the audio device.
390 * Input should be 0-255.
391 *
392 * Return the volume of the audio device 0-255 or -1.
393 */
394 int
395 rplay_audio_set_volume(int volume)
396 {
397 #ifndef FAKE_VOLUME
398
399 int mx;
400 int mxdevmask;
401 int left_vol;
402 int right_vol;
403 int vol = (int) (volume / 2.55);
404
405 vol |= ((vol & 0x00ff) << 8);
406
407 if ((mx = open(RPLAY_MIXER_DEVICE, O_RDONLY)) == -1)
408 {
409 report(REPORT_ERROR, "rplay_audio_set_volume: unable to open mixer device\n");
410 return (-1);
411 }
412
413 if (ioctl(mx, SOUND_MIXER_READ_DEVMASK, &mxdevmask) == -1)
414 {
415 report(REPORT_ERROR, "rplay_audio_set_volume: unable to get mixer device mask\n");
416 close(mx);
417 return (-1);
418 }
419
420 if (!(mxdevmask & SOUND_MIXER_PCM))
421 {
422 report(REPORT_ERROR, "rplay_audio_set_volume: pcm mixer device not installed\n");
423 close(mx);
424 return (-1);
425 }
426
427 if (ioctl(mx, SOUND_MIXER_WRITE_PCM, &vol) == -1)
428 {
429 report(REPORT_ERROR, "rplay_audio_set_volume: unable to set mixer volume\n");
430 close(mx);
431 return (-1);
432 }
433 else
434 {
435 left_vol = (int) ((vol & 0x00ff) * 2.55);
436 right_vol = (int) (((vol & 0xff00) >> 8) * 2.55);
437
438 vol = (int) ((left_vol + right_vol) / 2);
439
440 report(REPORT_DEBUG, "rplay_audio_set_volume: pcm-volume set to %d:%d => %d\n",
441 left_vol, right_vol, vol);
442 }
443
444 close(mx);
445
446 rplay_audio_volume = vol;
447
448 return rplay_audio_volume;
449
450 #else
451
452 if (volume < RPLAY_MIN_VOLUME)
453 {
454 volume = RPLAY_MIN_VOLUME;
455 }
456 else if (volume > RPLAY_MAX_VOLUME)
457 {
458 volume = RPLAY_MAX_VOLUME;
459 }
460 rplay_audio_volume = volume;
461
462 return rplay_audio_volume;
463
464 #endif
465 }