libaudio.h revision 1.12 1 /* $NetBSD: libaudio.h,v 1.12 2004/01/21 11:55:07 mrg Exp $ */
2
3 /*
4 * Copyright (c) 1999 Matthew R. Green
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
23 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31 /*
32 * audio formats
33 */
34 #define AUDIO_FORMAT_DEFAULT -1
35 #define AUDIO_FORMAT_NONE 1
36 #define AUDIO_FORMAT_SUN 2
37 #define AUDIO_FORMAT_WAV 3
38
39 int audio_format_from_str (char *);
40
41 /*
42 * We copy the Sun/NeXT on-disk audio header format and document what
43 * we know of it here.
44 *
45 * The header size appears to be an offset to where the data really
46 * begins, rather than defining the real length of the audio header.
47 * The Sun/NeXT audio format seems to only use 24 bytes of data (with
48 * an additional 8 bytes of nuls written, padding it to 32 bytes).
49 *
50 * If the size of the audio data is unknown (eg, reading from a pipe)
51 * the Sun demo audio tools place AUDIO_UNKNOWN_SIZE in the
52 * `data_size' member.
53 *
54 * For stereo data, the channels appear to be interleaved with the
55 * left channel first. For more channels, who knows?
56 */
57
58 /*
59 * This is the Sun/NeXT audio file magic value. Note that it
60 * is also `.snd' in ASCII.
61 */
62 #define AUDIO_FILE_MAGIC ((u_int32_t)0x2e736e64)
63 #define AUDIO_UNKNOWN_SIZE ((unsigned)(~0))
64
65 typedef struct {
66 u_int32_t magic;
67 u_int32_t hdr_size; /* header size; in bytes */
68 u_int32_t data_size; /* optional; in bytes */
69 u_int32_t encoding; /* see below */
70 u_int32_t sample_rate; /* per second */
71 u_int32_t channels; /* number of interleaved channels */
72 } sun_audioheader;
73
74 #define Audio_filehdr sun_audioheader /* SunOS compat(?) */
75
76 /*
77 * these are the types of "encoding" for above. taken from the
78 * SunOS <multimedia/audio_filehdr.h>.
79 */
80 #define AUDIO_FILE_ENCODING_MULAW_8 1
81 #define AUDIO_FILE_ENCODING_LINEAR_8 2
82 #define AUDIO_FILE_ENCODING_LINEAR_16 3
83 #define AUDIO_FILE_ENCODING_LINEAR_24 4
84 #define AUDIO_FILE_ENCODING_LINEAR_32 5
85 #define AUDIO_FILE_ENCODING_FLOAT 6
86 #define AUDIO_FILE_ENCODING_DOUBLE 7
87 #define AUDIO_FILE_ENCODING_ADPCM_G721 23
88 #define AUDIO_FILE_ENCODING_ADPCM_G722 24
89 #define AUDIO_FILE_ENCODING_ADPCM_G723_3 25
90 #define AUDIO_FILE_ENCODING_ADPCM_G723_5 26
91 #define AUDIO_FILE_ENCODING_ALAW_8 27
92
93 const char *audio_enc_from_val (int);
94 int audio_enc_to_val (const char *);
95
96 int audio_sun_to_encoding (int, int *, int *);
97 int audio_encoding_to_sun (int, int, int *);
98
99 /*
100 * M$ WAV files, info gleamed from sox sources
101 */
102
103 /*
104 * This is the WAV audio file magic value. Note that it
105 * is also `RIFF' and `WAVE' in ASCII.
106 */
107 #define WAVAUDIO_FILE_MAGIC_RIFF ((u_int32_t)0x52494646)
108 #define WAVAUDIO_FILE_MAGIC_WAVE ((u_int32_t)0x57415645)
109 #define WAVAUDIO_FILE_MAGIC_FMT ((u_int32_t)0x666d7420)
110 #define WAVAUDIO_FILE_MAGIC_DATA ((u_int32_t)0x64617461)
111
112 /* purloined from public Microsoft RIFF docs via sox */
113 #define WAVE_FORMAT_UNKNOWN (0x0000)
114 #define WAVE_FORMAT_PCM (0x0001)
115 #define WAVE_FORMAT_ADPCM (0x0002)
116 #define WAVE_FORMAT_ALAW (0x0006)
117 #define WAVE_FORMAT_MULAW (0x0007)
118 #define WAVE_FORMAT_OKI_ADPCM (0x0010)
119 #define WAVE_FORMAT_DIGISTD (0x0015)
120 #define WAVE_FORMAT_DIGIFIX (0x0016)
121 #define IBM_FORMAT_MULAW (0x0101)
122 #define IBM_FORMAT_ALAW (0x0102)
123 #define IBM_FORMAT_ADPCM (0x0103)
124
125 const char *wav_enc_from_val (int);
126
127 typedef struct {
128 char name[4];
129 u_int32_t len;
130 } wav_audioheaderpart;
131
132 typedef struct {
133 u_int16_t tag;
134 u_int16_t channels;
135 u_int32_t sample_rate;
136 u_int32_t avg_bps;
137 u_int16_t alignment;
138 u_int16_t bits_per_sample;
139 } wav_audioheaderfmt __attribute__((__packed__));
140
141 /* returns size of header, or -ve for failure */
142 ssize_t audio_wav_parse_hdr (void *, size_t, u_int *, u_int *, u_int *, u_int *, size_t *);
143
144 /*
145 * audio routine error codes
146 */
147 #define AUDIO_ENOENT -1 /* no such audio format */
148 #define AUDIO_ESHORTHDR -2 /* short header */
149 #define AUDIO_EWAVUNSUPP -3 /* WAV: unsupported file */
150 #define AUDIO_EWAVBADPCM -4 /* WAV: bad PCM bps */
151 #define AUDIO_EWAVNODATA -5 /* WAV: missing data */
152 #define AUDIO_EINTERNAL -6 /* internal error */
153
154 #define AUDIO_MAXERRNO 5
155
156 /* and something to get a string associated with this error */
157 const char *audio_errstring (int);
158
159 /*
160 * generic routines?
161 */
162 void decode_int (const char *, int *);
163 void decode_time (const char *, struct timeval *);
164 void decode_encoding (const char *, int *);
165
166 /*
167 * get/put 16/32 bits of big/little endian data
168 */
169 #include <sys/types.h>
170 #include <machine/endian.h>
171 #include <machine/bswap.h>
172
173 #if BYTE_ORDER == BIG_ENDIAN
174
175 #define getle16(v) bswap16(v)
176 #define getle32(v) bswap32(v)
177 #define getbe16(v) (v)
178 #define getbe32(v) (v)
179
180 #define putle16(x,v) (x) = bswap16(v)
181 #define putle32(x,v) (x) = bswap32(v)
182 #define putbe16(x,v) (x) = (v)
183 #define putbe32(x,v) (x) = (v)
184
185 #else
186
187 #define getle16(v) (v)
188 #define getle32(v) (v)
189 #define getbe16(v) bswap16(v)
190 #define getbe32(v) bswap32(v)
191
192 #define putle16(x,v) (x) = (v)
193 #define putle32(x,v) (x) = (v)
194 #define putbe16(x,v) (x) = bswap16(v)
195 #define putbe32(x,v) (x) = bswap32(v)
196
197 #endif
198