428
|
1 /* play.c - play a sound file on the speaker
|
|
2 **
|
|
3 ** Copyright (C) 1989 by Jef Poskanzer.
|
|
4 **
|
|
5 ** Modified 24-May-91 by Jamie Zawinski (for Lucid Emacs).
|
|
6 ** Modified 17-Dec-92 by Jamie Zawinski (largely rewritten for SunOS 4.1.3).
|
|
7 **
|
|
8 ** Permission to use, copy, modify, and distribute this software and its
|
|
9 ** documentation for any purpose and without fee is hereby granted, provided
|
|
10 ** that the above copyright notice appear in all copies and that both that
|
|
11 ** copyright notice and this permission notice appear in supporting
|
|
12 ** documentation. This software is provided "as is" without express or
|
|
13 ** implied warranty.
|
|
14 */
|
|
15
|
|
16 /* Synched up with: Not in FSF. */
|
|
17
|
563
|
18 /* This file Mule-ized by Ben Wing, 5-15-01. */
|
428
|
19
|
609
|
20 #define DONT_ENCAPSULATE
|
|
21
|
563
|
22 #include <config.h>
|
|
23 #include "lisp.h"
|
|
24 #include "sound.h"
|
428
|
25
|
563
|
26 #include "sysdep.h"
|
|
27 #include "sysfile.h"
|
|
28 #include "syssignal.h"
|
428
|
29
|
|
30 #include <multimedia/libaudio.h>
|
|
31 #include <multimedia/audio_device.h>
|
|
32
|
|
33 static SIGTYPE (*sighup_handler) (int sig);
|
|
34 static SIGTYPE (*sigint_handler) (int sig);
|
|
35 static SIGTYPE sighandler (int sig);
|
|
36
|
|
37 static int audio_fd;
|
|
38
|
|
39 #define audio_open() open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
|
|
40
|
442
|
41 static int initialized_device_p;
|
428
|
42 static int reset_volume_p, reset_device_p;
|
|
43 static double old_volume;
|
|
44 static Audio_hdr dev_hdr;
|
|
45
|
|
46 static int
|
563
|
47 init_device (int volume, UChar_Binary *data, int fd,
|
428
|
48 unsigned int *header_length)
|
|
49 {
|
|
50 #ifdef SUNOS4_0_3
|
|
51 if (header_length) *header_length = 0;
|
|
52 return 0;
|
|
53 #else
|
|
54 Audio_hdr file_hdr;
|
|
55
|
|
56 reset_volume_p = 0;
|
|
57 reset_device_p = 0;
|
|
58
|
|
59 if (data && fd) abort (); /* one or the other */
|
|
60
|
|
61 if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
|
|
62 {
|
563
|
63 sound_perror ("Not a valid audio device");
|
428
|
64 return 1;
|
|
65 }
|
|
66
|
|
67 if (AUDIO_SUCCESS != (data
|
|
68 ? audio_decode_filehdr (data, &file_hdr, header_length)
|
|
69 : audio_read_filehdr (fd, &file_hdr, 0, 0)))
|
|
70 {
|
|
71 if (data)
|
563
|
72 sound_perror ("invalid audio data");
|
428
|
73 else
|
563
|
74 sound_perror ("invalid audio file");
|
428
|
75 return 1;
|
|
76 }
|
|
77
|
|
78 audio_flush_play (audio_fd);
|
|
79
|
442
|
80 if (!initialized_device_p || (0 != audio_cmp_hdr (&dev_hdr, &file_hdr)))
|
428
|
81 {
|
|
82 Audio_hdr new_hdr;
|
|
83 new_hdr = file_hdr;
|
|
84 reset_device_p = 1;
|
442
|
85 initialized_device_p = 1;
|
428
|
86 if (AUDIO_SUCCESS != audio_set_play_config (audio_fd, &new_hdr))
|
|
87 {
|
563
|
88 Extbyte buf1 [100], buf2 [100], buf3 [250];
|
428
|
89 audio_enc_to_str (&file_hdr, buf1);
|
|
90 audio_enc_to_str (&new_hdr, buf2);
|
|
91 sprintf (buf3, "wanted %s, got %s", buf1, buf2);
|
563
|
92 sound_warn (buf3);
|
428
|
93 return 1;
|
|
94 }
|
|
95 }
|
|
96
|
|
97 if (volume < 0 || volume > 100)
|
|
98 {
|
563
|
99 Extbyte buf [255];
|
428
|
100 sprintf (buf, "volume must be between 0 and 100 (not %d)", volume);
|
563
|
101 sound_warn (buf);
|
428
|
102 return 1;
|
|
103 }
|
|
104 {
|
|
105 /* set the volume; scale it to 0.0 - 1.0 */
|
|
106 double V = (volume / 100.0);
|
|
107 audio_get_play_gain (audio_fd, &old_volume);
|
|
108 reset_volume_p = 1;
|
|
109 audio_set_play_gain (audio_fd, &V);
|
|
110 }
|
|
111
|
|
112 return 0;
|
|
113 #endif
|
|
114 }
|
|
115
|
|
116
|
|
117 static void
|
|
118 reset_device (int wait_p)
|
|
119 {
|
|
120 if (wait_p)
|
|
121 audio_drain (audio_fd, 1);
|
|
122 else
|
|
123 audio_flush_play (audio_fd);
|
|
124 if (reset_device_p)
|
|
125 audio_set_play_config (audio_fd, &dev_hdr);
|
|
126 if (reset_volume_p)
|
|
127 audio_set_play_gain (audio_fd, &old_volume);
|
|
128 }
|
|
129
|
|
130
|
|
131 void
|
563
|
132 play_sound_file (Extbyte *sound_file, int volume)
|
428
|
133 {
|
|
134 int rrtn, wrtn;
|
563
|
135 UChar_Binary buf [255];
|
428
|
136 int file_fd;
|
|
137
|
|
138 audio_fd = audio_open ();
|
|
139
|
|
140 if (audio_fd < 0)
|
|
141 {
|
563
|
142 sound_perror ("open /dev/audio");
|
428
|
143 return;
|
|
144 }
|
|
145
|
|
146 /* where to find the proto for signal()... */
|
613
|
147 sighup_handler = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGHUP, sighandler);
|
|
148 sigint_handler = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGINT, sighandler);
|
428
|
149
|
|
150 file_fd = open (sound_file, O_RDONLY, 0);
|
|
151 if (file_fd < 0)
|
|
152 {
|
563
|
153 sound_perror (sound_file);
|
428
|
154 goto END_OF_PLAY;
|
|
155 }
|
|
156
|
563
|
157 if (init_device (volume, (UChar_Binary *) 0, file_fd, (unsigned int *) 0))
|
428
|
158 goto END_OF_PLAY;
|
|
159
|
|
160 while (1)
|
|
161 {
|
563
|
162 rrtn = read (file_fd, (Char_Binary *) buf, sizeof (buf));
|
428
|
163 if (rrtn < 0)
|
|
164 {
|
563
|
165 sound_perror ("read");
|
428
|
166 goto END_OF_PLAY;
|
|
167 }
|
|
168 if (rrtn == 0)
|
|
169 break;
|
|
170
|
|
171 while (1)
|
|
172 {
|
563
|
173 wrtn = write (audio_fd, (Char_Binary *) buf, rrtn);
|
428
|
174 if (wrtn < 0)
|
|
175 {
|
563
|
176 sound_perror ("write");
|
428
|
177 goto END_OF_PLAY;
|
|
178 }
|
|
179 if (wrtn != 0)
|
|
180 break;
|
|
181
|
|
182 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
|
|
183 goto END_OF_PLAY;
|
|
184 }
|
|
185 if (wrtn != rrtn)
|
|
186 {
|
563
|
187 Extbyte warn_buf [255];
|
428
|
188 sprintf (warn_buf, "play: rrtn = %d, wrtn = %d", rrtn, wrtn);
|
563
|
189 sound_warn (warn_buf);
|
428
|
190 goto END_OF_PLAY;
|
|
191 }
|
|
192 }
|
|
193
|
|
194 END_OF_PLAY:
|
|
195
|
|
196 if (file_fd > 0)
|
|
197 close (file_fd);
|
|
198
|
|
199 if (audio_fd > 0)
|
|
200 {
|
|
201 reset_device (1);
|
|
202 close (audio_fd);
|
|
203 }
|
|
204
|
613
|
205 EMACS_SIGNAL (SIGHUP, sighup_handler);
|
|
206 EMACS_SIGNAL (SIGINT, sigint_handler);
|
428
|
207 }
|
|
208
|
|
209
|
442
|
210 int
|
563
|
211 play_sound_data (UChar_Binary *data, int length, int volume)
|
428
|
212 {
|
|
213 int wrtn, start = 0;
|
|
214 unsigned int ilen;
|
442
|
215 int result = 0;
|
428
|
216
|
|
217 audio_fd = -1;
|
|
218
|
442
|
219 if (length == 0) return 0;
|
428
|
220
|
|
221 /* this is just to get a better error message */
|
563
|
222 if (strncmp (".snd\0", (Char_Binary *) data, 4))
|
428
|
223 {
|
563
|
224 sound_warn ("Not valid audio data (bad magic number)");
|
428
|
225 goto END_OF_PLAY;
|
|
226 }
|
|
227 if (length <= sizeof (Audio_hdr))
|
|
228 {
|
563
|
229 sound_warn ("Not valid audio data (too short)");
|
428
|
230 goto END_OF_PLAY;
|
|
231 }
|
|
232
|
|
233 audio_fd = audio_open ();
|
|
234 if (audio_fd < 0)
|
442
|
235 return 0;
|
428
|
236
|
|
237 /* where to find the proto for signal()... */
|
613
|
238 sighup_handler = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGHUP, sighandler);
|
|
239 sigint_handler = (SIGTYPE (*) (int)) EMACS_SIGNAL (SIGINT, sighandler);
|
428
|
240
|
|
241 if (init_device (volume, data, 0, &ilen))
|
|
242 goto END_OF_PLAY;
|
|
243
|
|
244 data += (ilen<<2);
|
|
245 length -= (ilen<<2);
|
|
246 if (length <= 1)
|
|
247 goto END_OF_PLAY;
|
|
248
|
|
249 while (1)
|
|
250 {
|
563
|
251 wrtn = write (audio_fd, (Char_Binary *) (data+start), length-start);
|
428
|
252 if (wrtn < 0)
|
|
253 {
|
563
|
254 sound_perror ("write");
|
428
|
255 goto END_OF_PLAY;
|
|
256 }
|
|
257 if (wrtn != 0)
|
|
258 {
|
|
259 start += wrtn;
|
|
260 break;
|
|
261 }
|
|
262 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
|
|
263 goto END_OF_PLAY;
|
|
264 }
|
|
265 if (wrtn != length)
|
|
266 {
|
563
|
267 Extbyte buf [255];
|
428
|
268 sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
|
563
|
269 sound_warn (buf);
|
428
|
270 goto END_OF_PLAY;
|
|
271 }
|
|
272
|
442
|
273 result = 1;
|
|
274
|
428
|
275 END_OF_PLAY:
|
|
276
|
|
277 if (audio_fd > 0)
|
|
278 {
|
|
279 reset_device (1);
|
|
280 close (audio_fd);
|
|
281 }
|
|
282
|
613
|
283 EMACS_SIGNAL (SIGHUP, sighup_handler);
|
|
284 EMACS_SIGNAL (SIGINT, sigint_handler);
|
442
|
285
|
|
286 return result;
|
428
|
287 }
|
|
288
|
|
289 /* #### sigcontext doesn't exist in Solaris. This should be updated
|
|
290 to be correct for Solaris. */
|
442
|
291 static SIGTYPE
|
428
|
292 sighandler (int sig)
|
|
293 {
|
|
294 if (audio_fd > 0)
|
|
295 {
|
|
296 reset_device (0);
|
|
297 close (audio_fd);
|
|
298 }
|
|
299 if (sig == SIGHUP && sighup_handler)
|
|
300 sighup_handler (sig);
|
|
301 else if (sig == SIGINT && sigint_handler)
|
|
302 sigint_handler (sig);
|
|
303 else
|
|
304 exit (1);
|
|
305 }
|