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