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