Mercurial > emacs
annotate src/sound.c @ 27247:9de34873ed53
*** empty log message ***
author | Gerd Moellmann <gerd@gnu.org> |
---|---|
date | Sun, 09 Jan 2000 12:37:37 +0000 |
parents | 68290f44807a |
children | e17a1c6d3d69 |
rev | line source |
---|---|
25003 | 1 /* sound.c -- sound support. |
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents:
25722
diff
changeset
|
2 Copyright (C) 1998, 1999 Free Software Foundation. |
25003 | 3 |
4 This file is part of GNU Emacs. | |
5 | |
6 GNU Emacs is free software; you can redistribute it and/or modify | |
7 it under the terms of the GNU General Public License as published by | |
8 the Free Software Foundation; either version 2, or (at your option) | |
9 any later version. | |
10 | |
11 GNU Emacs is distributed in the hope that it will be useful, | |
12 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 GNU General Public License for more details. | |
15 | |
16 You should have received a copy of the GNU General Public License | |
17 along with GNU Emacs; see the file COPYING. If not, write to | |
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
19 Boston, MA 02111-1307, USA. */ | |
20 | |
21 /* Written by Gerd Moellmann <gerd@gnu.org>. Tested with Luigi's | |
22 driver on FreeBSD 2.2.7 with a SoundBlaster 16. */ | |
23 | |
24 #include <config.h> | |
25 | |
26 #if defined HAVE_SOUND | |
27 | |
28 #include <lisp.h> | |
29 #include <fcntl.h> | |
30 #include <unistd.h> | |
31 #include <sys/types.h> | |
32 #include <dispextern.h> | |
33 #include <errno.h> | |
34 | |
35 /* FreeBSD has machine/soundcard.h. Voxware sound driver docs mention | |
36 sys/soundcard.h. So, let's try whatever's there. */ | |
37 | |
38 #ifdef HAVE_MACHINE_SOUNDCARD_H | |
39 #include <machine/soundcard.h> | |
40 #endif | |
41 #ifdef HAVE_SYS_SOUNDCARD_H | |
42 #include <sys/soundcard.h> | |
43 #endif | |
44 | |
45 #define max(X, Y) ((X) > (Y) ? (X) : (Y)) | |
46 #define min(X, Y) ((X) < (Y) ? (X) : (Y)) | |
47 #define abs(X) ((X) < 0 ? -(X) : (X)) | |
48 | |
49 /* Structure forward declarations. */ | |
50 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
51 struct sound; |
25003 | 52 struct sound_device; |
53 | |
54 /* The file header of RIFF-WAVE files (*.wav). Files are always in | |
55 little-endian byte-order. */ | |
56 | |
57 struct wav_header | |
58 { | |
59 u_int32_t magic; | |
60 u_int32_t length; | |
61 u_int32_t chunk_type; | |
62 u_int32_t chunk_format; | |
63 u_int32_t chunk_length; | |
64 u_int16_t format; | |
65 u_int16_t channels; | |
66 u_int32_t sample_rate; | |
67 u_int32_t bytes_per_second; | |
68 u_int16_t sample_size; | |
69 u_int16_t precision; | |
70 u_int32_t chunk_data; | |
71 u_int32_t data_length; | |
72 }; | |
73 | |
74 /* The file header of Sun adio files (*.au). Files are always in | |
75 big-endian byte-order. */ | |
76 | |
77 struct au_header | |
78 { | |
79 /* ASCII ".snd" */ | |
80 u_int32_t magic_number; | |
81 | |
82 /* Offset of data part from start of file. Minimum value is 24. */ | |
83 u_int32_t data_offset; | |
84 | |
85 /* Size of data part, 0xffffffff if unknown. */ | |
86 u_int32_t data_size; | |
87 | |
88 /* Data encoding format. | |
89 1 8-bit ISDN u-law | |
90 2 8-bit linear PCM (REF-PCM) | |
91 3 16-bit linear PCM | |
92 4 24-bit linear PCM | |
93 5 32-bit linear PCM | |
94 6 32-bit IEEE floating-point | |
95 7 64-bit IEEE floating-point | |
96 23 8-bit u-law compressed using CCITT 0.721 ADPCM voice data | |
97 encoding scheme. */ | |
98 u_int32_t encoding; | |
99 | |
100 /* Number of samples per second. */ | |
101 u_int32_t sample_rate; | |
102 | |
103 /* Number of interleaved channels. */ | |
104 u_int32_t channels; | |
105 }; | |
106 | |
107 /* Maximum of all sound file headers sizes. */ | |
108 | |
109 #define MAX_SOUND_HEADER_BYTES \ | |
110 max (sizeof (struct wav_header), sizeof (struct au_header)) | |
111 | |
112 /* Interface structure for sound devices. */ | |
113 | |
114 struct sound_device | |
115 { | |
116 /* The name of the device or null meaning use a default device name. */ | |
117 char *file; | |
118 | |
119 /* File descriptor of the device. */ | |
120 int fd; | |
121 | |
122 /* Device-dependent format. */ | |
123 int format; | |
124 | |
125 /* Volume (0..100). Zero means unspecified. */ | |
126 int volume; | |
127 | |
128 /* Sample size. */ | |
129 int sample_size; | |
130 | |
131 /* Sample rate. */ | |
132 int sample_rate; | |
133 | |
134 /* Bytes per second. */ | |
135 int bps; | |
136 | |
137 /* 1 = mono, 2 = stereo, 0 = don't set. */ | |
138 int channels; | |
139 | |
140 /* Open device SD. */ | |
141 void (* open) P_ ((struct sound_device *sd)); | |
142 | |
143 /* Close device SD. */ | |
144 void (* close) P_ ((struct sound_device *sd)); | |
145 | |
146 /* Configure SD accoring to device-dependent parameters. */ | |
147 void (* configure) P_ ((struct sound_device *device)); | |
148 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
149 /* Choose a device-dependent format for outputting sound S. */ |
25003 | 150 void (* choose_format) P_ ((struct sound_device *sd, |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
151 struct sound *s)); |
25003 | 152 |
153 /* Write NYBTES bytes from BUFFER to device SD. */ | |
154 void (* write) P_ ((struct sound_device *sd, char *buffer, int nbytes)); | |
155 | |
156 /* A place for devices to store additional data. */ | |
157 void *data; | |
158 }; | |
159 | |
160 /* An enumerator for each supported sound file type. */ | |
161 | |
162 enum sound_type | |
163 { | |
164 RIFF, | |
165 SUN_AUDIO | |
166 }; | |
167 | |
168 /* Interface structure for sound files. */ | |
169 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
170 struct sound |
25003 | 171 { |
172 /* The type of the file. */ | |
173 enum sound_type type; | |
174 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
175 /* File descriptor of a sound file. */ |
25003 | 176 int fd; |
177 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
178 /* Pointer to sound file header. This contains header_size bytes |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
179 read from the start of a sound file. */ |
25003 | 180 char *header; |
181 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
182 /* Number of bytes raed from sound file. This is always <= |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
183 MAX_SOUND_HEADER_BYTES. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
184 int header_size; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
185 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
186 /* Sound data, if a string. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
187 Lisp_Object data; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
188 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
189 /* Play sound file S on device SD. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
190 void (* play) P_ ((struct sound *s, struct sound_device *sd)); |
25003 | 191 }; |
192 | |
193 /* Indices of attributes in a sound attributes vector. */ | |
194 | |
195 enum sound_attr | |
196 { | |
197 SOUND_FILE, | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
198 SOUND_DATA, |
25003 | 199 SOUND_DEVICE, |
200 SOUND_VOLUME, | |
201 SOUND_ATTR_SENTINEL | |
202 }; | |
203 | |
204 /* Symbols. */ | |
205 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
206 extern Lisp_Object QCfile, QCdata; |
25003 | 207 Lisp_Object QCvolume, QCdevice; |
208 Lisp_Object Qsound; | |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
209 Lisp_Object Qplay_sound_functions; |
25003 | 210 |
211 /* These are set during `play-sound' so that sound_cleanup has | |
212 access to them. */ | |
213 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
214 struct sound_device *current_sound_device; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
215 struct sound *current_sound; |
25003 | 216 |
217 /* Function prototypes. */ | |
218 | |
219 static void vox_open P_ ((struct sound_device *)); | |
220 static void vox_configure P_ ((struct sound_device *)); | |
221 static void vox_close P_ ((struct sound_device *sd)); | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
222 static void vox_choose_format P_ ((struct sound_device *, struct sound *)); |
25003 | 223 static void vox_init P_ ((struct sound_device *)); |
224 static void vox_write P_ ((struct sound_device *, char *, int)); | |
225 static void sound_perror P_ ((char *)); | |
226 static int parse_sound P_ ((Lisp_Object, Lisp_Object *)); | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
227 static void find_sound_type P_ ((struct sound *)); |
25003 | 228 static u_int32_t le2hl P_ ((u_int32_t)); |
229 static u_int16_t le2hs P_ ((u_int16_t)); | |
230 static u_int32_t be2hl P_ ((u_int32_t)); | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
231 static int wav_init P_ ((struct sound *)); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
232 static void wav_play P_ ((struct sound *, struct sound_device *)); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
233 static int au_init P_ ((struct sound *)); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
234 static void au_play P_ ((struct sound *, struct sound_device *)); |
25003 | 235 |
25722
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
236 #if 0 /* Currently not used. */ |
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
237 static u_int16_t be2hs P_ ((u_int16_t)); |
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
238 #endif |
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
239 |
25003 | 240 |
241 | |
242 /*********************************************************************** | |
243 General | |
244 ***********************************************************************/ | |
245 | |
246 /* Like perror, but signals an error. */ | |
247 | |
248 static void | |
249 sound_perror (msg) | |
250 char *msg; | |
251 { | |
252 error ("%s: %s", msg, strerror (errno)); | |
253 } | |
254 | |
255 | |
256 /* Parse sound specification SOUND, and fill ATTRS with what is | |
257 found. Value is non-zero if SOUND Is a valid sound specification. | |
258 A valid sound specification is a list starting with the symbol | |
259 `sound'. The rest of the list is a property list which may | |
260 contain the following key/value pairs: | |
261 | |
262 - `:file FILE' | |
263 | |
264 FILE is the sound file to play. If it isn't an absolute name, | |
265 it's searched under `data-directory'. | |
266 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
267 - `:data DATA' |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
268 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
269 DATA is a string containing sound data. Either :file or :data |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
270 may be present, but not both. |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
271 |
25003 | 272 - `:device DEVICE' |
273 | |
274 DEVICE is the name of the device to play on, e.g. "/dev/dsp2". | |
275 If not specified, a default device is used. | |
276 | |
277 - `:volume VOL' | |
278 | |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
279 VOL must be an integer in the range [0, 100], or a float in the |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
280 range [0, 1]. */ |
25003 | 281 |
282 static int | |
283 parse_sound (sound, attrs) | |
284 Lisp_Object sound; | |
285 Lisp_Object *attrs; | |
286 { | |
287 /* SOUND must be a list starting with the symbol `sound'. */ | |
288 if (!CONSP (sound) || !EQ (XCAR (sound), Qsound)) | |
289 return 0; | |
290 | |
291 sound = XCDR (sound); | |
292 attrs[SOUND_FILE] = Fplist_get (sound, QCfile); | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
293 attrs[SOUND_DATA] = Fplist_get (sound, QCdata); |
25003 | 294 attrs[SOUND_DEVICE] = Fplist_get (sound, QCdevice); |
295 attrs[SOUND_VOLUME] = Fplist_get (sound, QCvolume); | |
296 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
297 /* File name or data must be specified. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
298 if (!STRINGP (attrs[SOUND_FILE]) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
299 && !STRINGP (attrs[SOUND_DATA])) |
25003 | 300 return 0; |
301 | |
302 /* Volume must be in the range 0..100 or unspecified. */ | |
303 if (!NILP (attrs[SOUND_VOLUME])) | |
304 { | |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
305 if (INTEGERP (attrs[SOUND_VOLUME])) |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
306 { |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
307 if (XINT (attrs[SOUND_VOLUME]) < 0 |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
308 || XINT (attrs[SOUND_VOLUME]) > 100) |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
309 return 0; |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
310 } |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
311 else if (FLOATP (attrs[SOUND_VOLUME])) |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
312 { |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
313 if (XFLOAT_DATA (attrs[SOUND_VOLUME]) < 0 |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
314 || XFLOAT_DATA (attrs[SOUND_VOLUME]) > 1) |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
315 return 0; |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
316 } |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
317 else |
25003 | 318 return 0; |
319 } | |
320 | |
321 /* Device must be a string or unspecified. */ | |
322 if (!NILP (attrs[SOUND_DEVICE]) | |
323 && !STRINGP (attrs[SOUND_DEVICE])) | |
324 return 0; | |
325 | |
326 return 1; | |
327 } | |
328 | |
329 | |
330 /* Find out the type of the sound file whose file descriptor is FD. | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
331 S is the sound file structure to fill in. */ |
25003 | 332 |
333 static void | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
334 find_sound_type (s) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
335 struct sound *s; |
25003 | 336 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
337 if (!wav_init (s) && !au_init (s)) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
338 error ("Unknown sound format"); |
25003 | 339 } |
340 | |
341 | |
342 /* Function installed by play-sound with record_unwind_protect. */ | |
343 | |
344 static Lisp_Object | |
345 sound_cleanup (arg) | |
346 Lisp_Object arg; | |
347 { | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
348 if (current_sound_device) |
25003 | 349 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
350 current_sound_device->close (current_sound_device); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
351 if (current_sound->fd > 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
352 emacs_close (current_sound->fd); |
25003 | 353 } |
354 } | |
355 | |
356 | |
357 DEFUN ("play-sound", Fplay_sound, Splay_sound, 1, 1, 0, | |
358 "Play sound SOUND.") | |
359 (sound) | |
360 Lisp_Object sound; | |
361 { | |
362 Lisp_Object attrs[SOUND_ATTR_SENTINEL]; | |
363 Lisp_Object file; | |
364 struct gcpro gcpro1, gcpro2; | |
365 struct sound_device sd; | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
366 struct sound s; |
25003 | 367 Lisp_Object args[2]; |
368 int count = specpdl_ptr - specpdl; | |
369 | |
370 file = Qnil; | |
371 GCPRO2 (sound, file); | |
372 bzero (&sd, sizeof sd); | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
373 bzero (&s, sizeof s); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
374 current_sound_device = &sd; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
375 current_sound = &s; |
25003 | 376 record_unwind_protect (sound_cleanup, Qnil); |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
377 s.header = (char *) alloca (MAX_SOUND_HEADER_BYTES); |
25003 | 378 |
379 /* Parse the sound specification. Give up if it is invalid. */ | |
380 if (!parse_sound (sound, attrs)) | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
381 error ("Invalid sound specification"); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
382 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
383 if (STRINGP (attrs[SOUND_FILE])) |
25003 | 384 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
385 /* Open the sound file. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
386 s.fd = openp (Fcons (Vdata_directory, Qnil), |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
387 attrs[SOUND_FILE], "", &file, 0); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
388 if (s.fd < 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
389 sound_perror ("Open sound file"); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
390 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
391 /* Read the first bytes from the file. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
392 s.header_size = emacs_read (s.fd, s.header, MAX_SOUND_HEADER_BYTES); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
393 if (s.header_size < 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
394 sound_perror ("Reading sound file header"); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
395 } |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
396 else |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
397 { |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
398 s.data = attrs[SOUND_DATA]; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
399 bcopy (XSTRING (s.data)->data, s.header, |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
400 min (MAX_SOUND_HEADER_BYTES, STRING_BYTES (XSTRING (s.data)))); |
25003 | 401 } |
402 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
403 /* Find out the type of sound. Give up if we can't tell. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
404 find_sound_type (&s); |
25003 | 405 |
406 /* Set up a device. */ | |
407 if (STRINGP (attrs[SOUND_DEVICE])) | |
408 { | |
409 int len = XSTRING (attrs[SOUND_DEVICE])->size; | |
410 sd.file = (char *) alloca (len + 1); | |
411 strcpy (sd.file, XSTRING (attrs[SOUND_DEVICE])->data); | |
412 } | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
413 |
25003 | 414 if (INTEGERP (attrs[SOUND_VOLUME])) |
415 sd.volume = XFASTINT (attrs[SOUND_VOLUME]); | |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
416 else if (FLOATP (attrs[SOUND_VOLUME])) |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
417 sd.volume = XFLOAT_DATA (attrs[SOUND_VOLUME]) * 100; |
25003 | 418 |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
419 args[0] = Qplay_sound_functions; |
25003 | 420 args[1] = sound; |
421 Frun_hook_with_args (make_number (2), args); | |
422 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
423 /* There is only one type of device we currently support, the VOX |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
424 sound driver. Set up the device interface functions for that |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
425 device. */ |
25003 | 426 vox_init (&sd); |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
427 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
428 /* Open the device. */ |
25003 | 429 sd.open (&sd); |
430 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
431 /* Play the sound. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
432 s.play (&s, &sd); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
433 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
434 /* Close the input file, if any. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
435 if (!STRINGP (s.data)) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
436 { |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
437 emacs_close (s.fd); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
438 s.fd = -1; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
439 } |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
440 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
441 /* Close the device. */ |
25003 | 442 sd.close (&sd); |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
443 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
444 /* Clean up. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
445 current_sound_device = NULL; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
446 current_sound = NULL; |
25003 | 447 UNGCPRO; |
448 unbind_to (count, Qnil); | |
449 return Qnil; | |
450 } | |
451 | |
452 | |
453 /*********************************************************************** | |
454 Byte-order Conversion | |
455 ***********************************************************************/ | |
456 | |
457 /* Convert 32-bit value VALUE which is in little-endian byte-order | |
458 to host byte-order. */ | |
459 | |
460 static u_int32_t | |
461 le2hl (value) | |
462 u_int32_t value; | |
463 { | |
464 #ifdef WORDS_BIG_ENDIAN | |
465 unsigned char *p = (unsigned char *) &value; | |
466 value = p[0] + (p[1] << 8) + (p[2] << 16) + (p[3] << 24); | |
467 #endif | |
468 return value; | |
469 } | |
470 | |
471 | |
472 /* Convert 16-bit value VALUE which is in little-endian byte-order | |
473 to host byte-order. */ | |
474 | |
475 static u_int16_t | |
476 le2hs (value) | |
477 u_int16_t value; | |
478 { | |
479 #ifdef WORDS_BIG_ENDIAN | |
480 unsigned char *p = (unsigned char *) &value; | |
481 value = p[0] + (p[1] << 8); | |
482 #endif | |
483 return value; | |
484 } | |
485 | |
486 | |
487 /* Convert 32-bit value VALUE which is in big-endian byte-order | |
488 to host byte-order. */ | |
489 | |
490 static u_int32_t | |
491 be2hl (value) | |
492 u_int32_t value; | |
493 { | |
494 #ifndef WORDS_BIG_ENDIAN | |
495 unsigned char *p = (unsigned char *) &value; | |
496 value = p[3] + (p[2] << 8) + (p[1] << 16) + (p[0] << 24); | |
497 #endif | |
498 return value; | |
499 } | |
500 | |
501 | |
25722
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
502 #if 0 /* Currently not used. */ |
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
503 |
25003 | 504 /* Convert 16-bit value VALUE which is in big-endian byte-order |
505 to host byte-order. */ | |
506 | |
507 static u_int16_t | |
508 be2hs (value) | |
509 u_int16_t value; | |
510 { | |
511 #ifndef WORDS_BIG_ENDIAN | |
512 unsigned char *p = (unsigned char *) &value; | |
513 value = p[1] + (p[0] << 8); | |
514 #endif | |
515 return value; | |
516 } | |
517 | |
25722
2be6ced279d3
(Fplay_sound): Remove usused variables.
Gerd Moellmann <gerd@gnu.org>
parents:
25548
diff
changeset
|
518 #endif /* 0 */ |
25003 | 519 |
520 | |
521 /*********************************************************************** | |
522 RIFF-WAVE (*.wav) | |
523 ***********************************************************************/ | |
524 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
525 /* Try to initialize sound file S from S->header. S->header |
25003 | 526 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the |
527 sound file. If the file is a WAV-format file, set up interface | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
528 functions in S and convert header fields to host byte-order. |
25003 | 529 Value is non-zero if the file is a WAV file. */ |
530 | |
531 static int | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
532 wav_init (s) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
533 struct sound *s; |
25003 | 534 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
535 struct wav_header *header = (struct wav_header *) s->header; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
536 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
537 if (s->header_size < sizeof *header |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
538 || bcmp (s->header, "RIFF", 4) != 0) |
25003 | 539 return 0; |
540 | |
541 /* WAV files are in little-endian order. Convert the header | |
542 if on a big-endian machine. */ | |
543 header->magic = le2hl (header->magic); | |
544 header->length = le2hl (header->length); | |
545 header->chunk_type = le2hl (header->chunk_type); | |
546 header->chunk_format = le2hl (header->chunk_format); | |
547 header->chunk_length = le2hl (header->chunk_length); | |
548 header->format = le2hs (header->format); | |
549 header->channels = le2hs (header->channels); | |
550 header->sample_rate = le2hl (header->sample_rate); | |
551 header->bytes_per_second = le2hl (header->bytes_per_second); | |
552 header->sample_size = le2hs (header->sample_size); | |
553 header->precision = le2hs (header->precision); | |
554 header->chunk_data = le2hl (header->chunk_data); | |
555 header->data_length = le2hl (header->data_length); | |
556 | |
557 /* Set up the interface functions for WAV. */ | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
558 s->type = RIFF; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
559 s->play = wav_play; |
25003 | 560 |
561 return 1; | |
562 } | |
563 | |
564 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
565 /* Play RIFF-WAVE audio file S on sound device SD. */ |
25003 | 566 |
567 static void | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
568 wav_play (s, sd) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
569 struct sound *s; |
25003 | 570 struct sound_device *sd; |
571 { | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
572 struct wav_header *header = (struct wav_header *) s->header; |
25003 | 573 |
574 /* Let the device choose a suitable device-dependent format | |
575 for the file. */ | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
576 sd->choose_format (sd, s); |
25003 | 577 |
578 /* Configure the device. */ | |
579 sd->sample_size = header->sample_size; | |
580 sd->sample_rate = header->sample_rate; | |
581 sd->bps = header->bytes_per_second; | |
582 sd->channels = header->channels; | |
583 sd->configure (sd); | |
584 | |
585 /* Copy sound data to the device. The WAV file specification is | |
586 actually more complex. This simple scheme worked with all WAV | |
587 files I found so far. If someone feels inclined to implement the | |
588 whole RIFF-WAVE spec, please do. */ | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
589 if (STRINGP (s->data)) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
590 sd->write (sd, XSTRING (s->data)->data + sizeof *header, |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
591 STRING_BYTES (XSTRING (s->data)) - sizeof *header); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
592 else |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
593 { |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
594 char *buffer; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
595 int nbytes; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
596 int blksize = 2048; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
597 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
598 buffer = (char *) alloca (blksize); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
599 lseek (s->fd, sizeof *header, SEEK_SET); |
25003 | 600 |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
601 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
602 sd->write (sd, buffer, nbytes); |
25003 | 603 |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
604 if (nbytes < 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
605 sound_perror ("Reading sound file"); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
606 } |
25003 | 607 } |
608 | |
609 | |
610 | |
611 /*********************************************************************** | |
612 Sun Audio (*.au) | |
613 ***********************************************************************/ | |
614 | |
615 /* Sun audio file encodings. */ | |
616 | |
617 enum au_encoding | |
618 { | |
619 AU_ENCODING_ULAW_8 = 1, | |
620 AU_ENCODING_8, | |
621 AU_ENCODING_16, | |
622 AU_ENCODING_24, | |
623 AU_ENCODING_32, | |
624 AU_ENCODING_IEEE32, | |
625 AU_ENCODING_IEEE64, | |
626 AU_COMPRESSED = 23 | |
627 }; | |
628 | |
629 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
630 /* Try to initialize sound file S from S->header. S->header |
25003 | 631 contains the first MAX_SOUND_HEADER_BYTES number of bytes from the |
632 sound file. If the file is a AU-format file, set up interface | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
633 functions in S and convert header fields to host byte-order. |
25003 | 634 Value is non-zero if the file is an AU file. */ |
635 | |
636 static int | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
637 au_init (s) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
638 struct sound *s; |
25003 | 639 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
640 struct au_header *header = (struct au_header *) s->header; |
25003 | 641 |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
642 if (s->header_size < sizeof *header |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
643 || bcmp (s->header, ".snd", 4) != 0) |
25003 | 644 return 0; |
645 | |
646 header->magic_number = be2hl (header->magic_number); | |
647 header->data_offset = be2hl (header->data_offset); | |
648 header->data_size = be2hl (header->data_size); | |
649 header->encoding = be2hl (header->encoding); | |
650 header->sample_rate = be2hl (header->sample_rate); | |
651 header->channels = be2hl (header->channels); | |
652 | |
653 /* Set up the interface functions for AU. */ | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
654 s->type = SUN_AUDIO; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
655 s->play = au_play; |
25003 | 656 |
657 return 1; | |
658 } | |
659 | |
660 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
661 /* Play Sun audio file S on sound device SD. */ |
25003 | 662 |
663 static void | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
664 au_play (s, sd) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
665 struct sound *s; |
25003 | 666 struct sound_device *sd; |
667 { | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
668 struct au_header *header = (struct au_header *) s->header; |
25003 | 669 |
670 sd->sample_size = 0; | |
671 sd->sample_rate = header->sample_rate; | |
672 sd->bps = 0; | |
673 sd->channels = header->channels; | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
674 sd->choose_format (sd, s); |
25003 | 675 sd->configure (sd); |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
676 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
677 if (STRINGP (s->data)) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
678 sd->write (sd, XSTRING (s->data)->data + header->data_offset, |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
679 STRING_BYTES (XSTRING (s->data)) - header->data_offset); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
680 else |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
681 { |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
682 int blksize = 2048; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
683 char *buffer; |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
684 int nbytes; |
25003 | 685 |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
686 /* Seek */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
687 lseek (s->fd, header->data_offset, SEEK_SET); |
25003 | 688 |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
689 /* Copy sound data to the device. */ |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
690 buffer = (char *) alloca (blksize); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
691 while ((nbytes = emacs_read (s->fd, buffer, blksize)) > 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
692 sd->write (sd, buffer, nbytes); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
693 |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
694 if (nbytes < 0) |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
695 sound_perror ("Reading sound file"); |
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
696 } |
25003 | 697 } |
698 | |
699 | |
700 | |
701 /*********************************************************************** | |
702 Voxware Driver Interface | |
703 ***********************************************************************/ | |
704 | |
705 /* This driver is available on GNU/Linux, and the free BSDs. FreeBSD | |
706 has a compatible own driver aka Luigi's driver. */ | |
707 | |
708 | |
709 /* Open device SD. If SD->file is non-null, open that device, | |
710 otherwise use a default device name. */ | |
711 | |
712 static void | |
713 vox_open (sd) | |
714 struct sound_device *sd; | |
715 { | |
716 char *file; | |
717 | |
718 /* Open the sound device. Default is /dev/dsp. */ | |
719 if (sd->file) | |
720 file = sd->file; | |
721 else | |
722 file = "/dev/dsp"; | |
723 | |
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents:
25722
diff
changeset
|
724 sd->fd = emacs_open (file, O_WRONLY, 0); |
25003 | 725 if (sd->fd < 0) |
726 sound_perror (file); | |
727 } | |
728 | |
729 | |
730 /* Configure device SD from parameters in it. */ | |
731 | |
732 static void | |
733 vox_configure (sd) | |
734 struct sound_device *sd; | |
735 { | |
736 int requested; | |
737 | |
738 xassert (sd->fd >= 0); | |
739 | |
740 /* Device parameters apparently depend on each other in undocumented | |
741 ways (not to imply that there is any real documentation). Be | |
742 careful when reordering the calls below. */ | |
743 if (sd->sample_size > 0 | |
744 && ioctl (sd->fd, SNDCTL_DSP_SAMPLESIZE, &sd->sample_size) < 0) | |
745 sound_perror ("Setting sample size"); | |
746 | |
747 if (sd->bps > 0 | |
748 && ioctl (sd->fd, SNDCTL_DSP_SPEED, &sd->bps) < 0) | |
749 sound_perror ("Setting speed"); | |
750 | |
751 if (sd->sample_rate > 0 | |
752 && ioctl (sd->fd, SOUND_PCM_WRITE_RATE, &sd->sample_rate) < 0) | |
753 sound_perror ("Setting sample rate"); | |
754 | |
755 requested = sd->format; | |
756 if (ioctl (sd->fd, SNDCTL_DSP_SETFMT, &sd->format) < 0) | |
757 sound_perror ("Setting format"); | |
758 else if (requested != sd->format) | |
759 error ("Setting format"); | |
760 | |
761 if (sd->channels > 1 | |
762 && ioctl (sd->fd, SNDCTL_DSP_STEREO, &sd->channels) < 0) | |
763 sound_perror ("Setting channels"); | |
764 | |
765 if (sd->volume > 0 | |
766 && ioctl (sd->fd, SOUND_MIXER_WRITE_PCM, &sd->volume) < 0) | |
767 sound_perror ("Setting volume"); | |
768 } | |
769 | |
770 | |
771 /* Close device SD if it is open. */ | |
772 | |
773 static void | |
774 vox_close (sd) | |
775 struct sound_device *sd; | |
776 { | |
777 if (sd->fd >= 0) | |
778 { | |
779 /* Flush sound data, and reset the device. */ | |
780 ioctl (sd->fd, SNDCTL_DSP_SYNC, NULL); | |
781 ioctl (sd->fd, SNDCTL_DSP_RESET, NULL); | |
782 | |
783 /* Close the device. */ | |
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents:
25722
diff
changeset
|
784 emacs_close (sd->fd); |
25003 | 785 sd->fd = -1; |
786 } | |
787 } | |
788 | |
789 | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
790 /* Choose device-dependent format for device SD from sound file S. */ |
25003 | 791 |
792 static void | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
793 vox_choose_format (sd, s) |
25003 | 794 struct sound_device *sd; |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
795 struct sound *s; |
25003 | 796 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
797 if (s->type == RIFF) |
25003 | 798 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
799 struct wav_header *h = (struct wav_header *) s->header; |
25003 | 800 if (h->precision == 8) |
801 sd->format = AFMT_U8; | |
802 else if (h->precision == 16) | |
803 sd->format = AFMT_S16_LE; | |
804 else | |
805 error ("Unsupported WAV file format"); | |
806 } | |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
807 else if (s->type == SUN_AUDIO) |
25003 | 808 { |
27146
68290f44807a
(struct sound): Renamed from struct sound_file.
Gerd Moellmann <gerd@gnu.org>
parents:
26088
diff
changeset
|
809 struct au_header *header = (struct au_header *) s->header; |
25003 | 810 switch (header->encoding) |
811 { | |
812 case AU_ENCODING_ULAW_8: | |
813 case AU_ENCODING_IEEE32: | |
814 case AU_ENCODING_IEEE64: | |
815 sd->format = AFMT_MU_LAW; | |
816 break; | |
817 | |
818 case AU_ENCODING_8: | |
819 case AU_ENCODING_16: | |
820 case AU_ENCODING_24: | |
821 case AU_ENCODING_32: | |
822 sd->format = AFMT_S16_LE; | |
823 break; | |
824 | |
825 default: | |
826 error ("Unsupported AU file format"); | |
827 } | |
828 } | |
829 else | |
830 abort (); | |
831 } | |
832 | |
833 | |
834 /* Initialize device SD. Set up the interface functions in the device | |
835 structure. */ | |
836 | |
837 static void | |
838 vox_init (sd) | |
839 struct sound_device *sd; | |
840 { | |
841 sd->fd = -1; | |
842 sd->open = vox_open; | |
843 sd->close = vox_close; | |
844 sd->configure = vox_configure; | |
845 sd->choose_format = vox_choose_format; | |
846 sd->write = vox_write; | |
847 } | |
848 | |
849 | |
850 /* Write NBYTES bytes from BUFFER to device SD. */ | |
851 | |
852 static void | |
853 vox_write (sd, buffer, nbytes) | |
854 struct sound_device *sd; | |
855 char *buffer; | |
856 int nbytes; | |
857 { | |
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
parents:
25722
diff
changeset
|
858 int nwritten = emacs_write (sd->fd, buffer, nbytes); |
25003 | 859 if (nwritten < 0) |
860 sound_perror ("Writing to sound device"); | |
861 } | |
862 | |
863 | |
864 | |
865 /*********************************************************************** | |
866 Initialization | |
867 ***********************************************************************/ | |
868 | |
869 void | |
870 syms_of_sound () | |
871 { | |
872 QCdevice = intern (":device"); | |
873 staticpro (&QCdevice); | |
874 QCvolume = intern (":volume"); | |
875 staticpro (&QCvolume); | |
876 Qsound = intern ("sound"); | |
877 staticpro (&Qsound); | |
25548
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
878 Qplay_sound_functions = intern ("play-sound-functions"); |
df6548ca33fd
(Qplay_sound_functions): Replaces Qplay_sound_hook.
Gerd Moellmann <gerd@gnu.org>
parents:
25003
diff
changeset
|
879 staticpro (&Qplay_sound_functions); |
25003 | 880 |
881 defsubr (&Splay_sound); | |
882 } | |
883 | |
884 | |
885 void | |
886 init_sound () | |
887 { | |
888 } | |
889 | |
890 #endif /* HAVE_SOUND */ |