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