annotate src/sunplay.c @ 54:05472e90ae02 r19-16-pre2

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