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