xref: /netbsd-src/usr.bin/audio/common/libaudio.h (revision bcc8ec9959e7b01e313d813067bfb43a3ad70551)
1 /*	$NetBSD: libaudio.h,v 1.6 2000/12/22 11:38:42 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  * 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 (int);
84 int	audio_enc_to_val (const char *);
85 
86 int	audio_sun_to_encoding (int, int *, int *);
87 int	audio_encoding_to_sun (int, int, int *);
88 
89 /*
90  * M$ WAV files, info gleamed from sox sources
91  */
92 
93 /*
94  * This is the WAV audio file magic value.  Note that it
95  * is also `RIFF' and `WAVE' in ASCII.
96  */
97 #define	WAVAUDIO_FILE_MAGIC_RIFF	((u_int32_t)0x52494646)
98 #define	WAVAUDIO_FILE_MAGIC_WAVE	((u_int32_t)0x57415645)
99 #define	WAVAUDIO_FILE_MAGIC_FMT		((u_int32_t)0x666d7420)
100 #define	WAVAUDIO_FILE_MAGIC_DATA	((u_int32_t)0x64617461)
101 
102 /* purloined from public Microsoft RIFF docs via sox */
103 #define WAVE_FORMAT_UNKNOWN		(0x0000)
104 #define WAVE_FORMAT_PCM			(0x0001)
105 #define WAVE_FORMAT_ADPCM		(0x0002)
106 #define WAVE_FORMAT_ALAW		(0x0006)
107 #define WAVE_FORMAT_MULAW		(0x0007)
108 #define WAVE_FORMAT_OKI_ADPCM		(0x0010)
109 #define WAVE_FORMAT_DIGISTD		(0x0015)
110 #define WAVE_FORMAT_DIGIFIX		(0x0016)
111 #define IBM_FORMAT_MULAW		(0x0101)
112 #define IBM_FORMAT_ALAW			(0x0102)
113 #define IBM_FORMAT_ADPCM		(0x0103)
114 
115 typedef struct {
116 	char		name[4];
117 	u_int32_t	len;
118 } wav_audioheaderpart;
119 
120 typedef struct {
121 	u_int16_t	tag;
122 	u_int16_t	channels;
123 	u_int32_t	sample_rate;
124 	u_int32_t	avg_bps;
125 	u_int16_t	alignment;
126 	u_int16_t	bits_per_sample;
127 } wav_audioheaderfmt __attribute__((__packed__));
128 
129 /* returns size of header, or -1 */
130 size_t audio_parse_wav_hdr (void *, size_t, int *, int *, int *, int *);
131 
132 /*
133  * audio routine error codes
134  */
135 #define AUDIO_ENOENT		-1		/* no such audio format */
136 #define AUDIO_ESHORTHDR		-2		/* short header */
137 #define AUDIO_EWAVUNSUPP	-3		/* WAV: unsupported file */
138 #define AUDIO_EWAVBADPCM	-4		/* WAV: bad PCM bps */
139 #define AUDIO_EWAVNODATA	-5		/* WAV: missing data */
140 
141 #define AUDIO_MAXERRNO		5
142 
143 /* and something to get a string associated with this error */
144 const char *audio_errstring (int);
145 
146 /*
147  * generic routines?
148  */
149 void	decode_int (const char *, int *);
150 void	decode_time (const char *, struct timeval *);
151 void	decode_encoding (const char *, int *);
152 
153 /*
154  * get/put 16/32 bits of big/little endian data
155  */
156 #include <sys/types.h>
157 #include <machine/endian.h>
158 #include <machine/bswap.h>
159 
160 #if BYTE_ORDER == BIG_ENDIAN
161 
162 #define getle16(v)	bswap16(v)
163 #define getle32(v)	bswap32(v)
164 #define getbe16(v)	(v)
165 #define getbe32(v)	(v)
166 
167 #else
168 
169 #define getle16(v)	(v)
170 #define getle32(v)	(v)
171 #define getbe16(v)	bswap16(v)
172 #define getbe32(v)	bswap32(v)
173 
174 #endif
175