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