comparison src/sunplay.c @ 0:376386a54a3c r19-14

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