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