annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* play.c - play a sound file on the speaker
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 **
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 ** Copyright (C) 1989 by Jef Poskanzer.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4 **
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 ** Modified 24-May-91 by Jamie Zawinski (for Lucid Emacs).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6 ** Modified 17-Dec-92 by Jamie Zawinski (largely rewritten for SunOS 4.1.3).
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 **
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 ** Permission to use, copy, modify, and distribute this software and its
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 ** documentation for any purpose and without fee is hereby granted, provided
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 ** that the above copyright notice appear in all copies and that both that
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11 ** copyright notice and this permission notice appear in supporting
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 ** documentation. This software is provided "as is" without express or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ** implied warranty.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16 /* Synched up with: Not in FSF. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 #if __STDC__ || defined(STDC_HEADERS)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23 #include <stdlib.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 #include <fcntl.h> /* for open() */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 #include <string.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
30 #ifndef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
31 #include <sys/signal.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 #include <sys/fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 #include <sys/file.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36 /* libaudio.h includes a header which defines CONST. We temporarily
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 undefine it in order to eliminate a compiler warning. Yes, this is
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 a gross hack. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39 #undef CONST
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 #include <multimedia/libaudio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 #include <multimedia/audio_device.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 #undef CONST
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
43 #define CONST const
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
44
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 #ifdef emacs
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 # include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 # include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 # include "sysdep.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49 # include <errno.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 #include "syssignal.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 # define perror(string) \
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52 message("audio: %s, %s ", string, strerror (errno))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 # define warn(str) message ("audio: %s ", GETTEXT (str))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 #else /* !emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55 # define warn(str) fprintf (stderr, "%s\n", (str))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 #endif /* emacs */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 static SIGTYPE (*sighup_handler) (int sig);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 static SIGTYPE (*sigint_handler) (int sig);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60 static SIGTYPE sighandler (int sig);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 static int audio_fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #define audio_open() open ("/dev/audio", (O_WRONLY | O_NONBLOCK), 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 static int reset_volume_p, reset_device_p;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 static double old_volume;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 static Audio_hdr dev_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 void play_sound_file (char *name, int volume);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71 void play_sound_data (unsigned char *data, int length, int volume);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 static int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 init_device (int volume, unsigned char *data, int fd,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75 unsigned int *header_length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #ifdef SUNOS4_0_3
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 if (header_length) *header_length = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 Audio_hdr file_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83 reset_volume_p = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 reset_device_p = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86 if (data && fd) abort (); /* one or the other */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 if (AUDIO_SUCCESS != audio_get_play_config (audio_fd, &dev_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 perror ("Not a valid audio device");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 if (AUDIO_SUCCESS != (data
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 ? audio_decode_filehdr (data, &file_hdr, header_length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 : audio_read_filehdr (fd, &file_hdr, 0, 0)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 if (data)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 perror ("invalid audio data");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 perror ("invalid audio file");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105 audio_flush_play (audio_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 if (0 != audio_cmp_hdr (&dev_hdr, &file_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 Audio_hdr new_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110 new_hdr = file_hdr;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111 reset_device_p = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 if (AUDIO_SUCCESS != audio_set_play_config (audio_fd, &new_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114 char buf1 [100], buf2 [100], buf3 [250];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 audio_enc_to_str (&file_hdr, buf1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 audio_enc_to_str (&new_hdr, buf2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 sprintf (buf3, "wanted %s, got %s", buf1, buf2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118 warn (buf3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 if (volume < 0 || volume > 100)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 char buf [255];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126 sprintf (buf, "volume must be between 0 and 100 (not %d)", volume);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 warn (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 return 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 /* set the volume; scale it to 0.0 - 1.0 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 double V = (volume / 100.0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 audio_get_play_gain (audio_fd, &old_volume);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134 reset_volume_p = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 audio_set_play_gain (audio_fd, &V);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138 return 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 reset_device (int wait_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146 if (wait_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 audio_drain (audio_fd, 1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 audio_flush_play (audio_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 if (reset_device_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 audio_set_play_config (audio_fd, &dev_hdr);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 if (reset_volume_p)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 audio_set_play_gain (audio_fd, &old_volume);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 play_sound_file (char *sound_file, int volume)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 int rrtn, wrtn;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 unsigned char buf [255];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 int file_fd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
164 audio_fd = audio_open ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 if (audio_fd < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 perror ("open /dev/audio");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 /* where to find the proto for signal()... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 file_fd = open (sound_file, O_RDONLY, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 if (file_fd < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 perror (sound_file);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 if (init_device (volume, (unsigned char *) 0, file_fd, (unsigned int *) 0))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 rrtn = read (file_fd, (char *) buf, sizeof (buf));
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 if (rrtn < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191 perror ("read");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 if (rrtn == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199 wrtn = write (audio_fd, (char *) buf, rrtn);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 if (wrtn < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 perror ("write");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 if (wrtn != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211 if (wrtn != rrtn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 char warn_buf [255];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 sprintf (warn_buf, "play: rrtn = %d, wrtn = %d", rrtn, wrtn);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215 warn (warn_buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 END_OF_PLAY:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 if (file_fd > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223 close (file_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 if (audio_fd > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227 reset_device (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 close (audio_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 signal (SIGHUP, sighup_handler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 signal (SIGINT, sigint_handler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 play_sound_data (unsigned char *data, int length, int volume)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
239 int wrtn, start = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 unsigned int ilen;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242 audio_fd = -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
243
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 if (length == 0) return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246 /* this is just to get a better error message */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 if (strncmp (".snd\0", (char *) data, 4))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 warn ("Not valid audio data (bad magic number)");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252 if (length <= sizeof (Audio_hdr))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 warn ("Not valid audio data (too short)");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258 audio_fd = audio_open ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 if (audio_fd < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 perror ("open /dev/audio");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 return;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 /* where to find the proto for signal()... */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266 sighup_handler = (SIGTYPE (*) (int)) signal (SIGHUP, sighandler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 sigint_handler = (SIGTYPE (*) (int)) signal (SIGINT, sighandler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 if (init_device (volume, data, 0, &ilen))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 data += (ilen<<2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 length -= (ilen<<2);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 if (length <= 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279 wrtn = write (audio_fd, (char *) (data+start), length-start);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 if (wrtn < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 perror ("write");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285 if (wrtn != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 start += wrtn;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 if (AUDIO_ERR_INTERRUPTED == audio_drain (audio_fd, 1))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293 if (wrtn != length)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 char buf [255];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296 sprintf (buf, "play: rrtn = %d, wrtn = %d", length, wrtn);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 warn (buf);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 goto END_OF_PLAY;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 END_OF_PLAY:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 if (audio_fd > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 reset_device (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 close (audio_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 signal (SIGHUP, sighup_handler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 signal (SIGINT, sigint_handler);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 /* #### sigcontext doesn't exist in Solaris. This should be updated
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 to be correct for Solaris. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 static void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316 sighandler (int sig)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318 if (audio_fd > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 reset_device (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 close (audio_fd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323 if (sig == SIGHUP && sighup_handler)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
324 sighup_handler (sig);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
325 else if (sig == SIGINT && sigint_handler)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 sigint_handler (sig);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 else
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 }