XEmacs 21.2.29 "Hestia".
[chise/xemacs-chise.git.1] / src / sunplay.c
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>
44 #include "syssignal.h"
45 # define perror(string) \
46     message("audio: %s, %s ", string, strerror (errno))
47 # define warn(str) message ("audio: %s ", GETTEXT (str))
48 #else /* !emacs */
49 # define warn(str) fprintf (stderr, "%s\n", (str))
50 #endif /* emacs */
51
52 static SIGTYPE (*sighup_handler) (int sig);
53 static SIGTYPE (*sigint_handler) (int sig);
54 static SIGTYPE sighandler (int sig);
55
56 static int audio_fd;
57
58 #define audio_open()    open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
59
60 static int reset_volume_p, reset_device_p;
61 static double old_volume;
62 static Audio_hdr dev_hdr;
63
64 void play_sound_file (char *name, int volume);
65 void play_sound_data (unsigned char *data, int length, int volume);
66
67 static int
68 init_device (int volume, unsigned char *data, int fd,
69              unsigned int *header_length)
70 {
71 #ifdef SUNOS4_0_3
72   if (header_length) *header_length = 0;
73   return 0;
74 #else
75   Audio_hdr file_hdr;
76
77   reset_volume_p = 0;
78   reset_device_p = 0;
79
80   if (data && fd) abort (); /* one or the other */
81
82   if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
83     {
84       perror ("Not a valid audio device");
85       return 1;
86     }
87
88   if (AUDIO_SUCCESS != (data
89                         ? audio_decode_filehdr (data, &file_hdr, header_length)
90                         : audio_read_filehdr (fd, &file_hdr, 0, 0)))
91     {
92       if (data)
93         perror ("invalid audio data");
94       else
95         perror ("invalid audio file");
96       return 1;
97     }
98
99   audio_flush_play (audio_fd);
100
101   if (0 != audio_cmp_hdr (&dev_hdr, &file_hdr))
102     {
103       Audio_hdr new_hdr;
104       new_hdr = file_hdr;
105       reset_device_p = 1;
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
230 void
231 play_sound_data (unsigned char *data, int length, int volume)
232 {
233   int wrtn, start = 0;
234   unsigned int ilen;
235
236   audio_fd = -1;
237
238   if (length == 0) return;
239
240   /* this is just to get a better error message */
241   if (strncmp (".snd\0", (char *) data, 4))
242     {
243       warn ("Not valid audio data (bad magic number)");
244       goto END_OF_PLAY;
245     }
246   if (length <= sizeof (Audio_hdr))
247     {
248       warn ("Not valid audio data (too short)");
249       goto END_OF_PLAY;
250     }
251
252   audio_fd = audio_open ();
253   if (audio_fd < 0)
254     {
255       perror ("open /dev/audio");
256       return;
257     }
258
259   /* where to find the proto for signal()... */
260   sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
261   sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
262
263   if (init_device (volume, data, 0, &ilen))
264     goto END_OF_PLAY;
265
266   data   += (ilen<<2);
267   length -= (ilen<<2);
268   if (length <= 1)
269     goto END_OF_PLAY;
270
271   while (1)
272     {
273       wrtn = write (audio_fd, (char *) (data+start), length-start);
274       if (wrtn < 0)
275         {
276           perror ("write");
277           goto END_OF_PLAY;
278         }
279       if (wrtn != 0)
280         {
281           start += wrtn;
282           break;
283         }
284       if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
285         goto END_OF_PLAY;
286     }
287   if (wrtn != length)
288     {
289       char buf [255];
290       sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
291       warn (buf);
292       goto END_OF_PLAY;
293     }
294
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);
305 }
306
307 /* #### sigcontext doesn't exist in Solaris.  This should be updated
308    to be correct for Solaris. */
309 static SIGTYPE
310 sighandler (int sig)
311 {
312   if (audio_fd > 0)
313     {
314       reset_device (0);
315       close (audio_fd);
316     }
317   if (sig == SIGHUP && sighup_handler)
318     sighup_handler (sig);
319   else if (sig == SIGINT && sigint_handler)
320     sigint_handler (sig);
321   else
322     exit (1);
323 }