xref: /netbsd-src/usr.bin/audio/common/libaudio.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /*	$NetBSD: libaudio.h,v 1.20 2015/08/05 06:54:39 mrg Exp $	*/
2 
3 /*
4  * Copyright (c) 1999, 2009 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  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 /*
30  * audio formats
31  */
32 #define AUDIO_FORMAT_DEFAULT	-1
33 #define AUDIO_FORMAT_NONE	1
34 #define AUDIO_FORMAT_SUN	2
35 #define AUDIO_FORMAT_WAV	3
36 
37 int	audio_format_from_str (char *);
38 
39 /*
40  * We copy the Sun/NeXT on-disk audio header format and document what
41  * we know of it here.
42  *
43  * The header size appears to be an offset to where the data really
44  * begins, rather than defining the real length of the audio header.
45  * The Sun/NeXT audio format seems to only use 24 bytes of data (with
46  * an additional 8 bytes of nuls written, padding it to 32 bytes).
47  *
48  * If the size of the audio data is unknown (eg, reading from a pipe)
49  * the Sun demo audio tools place AUDIO_UNKNOWN_SIZE in the
50  * `data_size' member.
51  *
52  * For stereo data, the channels appear to be interleaved with the
53  * left channel first.  For more channels, who knows?
54  */
55 
56 /*
57  * This is the Sun/NeXT audio file magic value.  Note that it
58  * is also `.snd' in ASCII.
59  */
60 #define	AUDIO_FILE_MAGIC		((u_int32_t)0x2e736e64)
61 #define AUDIO_UNKNOWN_SIZE		((unsigned)(~0))
62 
63 typedef struct {
64 	u_int32_t	magic;
65 	u_int32_t	hdr_size;	/* header size; in bytes */
66 	u_int32_t	data_size;	/* optional; in bytes */
67 	u_int32_t	encoding;	/* see below */
68 	u_int32_t	sample_rate;	/* per second */
69 	u_int32_t	channels;	/* number of interleaved channels */
70 } sun_audioheader;
71 
72 #define Audio_filehdr sun_audioheader	/* SunOS compat(?) */
73 
74 /*
75  * these are the types of "encoding" for above.  taken from the
76  * SunOS <multimedia/audio_filehdr.h>.
77  */
78 #define	AUDIO_FILE_ENCODING_MULAW_8		1
79 #define	AUDIO_FILE_ENCODING_LINEAR_8		2
80 #define	AUDIO_FILE_ENCODING_LINEAR_16		3
81 #define	AUDIO_FILE_ENCODING_LINEAR_24		4
82 #define	AUDIO_FILE_ENCODING_LINEAR_32		5
83 #define	AUDIO_FILE_ENCODING_FLOAT		6
84 #define	AUDIO_FILE_ENCODING_DOUBLE		7
85 #define	AUDIO_FILE_ENCODING_ADPCM_G721		23
86 #define	AUDIO_FILE_ENCODING_ADPCM_G722		24
87 #define	AUDIO_FILE_ENCODING_ADPCM_G723_3	25
88 #define	AUDIO_FILE_ENCODING_ADPCM_G723_5	26
89 #define	AUDIO_FILE_ENCODING_ALAW_8		27
90 
91 const char *audio_enc_from_val (int);
92 int	audio_enc_to_val (const char *);
93 
94 int	audio_sun_to_encoding (int, u_int *, u_int *);
95 int	audio_encoding_to_sun (int, int, int *);
96 
97 /*
98  * M$ WAV files, info gleamed from sox sources
99  */
100 
101 /*
102  * This is the WAV audio file magic value.  Note that it
103  * is also `RIFF' and `WAVE' in ASCII.
104  */
105 #define	WAVAUDIO_FILE_MAGIC_RIFF	((u_int32_t)0x52494646)
106 #define	WAVAUDIO_FILE_MAGIC_WAVE	((u_int32_t)0x57415645)
107 #define	WAVAUDIO_FILE_MAGIC_FMT		((u_int32_t)0x666d7420)
108 #define	WAVAUDIO_FILE_MAGIC_DATA	((u_int32_t)0x64617461)
109 
110 /* purloined from public Microsoft RIFF docs via sox or mplayer */
111 #define WAVE_FORMAT_UNKNOWN		(0x0000)
112 #define WAVE_FORMAT_PCM			(0x0001)
113 #define WAVE_FORMAT_ADPCM		(0x0002)
114 #define WAVE_FORMAT_ALAW		(0x0006)
115 #define WAVE_FORMAT_MULAW		(0x0007)
116 #define WAVE_FORMAT_OKI_ADPCM		(0x0010)
117 #define WAVE_FORMAT_IMA_ADPCM		(0x0011)
118 #define WAVE_FORMAT_DIGISTD		(0x0015)
119 #define WAVE_FORMAT_DIGIFIX		(0x0016)
120 #define WAVE_FORMAT_DOLBY_AC2		(0x0030)
121 #define WAVE_FORMAT_GSM610		(0x0031)
122 #define WAVE_FORMAT_ROCKWELL_ADPCM	(0x003b)
123 #define WAVE_FORMAT_ROCKWELL_DIGITALK	(0x003c)
124 #define WAVE_FORMAT_G721_ADPCM		(0x0040)
125 #define WAVE_FORMAT_G728_CELP		(0x0041)
126 #define WAVE_FORMAT_MPEG		(0x0050)
127 #define WAVE_FORMAT_MPEGLAYER3		(0x0055)
128 #define WAVE_FORMAT_G726_ADPCM		(0x0064)
129 #define WAVE_FORMAT_G722_ADPCM		(0x0065)
130 #define IBM_FORMAT_MULAW		(0x0101)
131 #define IBM_FORMAT_ALAW			(0x0102)
132 #define IBM_FORMAT_ADPCM		(0x0103)
133 #define WAVE_FORMAT_EXTENSIBLE		(0xfffe)
134 
135 const char *wav_enc_from_val (int);
136 
137 typedef struct {
138 	char		name[4];
139 	u_int32_t	len;
140 } wav_audioheaderpart;
141 
142 typedef struct {
143 	u_int16_t	tag;
144 	u_int16_t	channels;
145 	u_int32_t	sample_rate;
146 	u_int32_t	avg_bps;
147 	u_int16_t	alignment;
148 	u_int16_t	bits_per_sample;
149 } __packed wav_audioheaderfmt;
150 
151 typedef struct {
152 	u_int16_t	len;
153 	u_int16_t	valid_bits;
154 	u_int32_t	speaker_pos_mask;
155 	u_int16_t	sub_tag;
156 	u_int8_t	dummy[14];
157 } __packed wav_audiohdrextensible;
158 
159 /* returns size of header, or -ve for failure */
160 ssize_t audio_wav_parse_hdr (void *, size_t, u_int *, u_int *, u_int *, u_int *, off_t *);
161 
162 extern int verbose;
163 
164 /*
165  * audio routine error codes
166  */
167 #define AUDIO_ENOENT		-1		/* no such audio format */
168 #define AUDIO_ESHORTHDR		-2		/* short header */
169 #define AUDIO_EWAVUNSUPP	-3		/* WAV: unsupported file */
170 #define AUDIO_EWAVBADPCM	-4		/* WAV: bad PCM bps */
171 #define AUDIO_EWAVNODATA	-5		/* WAV: missing data */
172 #define AUDIO_EINTERNAL		-6		/* internal error */
173 
174 #define AUDIO_MAXERRNO		5
175 
176 /* and something to get a string associated with this error */
177 const char *audio_errstring (int);
178 
179 /*
180  * generic routines?
181  */
182 void	decode_int (const char *, int *);
183 void	decode_uint (const char *, unsigned *);
184 void	decode_time (const char *, struct timeval *);
185 void	decode_encoding (const char *, int *);
186 
187 /*
188  * Track info, for reading/writing sun/wav header.
189  *
190  * Note that write_header() may change the values of format,
191  * encoding.
192  */
193 
194 struct track_info {
195 	int	outfd;
196 	char	*header_info;
197 	int	format;
198 	int	encoding;
199 	int	precision;
200 	int	qflag;
201 	off_t	total_size;
202 	int	sample_rate;
203 	int	channels;
204 };
205 
206 typedef void (*write_conv_func) (u_char *, int);
207 
208 void	write_header (struct track_info *);
209 write_conv_func write_get_conv_func(struct track_info *);
210 
211 /* backends for the above */
212 int sun_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
213 int wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
214 write_conv_func sun_write_get_conv_func(struct track_info *ti);
215 write_conv_func wav_write_get_conv_func(struct track_info *ti);
216 
217 extern char	audio_default_info[8];
218 
219 /*
220  * get/put 16/32 bits of big/little endian data
221  */
222 #include <sys/types.h>
223 #include <machine/endian.h>
224 #include <machine/bswap.h>
225 
226 #if BYTE_ORDER == BIG_ENDIAN
227 
228 #define getle16(v)	bswap16(v)
229 #define getle32(v)	bswap32(v)
230 #define getbe16(v)	(v)
231 #define getbe32(v)	(v)
232 
233 #define putle16(x,v)	(x) = bswap16(v)
234 #define putle32(x,v)	(x) = bswap32(v)
235 #define putbe16(x,v)	(x) = (v)
236 #define putbe32(x,v)	(x) = (v)
237 
238 #else
239 
240 #define getle16(v)	(v)
241 #define getle32(v)	(v)
242 #define getbe16(v)	bswap16(v)
243 #define getbe32(v)	bswap32(v)
244 
245 #define putle16(x,v)	(x) = (v)
246 #define putle32(x,v)	(x) = (v)
247 #define putbe16(x,v)	(x) = bswap16(v)
248 #define putbe32(x,v)	(x) = bswap32(v)
249 
250 #endif
251