xref: /titanic_44/usr/src/cmd/audio/include/wav.h (revision c28749e97052f09388969427adf7df641cdcdc22)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * This header file defines the .wav audio file format.
29  */
30 
31 #ifndef _WAV_H
32 #define	_WAV_H
33 
34 #pragma ident	"%Z%%M%	%I%	%E% SMI"
35 
36 #include <sys/types.h>
37 
38 #ifdef	__cplusplus
39 extern "C" {
40 #endif
41 
42 /*
43  * Define the on-disk audio file header for the .wav file format.
44  * By definition .wav files are little endian. Macros are provided
45  * to make the conversion easier.
46  *
47  * The .wav format is one of the variations of the RIFF format. To
48  * that end it contains a RIFF header chunk, a type chunk, a format
49  * chunk, and then one or more data chunks. The following illustrates
50  * the format:
51  *
52  *	RIFF	<Length of data>		RIFF header chunk
53  *	WAVE					type chunk
54  *	fmt<sp>					format chunk
55  *	DATA	<Length of data> <data>		data chunk (one or more)
56  *
57  * Since the RIFF headers never change for a .wav file there's no real reason
58  * to separate the header into the different chunks. Thus a single header
59  * structure is defined for the header.
60  *
61  * When building a .wav header the size of the data isn't always known.
62  * The following define is used for that situation.
63  */
64 #define	AUDIO_WAV_UNKNOWN_SIZE		(~0)
65 
66 struct wav_filehdr {
67 	uint32_t	wav_riff_ID;		/* RIFF file ID */
68 	int32_t		wav_riff_size;		/* size of file - wav_riff* */
69 	uint32_t	wav_type_ID;		/* file type ID */
70 	uint32_t	wav_fmt_ID;		/* format ID */
71 	uint32_t	wav_fmt_size;		/* size of wav_fmt_*'s */
72 	uint16_t	wav_fmt_encoding;	/* audio data encoding method */
73 	uint16_t	wav_fmt_channels;	/* number of channels */
74 	uint32_t	wav_fmt_sample_rate;	/* sample rate */
75 	uint32_t	wav_fmt_bytes_per_second; /* bytes per sec. of audio */
76 	uint16_t	wav_fmt_bytes_per_sample; /* bytes per audio sample */
77 	uint16_t	wav_fmt_bits_per_sample; /* bits per audio sample */
78 	uint32_t	wav_data_ID;		/* data ID */
79 	int32_t		wav_data_size;		/* size of the data */
80 };
81 typedef struct wav_filehdr wav_filehdr_t;
82 
83 /* define for wav_filehdr.wav_riff_ID */
84 #define	AUDIO_WAV_RIFF_ID		((uint32_t)0x46464952)	/* 'RIFF' */
85 
86 /* define for wav_filehdr.wav_wave_ID */
87 #define	AUDIO_WAV_TYPE_ID		((uint32_t)0x45564157)	/* 'WAVE' */
88 
89 /* define for wav_filehdr.wav_fmt_ID */
90 #define	AUDIO_WAV_FORMAT_ID		((uint32_t)0x20746d66)	/* 'fmt ' */
91 
92 /* define for wav_filehdr.wav_fmt_size */
93 #define	AUDIO_WAV_FORMAT_SIZE			0x10	/* constant value */
94 
95 /* defines for wav_filehdr.wav_fmt_encoding */
96 #define	AUDIO_WAV_FMT_ENCODING_UNKNOWN			0x0000
97 #define	AUDIO_WAV_FMT_ENCODING_PCM			0x0001
98 #define	AUDIO_WAV_FMT_ENCODING_MS_ADPCM			0x0002
99 #define	AUDIO_WAV_FMT_ENCODING_ALAW			0x0006
100 #define	AUDIO_WAV_FMT_ENCODING_MULAW			0x0007
101 #define	AUDIO_WAV_FMT_ENCODING_DVI_ADPCM		0x0011
102 
103 /* defines for wav_filehdr.wav_fmt_channels */
104 #define	AUDIO_WAV_FMT_CHANNELS_MONO			0
105 #define	AUDIO_WAV_FMT_CHANNELS_STEREO			1
106 
107 /* defines for wav_filehdr.wav_fmt_bytes_per_sample */
108 #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_8_BIT_MONO	1
109 #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_8_BIT_STEREO	2
110 #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_16_BIT_MONO	2
111 #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_16_BIT_STEREO	4
112 
113 /* defines for wav_filehdr.wav_fmt_bits_per_sample */
114 #define	AUDIO_WAV_FMT_BITS_PER_SAMPLE_8_BITS		8
115 #define	AUDIO_WAV_FMT_BITS_PER_SAMPLE_16_BITS		16
116 
117 /* defines for wav_filehdr.wav_data_ID */
118 #define	AUDIO_WAV_DATA_ID_LC		((uint32_t)0x41544144)	/* data */
119 #define	AUDIO_WAV_DATA_ID_UC		((uint32_t)0x61746164)	/* DATA */
120 
121 
122 /* byte swapping macros */
123 #if defined(__sparc)				/* big endian */
124 #define	AUDIO_WAV_FILE2HOST_INT(from, to)				\
125 		(*to) = ((((*from) >> 24) & 0xff) | (((*from) & 0xff) << 24) | \
126 		(((*from) >> 8) & 0xff00) | (((*from) & 0xff00) << 8))
127 #define	AUDIO_WAV_FILE2HOST_SHORT(from, to)				\
128 		(*to) = ((((*from) >> 8) & 0xff) | (((*from) & 0xff) << 8))
129 #define	AUDIO_WAV_HOST2FILE_INT(from, to)				\
130 		AUDIO_WAV_FILE2HOST_INT((from), (to))
131 #define	AUDIO_WAV_HOST2FILE_SHORT(from, to)				\
132 		AUDIO_WAV_FILE2HOST_SHORT((from), (to))
133 
134 #elif defined(__i386) || defined(__amd64)	/* little endian */
135 #define	AUDIO_WAV_FILE2HOST_INT(from, to)				\
136 		*((int *)(to)) = *((int *)(from))
137 #define	AUDIO_WAV_FILE2HOST_SHORT(from, to)				\
138 		*((short *)(to)) = *((short *)(from))
139 #define	AUDIO_WAV_HOST2FILE_INT(from, to)				\
140 		*((int *)(to)) = *((int *)(from))
141 #define	AUDIO_WAV_HOST2FILE_SHORT(from, to)				\
142 		*((short *)(to)) = *((short *)(from))
143 
144 #else
145 #error unknown machine type;
146 #endif	/* byte swapping */
147 
148 
149 #ifdef	__cplusplus
150 }
151 #endif
152 
153 #endif /* _WAV_H */
154