1/*	$NetBSD: libaudio.h,v 1.22 2024/03/08 06:57:59 mrg Exp $	*/
2
3/*
4 * Copyright (c) 1999, 2009, 2013, 2015, 2019, 2024 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
37int	audio_format_from_str (char *);
38
39/*
40 * Audio encoding formats; this is a additional to those set
41 * in sys/audioio.h, but with a large offset to avoid future
42 * conflicts (additional ones are libaudio-software only.)
43 *
44 * This is to support floating-point WAV files.  These require
45 * software conversion to a supported format.
46 */
47#define	AUDIO_ENCODING_LIBAUDIO_FLOAT32    1001	/* 32-bit IEEE FP. */
48#define	AUDIO_ENCODING_LIBAUDIO_FLOAT64    1002	/* 64-bit IEEE FP. */
49
50/*
51 * We copy the Sun/NeXT on-disk audio header format and document what
52 * we know of it here.
53 *
54 * The header size appears to be an offset to where the data really
55 * begins, rather than defining the real length of the audio header.
56 * The Sun/NeXT audio format seems to only use 24 bytes of data (with
57 * an additional 8 bytes of nuls written, padding it to 32 bytes).
58 *
59 * If the size of the audio data is unknown (eg, reading from a pipe)
60 * the Sun demo audio tools place AUDIO_UNKNOWN_SIZE in the
61 * `data_size' member.
62 *
63 * For stereo data, the channels appear to be interleaved with the
64 * left channel first.  For more channels, who knows?
65 */
66
67/*
68 * This is the Sun/NeXT audio file magic value.  Note that it
69 * is also `.snd' in ASCII.
70 */
71#define	AUDIO_FILE_MAGIC		((u_int32_t)0x2e736e64)
72#define AUDIO_UNKNOWN_SIZE		((unsigned)(~0))
73
74typedef struct {
75	u_int32_t	magic;
76	u_int32_t	hdr_size;	/* header size; in bytes */
77	u_int32_t	data_size;	/* optional; in bytes */
78	u_int32_t	encoding;	/* see below */
79	u_int32_t	sample_rate;	/* per second */
80	u_int32_t	channels;	/* number of interleaved channels */
81} sun_audioheader;
82
83#define Audio_filehdr sun_audioheader	/* SunOS compat(?) */
84
85/*
86 * these are the types of "encoding" for above.  taken from the
87 * SunOS <multimedia/audio_filehdr.h>.
88 */
89#define	AUDIO_FILE_ENCODING_MULAW_8		1
90#define	AUDIO_FILE_ENCODING_LINEAR_8		2
91#define	AUDIO_FILE_ENCODING_LINEAR_16		3
92#define	AUDIO_FILE_ENCODING_LINEAR_24		4
93#define	AUDIO_FILE_ENCODING_LINEAR_32		5
94#define	AUDIO_FILE_ENCODING_FLOAT		6
95#define	AUDIO_FILE_ENCODING_DOUBLE		7
96#define	AUDIO_FILE_ENCODING_ADPCM_G721		23
97#define	AUDIO_FILE_ENCODING_ADPCM_G722		24
98#define	AUDIO_FILE_ENCODING_ADPCM_G723_3	25
99#define	AUDIO_FILE_ENCODING_ADPCM_G723_5	26
100#define	AUDIO_FILE_ENCODING_ALAW_8		27
101
102const char *audio_enc_from_val (int);
103int	audio_enc_to_val (const char *);
104
105int	audio_sun_to_encoding (int, u_int *, u_int *);
106int	audio_encoding_to_sun (int, int, int *);
107
108/*
109 * RIFF WAVE files.  Sources: RFC 2361, and various Microsoft docs
110 * https://learn.microsoft.com/en-us/windows/win32/xaudio2/resource-interchange-file-format--riff-
111 * https://learn.microsoft.com/en-us/previous-versions/windows/hardware/design/dn653308(v=vs.85)
112 * "Multimedia Programming Interface and Data Specifications 1.0" chapter 4
113 */
114
115/*
116 * This is the WAV audio file magic value.  Note that it
117 * is also `RIFF' and `WAVE' in ASCII.
118 */
119#define	WAVAUDIO_FILE_MAGIC_RIFF	((u_int32_t)0x52494646)
120#define	WAVAUDIO_FILE_MAGIC_WAVE	((u_int32_t)0x57415645)
121#define	WAVAUDIO_FILE_MAGIC_FMT		((u_int32_t)0x666d7420)
122#define	WAVAUDIO_FILE_MAGIC_DATA	((u_int32_t)0x64617461)
123
124/* From RFC 2361 */
125#define WAVE_FORMAT_UNKNOWN		(0x0000)
126#define WAVE_FORMAT_PCM			(0x0001)
127#define WAVE_FORMAT_ADPCM		(0x0002)
128#define WAVE_FORMAT_IEEE_FLOAT		(0x0003)
129#define WAVE_FORMAT_VSELP		(0x0004)
130#define WAVE_FORMAT_IBM_CVSD		(0x0005)
131#define WAVE_FORMAT_ALAW		(0x0006)
132#define WAVE_FORMAT_MULAW		(0x0007)
133#define WAVE_FORMAT_OKI_ADPCM		(0x0010)
134#define WAVE_FORMAT_IMA_ADPCM		(0x0011)
135#define WAVE_FORMAT_MEDIASPACE_ADPCM	(0x0012)
136#define WAVE_FORMAT_SIERRA_ADPCM	(0x0013)
137#define WAVE_FORMAT_G723_ADPCM		(0x0014)
138#define WAVE_FORMAT_DIGISTD		(0x0015)
139#define WAVE_FORMAT_DIGIFIX		(0x0016)
140#define WAVE_FORMAT_DIALOGIC_OKI_ADPCM	(0x0017)
141#define WAVE_FORMAT_MEDIAVISION_ADPCM	(0x0018)
142#define WAVE_FORMAT_CU_CODEC		(0x0019)
143#define WAVE_FORMAT_YAMAHA_ADPCM	(0x0020)
144#define WAVE_FORMAT_SONARC		(0x0021)
145#define WAVE_FORMAT_DSPGROUP_TRUESPEECH	(0x0022)
146#define WAVE_FORMAT_ECHOSC1		(0x0023)
147#define WAVE_FORMAT_AUDIOFILE_AF36	(0x0024)
148#define WAVE_FORMAT_APTX		(0x0025)
149#define WAVE_FORMAT_AUDIOFILE_AF10	(0x0026)
150#define WAVE_FORMAT_PROSODY_1612	(0x0027)
151#define WAVE_FORMAT_LRC			(0x0028)
152#define WAVE_FORMAT_DOLBY_AC2		(0x0030)
153#define WAVE_FORMAT_GSM610		(0x0031)
154#define WAVE_FORMAT_MSNAUDIO		(0x0032)
155#define WAVE_FORMAT_ANTEX_ADPCME	(0x0033)
156#define WAVE_FORMAT_CONTROL_RES_VQLPC	(0x0034)
157#define WAVE_FORMAT_DIGIREAL		(0x0035)
158#define WAVE_FORMAT_DIGIADPCM		(0x0036)
159#define WAVE_FORMAT_CONTROL_RES_CR10	(0x0037)
160#define WAVE_FORMAT_NMS_VBXADPCM	(0x0038)
161#define WAVE_FORMAT_ROLAND_RDAC		(0x0039)
162#define WAVE_FORMAT_ECHOSC3		(0x003a)
163#define WAVE_FORMAT_ROCKWELL_ADPCM	(0x003b)
164#define WAVE_FORMAT_ROCKWELL_DIGITALK	(0x003c)
165#define WAVE_FORMAT_XEBEC		(0x003d)
166#define WAVE_FORMAT_G721_ADPCM		(0x0040)
167#define WAVE_FORMAT_G728_CELP		(0x0041)
168#define WAVE_FORMAT_MSG723		(0x0042)
169#define WAVE_FORMAT_MPEG		(0x0050)
170#define WAVE_FORMAT_RT24		(0x0052)
171#define WAVE_FORMAT_PAC			(0x0053)
172#define WAVE_FORMAT_MPEGLAYER3		(0x0055)
173#define WAVE_FORMAT_LUCENT_G723		(0x0059)
174#define WAVE_FORMAT_CIRRUS		(0x0060)
175#define WAVE_FORMAT_ESPCM		(0x0061)
176#define WAVE_FORMAT_VOXWARE		(0x0062)
177#define WAVE_FORMAT_CANOPUS_ATRAC	(0x0063)
178#define WAVE_FORMAT_G726_ADPCM		(0x0064)
179#define WAVE_FORMAT_G722_ADPCM		(0x0065)
180#define WAVE_FORMAT_DSAT		(0x0066)
181#define WAVE_FORMAT_DSAT_DISPLAY	(0x0067)
182#define WAVE_FORMAT_VOXWARE_BYTE_ALIGNED (0x0069)
183#define WAVE_FORMAT_VOXWARE_AC8		(0x0070)
184#define WAVE_FORMAT_VOXWARE_AC10	(0x0071)
185#define WAVE_FORMAT_VOXWARE_AC16	(0x0072)
186#define WAVE_FORMAT_VOXWARE_AC20	(0x0073)
187#define WAVE_FORMAT_VOXWARE_RT24	(0x0074)
188#define WAVE_FORMAT_VOXWARE_RT29	(0x0075)
189#define WAVE_FORMAT_VOXWARE_RT29HW	(0x0076)
190#define WAVE_FORMAT_VOXWARE_VR12	(0x0077)
191#define WAVE_FORMAT_VOXWARE_VR18	(0x0078)
192#define WAVE_FORMAT_VOXWARE_TQ40	(0x0079)
193#define WAVE_FORMAT_SOFTSOUND		(0x0080)
194#define WAVE_FORMAT_VOXWARE_TQ60	(0x0081)
195#define WAVE_FORMAT_MSRT24		(0x0082)
196#define WAVE_FORMAT_G729A		(0x0083)
197#define WAVE_FORMAT_MVI_MV12		(0x0084)
198#define WAVE_FORMAT_DF_G726		(0x0085)
199#define WAVE_FORMAT_DF_GSM610		(0x0086)
200#define WAVE_FORMAT_ISIAUDIO		(0x0088)
201#define WAVE_FORMAT_ONLIVE		(0x0089)
202#define WAVE_FORMAT_SBC24		(0x0091)
203#define WAVE_FORMAT_DOLBY_AC3_SPDIF	(0x0092)
204#define WAVE_FORMAT_ZYXEL_ADPCM		(0x0097)
205#define WAVE_FORMAT_PHILIPS_LPCBB	(0x0098)
206#define WAVE_FORMAT_PACKED		(0x0099)
207#define WAVE_FORMAT_RHETOREX_ADPCM	(0x0100)
208#define WAVE_FORMAT_IRAT		(0x0101)
209#define WAVE_FORMAT_VIVO_G723		(0x0111)
210#define WAVE_FORMAT_VIVO_SIREN		(0x0112)
211#define WAVE_FORMAT_DIGITAL_G723	(0x0123)
212#define WAVE_FORMAT_CREATIVE_ADPCM	(0x0200)
213#define WAVE_FORMAT_CREATIVE_FASTSPEECH8 (0x0202)
214#define WAVE_FORMAT_CREATIVE_FASTSPEECH10 (0x0203)
215#define WAVE_FORMAT_QUARTERDECK		(0x0220)
216#define WAVE_FORMAT_FM_TOWNS_SND	(0x0300)
217#define WAVE_FORMAT_BTV_DIGITAL		(0x0400)
218#define WAVE_FORMAT_VME_VMPCM		(0x0680)
219#define WAVE_FORMAT_OLIGSM		(0x1000)
220#define WAVE_FORMAT_OLIADPCM		(0x1001)
221#define WAVE_FORMAT_OLICELP		(0x1002)
222#define WAVE_FORMAT_OLISBC		(0x1003)
223#define WAVE_FORMAT_OLIOPR		(0x1004)
224#define WAVE_FORMAT_LH_CODEC		(0x1100)
225#define WAVE_FORMAT_NORRIS		(0x1400)
226#define WAVE_FORMAT_ISIAUDIO2		(0x1401)
227#define WAVE_FORMAT_SOUNDSPACE_MUSICOMPRESS (0x1500)
228#define WAVE_FORMAT_DVM			(0x2000)
229
230#define WAVE_FORMAT_EXTENSIBLE		(0xfffe)
231
232const char *wav_enc_from_val (int);
233
234typedef struct {
235	char		name[4];
236	u_int32_t	len;
237} wav_audioheaderpart;
238
239typedef struct {
240	u_int16_t	tag;
241	u_int16_t	channels;
242	u_int32_t	sample_rate;
243	u_int32_t	avg_bytes_per_sec;
244	u_int16_t	alignment;
245	u_int16_t	bits_per_sample;
246} __packed wav_audioheaderfmt;
247
248typedef struct {
249	u_int16_t	len;
250	u_int16_t	valid_bits;
251	u_int32_t	speaker_pos_mask;
252	u_int16_t	sub_tag;
253	u_int8_t	guid[14];
254} __packed wav_audiohdrextensible;
255
256/* returns size of header, or -ve for failure */
257ssize_t audio_wav_parse_hdr (void *, size_t, u_int *, u_int *, u_int *, u_int *, off_t *);
258
259extern int verbose;
260
261/*
262 * audio routine error codes
263 */
264#define AUDIO_ENOENT		-1		/* no such audio format */
265#define AUDIO_ESHORTHDR		-2		/* short header */
266#define AUDIO_EWAVUNSUPP	-3		/* WAV: unsupported file */
267#define AUDIO_EWAVBADPCM	-4		/* WAV: bad PCM bps */
268#define AUDIO_EWAVNODATA	-5		/* WAV: missing data */
269#define AUDIO_EINTERNAL		-6		/* internal error */
270
271#define AUDIO_MAXERRNO		5
272
273/* and something to get a string associated with this error */
274const char *audio_errstring (int);
275
276/*
277 * generic routines?
278 */
279void	decode_int (const char *, int *);
280void	decode_uint (const char *, unsigned *);
281void	decode_time (const char *, struct timeval *);
282void	decode_encoding (const char *, int *);
283
284/*
285 * Track info, for reading/writing sun/wav header.
286 *
287 * Note that write_header() may change the values of format,
288 * encoding.
289 */
290
291struct track_info {
292	int	outfd;
293	char	*header_info;
294	int	format;
295	int	encoding;
296	int	precision;
297	int	qflag;
298	off_t	total_size;
299	int	sample_rate;
300	int	channels;
301};
302
303typedef void (*write_conv_func) (u_char *, int);
304
305void	write_header (struct track_info *);
306write_conv_func write_get_conv_func(struct track_info *);
307
308/* backends for the above */
309int sun_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
310int wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
311write_conv_func sun_write_get_conv_func(struct track_info *ti);
312write_conv_func wav_write_get_conv_func(struct track_info *ti);
313
314extern char	audio_default_info[8];
315
316/*
317 * get/put 16/32 bits of big/little endian data
318 */
319#include <sys/types.h>
320#include <machine/endian.h>
321#include <machine/bswap.h>
322
323#if BYTE_ORDER == BIG_ENDIAN
324
325#define getle16(v)	bswap16(v)
326#define getle32(v)	bswap32(v)
327#define getbe16(v)	(v)
328#define getbe32(v)	(v)
329
330#define putle16(x,v)	(x) = bswap16(v)
331#define putle32(x,v)	(x) = bswap32(v)
332#define putbe16(x,v)	(x) = (v)
333#define putbe32(x,v)	(x) = (v)
334
335#else
336
337#define getle16(v)	(v)
338#define getle32(v)	(v)
339#define getbe16(v)	bswap16(v)
340#define getbe32(v)	bswap32(v)
341
342#define putle16(x,v)	(x) = (v)
343#define putle32(x,v)	(x) = (v)
344#define putbe16(x,v)	(x) = bswap16(v)
345#define putbe32(x,v)	(x) = bswap32(v)
346
347#endif
348