Home | History | Annotate | Line # | Download | only in common
libaudio.h revision 1.21
      1 /*	$NetBSD: libaudio.h,v 1.21 2019/11/09 12:46:44 mrg Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1999, 2009, 2013, 2015, 2019 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  * 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 
     74 typedef 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 
    102 const char *audio_enc_from_val (int);
    103 int	audio_enc_to_val (const char *);
    104 
    105 int	audio_sun_to_encoding (int, u_int *, u_int *);
    106 int	audio_encoding_to_sun (int, int, int *);
    107 
    108 /*
    109  * M$ WAV files, info gleamed from sox sources
    110  */
    111 
    112 /*
    113  * This is the WAV audio file magic value.  Note that it
    114  * is also `RIFF' and `WAVE' in ASCII.
    115  */
    116 #define	WAVAUDIO_FILE_MAGIC_RIFF	((u_int32_t)0x52494646)
    117 #define	WAVAUDIO_FILE_MAGIC_WAVE	((u_int32_t)0x57415645)
    118 #define	WAVAUDIO_FILE_MAGIC_FMT		((u_int32_t)0x666d7420)
    119 #define	WAVAUDIO_FILE_MAGIC_DATA	((u_int32_t)0x64617461)
    120 
    121 /* purloined from public Microsoft RIFF docs via sox, mplayer, or directly */
    122 #define WAVE_FORMAT_UNKNOWN		(0x0000)
    123 #define WAVE_FORMAT_PCM			(0x0001)
    124 #define WAVE_FORMAT_ADPCM		(0x0002)
    125 #define WAVE_FORMAT_IEEE_FLOAT		(0x0003)
    126 #define WAVE_FORMAT_ALAW		(0x0006)
    127 #define WAVE_FORMAT_MULAW		(0x0007)
    128 #define WAVE_FORMAT_OKI_ADPCM		(0x0010)
    129 #define WAVE_FORMAT_IMA_ADPCM		(0x0011)
    130 #define WAVE_FORMAT_DIGISTD		(0x0015)
    131 #define WAVE_FORMAT_DIGIFIX		(0x0016)
    132 #define WAVE_FORMAT_DOLBY_AC2		(0x0030)
    133 #define WAVE_FORMAT_GSM610		(0x0031)
    134 #define WAVE_FORMAT_ROCKWELL_ADPCM	(0x003b)
    135 #define WAVE_FORMAT_ROCKWELL_DIGITALK	(0x003c)
    136 #define WAVE_FORMAT_G721_ADPCM		(0x0040)
    137 #define WAVE_FORMAT_G728_CELP		(0x0041)
    138 #define WAVE_FORMAT_MPEG		(0x0050)
    139 #define WAVE_FORMAT_MPEGLAYER3		(0x0055)
    140 #define WAVE_FORMAT_G726_ADPCM		(0x0064)
    141 #define WAVE_FORMAT_G722_ADPCM		(0x0065)
    142 #define IBM_FORMAT_MULAW		(0x0101)
    143 #define IBM_FORMAT_ALAW			(0x0102)
    144 #define IBM_FORMAT_ADPCM		(0x0103)
    145 #define WAVE_FORMAT_EXTENSIBLE		(0xfffe)
    146 
    147 const char *wav_enc_from_val (int);
    148 
    149 typedef struct {
    150 	char		name[4];
    151 	u_int32_t	len;
    152 } wav_audioheaderpart;
    153 
    154 typedef struct {
    155 	u_int16_t	tag;
    156 	u_int16_t	channels;
    157 	u_int32_t	sample_rate;
    158 	u_int32_t	avg_bps;
    159 	u_int16_t	alignment;
    160 	u_int16_t	bits_per_sample;
    161 } __packed wav_audioheaderfmt;
    162 
    163 typedef struct {
    164 	u_int16_t	len;
    165 	u_int16_t	valid_bits;
    166 	u_int32_t	speaker_pos_mask;
    167 	u_int16_t	sub_tag;
    168 	u_int8_t	dummy[14];
    169 } __packed wav_audiohdrextensible;
    170 
    171 /* returns size of header, or -ve for failure */
    172 ssize_t audio_wav_parse_hdr (void *, size_t, u_int *, u_int *, u_int *, u_int *, off_t *);
    173 
    174 extern int verbose;
    175 
    176 /*
    177  * audio routine error codes
    178  */
    179 #define AUDIO_ENOENT		-1		/* no such audio format */
    180 #define AUDIO_ESHORTHDR		-2		/* short header */
    181 #define AUDIO_EWAVUNSUPP	-3		/* WAV: unsupported file */
    182 #define AUDIO_EWAVBADPCM	-4		/* WAV: bad PCM bps */
    183 #define AUDIO_EWAVNODATA	-5		/* WAV: missing data */
    184 #define AUDIO_EINTERNAL		-6		/* internal error */
    185 
    186 #define AUDIO_MAXERRNO		5
    187 
    188 /* and something to get a string associated with this error */
    189 const char *audio_errstring (int);
    190 
    191 /*
    192  * generic routines?
    193  */
    194 void	decode_int (const char *, int *);
    195 void	decode_uint (const char *, unsigned *);
    196 void	decode_time (const char *, struct timeval *);
    197 void	decode_encoding (const char *, int *);
    198 
    199 /*
    200  * Track info, for reading/writing sun/wav header.
    201  *
    202  * Note that write_header() may change the values of format,
    203  * encoding.
    204  */
    205 
    206 struct track_info {
    207 	int	outfd;
    208 	char	*header_info;
    209 	int	format;
    210 	int	encoding;
    211 	int	precision;
    212 	int	qflag;
    213 	off_t	total_size;
    214 	int	sample_rate;
    215 	int	channels;
    216 };
    217 
    218 typedef void (*write_conv_func) (u_char *, int);
    219 
    220 void	write_header (struct track_info *);
    221 write_conv_func write_get_conv_func(struct track_info *);
    222 
    223 /* backends for the above */
    224 int sun_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
    225 int wav_prepare_header(struct track_info *ti, void **hdrp, size_t *lenp, int *leftp);
    226 write_conv_func sun_write_get_conv_func(struct track_info *ti);
    227 write_conv_func wav_write_get_conv_func(struct track_info *ti);
    228 
    229 extern char	audio_default_info[8];
    230 
    231 /*
    232  * get/put 16/32 bits of big/little endian data
    233  */
    234 #include <sys/types.h>
    235 #include <machine/endian.h>
    236 #include <machine/bswap.h>
    237 
    238 #if BYTE_ORDER == BIG_ENDIAN
    239 
    240 #define getle16(v)	bswap16(v)
    241 #define getle32(v)	bswap32(v)
    242 #define getbe16(v)	(v)
    243 #define getbe32(v)	(v)
    244 
    245 #define putle16(x,v)	(x) = bswap16(v)
    246 #define putle32(x,v)	(x) = bswap32(v)
    247 #define putbe16(x,v)	(x) = (v)
    248 #define putbe32(x,v)	(x) = (v)
    249 
    250 #else
    251 
    252 #define getle16(v)	(v)
    253 #define getle32(v)	(v)
    254 #define getbe16(v)	bswap16(v)
    255 #define getbe32(v)	bswap32(v)
    256 
    257 #define putle16(x,v)	(x) = (v)
    258 #define putle32(x,v)	(x) = (v)
    259 #define putbe16(x,v)	(x) = bswap16(v)
    260 #define putbe32(x,v)	(x) = bswap32(v)
    261 
    262 #endif
    263