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