Debian Codegraph @

rplayd/audio/audio_sgi.c

raw symbols SCIP
1
2 /* $Id: audio_sgi.c,v 1.4 1999/03/10 07:58:10 boyns Exp $ */
3
4 /*
5 * Copyright (C) 1993-99 Mark R. Boyns <[email protected]>
6 *
7 * This file is part of rplay.
8 *
9 * rplay is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * rplay is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with rplay; see the file COPYING. If not, write to the
21 * Free Software Foundation, Inc.,
22 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
23 */
24
25
26
27 /* SGI audio code was written by Rob Kooper <[email protected]>. */
28
29
30 #include "rplayd.h"
31
32 /*
33 * System audio include files:
34 */
35 #include <sys/types.h>
36 #include <sys/file.h>
37 #include <sys/ioctl.h>
38 #include <sys/fcntl.h>
39 #include <sys/errno.h>
40 #include <audio.h>
41 #include <errno.h>
42
43 /*
44 * External variables:
45 */
46 extern char *rplay_audio_device;
47 extern int rplay_audio_sample_rate;
48 extern int rplay_audio_channels;
49 extern int rplay_audio_precision;
50 extern int rplay_audio_format;
51 extern int rplay_audio_port;
52 extern int optional_sample_rate;
53 extern int optional_precision;
54 extern int optional_channels;
55 extern int optional_format;
56 extern int optional_port;
57
58 /*
59 * Internal variables:
60 */
61 static ALport rplay_audio_fd = NULL;
62
63 /*
64 * Initialize the audio device.
65 * This routine must set the following external variables:
66 * rplay_audio_sample_rate
67 * rplay_audio_precision
68 * rplay_audio_channels
69 * rplay_audio_format
70 * rplay_audio_port
71 *
72 * and may use the following optional parameters:
73 * optional_sample_rate
74 * optional_precision
75 * optional_channels
76 * optional_format
77 * optional_port
78 *
79 * optional_* variables with values of zero should be ignored.
80 *
81 * Return 0 on success and -1 on error.
82 */
83 int
84 rplay_audio_init()
85 {
86 ALconfig SGIconfig;
87 long PVbuffer[4];
88
89 /*
90 * If the cache directory was not found the errno variable is 1. When
91 * calling ALsetwidth errno was not set to 0 when it was OK, and thus
92 * returned an error.
93 */
94 errno = 0;
95
96 /*
97 * Create a new config or use the one of the open port.
98 */
99 if (rplay_audio_fd == NULL)
100 {
101 SGIconfig = ALnewconfig();
102 }
103 else
104 {
105 SGIconfig = ALgetconfig(rplay_audio_fd);
106 }
107 if (SGIconfig == NULL)
108 {
109 return -1;
110 }
111
112 /*
113 * Set the precision and the format. The sgi can also play 24 bits
114 * sample rate. But that is not implemented (yet).
115 */
116 rplay_audio_precision = optional_precision ? optional_precision : 16;
117 switch (rplay_audio_precision)
118 {
119 case 8:
120 ALsetwidth(SGIconfig, AL_SAMPLE_8);
121 if (errno != 0)
122 {
123 return -1;
124 }
125 break;
126 case 16:
127 ALsetwidth(SGIconfig, AL_SAMPLE_16);
128 if (errno != 0)
129 {
130 return -1;
131 }
132 break;
133 default:
134 return -1;
135 }
136
137 /*
138 * The format is linear (twoscomp) or float/double. Since float/double
139 * is not understood by rplay it is left out.
140 */
141 rplay_audio_format = (rplay_audio_precision == 16) ? RPLAY_FORMAT_LINEAR_16 : RPLAY_FORMAT_LINEAR_8;
142 #ifdef AL_SAMPFMT_TWOSCOMP
143 if (ALsetsampfmt(SGIconfig, AL_SAMPFMT_TWOSCOMP) < 0)
144 if (errno != 0)
145 {
146 return -1;
147 }
148 #endif
149
150 /*
151 * The sgi will work in stereo by default. The 4 channel mode of
152 * the indy's is not (yet) supported.
153 */
154 rplay_audio_channels = optional_channels ? optional_channels : 2;
155 switch (rplay_audio_channels)
156 {
157 case 1:
158 ALsetchannels(SGIconfig, AL_MONO);
159 if (errno != 0)
160 {
161 return -1;
162 }
163 break;
164 case 2:
165 ALsetchannels(SGIconfig, AL_STEREO);
166 if (errno != 0)
167 {
168 return -1;
169 }
170 break;
171 default:
172 return -1;
173 }
174
175 /*
176 * Open the audio port.
177 */
178 rplay_audio_fd = ALopenport("rplayd", "w", SGIconfig);
179 if (rplay_audio_fd == NULL)
180 {
181 return -1;
182 }
183 ALfreeconfig(SGIconfig);
184
185 /*
186 * Set the sample rate of the output port. This defaults to 44100Khz
187 * which is CD quality!
188 */
189 rplay_audio_sample_rate = optional_sample_rate ? optional_sample_rate : 44100;
190 PVbuffer[0] = AL_OUTPUT_RATE;
191 PVbuffer[1] = rplay_audio_sample_rate;
192 ALsetparams(AL_DEFAULT_DEVICE, PVbuffer, 2);
193 if (errno != 0)
194 {
195 return -1;
196 }
197
198 /*
199 * Speaker and headphone(line) is the same. There is also digital but
200 * the volume is fixed there. So only speaker is implemented.
201 */
202 rplay_audio_port = RPLAY_AUDIO_PORT_SPEAKER;
203
204 return 0;
205 }
206
207 /*
208 * Open the audio device.
209 *
210 * Return 0 on success and -1 on error.
211 */
212 int
213 rplay_audio_open()
214 {
215 if (rplay_audio_init() < 0)
216 {
217 return -1;
218 }
219
220 return 0;
221 }
222
223 /*
224 * Is the audio device open?
225 *
226 * Return 1 for true and 0 for false.
227 */
228 int
229 rplay_audio_isopen()
230 {
231 return rplay_audio_fd != NULL;
232 }
233
234 /*
235 * Flush the audio device.
236 *
237 * Return 0 on success and -1 on error.
238 */
239 int
240 rplay_audio_flush()
241 {
242 return 0;
243 }
244
245 /*
246 * Write nbytes from buf to the audio device.
247 *
248 * Return the number of bytes written on success and -1 on error.
249 */
250 #ifdef __STDC__
251 int
252 rplay_audio_write(char *buf, int nbytes)
253 #else
254 int
255 rplay_audio_write(buf, nbytes)
256 char *buf;
257 int nbytes;
258 #endif
259 {
260 int samples;
261
262 if (rplay_audio_fd == NULL)
263 {
264 if (rplay_audio_open() < 0)
265 {
266 return -1;
267 }
268 }
269
270 /*
271 * Calculate the number of samples.
272 */
273 samples = nbytes / (rplay_audio_precision >> 3);
274
275 /*
276 * Write the samples to the audio port.
277 */
278 ALwritesamps(rplay_audio_fd, buf, samples);
279 if (errno != 0)
280 {
281 return nbytes;
282 }
283 return -1;
284 }
285
286 /*
287 * Close the audio device.
288 *
289 * Return 0 on success and -1 on error.
290 */
291 int
292 rplay_audio_close()
293 {
294 if (rplay_audio_fd != NULL)
295 {
296 ALcloseport(rplay_audio_fd);
297 }
298
299 rplay_audio_fd = NULL;
300
301 return 0;
302 }
303
304 /*
305 * Return the volume of the audio device.
306 *
307 * Return 0-255 or -1 on error.
308 */
309 int
310 rplay_audio_get_volume()
311 {
312 long PVbuffer[4];
313
314 #ifdef FAKE_VOLUME
315 return rplay_audio_volume;
316 #else /* not FAKE_VOLUME */
317 if (rplay_audio_fd == NULL)
318 {
319 rplay_audio_open();
320 }
321 if (rplay_audio_fd == NULL)
322 {
323 return -1;
324 }
325 PVbuffer[0] = AL_LEFT_SPEAKER_GAIN;
326 PVbuffer[2] = AL_RIGHT_SPEAKER_GAIN;
327 ALgetparams(AL_DEFAULT_DEVICE, PVbuffer, 4);
328 if (errno != 0)
329 {
330 return -1;
331 }
332 return PVbuffer[1];
333 #endif /* not FAKE_VOLUME */
334 }
335
336 /*
337 * Set the volume of the audio device.
338 * Input should be 0-255.
339 *
340 * Return the volume of the audio device 0-255 or -1.
341 */
342 #ifdef __STDC__
343 int
344 rplay_audio_set_volume(int volume)
345 #else
346 int
347 rplay_audio_set_volume(volume)
348 int volume;
349 #endif
350 {
351 long PVbuffer[4];
352
353 #ifdef FAKE_VOLUME
354 if (volume < RPLAY_MIN_VOLUME)
355 {
356 volume = RPLAY_MIN_VOLUME;
357 }
358 else if (volume > RPLAY_MAX_VOLUME)
359 {
360 volume = RPLAY_MAX_VOLUME;
361 }
362 rplay_audio_volume = volume;
363
364 return rplay_audio_volume;
365 #else /* not FAKE_VOLUME */
366
367 rplay_audio_volume = 0;
368
369 if (rplay_audio_fd == NULL)
370 {
371 rplay_audio_open();
372 }
373 if (rplay_audio_fd == NULL)
374 {
375 return -1;
376 }
377 PVbuffer[0] = AL_LEFT_SPEAKER_GAIN;
378 PVbuffer[1] = volume;
379 PVbuffer[2] = AL_RIGHT_SPEAKER_GAIN;
380 PVbuffer[3] = volume;
381 ALsetparams(AL_DEFAULT_DEVICE, PVbuffer, 4);
382 if (errno != 0)
383 {
384 return -1;
385 }
386
387 rplay_audio_volume = rplay_audio_get_volume();
388
389 return rplay_audio_volume;
390 #endif /* not FAKE_VOLUME */
391 }