libaudio.h revision 1.3.8.1 1 /* $NetBSD: libaudio.h,v 1.3.8.1 2000/06/23 16:30:12 minoura 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 * We copy the Sun/NeXT on-disk audio header format and document what
33 * we know of it here.
34 *
35 * The header size appears to be an offset to where the data really
36 * begins, rather than defining the real length of the audio header.
37 * The Sun/NeXT audio format seems to only use 24 bytes of data (with
38 * an additional 8 bytes of nuls written, padding it to 32 bytes).
39 *
40 * If the size of the audio data is unknown (eg, reading from a pipe)
41 * the Sun demo audio tools place AUDIO_UNKNOWN_SIZE in the
42 * `data_size' member.
43 *
44 * For stereo data, the channels appear to be interleaved with the
45 * left channel first. For more channels, who knows?
46 */
47
48 /*
49 * This is the Sun/NeXT audio file magic value. Note that it
50 * is also `.snd' in ASCII.
51 */
52 #define AUDIO_FILE_MAGIC ((u_int32_t)0x2e736e64)
53 #define AUDIO_UNKNOWN_SIZE ((unsigned)(~0))
54
55 typedef struct {
56 u_int32_t magic;
57 u_int32_t hdr_size; /* header size; in bytes */
58 u_int32_t data_size; /* optional; in bytes */
59 u_int32_t encoding; /* see below */
60 u_int32_t sample_rate; /* per second */
61 u_int32_t channels; /* number of interleaved channels */
62 } sun_audioheader;
63
64 #define Audio_filehdr sun_audioheader /* SunOS compat(?) */
65
66 /*
67 * these are the types of "encoding" for above. taken from the
68 * SunOS <multimedia/audio_filehdr.h>.
69 */
70 #define AUDIO_FILE_ENCODING_MULAW_8 1
71 #define AUDIO_FILE_ENCODING_LINEAR_8 2
72 #define AUDIO_FILE_ENCODING_LINEAR_16 3
73 #define AUDIO_FILE_ENCODING_LINEAR_24 4
74 #define AUDIO_FILE_ENCODING_LINEAR_32 5
75 #define AUDIO_FILE_ENCODING_FLOAT 6
76 #define AUDIO_FILE_ENCODING_DOUBLE 7
77 #define AUDIO_FILE_ENCODING_ADPCM_G721 23
78 #define AUDIO_FILE_ENCODING_ADPCM_G722 24
79 #define AUDIO_FILE_ENCODING_ADPCM_G723_3 25
80 #define AUDIO_FILE_ENCODING_ADPCM_G723_5 26
81 #define AUDIO_FILE_ENCODING_ALAW_8 27
82
83 char *audio_enc_from_val __P((int));
84 int audio_enc_to_val __P((const char *));
85
86 int audio_get_sun_encoding __P((int, int *, int *));
87
88 /*
89 * M$ WAV files, info gleamed from sox sources
90 */
91
92 /*
93 * This is the WAV audio file magic value. Note that it
94 * is also `RIFF' and `WAVE' in ASCII.
95 */
96 #define WAVAUDIO_FILE_MAGIC_RIFF ((u_int32_t)0x52494646)
97 #define WAVAUDIO_FILE_MAGIC_WAVE ((u_int32_t)0x57415645)
98 #define WAVAUDIO_FILE_MAGIC_FMT ((u_int32_t)0x666d7420)
99 #define WAVAUDIO_FILE_MAGIC_DATA ((u_int32_t)0x64617461)
100
101 /* purloined from public Microsoft RIFF docs via sox */
102 #define WAVE_FORMAT_UNKNOWN (0x0000)
103 #define WAVE_FORMAT_PCM (0x0001)
104 #define WAVE_FORMAT_ADPCM (0x0002)
105 #define WAVE_FORMAT_ALAW (0x0006)
106 #define WAVE_FORMAT_MULAW (0x0007)
107 #define WAVE_FORMAT_OKI_ADPCM (0x0010)
108 #define WAVE_FORMAT_DIGISTD (0x0015)
109 #define WAVE_FORMAT_DIGIFIX (0x0016)
110 #define IBM_FORMAT_MULAW (0x0101)
111 #define IBM_FORMAT_ALAW (0x0102)
112 #define IBM_FORMAT_ADPCM (0x0103)
113
114 typedef struct {
115 char name[4];
116 u_int32_t len;
117 } wav_audioheaderpart;
118
119 typedef struct {
120 u_int16_t tag;
121 u_int16_t channels;
122 u_int32_t sample_rate;
123 u_int32_t avg_bps;
124 u_int16_t alignment;
125 u_int16_t bits_per_sample;
126 } wav_audioheaderfmt __attribute__((__packed__));
127
128 /* returns size of header, or -1 */
129 size_t audio_parse_wav_hdr __P((void *, size_t, int *, int *, int *, int *));
130
131 /*
132 * audio routine error codes
133 */
134 #define AUDIO_ENOENT -1 /* no such audio format */
135 #define AUDIO_ESHORTHDR -2 /* short header */
136 #define AUDIO_EWAVUNSUPP -3 /* WAV: unsupported file */
137 #define AUDIO_EWAVBADPCM -4 /* WAV: bad PCM bps */
138 #define AUDIO_EWAVNODATA -5 /* WAV: missing data */
139
140 #define AUDIO_MAXERRNO 5
141
142 /* and something to get a string associated with this error */
143 const char *audio_errstring __P((int));
144
145 /*
146 * generic routines?
147 */
148 void decode_int __P((const char *, int *));
149 void decode_time __P((const char *, struct timeval *));
150 void decode_encoding __P((const char *, int *));
151
152 /*
153 * get/put 16/32 bits of big/little endian data
154 */
155 #include <sys/types.h>
156 #include <machine/endian.h>
157 #include <machine/bswap.h>
158
159 #if BYTE_ORDER == BIG_ENDIAN
160
161 #define getle16(v) bswap16(v)
162 #define getle32(v) bswap32(v)
163 #define getbe16(v) (v)
164 #define getbe32(v) (v)
165
166 #else
167
168 #define getle16(v) (v)
169 #define getle32(v) (v)
170 #define getbe16(v) bswap16(v)
171 #define getbe32(v) bswap32(v)
172
173 #endif
174