xref: /freebsd/sys/dev/sound/pcm/matrix.h (revision ebacd8013fe5f7fdf9f6a5b286f6680dd2891036)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2007-2009 Ariff Abdullah <ariff@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  *
28  * $FreeBSD$
29  */
30 
31 #ifndef _SND_MATRIX_H_
32 #define _SND_MATRIX_H_
33 
34 #undef SND_MULTICHANNEL
35 #ifndef SND_OLDSTEREO
36 #define SND_MULTICHANNEL	1
37 #endif
38 
39 /*
40  * XXX = unused, but part of the definition (will be used someday, maybe).
41  */
42 #define SND_CHN_T_FL		0	/* Front Left         */
43 #define SND_CHN_T_FR		1	/* Front Right        */
44 #define SND_CHN_T_FC		2	/* Front Center       */
45 #define SND_CHN_T_LF		3	/* Low Frequency      */
46 #define SND_CHN_T_BL		4	/* Back Left          */
47 #define SND_CHN_T_BR		5	/* Back Right         */
48 #define SND_CHN_T_FLC		6	/* Front Left Center  XXX */
49 #define SND_CHN_T_FRC		7	/* Front Right Center XXX */
50 #define SND_CHN_T_BC		8	/* Back Center        */
51 #define SND_CHN_T_SL		9	/* Side Left          */
52 #define SND_CHN_T_SR		10	/* Side Right         */
53 #define SND_CHN_T_TC		11	/* Top Center         XXX */
54 #define SND_CHN_T_TFL		12	/* Top Front Left     XXX */
55 #define SND_CHN_T_TFC		13	/* Top Front Center   XXX */
56 #define SND_CHN_T_TFR		14	/* Top Front Right    XXX */
57 #define SND_CHN_T_TBL		15	/* Top Back Left      XXX */
58 #define SND_CHN_T_TBC		16	/* Top Back Center    XXX */
59 #define SND_CHN_T_TBR		17	/* Top Back Right     XXX */
60 #define SND_CHN_T_MAX		18	/* Maximum channels   */
61 
62 #define SND_CHN_T_ZERO		(SND_CHN_T_MAX + 1)	/* Zero samples */
63 
64 #define SND_CHN_T_LABELS	{					\
65 	 "fl",  "fr",  "fc",  "lf",  "bl",  "br",			\
66 	"flc", "frc",  "bc",  "sl",  "sr",  "tc",			\
67 	"tfl", "tfc", "tfr", "tbl", "tbc", "tbr"			\
68 }
69 
70 #define SND_CHN_T_NAMES	{						\
71 	"Front Left", "Front Right", "Front Center",			\
72 	"Low Frequency Effects",					\
73 	"Back Left", "Back Right",					\
74 	"Front Left Center", "Front Right Center",			\
75 	"Back Center",							\
76 	"Side Left", "Side Right",					\
77 	"Top Center",							\
78 	"Top Front Left", "Top Front Center", "Top Front Right",	\
79 	"Top Back Left", "Top Back Center", "Top Back Right"		\
80 }
81 
82 #define SND_CHN_T_MASK_FL	(1 << SND_CHN_T_FL)
83 #define SND_CHN_T_MASK_FR	(1 << SND_CHN_T_FR)
84 #define SND_CHN_T_MASK_FC	(1 << SND_CHN_T_FC)
85 #define SND_CHN_T_MASK_LF	(1 << SND_CHN_T_LF)
86 #define SND_CHN_T_MASK_BL	(1 << SND_CHN_T_BL)
87 #define SND_CHN_T_MASK_BR	(1 << SND_CHN_T_BR)
88 #define SND_CHN_T_MASK_FLC	(1 << SND_CHN_T_FLC)
89 #define SND_CHN_T_MASK_FRC	(1 << SND_CHN_T_FRC)
90 #define SND_CHN_T_MASK_BC	(1 << SND_CHN_T_BC)
91 #define SND_CHN_T_MASK_SL	(1 << SND_CHN_T_SL)
92 #define SND_CHN_T_MASK_SR	(1 << SND_CHN_T_SR)
93 #define SND_CHN_T_MASK_TC	(1 << SND_CHN_T_TC)
94 #define SND_CHN_T_MASK_TFL	(1 << SND_CHN_T_TFL)
95 #define SND_CHN_T_MASK_TFC	(1 << SND_CHN_T_TFC)
96 #define SND_CHN_T_MASK_TFR	(1 << SND_CHN_T_TFR)
97 #define SND_CHN_T_MASK_TBL	(1 << SND_CHN_T_TBL)
98 #define SND_CHN_T_MASK_TBC	(1 << SND_CHN_T_TBC)
99 #define SND_CHN_T_MASK_TBR	(1 << SND_CHN_T_TBR)
100 
101 #define SND_CHN_LEFT_MASK	(SND_CHN_T_MASK_FL  |			\
102 				 SND_CHN_T_MASK_BL  |			\
103 				 SND_CHN_T_MASK_FLC |			\
104 				 SND_CHN_T_MASK_SL  |			\
105 				 SND_CHN_T_MASK_TFL |			\
106 				 SND_CHN_T_MASK_TBL)
107 
108 #define SND_CHN_RIGHT_MASK	(SND_CHN_T_MASK_FR  |			\
109 				 SND_CHN_T_MASK_BR  |			\
110 				 SND_CHN_T_MASK_FRC |			\
111 				 SND_CHN_T_MASK_SR  |			\
112 				 SND_CHN_T_MASK_TFR |			\
113 				 SND_CHN_T_MASK_TBR)
114 
115 #define SND_CHN_CENTER_MASK	(SND_CHN_T_MASK_FC  |			\
116 				 SND_CHN_T_MASK_BC  |			\
117 				 SND_CHN_T_MASK_TC  |			\
118 				 SND_CHN_T_MASK_TFC |			\
119 				 SND_CHN_T_MASK_TBC |			\
120 				 SND_CHN_T_MASK_LF)	/* XXX what?!? */
121 
122 /*
123  * Matrix identity.
124  */
125 
126 /* 1 @ Mono 1.0 */
127 #define SND_CHN_MATRIX_1_0	0
128 #define SND_CHN_MATRIX_1	SND_CHN_MATRIX_1_0
129 
130 /* 2 @ Stereo 2.0 */
131 #define SND_CHN_MATRIX_2_0	1
132 #define SND_CHN_MATRIX_2	SND_CHN_MATRIX_2_0
133 
134 /* 3 @ 2.1 (lfe), 3.0 (rear center, DEFAULT) */
135 #define SND_CHN_MATRIX_2_1	2
136 #define SND_CHN_MATRIX_3_0	3
137 #define SND_CHN_MATRIX_3	SND_CHN_MATRIX_3_0
138 
139 /* 4 @ 3.1 (lfe), 4.0 (Quadraphonic, DEFAULT) */
140 #define SND_CHN_MATRIX_3_1	4
141 #define SND_CHN_MATRIX_4_0	5
142 #define SND_CHN_MATRIX_4	SND_CHN_MATRIX_4_0
143 
144 /* 5 @ 4.1 (lfe), 5.0 (center, DEFAULT) */
145 #define SND_CHN_MATRIX_4_1	6
146 #define SND_CHN_MATRIX_5_0	7
147 #define SND_CHN_MATRIX_5	SND_CHN_MATRIX_5_0
148 
149 /* 6 @ 5.1 (lfe, DEFAULT), 6.0 (rear center) */
150 #define SND_CHN_MATRIX_5_1	8
151 #define SND_CHN_MATRIX_6_0	9
152 #define SND_CHN_MATRIX_6	SND_CHN_MATRIX_5_1
153 
154 /* 7 @ 6.1 (lfe, DEFAULT), 7.0 */
155 #define SND_CHN_MATRIX_6_1	10
156 #define SND_CHN_MATRIX_7_0	11
157 #define SND_CHN_MATRIX_7	SND_CHN_MATRIX_6_1
158 
159 /* 8 @ 7.1 (lfe) */
160 #define SND_CHN_MATRIX_7_1	12
161 #define SND_CHN_MATRIX_8	SND_CHN_MATRIX_7_1
162 
163 #define SND_CHN_MATRIX_MAX	13
164 
165 #define SND_CHN_MATRIX_BEGIN	SND_CHN_MATRIX_1_0
166 #define SND_CHN_MATRIX_END	SND_CHN_MATRIX_7_1
167 
168 /* Custom matrix identity */
169 #define SND_CHN_MATRIX_DRV		-4	/* driver own identity   */
170 #define SND_CHN_MATRIX_PCMCHANNEL	-3	/* PCM channel identity  */
171 #define SND_CHN_MATRIX_MISC		-2	/* misc, custom defined  */
172 #define SND_CHN_MATRIX_UNKNOWN		-1	/* unknown               */
173 
174 #define SND_CHN_T_VOL_0DB	SND_CHN_T_MAX
175 #define SND_CHN_T_VOL_MAX	(SND_CHN_T_VOL_0DB + 1)
176 
177 #define SND_CHN_T_BEGIN		SND_CHN_T_FL
178 #define SND_CHN_T_END		SND_CHN_T_TBR
179 #define SND_CHN_T_STEP		1
180 #define SND_CHN_MIN		1
181 
182 #ifdef SND_MULTICHANNEL
183 #define SND_CHN_MAX		8
184 #else
185 #define SND_CHN_MAX		2
186 #endif
187 
188 /*
189  * Multichannel interleaved volume matrix. Each calculated value relative
190  * to master and 0db will be stored in each CLASS + 1 as long as
191  * chn_setvolume_matrix() or the equivalent CHN_SETVOLUME() macros is
192  * used (see channel.c).
193  */
194 #define SND_VOL_C_MASTER	0
195 #define SND_VOL_C_PCM		1
196 #define SND_VOL_C_PCM_VAL	2
197 #define SND_VOL_C_MAX		3
198 
199 #define SND_VOL_C_BEGIN		SND_VOL_C_PCM
200 #define SND_VOL_C_END		SND_VOL_C_PCM
201 #define SND_VOL_C_STEP		2
202 
203 #define SND_VOL_C_VAL(x)	((x) + 1)
204 
205 #define SND_VOL_0DB_MIN		1
206 #define SND_VOL_0DB_MAX		100
207 
208 #define SND_VOL_0DB_MASTER	100
209 #define SND_VOL_0DB_PCM		45
210 
211 #define SND_VOL_RESOLUTION	8
212 #define SND_VOL_FLAT		(1 << SND_VOL_RESOLUTION)
213 
214 #define SND_VOL_CALC_SAMPLE(x, y)	(((x) * (y)) >> SND_VOL_RESOLUTION)
215 
216 #define SND_VOL_CALC_VAL(x, y, z)					\
217 			(((((x)[y][z] << SND_VOL_RESOLUTION) /		\
218 			 (x)[y][SND_CHN_T_VOL_0DB]) *			\
219 			 (x)[SND_VOL_C_MASTER][z]) /			\
220 			 (x)[SND_VOL_C_MASTER][SND_CHN_T_VOL_0DB])	\
221 
222 #endif	/* !_SND_MATRIX_H_ */
223