1 /* play.c - play a sound file on the speaker
3 ** Copyright (C) 1989 by Jef Poskanzer.
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).
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
16 /* Synched up with: Not in FSF. */
22 #if __STDC__ || defined(STDC_HEADERS)
25 #include <fcntl.h> /* for open() */
31 #include <sys/signal.h>
33 #include <sys/fcntl.h>
36 /* libaudio.h includes a header which defines CONST. We temporarily
37 undefine it in order to eliminate a compiler warning. Yes, this is
40 #include <multimedia/libaudio.h>
41 #include <multimedia/audio_device.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))
55 # define warn(str) fprintf (stderr, "%s\n", (str))
58 static SIGTYPE (*sighup_handler) (int sig);
59 static SIGTYPE (*sigint_handler) (int sig);
60 static SIGTYPE sighandler (int sig);
64 #define audio_open() open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
66 static int reset_volume_p, reset_device_p;
67 static double old_volume;
68 static Audio_hdr dev_hdr;
70 void play_sound_file (char *name, int volume);
71 void play_sound_data (unsigned char *data, int length, int volume);
74 init_device (int volume, unsigned char *data, int fd,
75 unsigned int *header_length)
78 if (header_length) *header_length = 0;
86 if (data && fd) abort (); /* one or the other */
88 if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
90 perror ("Not a valid audio device");
94 if (AUDIO_SUCCESS != (data
95 ? audio_decode_filehdr (data, &file_hdr, header_length)
96 : audio_read_filehdr (fd, &file_hdr, 0, 0)))
99 perror ("invalid audio data");
101 perror ("invalid audio file");
105 audio_flush_play (audio_fd);
107 if (0 != audio_cmp_hdr (&dev_hdr, &file_hdr))
112 if (AUDIO_SUCCESS != audio_set_play_config (audio_fd, &new_hdr))
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);
123 if (volume < 0 || volume > 100)
126 sprintf (buf, "volume must be between 0 and 100 (not %d)", volume);
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);
135 audio_set_play_gain (audio_fd, &V);
144 reset_device (int wait_p)
147 audio_drain (audio_fd, 1);
149 audio_flush_play (audio_fd);
151 audio_set_play_config (audio_fd, &dev_hdr);
153 audio_set_play_gain (audio_fd, &old_volume);
158 play_sound_file (char *sound_file, int volume)
161 unsigned char buf [255];
164 audio_fd = audio_open ();
168 perror ("open /dev/audio");
172 /* where to find the proto for signal()... */
173 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
174 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
176 file_fd = open (sound_file, O_RDONLY, 0);
183 if (init_device (volume, (unsigned char *) 0, file_fd, (unsigned int *) 0))
188 rrtn = read (file_fd, (char *) buf, sizeof (buf));
199 wrtn = write (audio_fd, (char *) buf, rrtn);
208 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
214 sprintf (warn_buf, "play: rrtn = %d, wrtn = %d", rrtn, wrtn);
231 signal (SIGHUP, sighup_handler);
232 signal (SIGINT, sigint_handler);
237 play_sound_data (unsigned char *data, int length, int volume)
244 if (length == 0) return;
246 /* this is just to get a better error message */
247 if (strncmp (".snd\0", (char *) data, 4))
249 warn ("Not valid audio data (bad magic number)");
252 if (length <= sizeof (Audio_hdr))
254 warn ("Not valid audio data (too short)");
258 audio_fd = audio_open ();
261 perror ("open /dev/audio");
265 /* where to find the proto for signal()... */
266 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
267 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
269 if (init_device (volume, data, 0, &ilen))
279 wrtn = write (audio_fd, (char *) (data+start), length-start);
290 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
296 sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
309 signal (SIGHUP, sighup_handler);
310 signal (SIGINT, sigint_handler);
313 /* #### sigcontext doesn't exist in Solaris. This should be updated
314 to be correct for Solaris. */
323 if (sig == SIGHUP && sighup_handler)
324 sighup_handler (sig);
325 else if (sig == SIGINT && sigint_handler)
326 sigint_handler (sig);