xref: /illumos-gate/usr/src/cmd/audio/include/wav.h (revision 3197c708)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3197c708SJimmy Vetayases  * Common Development and Distribution License (the "License").
6*3197c708SJimmy Vetayases  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*3197c708SJimmy Vetayases  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * This header file defines the .wav audio file format.
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #ifndef _WAV_H
317c478bd9Sstevel@tonic-gate #define	_WAV_H
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include <sys/types.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
367c478bd9Sstevel@tonic-gate extern "C" {
377c478bd9Sstevel@tonic-gate #endif
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Define the on-disk audio file header for the .wav file format.
417c478bd9Sstevel@tonic-gate  * By definition .wav files are little endian. Macros are provided
427c478bd9Sstevel@tonic-gate  * to make the conversion easier.
437c478bd9Sstevel@tonic-gate  *
447c478bd9Sstevel@tonic-gate  * The .wav format is one of the variations of the RIFF format. To
457c478bd9Sstevel@tonic-gate  * that end it contains a RIFF header chunk, a type chunk, a format
467c478bd9Sstevel@tonic-gate  * chunk, and then one or more data chunks. The following illustrates
477c478bd9Sstevel@tonic-gate  * the format:
487c478bd9Sstevel@tonic-gate  *
497c478bd9Sstevel@tonic-gate  *	RIFF	<Length of data>		RIFF header chunk
507c478bd9Sstevel@tonic-gate  *	WAVE					type chunk
517c478bd9Sstevel@tonic-gate  *	fmt<sp>					format chunk
527c478bd9Sstevel@tonic-gate  *	DATA	<Length of data> <data>		data chunk (one or more)
537c478bd9Sstevel@tonic-gate  *
547c478bd9Sstevel@tonic-gate  * Since the RIFF headers never change for a .wav file there's no real reason
557c478bd9Sstevel@tonic-gate  * to separate the header into the different chunks. Thus a single header
567c478bd9Sstevel@tonic-gate  * structure is defined for the header.
577c478bd9Sstevel@tonic-gate  *
587c478bd9Sstevel@tonic-gate  * When building a .wav header the size of the data isn't always known.
597c478bd9Sstevel@tonic-gate  * The following define is used for that situation.
607c478bd9Sstevel@tonic-gate  */
617c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_UNKNOWN_SIZE		(~0)
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate struct wav_filehdr {
647c478bd9Sstevel@tonic-gate 	uint32_t	wav_riff_ID;		/* RIFF file ID */
657c478bd9Sstevel@tonic-gate 	int32_t		wav_riff_size;		/* size of file - wav_riff* */
667c478bd9Sstevel@tonic-gate 	uint32_t	wav_type_ID;		/* file type ID */
677c478bd9Sstevel@tonic-gate 	uint32_t	wav_fmt_ID;		/* format ID */
687c478bd9Sstevel@tonic-gate 	uint32_t	wav_fmt_size;		/* size of wav_fmt_*'s */
697c478bd9Sstevel@tonic-gate 	uint16_t	wav_fmt_encoding;	/* audio data encoding method */
707c478bd9Sstevel@tonic-gate 	uint16_t	wav_fmt_channels;	/* number of channels */
717c478bd9Sstevel@tonic-gate 	uint32_t	wav_fmt_sample_rate;	/* sample rate */
727c478bd9Sstevel@tonic-gate 	uint32_t	wav_fmt_bytes_per_second; /* bytes per sec. of audio */
737c478bd9Sstevel@tonic-gate 	uint16_t	wav_fmt_bytes_per_sample; /* bytes per audio sample */
747c478bd9Sstevel@tonic-gate 	uint16_t	wav_fmt_bits_per_sample; /* bits per audio sample */
757c478bd9Sstevel@tonic-gate 	uint32_t	wav_data_ID;		/* data ID */
767c478bd9Sstevel@tonic-gate 	int32_t		wav_data_size;		/* size of the data */
777c478bd9Sstevel@tonic-gate };
787c478bd9Sstevel@tonic-gate typedef struct wav_filehdr wav_filehdr_t;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate /* define for wav_filehdr.wav_riff_ID */
817c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_RIFF_ID		((uint32_t)0x46464952)	/* 'RIFF' */
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate /* define for wav_filehdr.wav_wave_ID */
847c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_TYPE_ID		((uint32_t)0x45564157)	/* 'WAVE' */
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate /* define for wav_filehdr.wav_fmt_ID */
877c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FORMAT_ID		((uint32_t)0x20746d66)	/* 'fmt ' */
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate /* define for wav_filehdr.wav_fmt_size */
907c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FORMAT_SIZE			0x10	/* constant value */
917c478bd9Sstevel@tonic-gate 
927c478bd9Sstevel@tonic-gate /* defines for wav_filehdr.wav_fmt_encoding */
937c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_UNKNOWN			0x0000
947c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_PCM			0x0001
957c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_MS_ADPCM			0x0002
967c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_ALAW			0x0006
977c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_MULAW			0x0007
987c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_ENCODING_DVI_ADPCM		0x0011
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate /* defines for wav_filehdr.wav_fmt_channels */
1017c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_CHANNELS_MONO			0
1027c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_CHANNELS_STEREO			1
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /* defines for wav_filehdr.wav_fmt_bytes_per_sample */
1057c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_8_BIT_MONO	1
1067c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_8_BIT_STEREO	2
1077c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_16_BIT_MONO	2
1087c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BYTES_PER_SAMPLE_16_BIT_STEREO	4
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate /* defines for wav_filehdr.wav_fmt_bits_per_sample */
1117c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BITS_PER_SAMPLE_8_BITS		8
1127c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FMT_BITS_PER_SAMPLE_16_BITS		16
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /* defines for wav_filehdr.wav_data_ID */
115*3197c708SJimmy Vetayases #define	AUDIO_WAV_DATA_ID_UC		((uint32_t)0x41544144)	/* DATA */
116*3197c708SJimmy Vetayases #define	AUDIO_WAV_DATA_ID_LC		((uint32_t)0x61746164)	/* data */
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /* byte swapping macros */
1207c478bd9Sstevel@tonic-gate #if defined(__sparc)				/* big endian */
1217c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FILE2HOST_INT(from, to)				\
1227c478bd9Sstevel@tonic-gate 		(*to) = ((((*from) >> 24) & 0xff) | (((*from) & 0xff) << 24) | \
1237c478bd9Sstevel@tonic-gate 		(((*from) >> 8) & 0xff00) | (((*from) & 0xff00) << 8))
1247c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FILE2HOST_SHORT(from, to)				\
1257c478bd9Sstevel@tonic-gate 		(*to) = ((((*from) >> 8) & 0xff) | (((*from) & 0xff) << 8))
1267c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_HOST2FILE_INT(from, to)				\
1277c478bd9Sstevel@tonic-gate 		AUDIO_WAV_FILE2HOST_INT((from), (to))
1287c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_HOST2FILE_SHORT(from, to)				\
1297c478bd9Sstevel@tonic-gate 		AUDIO_WAV_FILE2HOST_SHORT((from), (to))
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate #elif defined(__i386) || defined(__amd64)	/* little endian */
1327c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FILE2HOST_INT(from, to)				\
1337c478bd9Sstevel@tonic-gate 		*((int *)(to)) = *((int *)(from))
1347c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_FILE2HOST_SHORT(from, to)				\
1357c478bd9Sstevel@tonic-gate 		*((short *)(to)) = *((short *)(from))
1367c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_HOST2FILE_INT(from, to)				\
1377c478bd9Sstevel@tonic-gate 		*((int *)(to)) = *((int *)(from))
1387c478bd9Sstevel@tonic-gate #define	AUDIO_WAV_HOST2FILE_SHORT(from, to)				\
1397c478bd9Sstevel@tonic-gate 		*((short *)(to)) = *((short *)(from))
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate #else
1427c478bd9Sstevel@tonic-gate #error unknown machine type;
1437c478bd9Sstevel@tonic-gate #endif	/* byte swapping */
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate #ifdef	__cplusplus
1477c478bd9Sstevel@tonic-gate }
1487c478bd9Sstevel@tonic-gate #endif
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate #endif /* _WAV_H */
151