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