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