428
|
1 /* Play sound using the SGI audio library
|
|
2 written by Simon Leinen <simon@lia.di.epfl.ch>
|
|
3 Copyright (C) 1992 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
|
6
|
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
|
11
|
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
|
16
|
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
|
22 /* Synched up with: Not in FSF. */
|
|
23
|
563
|
24 /* This file Mule-ized by Ben Wing, 5-15-01. */
|
|
25
|
609
|
26 #define DONT_ENCAPSULATE
|
|
27
|
428
|
28 #include <config.h>
|
|
29 #include "lisp.h"
|
|
30
|
563
|
31 #include "sound.h"
|
|
32
|
|
33 #include "sysfile.h"
|
|
34 #include "sysproc.h" /* netinet/in.h for ntohl() etc. */
|
428
|
35
|
609
|
36 #include <audio.h>
|
|
37
|
428
|
38 /* Configuration options */
|
|
39
|
|
40 /* ability to parse Sun/NeXT (.au or .snd) audio file headers. The
|
|
41 .snd format supports all sampling rates and sample widths that are
|
|
42 commonly used, as well as stereo. It is also easy to parse. */
|
|
43 #ifndef HAVE_SND_FILES
|
|
44 #define HAVE_SND_FILES 1
|
|
45 #endif
|
|
46
|
|
47 /* support for eight-but mu-law encoding. This is a useful compaction
|
|
48 technique, and most sounds from the Sun universe are in this
|
|
49 format. */
|
|
50 #ifndef HAVE_MULAW_8
|
|
51 #define HAVE_MULAW_8 1
|
|
52 #endif
|
|
53
|
|
54 /* if your machine is very slow, you have to use a table lookup to
|
|
55 convert mulaw samples to linear. This makes Emacs bigger so try to
|
|
56 avoid it. */
|
|
57 #ifndef USE_MULAW_DECODE_TABLE
|
|
58 #define USE_MULAW_DECODE_TABLE 0
|
|
59 #endif
|
|
60
|
|
61 /* support for linear encoding -- useful if you want better quality.
|
|
62 This enables 8, 16 and 24 bit wide samples. */
|
|
63 #ifndef HAVE_LINEAR
|
|
64 #define HAVE_LINEAR 1
|
|
65 #endif
|
|
66
|
|
67 /* support for 32 bit wide samples. If you notice the difference
|
|
68 between 32 and 24 bit samples, you must have very good ears. Since
|
|
69 the SGI audio library only supports 24 bit samples, each sample has
|
|
70 to be shifted right by 8 bits anyway. So you should probably just
|
|
71 convert all your 32 bit audio files to 24 bit. */
|
|
72 #ifndef HAVE_LINEAR_32
|
|
73 #define HAVE_LINEAR_32 0
|
|
74 #endif
|
|
75
|
|
76 /* support for stereo sound. Imagine the cool applications of this:
|
|
77 finally you don't just hear a beep -- you also know immediately
|
|
78 *where* something went wrong! Unfortunately the programming
|
|
79 interface only takes a single volume argument so far. */
|
|
80 #ifndef HAVE_STEREO
|
|
81 #define HAVE_STEREO 1
|
|
82 #endif
|
|
83
|
|
84 /* the play routine can be interrupted between chunks, so we choose a
|
|
85 small chunksize to keep the system responsive (2000 samples
|
|
86 correspond to a quarter of a second for .au files. If you
|
|
87 HAVE_STEREO, the chunksize should probably be even. */
|
|
88 #define CHUNKSIZE 8000
|
|
89
|
|
90 /* the format assumed for header-less audio data. The following
|
|
91 assumes ".au" format (8000 samples/sec mono 8-bit mulaw). */
|
|
92 #define DEFAULT_SAMPLING_RATE 8000
|
|
93 #define DEFAULT_CHANNEL_COUNT 1
|
|
94 #define DEFAULT_FORMAT AFmulaw8
|
|
95
|
|
96 /* Data structures */
|
|
97
|
|
98 /* an AudioContext describes everything we want to know about how a
|
|
99 particular sound snippet should be played. It is split into three
|
|
100 parts (device, port and buffer) for implementation reasons. The
|
|
101 device part corresponds to the state of the output device and must
|
|
102 be reverted after playing the samples. The port part corresponds
|
|
103 to an ALport; we want to allocate a minimal number of these since
|
|
104 there are only four of them system-wide, but on the other hand we
|
|
105 can't use the same port for mono and stereo. The buffer part
|
|
106 corresponds to the sound data itself. */
|
|
107
|
|
108 typedef struct _AudioContextRec * AudioContext;
|
|
109
|
|
110 typedef struct
|
|
111 {
|
|
112 long device;
|
|
113 int left_speaker_gain;
|
|
114 int right_speaker_gain;
|
|
115 long output_rate;
|
|
116 }
|
|
117 AudioDeviceRec, * AudioDevice;
|
|
118
|
|
119 /* supported sound data formats */
|
|
120
|
|
121 typedef enum
|
|
122 {
|
|
123 AFunknown,
|
|
124 #if HAVE_MULAW_8
|
|
125 AFmulaw8,
|
|
126 #endif
|
|
127 #if HAVE_LINEAR
|
|
128 AFlinear8,
|
|
129 AFlinear16,
|
|
130 AFlinear24,
|
|
131 #if HAVE_LINEAR_32
|
|
132 AFlinear32,
|
|
133 #endif
|
|
134 #endif
|
|
135 AFillegal
|
|
136 }
|
|
137 AudioFormat;
|
|
138
|
|
139 typedef struct
|
|
140 {
|
|
141 ALport port;
|
|
142 AudioFormat format;
|
|
143 unsigned nchan;
|
|
144 unsigned queue_size;
|
|
145 }
|
|
146 AudioPortRec, * AudioPort;
|
|
147
|
|
148 typedef struct
|
|
149 {
|
|
150 void * data;
|
|
151 unsigned long size;
|
|
152 void (* write_chunk_function) (void *, void *, AudioContext);
|
|
153 }
|
|
154 AudioBufferRec, * AudioBuffer;
|
|
155
|
|
156 typedef struct _AudioContextRec
|
|
157 {
|
|
158 AudioDeviceRec device;
|
|
159 AudioPortRec port;
|
|
160 AudioBufferRec buffer;
|
|
161 }
|
|
162 AudioContextRec;
|
|
163
|
|
164 #define ac_device device.device
|
|
165 #define ac_left_speaker_gain device.left_speaker_gain
|
|
166 #define ac_right_speaker_gain device.right_speaker_gain
|
|
167 #define ac_output_rate device.output_rate
|
|
168 #define ac_port port.port
|
|
169 #define ac_format port.format
|
|
170 #define ac_nchan port.nchan
|
|
171 #define ac_queue_size port.queue_size
|
|
172 #define ac_data buffer.data
|
|
173 #define ac_size buffer.size
|
|
174 #define ac_write_chunk_function buffer.write_chunk_function
|
|
175
|
|
176 /* Forward declarations */
|
|
177
|
|
178 static Lisp_Object close_sound_file (Lisp_Object);
|
563
|
179 static AudioContext audio_initialize (UChar_Binary *, int, int);
|
609
|
180 static int play_internal (UChar_Binary *, int, AudioContext);
|
428
|
181 static void drain_audio_port (AudioContext);
|
|
182 static void write_mulaw_8_chunk (void *, void *, AudioContext);
|
|
183 static void write_linear_chunk (void *, void *, AudioContext);
|
|
184 static void write_linear_32_chunk (void *, void *, AudioContext);
|
|
185 static Lisp_Object restore_audio_port (Lisp_Object);
|
|
186 static AudioContext initialize_audio_port (AudioContext);
|
|
187 static int open_audio_port (AudioContext, AudioContext);
|
|
188 static void adjust_audio_volume (AudioDevice);
|
|
189 static void get_current_volumes (AudioDevice);
|
|
190 static int set_channels (ALconfig, unsigned);
|
|
191 static int set_output_format (ALconfig, AudioFormat);
|
|
192 static int parse_snd_header (void*, long, AudioContext);
|
|
193
|
|
194 /* are we looking at an NeXT/Sun audio header? */
|
|
195 #define LOOKING_AT_SND_HEADER_P(address) \
|
|
196 (!strncmp(".snd", (char *)(address), 4))
|
|
197
|
|
198 static Lisp_Object
|
|
199 close_sound_file (Lisp_Object closure)
|
|
200 {
|
|
201 close (XINT (closure));
|
|
202 return Qnil;
|
|
203 }
|
|
204
|
|
205 void
|
563
|
206 play_sound_file (Extbyte *sound_file, int volume)
|
428
|
207 {
|
|
208 int count = specpdl_depth ();
|
|
209 int input_fd;
|
563
|
210 UChar_Binary buffer[CHUNKSIZE];
|
428
|
211 int bytes_read;
|
|
212 AudioContext ac = (AudioContext) 0;
|
|
213
|
|
214 input_fd = open (sound_file, O_RDONLY);
|
|
215 if (input_fd == -1)
|
|
216 /* no error message -- this can't happen
|
|
217 because Fplay_sound_file has checked the
|
|
218 file for us. */
|
|
219 return;
|
|
220
|
|
221 record_unwind_protect (close_sound_file, make_int (input_fd));
|
|
222
|
|
223 while ((bytes_read = read (input_fd, buffer, CHUNKSIZE)) > 0)
|
|
224 {
|
|
225 if (ac == (AudioContext) 0)
|
|
226 {
|
|
227 ac = audio_initialize (buffer, bytes_read, volume);
|
|
228 if (ac == 0)
|
|
229 return;
|
|
230 }
|
|
231 else
|
|
232 {
|
|
233 ac->ac_data = buffer;
|
|
234 ac->ac_size = bytes_read;
|
|
235 }
|
|
236 play_internal (buffer, bytes_read, ac);
|
|
237 }
|
|
238 drain_audio_port (ac);
|
|
239 unbind_to (count, Qnil);
|
|
240 }
|
|
241
|
|
242 static long
|
|
243 saved_device_state[] = {
|
|
244 AL_OUTPUT_RATE, 0,
|
|
245 AL_LEFT_SPEAKER_GAIN, 0,
|
|
246 AL_RIGHT_SPEAKER_GAIN, 0,
|
|
247 };
|
|
248
|
|
249 static Lisp_Object
|
|
250 restore_audio_port (Lisp_Object closure)
|
|
251 {
|
|
252 Lisp_Object * contents = XVECTOR_DATA (closure);
|
|
253 saved_device_state[1] = XINT (contents[0]);
|
|
254 saved_device_state[3] = XINT (contents[1]);
|
|
255 saved_device_state[5] = XINT (contents[2]);
|
|
256 ALsetparams (AL_DEFAULT_DEVICE, saved_device_state, 6);
|
|
257 return Qnil;
|
|
258 }
|
|
259
|
609
|
260 int
|
563
|
261 play_sound_data (UChar_Binary *data, int length, int volume)
|
428
|
262 {
|
|
263 int count = specpdl_depth ();
|
|
264 AudioContext ac;
|
609
|
265 int result;
|
428
|
266
|
|
267 ac = audio_initialize (data, length, volume);
|
|
268 if (ac == (AudioContext) 0)
|
609
|
269 return 0;
|
|
270 result = play_internal (data, length, ac);
|
428
|
271 drain_audio_port (ac);
|
|
272 unbind_to (count, Qnil);
|
609
|
273 return result;
|
428
|
274 }
|
|
275
|
|
276 static AudioContext
|
563
|
277 audio_initialize (UChar_Binary *data, int length, int volume)
|
428
|
278 {
|
|
279 Lisp_Object audio_port_state[3];
|
|
280 static AudioContextRec desc;
|
|
281 AudioContext ac;
|
|
282
|
|
283 desc.ac_right_speaker_gain
|
|
284 = desc.ac_left_speaker_gain
|
|
285 = volume * 256 / 100;
|
|
286 desc.ac_device = AL_DEFAULT_DEVICE;
|
|
287
|
|
288 #if HAVE_SND_FILES
|
|
289 if (LOOKING_AT_SND_HEADER_P (data))
|
|
290 {
|
|
291 if (parse_snd_header (data, length, & desc)==-1)
|
563
|
292 report_sound_error ("decoding .snd header", Qunbound);
|
428
|
293 }
|
|
294 else
|
|
295 #endif
|
|
296 {
|
|
297 desc.ac_data = data;
|
|
298 desc.ac_size = length;
|
|
299 desc.ac_output_rate = DEFAULT_SAMPLING_RATE;
|
|
300 desc.ac_nchan = DEFAULT_CHANNEL_COUNT;
|
|
301 desc.ac_format = DEFAULT_FORMAT;
|
|
302 desc.ac_write_chunk_function = write_mulaw_8_chunk;
|
|
303 }
|
|
304
|
|
305 /* Make sure that the audio port is reset to
|
|
306 its initial characteristics after exit */
|
|
307 ALgetparams (desc.ac_device, saved_device_state,
|
|
308 sizeof (saved_device_state) / sizeof (long));
|
|
309 audio_port_state[0] = make_int (saved_device_state[1]);
|
|
310 audio_port_state[1] = make_int (saved_device_state[3]);
|
|
311 audio_port_state[2] = make_int (saved_device_state[5]);
|
|
312 record_unwind_protect (restore_audio_port,
|
|
313 Fvector (3, &audio_port_state[0]));
|
|
314
|
|
315 ac = initialize_audio_port (& desc);
|
|
316 desc = * ac;
|
|
317 return ac;
|
|
318 }
|
|
319
|
609
|
320 static int
|
563
|
321 play_internal (UChar_Binary *data, int length, AudioContext ac)
|
428
|
322 {
|
563
|
323 UChar_Binary * limit;
|
428
|
324 if (ac == (AudioContext) 0)
|
609
|
325 return 0;
|
428
|
326
|
563
|
327 data = (UChar_Binary *) ac->ac_data;
|
428
|
328 limit = data + ac->ac_size;
|
|
329 while (data < limit)
|
|
330 {
|
563
|
331 UChar_Binary * chunklimit = data + CHUNKSIZE;
|
428
|
332
|
|
333 if (chunklimit > limit)
|
|
334 chunklimit = limit;
|
|
335
|
|
336 QUIT;
|
|
337
|
|
338 (* ac->ac_write_chunk_function) (data, chunklimit, ac);
|
|
339 data = chunklimit;
|
|
340 }
|
609
|
341
|
|
342 return 1;
|
428
|
343 }
|
|
344
|
|
345 static void
|
|
346 drain_audio_port (AudioContext ac)
|
|
347 {
|
|
348 while (ALgetfilled (ac->ac_port) > 0)
|
|
349 sginap(1);
|
|
350 }
|
|
351
|
|
352 /* Methods to write a "chunk" from a buffer containing audio data to
|
|
353 an audio port. This may involve some conversion if the output
|
|
354 device doesn't directly support the format the audio data is in. */
|
|
355
|
|
356 #if HAVE_MULAW_8
|
|
357
|
|
358 #if USE_MULAW_DECODE_TABLE
|
|
359 #include "libst.h"
|
|
360 #else /* not USE_MULAW_DECODE_TABLE */
|
|
361 static int
|
|
362 st_ulaw_to_linear (int u)
|
|
363 {
|
442
|
364 static const short table[] = {0,132,396,924,1980,4092,8316,16764};
|
428
|
365 int u1 = ~u;
|
|
366 short exponent = (u1 >> 4) & 0x07;
|
|
367 int mantissa = u1 & 0x0f;
|
|
368 int unsigned_result = table[exponent]+(mantissa << (exponent+3));
|
|
369 return u1 & 0x80 ? -unsigned_result : unsigned_result;
|
|
370 }
|
|
371 #endif /* not USE_MULAW_DECODE_TABLE */
|
|
372
|
|
373 static void
|
|
374 write_mulaw_8_chunk (void *buffer, void *chunklimit, AudioContext ac)
|
|
375 {
|
563
|
376 UChar_Binary * data = (UChar_Binary *) buffer;
|
|
377 UChar_Binary * limit = (UChar_Binary *) chunklimit;
|
428
|
378 short * obuf, * bufp;
|
|
379 long n_samples = limit - data;
|
|
380
|
|
381 obuf = alloca_array (short, n_samples);
|
|
382 bufp = &obuf[0];
|
|
383
|
|
384 while (data < limit)
|
|
385 *bufp++ = st_ulaw_to_linear (*data++);
|
|
386 ALwritesamps (ac->ac_port, obuf, n_samples);
|
|
387 }
|
|
388 #endif /* HAVE_MULAW_8 */
|
|
389
|
|
390 #if HAVE_LINEAR
|
|
391 static void
|
|
392 write_linear_chunk (void *data, void *limit, AudioContext ac)
|
|
393 {
|
|
394 unsigned n_samples;
|
|
395
|
|
396 switch (ac->ac_format)
|
|
397 {
|
|
398 case AFlinear16: n_samples = (short *) limit - (short *) data; break;
|
563
|
399 case AFlinear8: n_samples = (Char_Binary *) limit - (Char_Binary *) data; break;
|
428
|
400 default: n_samples = (long *) limit - (long *) data; break;
|
|
401 }
|
|
402 ALwritesamps (ac->ac_port, data, (long) n_samples);
|
|
403 }
|
|
404
|
|
405 #if HAVE_LINEAR_32
|
|
406 static void
|
|
407 write_linear_32_chunk (void *buffer, void *chunklimit, AudioContext ac)
|
|
408 {
|
|
409 long * data = (long *) buffer;
|
|
410 long * limit = (long *) chunklimit;
|
|
411 long * obuf, * bufp;
|
|
412 long n_samples = limit-data;
|
|
413
|
|
414 obuf = alloca_array (long, n_samples);
|
|
415 bufp = &obuf[0];
|
|
416
|
|
417 while (data < limit)
|
|
418 *bufp++ = *data++ >> 8;
|
|
419 ALwritesamps (ac->ac_port, obuf, n_samples);
|
|
420 }
|
|
421 #endif /* HAVE_LINEAR_32 */
|
|
422 #endif /* HAVE_LINEAR */
|
|
423
|
|
424 static AudioContext
|
|
425 initialize_audio_port (AudioContext desc)
|
|
426 {
|
|
427 /* we can't use the same port for mono and stereo */
|
|
428 static AudioContextRec mono_port_state
|
|
429 = { { 0, 0, 0, 0 },
|
|
430 { (ALport) 0, AFunknown, 1, 0 },
|
|
431 { (void *) 0, (unsigned long) 0 } };
|
|
432 #if HAVE_STEREO
|
|
433 static AudioContextRec stereo_port_state
|
|
434 = { { 0, 0, 0, 0 },
|
|
435 { (ALport) 0, AFunknown, 2, 0 },
|
|
436 { (void *) 0, (unsigned long) 0 } };
|
|
437 static AudioContext return_ac;
|
|
438
|
|
439 switch (desc->ac_nchan)
|
|
440 {
|
|
441 case 1: return_ac = & mono_port_state; break;
|
|
442 case 2: return_ac = & stereo_port_state; break;
|
|
443 default: return (AudioContext) 0;
|
|
444 }
|
|
445 #else /* not HAVE_STEREO */
|
|
446 static AudioContext return_ac = & mono_port_state;
|
|
447 #endif /* not HAVE_STEREO */
|
|
448
|
|
449 return_ac->device = desc->device;
|
|
450 return_ac->buffer = desc->buffer;
|
|
451 return_ac->ac_format = desc->ac_format;
|
|
452 return_ac->ac_queue_size = desc->ac_queue_size;
|
|
453
|
|
454 if (return_ac->ac_port==(ALport) 0)
|
|
455 {
|
|
456 if ((open_audio_port (return_ac, desc))==-1)
|
|
457 {
|
563
|
458 report_sound_error ("Open audio port", Qunbound);
|
428
|
459 return (AudioContext) 0;
|
|
460 }
|
|
461 }
|
|
462 else
|
|
463 {
|
|
464 ALconfig config = ALgetconfig (return_ac->ac_port);
|
|
465 int changed = 0;
|
|
466 long params[2];
|
|
467
|
|
468 params[0] = AL_OUTPUT_RATE;
|
|
469 ALgetparams (return_ac->ac_device, params, 2);
|
|
470 return_ac->ac_output_rate = params[1];
|
|
471
|
|
472 if (return_ac->ac_output_rate != desc->ac_output_rate)
|
|
473 {
|
|
474 return_ac->ac_output_rate = params[1] = desc->ac_output_rate;
|
|
475 ALsetparams (return_ac->ac_device, params, 2);
|
|
476 }
|
|
477 if ((changed = set_output_format (config, return_ac->ac_format))==-1)
|
|
478 return (AudioContext) 0;
|
|
479 return_ac->ac_format = desc->ac_format;
|
|
480 if (changed)
|
|
481 ALsetconfig (return_ac->ac_port, config);
|
|
482 }
|
|
483 return_ac->ac_write_chunk_function = desc->ac_write_chunk_function;
|
|
484 get_current_volumes (& return_ac->device);
|
|
485 if (return_ac->ac_left_speaker_gain != desc->ac_left_speaker_gain
|
|
486 || return_ac->ac_right_speaker_gain != desc->ac_right_speaker_gain)
|
|
487 adjust_audio_volume (& desc->device);
|
|
488 return return_ac;
|
|
489 }
|
|
490
|
|
491 static int
|
|
492 open_audio_port (AudioContext return_ac, AudioContext desc)
|
|
493 {
|
|
494 ALconfig config = ALnewconfig();
|
|
495 long params[2];
|
|
496
|
|
497 adjust_audio_volume (& desc->device);
|
|
498 return_ac->ac_left_speaker_gain = desc->ac_left_speaker_gain;
|
|
499 return_ac->ac_right_speaker_gain = desc->ac_right_speaker_gain;
|
|
500 params[0] = AL_OUTPUT_RATE;
|
|
501 params[1] = desc->ac_output_rate;
|
|
502 ALsetparams (desc->ac_device, params, 2);
|
|
503 return_ac->ac_output_rate = desc->ac_output_rate;
|
|
504 if (set_channels (config, desc->ac_nchan)==-1)
|
|
505 return -1;
|
|
506 return_ac->ac_nchan = desc->ac_nchan;
|
|
507 if (set_output_format (config, desc->ac_format)==-1)
|
|
508 return -1;
|
|
509 return_ac->ac_format = desc->ac_format;
|
|
510 ALsetqueuesize (config, (long) CHUNKSIZE);
|
|
511 return_ac->ac_port = ALopenport("XEmacs audio output", "w", config);
|
|
512 ALfreeconfig (config);
|
|
513 if (return_ac->ac_port==0)
|
|
514 {
|
563
|
515 report_sound_error ("Opening audio output port", Qunbound);
|
428
|
516 return -1;
|
|
517 }
|
|
518 return 0;
|
|
519 }
|
|
520
|
|
521 static int
|
|
522 set_channels (ALconfig config, unsigned int nchan)
|
|
523 {
|
|
524 switch (nchan)
|
|
525 {
|
|
526 case 1: ALsetchannels (config, AL_MONO); break;
|
|
527 #if HAVE_STEREO
|
|
528 case 2: ALsetchannels (config, AL_STEREO); break;
|
|
529 #endif /* HAVE_STEREO */
|
|
530 default:
|
563
|
531 report_sound_error ("Unsupported channel count",
|
|
532 make_int (nchan));
|
428
|
533 return -1;
|
|
534 }
|
|
535 return 0;
|
|
536 }
|
|
537
|
|
538 static int
|
|
539 set_output_format (ALconfig config, AudioFormat format)
|
|
540 {
|
|
541 long samplesize;
|
|
542 long old_samplesize;
|
|
543
|
|
544 switch (format)
|
|
545 {
|
|
546 #if HAVE_MULAW_8
|
|
547 case AFmulaw8:
|
|
548 #endif
|
|
549 #if HAVE_LINEAR
|
|
550 case AFlinear16:
|
|
551 #endif
|
|
552 #if HAVE_MULAW_8 || HAVE_LINEAR
|
|
553 samplesize = AL_SAMPLE_16;
|
|
554 break;
|
|
555 #endif
|
|
556 #if HAVE_LINEAR
|
|
557 case AFlinear8:
|
|
558 samplesize = AL_SAMPLE_8;
|
|
559 break;
|
|
560 case AFlinear24:
|
|
561 #if HAVE_LINEAR_32
|
|
562 case AFlinear32:
|
|
563 samplesize = AL_SAMPLE_24;
|
|
564 break;
|
|
565 #endif
|
|
566 #endif
|
|
567 default:
|
563
|
568 report_sound_error ("Unsupported audio format", make_int (format));
|
428
|
569 return -1;
|
|
570 }
|
|
571 old_samplesize = ALgetwidth (config);
|
|
572 if (old_samplesize==samplesize)
|
|
573 return 0;
|
|
574 ALsetwidth (config, samplesize);
|
|
575 return 1;
|
|
576 }
|
|
577
|
|
578 static void
|
|
579 adjust_audio_volume (AudioDevice device)
|
|
580 {
|
|
581 long params[4];
|
|
582 params[0] = AL_LEFT_SPEAKER_GAIN;
|
|
583 params[1] = device->left_speaker_gain;
|
|
584 params[2] = AL_RIGHT_SPEAKER_GAIN;
|
|
585 params[3] = device->right_speaker_gain;
|
|
586 ALsetparams (device->device, params, 4);
|
|
587 }
|
|
588
|
|
589 static void
|
|
590 get_current_volumes (AudioDevice device)
|
|
591 {
|
|
592 long params[4];
|
|
593 params[0] = AL_LEFT_SPEAKER_GAIN;
|
|
594 params[2] = AL_RIGHT_SPEAKER_GAIN;
|
|
595 ALgetparams (device->device, params, 4);
|
|
596 device->left_speaker_gain = params[1];
|
|
597 device->right_speaker_gain = params[3];
|
|
598 }
|
|
599
|
|
600 #if HAVE_SND_FILES
|
|
601
|
|
602 /* Parsing .snd (NeXT/Sun) headers */
|
|
603
|
|
604 typedef struct
|
|
605 {
|
|
606 int magic;
|
|
607 int dataLocation;
|
|
608 int dataSize;
|
|
609 int dataFormat;
|
|
610 int samplingRate;
|
|
611 int channelCount;
|
563
|
612 Char_Binary info[4];
|
428
|
613 }
|
|
614 SNDSoundStruct;
|
|
615 #define SOUND_TO_HOST_INT(x) ntohl(x)
|
|
616
|
|
617 typedef enum
|
|
618 {
|
|
619 SND_FORMAT_FORMAT_UNSPECIFIED,
|
|
620 SND_FORMAT_MULAW_8,
|
|
621 SND_FORMAT_LINEAR_8,
|
|
622 SND_FORMAT_LINEAR_16,
|
|
623 SND_FORMAT_LINEAR_24,
|
|
624 SND_FORMAT_LINEAR_32,
|
|
625 SND_FORMAT_FLOAT,
|
|
626 SND_FORMAT_DOUBLE,
|
|
627 SND_FORMAT_INDIRECT,
|
|
628 SND_FORMAT_NESTED,
|
|
629 SND_FORMAT_DSP_CODE,
|
|
630 SND_FORMAT_DSP_DATA_8,
|
|
631 SND_FORMAT_DSP_DATA_16,
|
|
632 SND_FORMAT_DSP_DATA_24,
|
|
633 SND_FORMAT_DSP_DATA_32,
|
|
634 SND_FORMAT_DSP_unknown_15,
|
|
635 SND_FORMAT_DISPLAY,
|
|
636 SND_FORMAT_MULAW_SQUELCH,
|
|
637 SND_FORMAT_EMPHASIZED,
|
|
638 SND_FORMAT_COMPRESSED,
|
|
639 SND_FORMAT_COMPRESSED_EMPHASIZED,
|
|
640 SND_FORMAT_DSP_COMMANDS,
|
|
641 SND_FORMAT_DSP_COMMANDS_SAMPLES
|
|
642 }
|
|
643 SNDFormatCode;
|
|
644
|
|
645 static int
|
|
646 parse_snd_header (void *header, long length, AudioContext desc)
|
|
647 {
|
|
648 #define hp ((SNDSoundStruct *) (header))
|
|
649 long limit;
|
|
650
|
|
651 #if HAVE_LINEAR
|
|
652 desc->ac_write_chunk_function = write_linear_chunk;
|
|
653 #endif
|
|
654 switch ((SNDFormatCode) SOUND_TO_HOST_INT (hp->dataFormat))
|
|
655 {
|
|
656 #if HAVE_MULAW_8
|
|
657 case SND_FORMAT_MULAW_8:
|
|
658 desc->ac_format = AFmulaw8;
|
|
659 desc->ac_write_chunk_function = write_mulaw_8_chunk;
|
|
660 break;
|
|
661 #endif
|
|
662 #if HAVE_LINEAR
|
|
663 case SND_FORMAT_LINEAR_8:
|
|
664 desc->ac_format = AFlinear8;
|
|
665 break;
|
|
666 case SND_FORMAT_LINEAR_16:
|
|
667 desc->ac_format = AFlinear16;
|
|
668 break;
|
|
669 case SND_FORMAT_LINEAR_24:
|
|
670 desc->ac_format = AFlinear24;
|
|
671 break;
|
|
672 #endif
|
|
673 #if HAVE_LINEAR_32
|
|
674 case SND_FORMAT_LINEAR_32:
|
|
675 desc->ac_format = AFlinear32;
|
|
676 desc->ac_write_chunk_function = write_linear_32_chunk;
|
|
677 break;
|
|
678 #endif
|
|
679 default:
|
|
680 desc->ac_format = AFunknown;
|
|
681 }
|
|
682 desc->ac_output_rate = SOUND_TO_HOST_INT (hp->samplingRate);
|
|
683 desc->ac_nchan = SOUND_TO_HOST_INT (hp->channelCount);
|
563
|
684 desc->ac_data = (Char_Binary *) header + SOUND_TO_HOST_INT (hp->dataLocation);
|
|
685 limit = (Char_Binary *) header + length - (Char_Binary *) desc->ac_data;
|
428
|
686 desc->ac_size = SOUND_TO_HOST_INT (hp->dataSize);
|
|
687 if (desc->ac_size > limit) desc->ac_size = limit;
|
|
688 return 0;
|
|
689 #undef hp
|
|
690 }
|
|
691 #endif /* HAVE_SND_FILES */
|
|
692
|