summaryrefslogtreecommitdiff
path: root/usr/src/cmd/audio/include/audio_hdr.h
blob: e0b5b22b8e9c1e8871d7c5263de5f09b5ce9c536 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 1992-2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#ifndef _MULTIMEDIA_AUDIO_HDR_H
#define	_MULTIMEDIA_AUDIO_HDR_H

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#ifdef __cplusplus
extern "C" {
#endif

/*
 * Define an in-core audio data header.
 *
 * This is different that the on-disk file header.
 * The fields defined here are preliminary at best.
 */

/*
 * The audio header contains the following fields:
 *
 *      endian                  Byte order of 16-bit or greater PCM,
 *                                and possibly floating point data.
 *
 *	sample_rate		Number of samples per second (per channel).
 *
 *	samples_per_unit	This field describes the number of samples
 *				  represented by each sample unit (which, by
 *				  definition, are aligned on byte boundaries).
 *				  Audio samples may be stored individually
 *				  or, in the case of compressed formats
 *				  (e.g., ADPCM), grouped in algorithm-
 *				  specific ways.  If the data is bit-packed,
 *				  this field tells the number of samples
 *				  needed to get to a byte boundary.
 *
 *	bytes_per_unit		Number of bytes stored for each sample unit
 *
 *	channels		Number of interleaved sample units.
 *				   For any given time quantum, the set
 *				   consisting of 'channels' sample units
 *				   is called a sample frame.  Seeks in
 *				   the data should be aligned to the start
 *				   of the nearest sample frame.
 *
 *	encoding		Data encoding format.
 *
 *	data_size		Number of bytes in the data.
 *				   This value is advisory only, and may
 *				   be set to the value AUDIO_UNKNOWN_SIZE
 *				   if the data size is unknown (for
 *				   instance, if the data is being
 *				   recorded or generated and piped
 *				   to another process).
 *
 * The first four values are used to compute the byte offset given a
 * particular time, and vice versa.  Specifically:
 *
 *	seconds = offset / C
 *	offset = seconds * C
 * where:
 *	C = (channels * bytes_per_unit * sample_rate) / samples_per_unit
 *
 *
 */
typedef struct {
	unsigned	sample_rate;		/* samples per second */
	unsigned	samples_per_unit;	/* samples per unit */
	unsigned	bytes_per_unit;		/* bytes per sample unit */
	unsigned	channels;		/* # of interleaved channels */
	unsigned	encoding;		/* data encoding format */
	unsigned	endian;			/* byte order */
	unsigned	data_size;		/* length of data (optional) */
} Audio_hdr;

/*
 * Define the possible encoding types.
 * Note that the names that overlap the encodings in <sun/audioio.h>
 * must have the same values.
 */
#define	AUDIO_ENCODING_NONE	(0)	/* No encoding specified ... */
#define	AUDIO_ENCODING_ULAW	(1)	/* ISDN u-law */
#define	AUDIO_ENCODING_ALAW	(2)	/* ISDN A-law */
#define	AUDIO_ENCODING_LINEAR	(3)	/* PCM 2's-complement (0-center) */
#define	AUDIO_ENCODING_FLOAT	(100)	/* IEEE float (-1. <-> +1.) */
#define	AUDIO_ENCODING_G721	(101)	/* CCITT g.721 ADPCM */
#define	AUDIO_ENCODING_G722	(102)	/* CCITT g.722 ADPCM */
#define	AUDIO_ENCODING_G723	(103)	/* CCITT g.723 ADPCM */
#define	AUDIO_ENCODING_DVI	(104)	/* DVI ADPCM */

/*
 * Define the possible endian types.
 */
#define	AUDIO_ENDIAN_BIG 0	/* SPARC, 68000, etc. */
#define	AUDIO_ENDIAN_SMALL 1	/* Intel */
#define	AUDIO_ENDIAN_UNKNOWN 2	/* Unknown endian */

/* Value used for indeterminate size (e.g., data passed through a pipe) */
#define	AUDIO_UNKNOWN_SIZE	((unsigned)(~0))


/* Define conversion macros for integer<->floating-point conversions */

/* double to 8,16,32-bit linear */
#define	audio_d2c(X)		((X) >= 1. ? 127 : (X) <= -1. ? -127 :	\
				    (char)(rint((X) * 127.)))
#define	audio_d2s(X)		((X) >= 1. ? 32767 : (X) <= -1. ? -32767 :\
				    (short)(rint((X) * 32767.)))
#define	audio_d2l(X)		((X) >= 1. ? 2147483647 : (X) <= -1. ?	\
				    -2147483647 :			\
				    (long)(rint((X) * 2147483647.)))

/* 8,16,32-bit linear to double */
#define	audio_c2d(X)		(((unsigned char)(X)) == 0x80 ? -1. :	\
				    ((double)((char)(X))) / 127.)
#define	audio_s2d(X)		(((unsigned short)(X)) == 0x8000 ? -1. :\
				    ((double)((short)(X))) / 32767.)
#define	audio_l2d(X)		(((unsigned long)(X)) == 0x80000000 ? -1. :\
				    ((double)((long)(X))) / 2147483647.)

#ifdef __cplusplus
}
#endif

#endif /* !_MULTIMEDIA_AUDIO_HDR_H */