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 "nativesound.h"
45 #include "syssignal.h"
46 # define perror(string) \
47 message("audio: %s, %s ", string, strerror (errno))
48 # define warn(str) message ("audio: %s ", GETTEXT (str))
50 # define warn(str) fprintf (stderr, "%s\n", (str))
53 static SIGTYPE (*sighup_handler) (int sig);
54 static SIGTYPE (*sigint_handler) (int sig);
55 static SIGTYPE sighandler (int sig);
59 #define audio_open() open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
61 static int initialized_device_p;
62 static int reset_volume_p, reset_device_p;
63 static double old_volume;
64 static Audio_hdr dev_hdr;
67 init_device (int volume, unsigned char *data, int fd,
68 unsigned int *header_length)
71 if (header_length) *header_length = 0;
79 if (data && fd) abort (); /* one or the other */
81 if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
83 perror ("Not a valid audio device");
87 if (AUDIO_SUCCESS != (data
88 ? audio_decode_filehdr (data, &file_hdr, header_length)
89 : audio_read_filehdr (fd, &file_hdr, 0, 0)))
92 perror ("invalid audio data");
94 perror ("invalid audio file");
98 audio_flush_play (audio_fd);
100 if (!initialized_device_p || (0 != audio_cmp_hdr (&dev_hdr, &file_hdr)))
105 initialized_device_p = 1;
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)
239 if (length == 0) return 0;
241 /* this is just to get a better error message */
242 if (strncmp (".snd\0", (char *) data, 4))
244 warn ("Not valid audio data (bad magic number)");
247 if (length <= sizeof (Audio_hdr))
249 warn ("Not valid audio data (too short)");
253 audio_fd = audio_open ();
257 /* where to find the proto for signal()... */
258 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
259 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
261 if (init_device (volume, data, 0, &ilen))
271 wrtn = write (audio_fd, (char *) (data+start), length-start);
282 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
288 sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
303 signal (SIGHUP, sighup_handler);
304 signal (SIGINT, sigint_handler);
309 /* #### sigcontext doesn't exist in Solaris. This should be updated
310 to be correct for Solaris. */
319 if (sig == SIGHUP && sighup_handler)
320 sighup_handler (sig);
321 else if (sig == SIGINT && sigint_handler)
322 sigint_handler (sig);