annotate src/sgiplay.c @ 609:13e3d7ae7155

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