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 #include <multimedia/libaudio.h>
37 #include <multimedia/audio_device.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))
49 # define warn(str) fprintf (stderr, "%s\n", (str))
52 static SIGTYPE (*sighup_handler) (int sig);
53 static SIGTYPE (*sigint_handler) (int sig);
54 static SIGTYPE sighandler (int sig);
58 #define audio_open() open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
60 static int reset_volume_p, reset_device_p;
61 static double old_volume;
62 static Audio_hdr dev_hdr;
64 void play_sound_file (char *name, int volume);
65 void play_sound_data (unsigned char *data, int length, int volume);
68 init_device (int volume, unsigned char *data, int fd,
69 unsigned int *header_length)
72 if (header_length) *header_length = 0;
80 if (data && fd) abort (); /* one or the other */
82 if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
84 perror ("Not a valid audio device");
88 if (AUDIO_SUCCESS != (data
89 ? audio_decode_filehdr (data, &file_hdr, header_length)
90 : audio_read_filehdr (fd, &file_hdr, 0, 0)))
93 perror ("invalid audio data");
95 perror ("invalid audio file");
99 audio_flush_play (audio_fd);
101 if (0 != audio_cmp_hdr (&dev_hdr, &file_hdr))
106 if (AUDIO_SUCCESS != audio_set_play_config (audio_fd, &new_hdr))
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);
117 if (volume < 0 || volume > 100)
120 sprintf (buf, "volume must be between 0 and 100 (not %d)", volume);
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);
129 audio_set_play_gain (audio_fd, &V);
138 reset_device (int wait_p)
141 audio_drain (audio_fd, 1);
143 audio_flush_play (audio_fd);
145 audio_set_play_config (audio_fd, &dev_hdr);
147 audio_set_play_gain (audio_fd, &old_volume);
152 play_sound_file (char *sound_file, int volume)
155 unsigned char buf [255];
158 audio_fd = audio_open ();
162 perror ("open /dev/audio");
166 /* where to find the proto for signal()... */
167 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
168 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
170 file_fd = open (sound_file, O_RDONLY, 0);
177 if (init_device (volume, (unsigned char *) 0, file_fd, (unsigned int *) 0))
182 rrtn = read (file_fd, (char *) buf, sizeof (buf));
193 wrtn = write (audio_fd, (char *) buf, rrtn);
202 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
208 sprintf (warn_buf, "play: rrtn = %d, wrtn = %d", rrtn, wrtn);
225 signal (SIGHUP, sighup_handler);
226 signal (SIGINT, sigint_handler);
231 play_sound_data (unsigned char *data, int length, int volume)
238 if (length == 0) return;
240 /* this is just to get a better error message */
241 if (strncmp (".snd\0", (char *) data, 4))
243 warn ("Not valid audio data (bad magic number)");
246 if (length <= sizeof (Audio_hdr))
248 warn ("Not valid audio data (too short)");
252 audio_fd = audio_open ();
255 perror ("open /dev/audio");
259 /* where to find the proto for signal()... */
260 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
261 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
263 if (init_device (volume, data, 0, &ilen))
273 wrtn = write (audio_fd, (char *) (data+start), length-start);
284 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
290 sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
303 signal (SIGHUP, sighup_handler);
304 signal (SIGINT, sigint_handler);
307 /* #### sigcontext doesn't exist in Solaris. This should be updated
308 to be correct for Solaris. */
317 if (sig == SIGHUP && sighup_handler)
318 sighup_handler (sig);
319 else if (sig == SIGINT && sigint_handler)
320 sigint_handler (sig);