annotate src/linuxplay.c @ 388:aabb7f5b1c81 r21-2-9

Import from CVS: tag r21-2-9
author cvs
date Mon, 13 Aug 2007 11:09:42 +0200
parents 8626e4521993
children 74fd4e045ea6
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 /* linuxplay.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) 1995,96 by Markus Gutschke (gutschk@math.uni-muenster.de)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
4 ** This is version 1.3 of linuxplay.c
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
5 **
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
6 ** Parts of this code were inspired by sunplay.c, which is copyright 1989 by
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
7 ** Jef Poskanzer and 1991,92 by Jamie Zawinski; c.f. sunplay.c for further
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
8 ** information.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
9 **
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
10 ** Permission to use, copy, modify, and distribute this software and its
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
11 ** documentation for any purpose and without fee is hereby granted, provided
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
12 ** that the above copyright notice appear in all copies and that both that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
13 ** copyright notice and this permission notice appear in supporting
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
14 ** documentation. This software is provided "as is" without express or
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
15 ** implied warranty.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
16 **
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
17 ** Changelog:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
18 ** 1.0 -- first release; supports SunAudio, Wave and RAW file formats
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
19 ** detects (and rejects) VOC file format
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
20 ** tested with PC-Speaker driver only
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
21 ** 1.1 -- fixed bug with playback of stereo Wave files
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
22 ** fixed VOC file detection
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
23 ** fixed mono/8bit conversion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
24 ** cleaned up mixer programming (c.f. VoxWare-SDK)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
25 ** tested with PC-Speaker driver and with PAS16 soundcard
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
26 ** 1.2 -- first (incompatible) attempt at fixing reliable signal handling
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
27 ** 1.3 -- changed signal handling to use reliable signals; this is done
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
28 ** by including "syssignal.h"; it fixes nasty program crashes
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
29 ** when using native sound in TTY mode.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
30 ** added support for DEC audio file format (this is basically the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
31 ** same as Sun audio, but uses little endian format, instead).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
32 ** strip the header from Sun audio and DEC audio files in order to
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
33 ** prevent noise at beginning of samples (thanks to Thomas Pundt
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
34 ** <pundtt@math.uni-muenster.de> for pointing out this bug and
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
35 ** providing information on the file format).
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
36 ** added a few more conversion routines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
37 ** made the code even more tolerant to the limits imposed by some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
38 ** soundcards and try to accept soundfiles even if they are not
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
39 ** fully conformant to the standard.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
40 ** 1.4 -- increased header size to 256; I hope there is no sample software
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
41 ** that requires this much.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
42 ** added code for converting from signed to unsigned format as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
43 ** some soundcards cannot handle signed 8bit data.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
44 */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
45
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
46 /* Synched up with: Not in FSF. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
47
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
48 #define HEADERSZ 256 /* has to be at least as big as the biggest header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
49 #define SNDBUFSZ 2048 /* has to be at least as big as HEADERSZ */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
50
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
51 /* XEmacs beta testers say: undef this by default. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
52 #undef NOVOLUMECTRLFORMULAW /* Changing the volume for uLaw-encoded
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
53 samples sounds very poor; possibly,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
54 this is true only for the PC-Snd
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
55 driver, so undefine this symbol at your
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
56 discretion */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
57
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
58 #ifdef HAVE_CONFIG_H
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
59 #include <config.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
60 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
61
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
62 #include <errno.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
63 #include <fcntl.h>
149
538048ae2ab8 Import from CVS: tag r20-3b1
cvs
parents: 129
diff changeset
64 #include SOUNDCARD_H_PATH /* Path computed by configure */
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
65 #include <stdio.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
66 #include <stdlib.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
67 #include <string.h>
377
d883f39b8495 Import from CVS: tag r21-2b4
cvs
parents: 371
diff changeset
68 #include <fcntl.h>
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
69 #include <sys/file.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
70 #include <sys/ioctl.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
71 #include <sys/signal.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
72 #include <unistd.h>
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
73
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
74 #ifdef LINUXPLAYSTANDALONE
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
75 #define perror(str) fprintf(stderr,"audio: %s %s\n",str,strerror(errno));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
76 #define warn(str) fprintf(stderr,"audio: %s\n",str);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
77 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
78 #include "lisp.h"
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
79 #include "syssignal.h"
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
80 #include "sysfile.h"
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
81 #define perror(str) message("audio: %s, %s ",str,strerror(errno))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
82 #define warn(str) message("audio: %s ",GETTEXT(str))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
83 #endif
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
84
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
85 #ifdef __GNUC__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
86 #define UNUSED(x) ((void)(x))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
87 #else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
88 #define UNUSED(x)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
89 #define __inline__
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
90 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
91
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
92 static void (*sighup_handler)(int);
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
93 static void (*sigint_handler)(int);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
94
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
95 /* Maintain global variable for keeping parser state information; this struct
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
96 is set to zero before the first invocation of the parser. The use of a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
97 global variable prevents multiple concurrent executions of this code, but
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
98 this does not happen anyways... */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
99 enum wvState
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
100 { wvMain,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
101 wvSubchunk,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
102 wvOutOfBlock,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
103 wvSkipChunk,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
104 wvSoundChunk,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
105 wvFatal,
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
106 wvFatalNotify
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
107 };
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
108
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
109 static union {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
110 struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
111 int align;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
112 enum wvState state;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
113 size_t left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
114 unsigned char leftover[HEADERSZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
115 signed long chunklength;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
116 } wave;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
117 struct {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
118 int align;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
119 int isdata;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
120 int skipping;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
121 size_t left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
122 unsigned char leftover[HEADERSZ];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
123 } audio;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
124 } parsestate;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
125
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
126 /* Use a global buffer as scratch-pad for possible conversions of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
127 sampling format */
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
128 unsigned char linuxplay_sndbuf[SNDBUFSZ];
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
129
282
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 272
diff changeset
130 static int mix_fd;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 272
diff changeset
131 static int audio_vol;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 272
diff changeset
132 static int audio_fd;
c42ec1d1cded Import from CVS: tag r21-0b39
cvs
parents: 272
diff changeset
133 static char *audio_dev = "/dev/dsp";
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
134
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
135 typedef enum {fmtIllegal,fmtRaw,fmtVoc,fmtWave,fmtSunAudio} fmtType;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
136
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
137 /* Intercept SIGINT and SIGHUP in order to close the audio and mixer
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
138 devices before terminating sound output; this requires reliable
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
139 signals as provided by "syssignal.h" */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
140 static void sighandler(int sig)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
141 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
142 if (mix_fd > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
143 if (audio_vol >= 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
144 ioctl(mix_fd,SOUND_MIXER_WRITE_PCM,&audio_vol);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
145 audio_vol = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
146 if (mix_fd != audio_fd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
147 close(mix_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
148 mix_fd = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
149 if (audio_fd > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
150 ioctl(audio_fd,SNDCTL_DSP_SYNC,NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
151 ioctl(audio_fd,SNDCTL_DSP_RESET,NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
152 close(audio_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
153 audio_fd = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
154 if (sig == SIGHUP && sighup_handler) sighup_handler(sig);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
155 else if (sig == SIGINT && sigint_handler) sigint_handler(sig);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
156 else exit(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
157 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
158
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
159 /* There is no special treatment required for parsing raw data files; we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
160 assume that these files contain data in 8bit unsigned format that
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
161 has been sampled at 8kHz; there is no extra header */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
162 static size_t parseraw(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
163 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
164 int rc = *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
165
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
166 *outbuf = *data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
167 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
168 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
169 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
170
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
171 /* Currently we cannot cope with files in VOC format; if you really need
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
172 to play these files, they should be converted by using SOX */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
173 static size_t parsevoc(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
174 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
175 UNUSED(data);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
176 UNUSED(sz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
177 UNUSED(outbuf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
178 return(0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
179 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
180
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
181 /* We need to perform some look-ahead in order to parse files in WAVE format;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
182 this might require re-partioning of the data segments if headers cross the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
183 boundaries between two read operations. This is done in a two-step way:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
184 first we request a certain amount of bytes... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
185 static __inline__ int waverequire(void **data,size_t *sz,size_t rq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
186 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
187 int rc = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
188
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
189 if (rq > HEADERSZ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
190 warn("Header size exceeded while parsing WAVE file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
191 parsestate.wave.state = wvFatal;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
192 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
193 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
194 if ((rq -= parsestate.wave.left) <= 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
195 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
196 if (rq > *sz) {rq = *sz; rc = 0;}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
197 memcpy(parsestate.wave.leftover+parsestate.wave.left,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
198 *data,rq);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
199 parsestate.wave.left += rq;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
200 (*(unsigned char **)data) += rq;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
201 *sz -= rq;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
202 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
203 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
204
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
205 /* ...and next we remove this many bytes from the buffer */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
206 static __inline__ void waveremove(size_t rq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
207 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
208 if (parsestate.wave.left <= rq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
209 parsestate.wave.left = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
210 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
211 parsestate.wave.left -= rq;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
212 memmove(parsestate.wave.leftover,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
213 parsestate.wave.leftover+rq,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
214 parsestate.wave.left); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
215 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
216 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
217
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
218 /* Sound files in WAVE format can contain an arbitrary amount of tagged
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
219 chunks; this requires quite some effort for parsing the data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
220 static size_t parsewave(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
221 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
222 for (;;)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
223 switch (parsestate.wave.state) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
224 case wvMain:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
225 if (!waverequire(data,sz,20))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
226 return(0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
227 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
228 parsestate.wave.chunklength = parsestate.wave.leftover[16] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
229 256*(parsestate.wave.leftover[17] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
230 256*(parsestate.wave.leftover[18] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
231 256*parsestate.wave.leftover[19]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
232 waveremove(20);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
233 parsestate.wave.state = wvSubchunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
234 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
235 case wvSubchunk:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
236 if (!waverequire(data,sz,parsestate.wave.chunklength))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
237 return(0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
238 parsestate.wave.align = parsestate.wave.chunklength < 14 ? 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
239 : parsestate.wave.leftover[12];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
240 if (parsestate.wave.align != 1 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
241 parsestate.wave.align != 2 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
242 parsestate.wave.align != 4) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
243 warn("Illegal datawidth detected while parsing WAVE file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
244 parsestate.wave.state = wvFatal; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
245 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
246 parsestate.wave.state = wvOutOfBlock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
247 waveremove(parsestate.wave.chunklength);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
248 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
249 case wvOutOfBlock:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
250 if (!waverequire(data,sz,8))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
251 return(0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
252 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
253 parsestate.wave.chunklength = parsestate.wave.leftover[4] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
254 256*(parsestate.wave.leftover[5] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
255 256*(parsestate.wave.leftover[6] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
256 256*(parsestate.wave.leftover[7] & 0x7F)));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
257 if (memcmp(parsestate.wave.leftover,"data",4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
258 parsestate.wave.state = wvSkipChunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
259 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
260 parsestate.wave.state = wvSoundChunk;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
261 waveremove(8);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
262 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
263 case wvSkipChunk:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
264 if (parsestate.wave.chunklength > 0 && *sz > 0 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
265 (signed long)*sz < (signed long)parsestate.wave.chunklength) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
266 parsestate.wave.chunklength -= *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
267 *sz = 0; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
268 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
269 if (parsestate.wave.chunklength > 0 && *sz > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
270 *sz -= parsestate.wave.chunklength;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
271 (*(unsigned char **)data) += parsestate.wave.chunklength; }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
272 parsestate.wave.state = wvOutOfBlock; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
273 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
274 case wvSoundChunk: {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
275 size_t count,rq;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
276 if (parsestate.wave.left) { /* handle leftover bytes from last
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
277 alignment operation */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
278 count = parsestate.wave.left;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
279 rq = HEADERSZ-count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
280 if (rq > (size_t) parsestate.wave.chunklength)
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
281 rq = parsestate.wave.chunklength;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
282 if (!waverequire(data,sz,rq)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
283 parsestate.wave.chunklength -= parsestate.wave.left - count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
284 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
285 parsestate.wave.chunklength -= rq;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
286 *outbuf = parsestate.wave.leftover;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
287 parsestate.wave.left = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
288 return(rq); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
289 if (*sz >= (size_t) parsestate.wave.chunklength) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
290 count = parsestate.wave.chunklength;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
291 rq = 0; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
292 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
293 count = *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
294 count -= rq = count % parsestate.wave.align; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
295 *outbuf = *data;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
296 (*(unsigned char **)data) += count;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
297 *sz -= count;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
298 if ((parsestate.wave.chunklength -= count) < parsestate.wave.align) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
299 parsestate.wave.state = wvOutOfBlock;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
300 /* Some broken software (e.g. SOX) attaches junk to the end of a sound
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
301 chunk; so, let's ignore this... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
302 if (parsestate.wave.chunklength)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
303 parsestate.wave.state = wvSkipChunk; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
304 else if (rq)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
305 /* align data length to a multiple of datasize; keep additional data
110
fe104dbd9147 Import from CVS: tag r20-1b7
cvs
parents: 98
diff changeset
306 in "leftover" buffer --- this is necessary to ensure proper
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
307 functioning of the sndcnv... routines */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
308 waverequire(data,sz,rq);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
309 return(count); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
310 case wvFatalNotify:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
311 warn("Irrecoverable error while parsing WAVE file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
312 parsestate.wave.state = wvFatal;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
313 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
314 case wvFatal:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
315 default:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
316 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
317 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
318 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
319
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
320 /* Strip the header from files in Sun/DEC audio format; this requires some
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
321 extra processing as the header can be an arbitrary size and it might
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
322 result in alignment errors for subsequent conversions --- thus we do
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
323 some buffering, where needed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
324 static size_t parsesundecaudio(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
325 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
326 /* There is data left over from the last invocation of this function; join
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
327 it with the new data and return a sound chunk that is as big as a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
328 single entry */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
329 if (parsestate.audio.left) {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
330 if (parsestate.audio.left + *sz > (size_t) parsestate.audio.align) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
331 int count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
332 memmove(parsestate.audio.leftover + parsestate.audio.left,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
333 *data,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
334 count = parsestate.audio.align - parsestate.audio.left);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
335 *outbuf = parsestate.audio.leftover;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
336 *sz -= count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
337 *data = (*(char **)data) + count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
338 parsestate.audio.left = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
339 return(parsestate.audio.align); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
340 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
341 /* We need even more data in order to get one complete single entry! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
342 memmove(parsestate.audio.leftover + parsestate.audio.left,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
343 *data,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
344 *sz);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
345 *data = (*(char **)data) + *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
346 parsestate.audio.left += *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
347 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
348 return(0); } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
349
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
350 /* This is the main sound chunk, strip of any extra data that does not fit
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
351 the alignment requirements and move these bytes into the leftover buffer*/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
352 if (parsestate.audio.isdata) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
353 int rc = *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
354 *outbuf = *data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
355 if ((parsestate.audio.left = rc % parsestate.audio.align) != 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
356 memmove(parsestate.audio.leftover,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
357 (char *)*outbuf + rc - parsestate.audio.left,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
358 parsestate.audio.left);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
359 rc -= parsestate.audio.left; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
360 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
361 return(rc); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
362
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
363 /* This is the first invocation of this function; we need to parse the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
364 header information and determine how many bytes we need to skip until
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
365 the start of the sound chunk */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
366 if (!parsestate.audio.skipping) {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
367 unsigned char *header = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
368 if (*sz < 8) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
369 warn("Irrecoverable error while parsing Sun/DEC audio file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
370 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
371 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
372 if (header[3]) { /* Sun audio (big endian) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
373 parsestate.audio.align = ((header[15] > 2)+1)*header[23];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
374 parsestate.audio.skipping = header[7]+256*(header[6]+256*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
375 (header[5]+256*header[4])); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
376 else { /* DEC audio (little endian) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
377 parsestate.audio.align = ((header[12] > 2)+1)*header[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
378 parsestate.audio.skipping = header[4]+256*(header[5]+256*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
379 (header[6]+256*header[7])); }}
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
380
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
381 /* We are skipping extra data that has been attached to header; most usually
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
382 this will be just a comment, such as the original filename and/or the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
383 creation date. Make sure that we do not return less than one single sound
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
384 sample entry to the caller; if this happens, rather decide to move those
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
385 few bytes into the leftover buffer and deal with it later */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
386 if (*sz >= (size_t) parsestate.audio.skipping) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
387 /* Skip just the header information and return the sound chunk */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
388 int rc = *sz - parsestate.audio.skipping;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
389 *outbuf = (char *)*data + parsestate.audio.skipping;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
390 if ((parsestate.audio.left = rc % parsestate.audio.align) != 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
391 memmove(parsestate.audio.leftover,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
392 (char *)*outbuf + rc - parsestate.audio.left,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
393 parsestate.audio.left);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
394 rc -= parsestate.audio.left; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
395 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
396 parsestate.audio.skipping = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
397 parsestate.audio.isdata++;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
398 return(rc); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
399 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
400 /* Skip everything */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
401 parsestate.audio.skipping -= *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
402 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
403 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
404
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
405 /* If the soundcard could not be set to natively support the data format, we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
406 try to do some limited on-the-fly conversion to a different format; if
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
407 no conversion is needed, though, we can output directly */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
408 static size_t sndcnvnop(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
409 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
410 int rc = *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
411
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
412 *outbuf = *data;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
413 *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
414 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
415 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
416
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
417 /* Convert 8 bit unsigned stereo data to 8 bit unsigned mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
418 static size_t sndcnv8U_2mono(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
419 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
420 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
421 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
422 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
423
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
424 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
425 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
426 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
427 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
428 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
429 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
430 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
431 while (count--)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
432 *dest++ = (unsigned char)(((int)*(src)++ +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
433 (int)*(src)++) / 2);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
434 *data = src;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
435 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
436 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
437
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
438 /* Convert 8 bit signed stereo data to 8 bit signed mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
439 static size_t sndcnv8S_2mono(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
440 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
441 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
442 REGISTER unsigned char *dest;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
443 int rc, count;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
444
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
445 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
446 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
447 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
448 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
449 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
450 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
451 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
452 while (count--)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
453 *dest++ = (unsigned char)(((int)*((signed char *)(src++)) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
454 (int)*((signed char *)(src++))) / 2);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
455 *data = src;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
456 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
457 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
458
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
459 /* Convert 8 bit signed stereo data to 8 bit unsigned mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
460 static size_t sndcnv2monounsigned(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
461 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
462 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
463 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
464 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
465
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
466 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
467 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
468 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
469 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
470 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
471 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
472 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
473 while (count--)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
474 *dest++ = (unsigned char)(((int)*((signed char *)(src++)) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
475 (int)*((signed char *)(src++))) / 2) ^ 0x80;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
476 *data = src;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
477 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
478 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
479
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
480 /* Convert 8 bit signed mono data to 8 bit unsigned mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
481 static size_t sndcnv2unsigned(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
482 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
483 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
484 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
485 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
486
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
487 count = *sz;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
488 if (count > SNDBUFSZ) { *sz -= SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
489 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
490 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
491 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
492 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
493 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
494 while (count--)
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
495 *dest++ = *(src)++ ^ 0x80;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
496 *data = src;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
497 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
498 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
499
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
500 /* Convert a number in the range -32768..32767 to an 8 bit ulaw encoded
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
501 number --- I hope, I got this conversion right :-) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
502 static __inline__ signed char int2ulaw(int i)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
503 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
504 /* Lookup table for fast calculation of number of bits that need shifting*/
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
505 static short int t_bits[128] = {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
506 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
507 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
508 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
509 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7};
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
510 REGISTER int bits,logi;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
511
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
512 /* unrolling this condition (hopefully) improves execution speed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
513 if (i < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
514 if ((i = (132-i)) > 0x7FFF) i = 0x7FFF;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
515 logi = (i >> ((bits = t_bits[i/256])+4));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
516 return((bits << 4 | logi) ^ 0x7F); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
517 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
518 if ((i = 132+i) > 0x7FFF) i = 0x7FFF;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
519 logi = (i >> ((bits = t_bits[i/256])+4));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
520 return(~(bits << 4 | logi)); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
521 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
522
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
523 /* Convert 8 bit ulaw stereo data to 8 bit ulaw mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
524 static size_t sndcnvULaw_2mono(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
525 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
526
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
527 static short int ulaw2int[256] = {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
528 /* Precomputed lookup table for conversion from ulaw to 15 bit signed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
529 -16062,-15550,-15038,-14526,-14014,-13502,-12990,-12478,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
530 -11966,-11454,-10942,-10430, -9918, -9406, -8894, -8382,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
531 -7998, -7742, -7486, -7230, -6974, -6718, -6462, -6206,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
532 -5950, -5694, -5438, -5182, -4926, -4670, -4414, -4158,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
533 -3966, -3838, -3710, -3582, -3454, -3326, -3198, -3070,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
534 -2942, -2814, -2686, -2558, -2430, -2302, -2174, -2046,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
535 -1950, -1886, -1822, -1758, -1694, -1630, -1566, -1502,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
536 -1438, -1374, -1310, -1246, -1182, -1118, -1054, -990,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
537 -942, -910, -878, -846, -814, -782, -750, -718,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
538 -686, -654, -622, -590, -558, -526, -494, -462,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
539 -438, -422, -406, -390, -374, -358, -342, -326,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
540 -310, -294, -278, -262, -246, -230, -214, -198,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
541 -186, -178, -170, -162, -154, -146, -138, -130,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
542 -122, -114, -106, -98, -90, -82, -74, -66,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
543 -60, -56, -52, -48, -44, -40, -36, -32,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
544 -28, -24, -20, -16, -12, -8, -4, +0,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
545 +16062,+15550,+15038,+14526,+14014,+13502,+12990,+12478,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
546 +11966,+11454,+10942,+10430, +9918, +9406, +8894, +8382,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
547 +7998, +7742, +7486, +7230, +6974, +6718, +6462, +6206,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
548 +5950, +5694, +5438, +5182, +4926, +4670, +4414, +4158,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
549 +3966, +3838, +3710, +3582, +3454, +3326, +3198, +3070,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
550 +2942, +2814, +2686, +2558, +2430, +2302, +2174, +2046,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
551 +1950, +1886, +1822, +1758, +1694, +1630, +1566, +1502,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
552 +1438, +1374, +1310, +1246, +1182, +1118, +1054, +990,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
553 +942, +910, +878, +846, +814, +782, +750, +718,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
554 +686, +654, +622, +590, +558, +526, +494, +462,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
555 +438, +422, +406, +390, +374, +358, +342, +326,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
556 +310, +294, +278, +262, +246, +230, +214, +198,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
557 +186, +178, +170, +162, +154, +146, +138, +130,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
558 +122, +114, +106, +98, +90, +82, +74, +66,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
559 +60, +56, +52, +48, +44, +40, +36, +32,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
560 +28, +24, +20, +16, +12, +8, +4, +0};
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
561
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
562 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
563 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
564 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
565
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
566 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
567 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
568 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
569 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
570 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
571 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
572 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
573 while (count--)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
574 /* it is not possible to directly interpolate between two ulaw encoded
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
575 data bytes, thus we need to convert to linear format first and later
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
576 we convert back to ulaw format */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
577 *dest++ = int2ulaw(ulaw2int[*(src)++] +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
578 ulaw2int[*(src)++]);
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
579 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
580 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
581 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
582
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
583 /* Convert 16 bit little endian signed stereo data to 16 bit little endian
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
584 signed mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
585 static size_t sndcnv16_2monoLE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
586 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
587 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
588 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
589 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
590 signed short i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
591
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
592 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
593 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
594 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
595 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
596 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
597 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
598 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
599 for (count /= 2; count--; ) {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
600 i = ((int)(src[0]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
601 256*(int)(src[1]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
602 (int)(src[2]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
603 256*(int)(src[3])) / 2;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
604 src += 4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
605 *dest++ = (unsigned char)(i & 0xFF);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
606 *dest++ = (unsigned char)((i / 256) & 0xFF); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
607 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
608 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
609 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
610
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
611 /* Convert 16 bit big endian signed stereo data to 16 bit big endian
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
612 signed mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
613 static size_t sndcnv16_2monoBE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
614 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
615 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
616 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
617 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
618 signed short i;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
619
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
620 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
621 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
622 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
623 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
624 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
625 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
626 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
627 for (count /= 2; count--; ) {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
628 i = ((int)(src[1]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
629 256*(int)(src[0]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
630 (int)(src[3]) +
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
631 256*(int)(src[2])) / 2;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
632 src += 4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
633 *dest++ = (unsigned char)((i / 256) & 0xFF);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
634 *dest++ = (unsigned char)(i & 0xFF); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
635 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
636 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
637 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
638
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
639 /* Convert 16 bit little endian signed data to 8 bit unsigned data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
640 static size_t sndcnv2byteLE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
641 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
642 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
643 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
644 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
645
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
646 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
647 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
648 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
649 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
650 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
651 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
652 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
653 while (count--) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
654 *dest++ = (unsigned char)(((signed char *)src)[1] ^ (signed char)0x80);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
655 src += 2;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
656 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
657 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
658 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
659 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
660
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
661 /* Convert 16 bit big endian signed data to 8 bit unsigned data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
662 static size_t sndcnv2byteBE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
663 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
664 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
665 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
666 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
667
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
668 count = *sz / 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
669 if (count > SNDBUFSZ) { *sz -= 2*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
670 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
671 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
672 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
673 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
674 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
675 while (count--) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
676 *dest++ = (unsigned char)(((signed char *)src)[0] ^ (signed char)0x80);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
677 src += 2;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
678 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
679 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
680 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
681 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
682
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
683 /* Convert 16 bit little endian signed stereo data to 8 bit unsigned
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
684 mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
685 static size_t sndcnv2monobyteLE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
686 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
687 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
688 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
689 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
690
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
691 count = *sz / 4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
692 if (count > SNDBUFSZ) { *sz -= 4*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
693 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
694 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
695 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
696 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
697 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
698 while (count--) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
699 *dest++ = (unsigned char)(((int)((signed char *)src)[1] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
700 (int)((signed char *)src)[3]) / 2 ^ 0x80);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
701 src += 4;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
702 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
703 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
704 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
705 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
706
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
707 /* Convert 16 bit big endian signed stereo data to 8 bit unsigned
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
708 mono data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
709 static size_t sndcnv2monobyteBE(void **data,size_t *sz,void **outbuf)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
710 {
203
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
711 REGISTER unsigned char *src;
850242ba4a81 Import from CVS: tag r20-3b28
cvs
parents: 149
diff changeset
712 REGISTER unsigned char *dest;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
713 int rc,count;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
714
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
715 count = *sz / 4;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
716 if (count > SNDBUFSZ) { *sz -= 4*SNDBUFSZ; count = SNDBUFSZ; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
717 else *sz = 0;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
718 rc = count;
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
719 src = (unsigned char *) *data;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
720 *outbuf =
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
721 dest = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
722 while (count--) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
723 *dest++ = (unsigned char)(((int)((signed char *)src)[0] +
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
724 (int)((signed char *)src)[2]) / 2 ^ 0x80);
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
725 src += 4;
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
726 }
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
727 *data = src;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
728 return(rc);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
729 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
730
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
731 /* Look at the header of the sound file and try to determine the format;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
732 we can recognize files in VOC, WAVE, and, Sun/DEC-audio format--- everything
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
733 else is assumed to be raw 8 bit unsigned data sampled at 8kHz */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
734 static fmtType analyze_format(unsigned char *format,int *fmt,int *speed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
735 int *tracks,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
736 size_t (**parsesndfile)(void **,size_t *sz,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
737 void **))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
738 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
739 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
740 if (!memcmp(format,"Creative Voice File\x1A\x1A\x00",22) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
741 (format[22]+256*format[23]) ==
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
742 ((0x1233-format[24]-256*format[25])&0xFFFF)) { /* VOC */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
743 *fmt = AFMT_U8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
744 *speed = 8000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
745 *tracks = 2;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
746 *parsesndfile = parsevoc;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
747 return(fmtVoc); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
748 else if (!memcmp(format,"RIFF",4) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
749 !memcmp(format+8,"WAVEfmt ",8)) { /* WAVE */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
750 if (memcmp(format+20,"\001\000\001"/* PCM mono */,4) &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
751 memcmp(format+20,"\001\000\002"/* PCM stereo */,4))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
752 return(fmtIllegal);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
753 *fmt = (format[32]/(*tracks = format[22])) == 1 ?
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
754 AFMT_U8 : AFMT_S16_LE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
755 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
756 *speed = format[24]+256*(format[25]+256*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
757 (format[26]+256*format[27]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
758 *parsesndfile = parsewave;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
759 return(fmtWave); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
760 else if (!memcmp(format,".snd",4)) { /* Sun Audio (big endian) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
761 if (format[7]+256*(format[6]+256*(format[5]+256*format[4])) < 24) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
762 *fmt = AFMT_MU_LAW;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
763 *speed = 8000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
764 *tracks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
765 *parsesndfile = parsesundecaudio;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
766 return(fmtSunAudio); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
767 if (!memcmp(format+12,"\000\000\000\001",4)) *fmt = AFMT_MU_LAW;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
768 else if (!memcmp(format+12,"\000\000\000\002",4)) *fmt = AFMT_S8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
769 else if (!memcmp(format+12,"\000\000\000\003",4)) *fmt = AFMT_S16_BE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
770 else return(fmtIllegal);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
771 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
772 *speed = format[19]+256*(format[18]+256*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
773 (format[17]+256*format[16]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
774 *tracks = format[23];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
775 *parsesndfile = parsesundecaudio;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
776 return(fmtSunAudio); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
777 else if (!memcmp(format,".sd",4)) { /* DEC Audio (little endian) */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
778 if (format[4]+256*(format[5]+256*(format[6]+256*format[7])) < 24) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
779 *fmt = AFMT_MU_LAW;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
780 *speed = 8000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
781 *tracks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
782 *parsesndfile = parsesundecaudio;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
783 return(fmtSunAudio); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
784 if (!memcmp(format+12,"\001\000\000",4)) *fmt = AFMT_MU_LAW;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
785 else if (!memcmp(format+12,"\002\000\000",4)) *fmt = AFMT_S8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
786 else if (!memcmp(format+12,"\003\000\000",4)) *fmt = AFMT_S16_LE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
787 else return(fmtIllegal);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
788 /* Keep compatibility with Linux 68k, etc. by not relying on byte-sex */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
789 *speed = format[16]+256*(format[17]+256*
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
790 (format[18]+256*format[19]));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
791 *tracks = format[20];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
792 *parsesndfile = parsesundecaudio;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
793 return(fmtSunAudio); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
794 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
795 *fmt = AFMT_U8;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
796 *speed = 8000;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
797 *tracks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
798 *parsesndfile = parseraw;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
799 return(fmtRaw); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
800 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
801
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
802 /* Initialize the soundcard and mixer device with the parameters that we
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
803 found in the header of the sound file. If the soundcard is not capable of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
804 natively supporting the required parameters, then try to set up conversion
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
805 routines.
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
806 The difficulty with setting up the sound card is that the parameters are
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
807 not fully orthogonal; changing one of them might affect some of the
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
808 others, too. Thus we do quite a lot of double checking; actually most of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
809 this is not needed right now, but it will come in handy, if the kernel's
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
810 sounddriver ever changes or if third-party sounddrivers are used. */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
811 static int audio_init(int mixx_fd, int auddio_fd, int fmt, int speed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
812 int tracks, int *volume,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
813 size_t (**sndcnv) (void **, size_t *sz, void **))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
814 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
815 int i,the_speed,the_stereo,the_fmt;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
816
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
817 *sndcnv = sndcnvnop;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
818
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
819 if (ioctl(auddio_fd,SNDCTL_DSP_SYNC,NULL) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
820 perror("SNDCTL_DSP_SYNC");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
821 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
822
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
823 /* Initialize sound hardware with preferred parameters */
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
824
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
825 /* If the sound hardware cannot support 16 bit format or requires a
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
826 different byte sex then try to drop to 8 bit format */
129
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
827
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
828 the_fmt = fmt;
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
829 if(ioctl(audio_fd,SNDCTL_DSP_SETFMT,&the_fmt) < 0) {
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
830 perror("SNDCTL_DSP_SETFMT");
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
831 return(0);
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
832 }
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
833
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
834 if (fmt != the_fmt) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
835 if (fmt == AFMT_S16_LE || fmt == AFMT_S16_BE) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
836 *sndcnv = fmt == AFMT_S16_BE ? sndcnv2byteBE : sndcnv2byteLE;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
837 if (((i=fmt=AFMT_U8),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
838 fmt != i || ioctl(audio_fd,SNDCTL_DSP_SETFMT,&the_fmt) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
839 fmt != the_fmt) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
840 perror("SNDCTL_DSP_SETFMT");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
841 return(0); } }
209
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
842 else if (fmt == AFMT_MU_LAW && the_fmt == AFMT_U8 ) {
41ff10fd062f Import from CVS: tag r20-4b3
cvs
parents: 203
diff changeset
843 /* the kernel will convert for us */ }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
844 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
845 perror("SNDCTL_DSP_SETFMT");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
846 return(0); } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
847 else if (fmt == AFMT_S8) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
848 *sndcnv = sndcnv2unsigned;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
849 if (((i=fmt=AFMT_U8),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
850 fmt != i || ioctl(audio_fd,SNDCTL_DSP_SETFMT,&the_fmt) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
851 fmt != the_fmt) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
852 perror("SNDCTRL_DSP_SETFMT");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
853 return(0); } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
854
129
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
855 /* The PCSP driver does not support reading of the sampling rate via the
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
856 SOUND_PCM_READ_RATE ioctl; determine "the_speed" here */
129
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
857 the_speed = speed; ioctl(audio_fd,SNDCTL_DSP_SPEED,&the_speed);
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
858 /* The PCSP driver does not support reading of the mono/stereo flag, thus
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
859 we assume, that failure to change this mode means we are in mono mode */
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
860 if (((i = (the_stereo = tracks)-1),ioctl(audio_fd,SNDCTL_DSP_STEREO,&i)) < 0)
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
861 the_stereo = 1;
e292c9648bb9 Import from CVS: tag xemacs-20-1p3
cvs
parents: 124
diff changeset
862
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
863 /* Try to request stereo playback (if needed); if this cannot be supported
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
864 by the hardware, then install conversion routines for mono playback */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
865
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
866 /* This ioctl will fail if we use the PCSP driver; thus the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
867 "the_stereo" is still unchanged */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
868 ioctl(audio_fd,SOUND_PCM_READ_CHANNELS,&the_stereo);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
869 if (tracks != the_stereo) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
870 if (tracks == 2) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
871 tracks = 1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
872 *sndcnv = *sndcnv == sndcnv2byteLE ? sndcnv2monobyteLE :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
873 *sndcnv == sndcnv2byteBE ? sndcnv2monobyteBE :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
874 *sndcnv == sndcnv2unsigned ? sndcnv2monounsigned :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
875 the_fmt == AFMT_S16_LE ? sndcnv16_2monoLE :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
876 the_fmt == AFMT_S16_BE ? sndcnv16_2monoBE :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
877 the_fmt == AFMT_S8 ? sndcnv8S_2mono :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
878 the_fmt == AFMT_U8 ? sndcnv8U_2mono :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
879 the_fmt == AFMT_MU_LAW ? sndcnvULaw_2mono : NULL;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
880 if (*sndcnv == NULL) { /* this should not happen */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
881 perror("SNDCTL_DSP_STEREO");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
882 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
883 /* Switch to mono mode */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
884 if (((i = 0),ioctl(audio_fd,SNDCTL_DSP_STEREO,&i)) < 0 || i) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
885 perror("SNDCTL_DSP_STEREO");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
886 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
887 /* Now double check that everything is set as expected */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
888 if (((i = AFMT_QUERY),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
889 (i != the_fmt &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
890 (((i=the_fmt),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
891 i != the_fmt ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
892 ((i = AFMT_QUERY),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
893 i != the_fmt)) ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
894 (ioctl(audio_fd,SOUND_PCM_READ_CHANNELS,&i) >= 0 &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
895 i != 1)) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
896 /* There was no way that we could set the soundcard to a meaningful
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
897 mode */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
898 perror("SNDCTL_DSP_SETFMT and SNDCTL_DSP_STEREO");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
899 return(0); } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
900 else {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
901 /* Somebody set the soundcard to stereo even though we requested
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
902 mono; this should not happen... */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
903 if (((i = the_stereo = tracks),ioctl(audio_fd,SNDCTL_DSP_STEREO,&i))<0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
904 i != the_stereo-1) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
905 perror("SNDCTL_DSP_STEREO");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
906 return(0); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
907 if (((i = AFMT_QUERY),ioctl(audio_fd,SNDCTL_DSP_SETFMT,&i)) < 0 ||
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
908 i != the_fmt) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
909 perror("SNDCTL_DSP_SETFMT");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
910 return(0); } } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
911
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
912 /* Fail if deviations from desired sampling frequency are too big */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
913
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
914 /* This ioctl will fail if we use the PCSP driver; thus the value of
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
915 "the_speed" is still unchanged */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
916 ioctl(audio_fd,SOUND_PCM_READ_RATE,&the_speed);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
917 if (speed*14 < the_speed*10 || speed*6 > the_speed*10) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
918 char buffer[256];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
919 sprintf(buffer,"SNDCTL_DSP_SPEED (req: %d, rtn: %d)",speed,the_speed);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
920 perror(buffer);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
921 return(0); }
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
922
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
923 /* Use the mixer device for setting the playback volume */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
924 if (mixx_fd > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
925 int vol = *volume & 0xFF;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
926 if (ioctl(mixx_fd,SOUND_MIXER_READ_PCM,volume) < 0)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
927 *volume = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
928 if (vol < 0) vol = 0; else if (vol > 100) vol = 100;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
929 #ifdef NOVOLUMECTRLFORMULAW
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
930 if (fmt == AFMT_MU_LAW)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
931 vol = 100;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
932 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
933 vol |= 256*vol;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
934 /* Do not signal an error, if volume control is unavailable! */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
935 ioctl(mixx_fd,SOUND_MIXER_WRITE_PCM,&vol); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
936
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
937 #if defined(LINUXPLAYSTANDALONE) && 1
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
938 /* Debugging output is displayed only when compiled as stand-alone version */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
939 {int the_volume;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
940 the_fmt = AFMT_QUERY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
941 ioctl(audio_fd,SNDCTL_DSP_SETFMT,&the_fmt);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
942 ioctl(auddio_fd,SOUND_PCM_READ_CHANNELS,&the_stereo);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
943 ioctl(auddio_fd,SOUND_PCM_READ_RATE,&the_speed);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
944 ioctl(mixx_fd,SOUND_MIXER_READ_PCM,&the_volume);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
945 fprintf(stderr,"%s, %s, %dHz, L:%d/R:%d\n",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
946 the_fmt == AFMT_MU_LAW ? "AFMT_MU_LAW" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
947 the_fmt == AFMT_A_LAW ? "AFMT_A_LAW" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
948 the_fmt == AFMT_IMA_ADPCM ? "AFMT_IMA_ADPCM" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
949 the_fmt == AFMT_U8 ? "AFMT_U8" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
950 the_fmt == AFMT_S16_LE ? "AFMT_S16_LE" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
951 the_fmt == AFMT_S16_BE ? "AFMT_S16_BE" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
952 the_fmt == AFMT_S8 ? "AFMT_S8" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
953 the_fmt == AFMT_U16_LE ? "AFMT_U16_LE" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
954 the_fmt == AFMT_U16_BE ? "AFMT_U16_BE" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
955 the_fmt == AFMT_MPEG ? "AFMT_MPEG" :
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
956 "AFMT_???",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
957 the_stereo == 2 ? "stereo" : "mono",
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
958 the_speed,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
959 the_volume / 256, the_volume % 256); }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
960 #endif
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
961
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
962 return(1);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
963 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
964
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
965 /* XEmacs requires code both for playback of pre-loaded data and for playback
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
966 from a soundfile; we use one function for both cases */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
967 static void linux_play_data_or_file(int fd,unsigned char *data,
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
968 int length,int volume)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
969 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
970 size_t (*parsesndfile)(void **dayta,size_t *sz,void **outbuf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
971 size_t (*sndcnv)(void **dayta,size_t *sz,void **);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
972 fmtType ffmt;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
973 int fmt,speed,tracks;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
974 unsigned char *pptr,*optr,*cptr,*sptr;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
975 int wrtn,rrtn,crtn,prtn;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
976
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
977 /* We need to read at least the header information before we can start
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
978 doing anything */
233
52952cbfc5b5 Import from CVS: tag r20-5b15
cvs
parents: 209
diff changeset
979 if (!data || length < HEADERSZ) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
980 if (fd < 0) return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
981 else {
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
982 length = read(fd,linuxplay_sndbuf,SNDBUFSZ);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
983 if (length < HEADERSZ)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
984 return;
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
985 data = linuxplay_sndbuf;
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
986 length = SNDBUFSZ; }
233
52952cbfc5b5 Import from CVS: tag r20-5b15
cvs
parents: 209
diff changeset
987 }
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
988
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
989 ffmt = analyze_format(data,&fmt,&speed,&tracks,&parsesndfile);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
990
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
991 if (ffmt != fmtRaw && ffmt != fmtSunAudio && ffmt != fmtWave) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
992 warn("Unsupported file format (neither RAW, nor Sun/DECAudio, nor WAVE)");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
993 return; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
994
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
995 /* The VoxWare-SDK discourages opening /dev/audio; opening /dev/dsp and
380
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
996 properly initializing it via ioctl() is preferred */
8626e4521993 Import from CVS: tag r21-2-5
cvs
parents: 377
diff changeset
997 if ((audio_fd=open(audio_dev, O_WRONLY | O_NONBLOCK, 0)) < 0) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
998 perror(audio_dev);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
999 if (mix_fd > 0 && mix_fd != audio_fd) { close(mix_fd); mix_fd = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1000 return; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1001
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1002 /* The VoxWare-SDK discourages direct manipulation of the mixer device as
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1003 this could lead to problems, when multiple sound cards are installed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1004 mix_fd = audio_fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1005
98
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
1006 sighup_handler = signal(SIGHUP, sighandler);
0d2f883870bc Import from CVS: tag r20-1b1
cvs
parents: 70
diff changeset
1007 sigint_handler = signal(SIGINT, sighandler);
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1008
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1009 if (!audio_init(mix_fd,audio_fd,fmt,speed,tracks,&volume,&sndcnv))
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1010 goto END_OF_PLAY;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1011 audio_vol = volume;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1012
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1013 /* Initialize global parser state information to zero */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1014 memset(&parsestate,0,sizeof(parsestate));
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1015
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1016 /* Mainloop: read a block of data, parse its contents, perform all
110
fe104dbd9147 Import from CVS: tag r20-1b7
cvs
parents: 98
diff changeset
1017 the necessary conversions and output it to the sound
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1018 device; repeat until all data has been processed */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1019 rrtn = length;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1020 do {
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
1021 for (pptr = data; (prtn = parsesndfile((void **)&pptr,(size_t *)&rrtn,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1022 (void **)&optr)) > 0; )
272
c5d627a313b1 Import from CVS: tag r21-0b34
cvs
parents: 233
diff changeset
1023 for (cptr = optr; (crtn = sndcnv((void **)&cptr,(size_t *) &prtn,
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1024 (void **)&sptr)) > 0; ) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1025 for (;;) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1026 if ((wrtn = write(audio_fd,sptr,crtn)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1027 perror("write"); goto END_OF_PLAY; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1028 else if (wrtn) break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1029 else if (ioctl(audio_fd,SNDCTL_DSP_SYNC,NULL) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1030 perror("SNDCTL_DSP_SYNC"); goto END_OF_PLAY; } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1031 if (wrtn != crtn) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1032 char buf[255];
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1033 sprintf(buf,"play: crtn = %d, wrtn = %d",crtn,wrtn);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1034 warn(buf);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1035 goto END_OF_PLAY; } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1036 if (fd >= 0) {
124
9b50b4588a93 Import from CVS: tag r20-1b15
cvs
parents: 110
diff changeset
1037 if ((rrtn = read(fd,linuxplay_sndbuf,SNDBUFSZ)) < 0) {
0
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1038 perror("read"); goto END_OF_PLAY; } }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1039 else
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1040 break;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1041 } while (rrtn > 0);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1042
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1043 /* Verify that we could fully parse the entire soundfile; this is needed
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1044 only for files in WAVE format */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1045 if (ffmt == fmtWave && parsestate.wave.state != wvOutOfBlock &&
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1046 parsestate.wave.state != wvFatal)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1047 warn("Unexpected end of WAVE file");
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1048
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1049 END_OF_PLAY:
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1050 /* Now cleanup all used resources */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1051
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1052 ioctl(audio_fd,SNDCTL_DSP_SYNC,NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1053 ioctl(audio_fd,SNDCTL_DSP_RESET,NULL);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1054
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1055 signal(SIGHUP,sighup_handler);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1056 signal(SIGINT,sigint_handler);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1057
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1058 if (mix_fd > 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1059 if (audio_vol >= 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1060 ioctl(mix_fd,SOUND_MIXER_WRITE_PCM,&audio_vol);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1061 audio_vol = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1062 if (mix_fd != audio_fd)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1063 close(mix_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1064 mix_fd = -1; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1065
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1066 close(audio_fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1067 audio_fd = -1;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1068
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1069 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1070 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1071
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1072 /* Call "linux_play_data_or_file" with the appropriate parameters for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1073 playing a soundfile */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1074 void play_sound_file (char *sound_file, int volume);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1075 void play_sound_file (char *sound_file, int volume)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1076 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1077 int fd;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1078
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1079 if ((fd=open(sound_file,O_RDONLY,0)) < 0) {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1080 perror(sound_file);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1081 return; }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1082 linux_play_data_or_file(fd,NULL,0,volume);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1083 close(fd);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1084 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1085 }
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1086
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1087 /* Call "linux_play_data_or_file" with the appropriate parameters for
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1088 playing pre-loaded data */
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1089 void play_sound_data (unsigned char *data, int length, int volume);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1090 void play_sound_data (unsigned char *data, int length, int volume)
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1091 {
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1092 linux_play_data_or_file(-1,data,length,volume);
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1093 return;
376386a54a3c Import from CVS: tag r19-14
cvs
parents:
diff changeset
1094 }