1 /*******************************************************************************
2  * Copyright 2009-2016 Jörg Müller
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  ******************************************************************************/
16 
17 #pragma once
18 
19 /**
20  * @file Specification.h
21  * @ingroup respec
22  * Defines all important macros and basic data structures for stream format descriptions.
23  */
24 
25 #include "Audaspace.h"
26 
27 /// The size of a format in bytes.
28 #define AUD_FORMAT_SIZE(format) (format & 0x0F)
29 /// The size of a sample in the specified device format in bytes.
30 #define AUD_DEVICE_SAMPLE_SIZE(specs) (specs.channels * (specs.format & 0x0F))
31 /// The size of a sample in the specified format in bytes.
32 #define AUD_SAMPLE_SIZE(specs) (specs.channels * sizeof(sample_t))
33 
34 /// Compares two audio data specifications.
35 #define AUD_COMPARE_SPECS(s1, s2) ((s1.rate == s2.rate) && (s1.channels == s2.channels))
36 
37 /// Returns the bit for a channel mask.
38 #define AUD_CHANNEL_BIT(channel) (0x01 << channel)
39 
40 AUD_NAMESPACE_BEGIN
41 
42 /**
43  * The format of a sample.
44  * The last 4 bit save the byte count of the format.
45  */
46 enum SampleFormat
47 {
48 	FORMAT_INVALID = 0x00,		/// Invalid sample format.
49 	FORMAT_U8      = 0x01,		/// 1 byte unsigned byte.
50 	FORMAT_S16     = 0x12,		/// 2 byte signed integer.
51 	FORMAT_S24     = 0x13,		/// 3 byte signed integer.
52 	FORMAT_S32     = 0x14,		/// 4 byte signed integer.
53 	FORMAT_FLOAT32 = 0x24,		/// 4 byte float.
54 	FORMAT_FLOAT64 = 0x28		/// 8 byte float.
55 };
56 
57 /// The channel count.
58 enum Channels
59 {
60 	CHANNELS_INVALID    = 0,	/// Invalid channel count.
61 	CHANNELS_MONO       = 1,	/// Mono.
62 	CHANNELS_STEREO     = 2,	/// Stereo.
63 	CHANNELS_STEREO_LFE = 3,	/// Stereo with LFE channel.
64 	CHANNELS_SURROUND4  = 4,	/// 4 channel surround sound.
65 	CHANNELS_SURROUND5  = 5,	/// 5 channel surround sound.
66 	CHANNELS_SURROUND51 = 6,	/// 5.1 surround sound.
67 	CHANNELS_SURROUND61 = 7,	/// 6.1 surround sound.
68 	CHANNELS_SURROUND71 = 8	/// 7.1 surround sound.
69 };
70 
71 /// The channel names.
72 enum Channel
73 {
74 	CHANNEL_FRONT_LEFT = 0,
75 	CHANNEL_FRONT_RIGHT,
76 	CHANNEL_FRONT_CENTER,
77 	CHANNEL_LFE,
78 	CHANNEL_REAR_LEFT,
79 	CHANNEL_REAR_RIGHT,
80 	CHANNEL_REAR_CENTER,
81 	CHANNEL_SIDE_LEFT,
82 	CHANNEL_SIDE_RIGHT,
83 	CHANNEL_MAX
84 };
85 
86 /**
87  * The sample rate tells how many samples are played back within one second.
88  * Some exotic formats may use other sample rates than provided here.
89  */
90 enum DefaultSampleRate
91 {
92 	RATE_INVALID = 0,			/// Invalid sample rate.
93 	RATE_8000    = 8000,		/// 8000 Hz.
94 	RATE_16000   = 16000,		/// 16000 Hz.
95 	RATE_11025   = 11025,		/// 11025 Hz.
96 	RATE_22050   = 22050,		/// 22050 Hz.
97 	RATE_32000   = 32000,		/// 32000 Hz.
98 	RATE_44100   = 44100,		/// 44100 Hz.
99 	RATE_48000   = 48000,		/// 48000 Hz.
100 	RATE_88200   = 88200,		/// 88200 Hz.
101 	RATE_96000   = 96000,		/// 96000 Hz.
102 	RATE_192000  = 192000		/// 192000 Hz.
103 };
104 
105 /// Sample rate type.
106 typedef double SampleRate;
107 
108 /// Specification of a sound source.
109 struct Specs
110 {
111 	/// Sample rate in Hz.
112 	SampleRate rate;
113 
114 	/// Channel count.
115 	Channels channels;
116 };
117 
118 /// Specification of a sound device.
119 struct DeviceSpecs
120 {
121 	/// Sample format.
122 	SampleFormat format;
123 
124 	union
125 	{
126 		struct
127 		{
128 			/// Sample rate in Hz.
129 			SampleRate rate;
130 
131 			/// Channel count.
132 			Channels channels;
133 		};
134 		Specs specs;
135 	};
136 };
137 
138 AUD_NAMESPACE_END
139