1 /*
2 * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
3 *
4 * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
5 * the the people's Republic of China and other countries.
6 * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
7 *
8 * DISCLAIMER
9 * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
10 * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
11 * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
12 * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
13 * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
14 * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
15 * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
16 *
17 *
18 * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
19 * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
20 * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
21 * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
22 * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
28 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
30 * OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <errno.h>
35 #include "pcm_local.h"
36 #include <aw-alsa-lib/pcm_plugin.h>
37 #include <aw-alsa-lib/pcm_config.h>
38 #include <aw-alsa-lib/control.h>
39 #include "pcm_plugin_generic.h"
40 #include <math.h>
41
42 typedef struct {
43 /* This field need to be the first */
44 snd_pcm_plugin_t plug;
45 snd_pcm_format_t sformat;
46 unsigned int cchannels;
47 /*snd_ctl*/
48 unsigned int cur_vol[2];
49 unsigned int max_val; /* max index */
50 unsigned int zero_dB_val; /* index at 0 dB */
51 double min_dB;
52 double max_dB;
53 unsigned int *dB_value;
54 const snd_pcm_softvol_control_t *control;
55 } snd_pcm_softvol_t;
56
57 #define VOL_SCALE_SHIFT 16
58 #define VOL_SCALE_MASK ((1 << VOL_SCALE_SHIFT) - 1)
59
60 #define PRESET_RESOLUTION 256
61 #define PRESET_MIN_DB -51.0
62 #define ZERO_DB 0.0
63 #define MAX_DB_UPPER_LIMIT 50
64
65 static const unsigned int preset_dB_value[PRESET_RESOLUTION] = {
66 0x00b8, 0x00bd, 0x00c1, 0x00c5, 0x00ca, 0x00cf, 0x00d4, 0x00d9,
67 0x00de, 0x00e3, 0x00e8, 0x00ed, 0x00f3, 0x00f9, 0x00fe, 0x0104,
68 0x010a, 0x0111, 0x0117, 0x011e, 0x0124, 0x012b, 0x0132, 0x0139,
69 0x0140, 0x0148, 0x0150, 0x0157, 0x015f, 0x0168, 0x0170, 0x0179,
70 0x0181, 0x018a, 0x0194, 0x019d, 0x01a7, 0x01b0, 0x01bb, 0x01c5,
71 0x01cf, 0x01da, 0x01e5, 0x01f1, 0x01fc, 0x0208, 0x0214, 0x0221,
72 0x022d, 0x023a, 0x0248, 0x0255, 0x0263, 0x0271, 0x0280, 0x028f,
73 0x029e, 0x02ae, 0x02be, 0x02ce, 0x02df, 0x02f0, 0x0301, 0x0313,
74 0x0326, 0x0339, 0x034c, 0x035f, 0x0374, 0x0388, 0x039d, 0x03b3,
75 0x03c9, 0x03df, 0x03f7, 0x040e, 0x0426, 0x043f, 0x0458, 0x0472,
76 0x048d, 0x04a8, 0x04c4, 0x04e0, 0x04fd, 0x051b, 0x053a, 0x0559,
77 0x0579, 0x0599, 0x05bb, 0x05dd, 0x0600, 0x0624, 0x0648, 0x066e,
78 0x0694, 0x06bb, 0x06e3, 0x070c, 0x0737, 0x0762, 0x078e, 0x07bb,
79 0x07e9, 0x0818, 0x0848, 0x087a, 0x08ac, 0x08e0, 0x0915, 0x094b,
80 0x0982, 0x09bb, 0x09f5, 0x0a30, 0x0a6d, 0x0aab, 0x0aeb, 0x0b2c,
81 0x0b6f, 0x0bb3, 0x0bf9, 0x0c40, 0x0c89, 0x0cd4, 0x0d21, 0x0d6f,
82 0x0dbf, 0x0e11, 0x0e65, 0x0ebb, 0x0f12, 0x0f6c, 0x0fc8, 0x1026,
83 0x1087, 0x10e9, 0x114e, 0x11b5, 0x121f, 0x128b, 0x12fa, 0x136b,
84 0x13df, 0x1455, 0x14ce, 0x154a, 0x15c9, 0x164b, 0x16d0, 0x1758,
85 0x17e4, 0x1872, 0x1904, 0x1999, 0x1a32, 0x1ace, 0x1b6e, 0x1c11,
86 0x1cb9, 0x1d64, 0x1e13, 0x1ec7, 0x1f7e, 0x203a, 0x20fa, 0x21bf,
87 0x2288, 0x2356, 0x2429, 0x2500, 0x25dd, 0x26bf, 0x27a6, 0x2892,
88 0x2984, 0x2a7c, 0x2b79, 0x2c7c, 0x2d85, 0x2e95, 0x2fab, 0x30c7,
89 0x31ea, 0x3313, 0x3444, 0x357c, 0x36bb, 0x3801, 0x394f, 0x3aa5,
90 0x3c02, 0x3d68, 0x3ed6, 0x404d, 0x41cd, 0x4355, 0x44e6, 0x4681,
91 0x4826, 0x49d4, 0x4b8c, 0x4d4f, 0x4f1c, 0x50f3, 0x52d6, 0x54c4,
92 0x56be, 0x58c3, 0x5ad4, 0x5cf2, 0x5f1c, 0x6153, 0x6398, 0x65e9,
93 0x6849, 0x6ab7, 0x6d33, 0x6fbf, 0x7259, 0x7503, 0x77bd, 0x7a87,
94 0x7d61, 0x804d, 0x834a, 0x8659, 0x897a, 0x8cae, 0x8ff5, 0x934f,
95 0x96bd, 0x9a40, 0x9dd8, 0xa185, 0xa548, 0xa922, 0xad13, 0xb11b,
96 0xb53b, 0xb973, 0xbdc5, 0xc231, 0xc6b7, 0xcb58, 0xd014, 0xd4ed,
97 0xd9e3, 0xdef6, 0xe428, 0xe978, 0xeee8, 0xf479, 0xfa2b, 0xffff,
98 };
99
100 typedef union {
101 int i;
102 short s[2];
103 } val_t;
MULTI_DIV_32x16(int a,unsigned short b)104 static inline int MULTI_DIV_32x16(int a, unsigned short b)
105
106 {
107 val_t v, x, y;
108 v.i = a;
109 y.i = 0;
110 x.i = (unsigned short)v.s[0];
111 x.i *= b;
112 y.s[0] = x.s[1];
113 y.i += (int)v.s[1] * b;
114
115 return y.i;
116 }
117
118 /* unused, only support little endian */
119 #ifndef bswap_32
120 #define bswap_32(a) (a)
121 #endif
122
123 #ifndef bswap_16
124 #define bswap_16(a) (a)
125 #endif
126
MULTI_DIV_int(int a,unsigned int b,int swap)127 static inline int MULTI_DIV_int(int a, unsigned int b, int swap)
128 {
129 unsigned int gain = (b >> VOL_SCALE_SHIFT);
130 int fraction;
131 a = swap ? (int)bswap_32(a) : a;
132 fraction = MULTI_DIV_32x16(a, b & VOL_SCALE_MASK);
133 if (gain) {
134 long long amp = (long long)a * gain + fraction;
135 if (amp > (int)0x7fffffff)
136 amp = (int)0x7fffffff;
137 else if (amp < (int)0x80000000)
138 amp = (int)0x80000000;
139 return swap ? (int)bswap_32((int)amp) : (int)amp;
140 }
141 return swap ? (int)bswap_32(fraction) : fraction;
142 }
143
MULTI_DIV_short(short a,unsigned int b,int swap)144 static inline short MULTI_DIV_short(short a, unsigned int b, int swap)
145 {
146 unsigned int gain = b >> VOL_SCALE_SHIFT;
147 int fraction;
148 a = swap ? (short)bswap_16(a) : a;
149 fraction = (int)(a * (b & VOL_SCALE_MASK)) >> VOL_SCALE_SHIFT;
150 if (gain) {
151 int amp = a * gain + fraction;
152 if (abs(amp) > 0x7fff)
153 amp = (a<0) ? (short)0x8000 : (short)0x7fff;
154 return swap ? (short)bswap_16((short)amp) : (short)amp;
155 }
156 return swap ? (short)bswap_16((short)fraction) : (short)fraction;
157 }
158
159 #define CONVERT_AREA(TYPE, swap) do { \
160 unsigned int ch, fr; \
161 TYPE *src, *dst; \
162 for (ch = 0; ch < channels; ch++) { \
163 src_area = &src_areas[ch]; \
164 dst_area = &dst_areas[ch]; \
165 src = snd_pcm_channel_area_addr(src_area, src_offset); \
166 dst = snd_pcm_channel_area_addr(dst_area, dst_offset); \
167 src_step = snd_pcm_channel_area_step(src_area) / sizeof(TYPE); \
168 dst_step = snd_pcm_channel_area_step(dst_area) / sizeof(TYPE); \
169 GET_VOL_SCALE; \
170 fr = frames; \
171 if (! vol_scale) { \
172 while (fr--) { \
173 *dst = 0; \
174 dst += dst_step; \
175 } \
176 } else if (vol_scale == 0xffff) { \
177 while (fr--) { \
178 *dst = *src; \
179 src += src_step; \
180 dst += dst_step; \
181 } \
182 } else { \
183 while (fr--) { \
184 *dst = (TYPE) MULTI_DIV_##TYPE(*src, vol_scale, swap); \
185 src += src_step; \
186 dst += dst_step; \
187 } \
188 } \
189 } \
190 } while (0)
191
192 #define GET_VOL_SCALE \
193 switch (ch) { \
194 case 0: \
195 case 2: \
196 vol_scale = (channels == ch + 1) ? vol_c : vol[0]; \
197 break; \
198 case 4: \
199 case 5: \
200 vol_scale = vol_c; \
201 break; \
202 default: \
203 vol_scale = vol[ch & 1]; \
204 break; \
205 }
206
softvol_convert_stereo_vol(snd_pcm_softvol_t * svol,const snd_pcm_channel_area_t * dst_areas,snd_pcm_uframes_t dst_offset,const snd_pcm_channel_area_t * src_areas,snd_pcm_uframes_t src_offset,unsigned int channels,snd_pcm_uframes_t frames)207 static void softvol_convert_stereo_vol(snd_pcm_softvol_t *svol,
208 const snd_pcm_channel_area_t *dst_areas,
209 snd_pcm_uframes_t dst_offset,
210 const snd_pcm_channel_area_t *src_areas,
211 snd_pcm_uframes_t src_offset,
212 unsigned int channels,
213 snd_pcm_uframes_t frames)
214 {
215 const snd_pcm_channel_area_t *dst_area, *src_area;
216 unsigned int src_step, dst_step;
217 unsigned int vol_scale, vol[2], vol_c;
218
219 #if 0
220 /* for test */
221 snd_pcm_areas_copy(dst_areas, dst_offset, src_areas, src_offset,
222 channels, frames, svol->sformat);
223 return;
224 #endif
225 if (svol->cur_vol[0] == 0 && svol->cur_vol[1] == 0) {
226 snd_pcm_areas_silence(dst_areas, dst_offset, channels, frames,
227 svol->sformat);
228 return;
229 } else if (svol->zero_dB_val && svol->cur_vol[0] == svol->zero_dB_val &&
230 svol->cur_vol[1] == svol->zero_dB_val) {
231 snd_pcm_areas_copy(dst_areas, dst_offset, src_areas, src_offset,
232 channels, frames, svol->sformat);
233 return;
234 }
235
236 if (svol->max_val == 1) {
237 vol[0] = svol->cur_vol[0] ? 0xffff : 0;
238 vol[1] = svol->cur_vol[1] ? 0xffff : 0;
239 vol_c = vol[0] | vol[1];
240 } else {
241 vol[0] = svol->dB_value[svol->cur_vol[0]];
242 vol[1] = svol->dB_value[svol->cur_vol[1]];
243 vol_c = svol->dB_value[(svol->cur_vol[0] + svol->cur_vol[1]) / 2];
244 }
245 switch (svol->sformat) {
246 case SND_PCM_FORMAT_S16_LE:
247 case SND_PCM_FORMAT_S16_BE:
248 /* 16bit samples */
249 CONVERT_AREA(short,
250 !snd_pcm_format_little_endian(svol->sformat));
251 break;
252 case SND_PCM_FORMAT_S32_LE:
253 case SND_PCM_FORMAT_S32_BE:
254 /* 32bit samples */
255 CONVERT_AREA(int,
256 !snd_pcm_format_little_endian(svol->sformat));
257 break;
258 #if 0
259 case SND_PCM_FORMAT_S24_3LE:
260 CONVERT_AREA_S24_3LE();
261 #endif
262 break;
263 default:
264 break;
265 }
266 }
267
268 #undef GET_VOL_SCALE
269 #define GET_VOL_SCALE
softvol_convert_mono_vol(snd_pcm_softvol_t * svol,const snd_pcm_channel_area_t * dst_areas,snd_pcm_uframes_t dst_offset,const snd_pcm_channel_area_t * src_areas,snd_pcm_uframes_t src_offset,unsigned int channels,snd_pcm_uframes_t frames)270 static void softvol_convert_mono_vol(snd_pcm_softvol_t *svol,
271 const snd_pcm_channel_area_t *dst_areas,
272 snd_pcm_uframes_t dst_offset,
273 const snd_pcm_channel_area_t *src_areas,
274 snd_pcm_uframes_t src_offset,
275 unsigned int channels,
276 snd_pcm_uframes_t frames)
277 {
278 const snd_pcm_channel_area_t *dst_area, *src_area;
279 unsigned int src_step, dst_step;
280 unsigned int vol_scale;
281
282 #if 0
283 /* for test */
284 snd_pcm_areas_copy(dst_areas, dst_offset, src_areas, src_offset,
285 channels, frames, svol->sformat);
286 return;
287 #else
288 if (svol->cur_vol[0] == 0) {
289 snd_pcm_areas_silence(dst_areas, dst_offset, channels, frames,
290 svol->sformat);
291 return;
292 } else if (svol->zero_dB_val && svol->cur_vol[0] == svol->zero_dB_val) {
293 snd_pcm_areas_copy(dst_areas, dst_offset, src_areas, src_offset,
294 channels, frames, svol->sformat);
295 return;
296 }
297 #endif
298
299 if (svol->max_val == 1)
300 vol_scale = svol->cur_vol[0] ? 0xffff : 0;
301 else
302 vol_scale = svol->dB_value[svol->cur_vol[0]];
303 switch (svol->sformat) {
304 case SND_PCM_FORMAT_S16_LE:
305 case SND_PCM_FORMAT_S16_BE:
306 /* 16bit samples */
307 CONVERT_AREA(short,
308 !snd_pcm_format_little_endian(svol->sformat));
309 break;
310 case SND_PCM_FORMAT_S32_LE:
311 case SND_PCM_FORMAT_S32_BE:
312 /* 32bit samples */
313 CONVERT_AREA(int,
314 !snd_pcm_format_little_endian(svol->sformat));
315 break;
316 #if 0
317 case SND_PCM_FORMAT_S24_3LE:
318 CONVERT_AREA_S24_3LE();
319 #endif
320 break;
321 default:
322 break;
323 }
324 }
325
get_current_volume(snd_pcm_softvol_t * svol)326 static void get_current_volume(snd_pcm_softvol_t *svol)
327 {
328 unsigned int i;
329 snd_ctl_info_t info;
330 int ret;
331
332 ret = snd_ctl_get(svol->control->card_name,
333 svol->control->control_name,
334 &info);
335 if (ret < 0) {
336 awalsa_err("get ctl elem[%s] failed\n", svol->control->control_name);
337 return;
338 }
339 for (i = 0; i < svol->cchannels && i < 2; i++) {
340 svol->cur_vol[i] = (unsigned int)info.value;
341 }
342 }
343
344 static snd_pcm_uframes_t
snd_pcm_softvol_read_areas(snd_pcm_t * pcm,const snd_pcm_channel_area_t * areas,snd_pcm_uframes_t offset,snd_pcm_uframes_t size,const snd_pcm_channel_area_t * slave_areas,snd_pcm_uframes_t slave_offset,snd_pcm_uframes_t * slave_sizep)345 snd_pcm_softvol_read_areas(snd_pcm_t *pcm,
346 const snd_pcm_channel_area_t *areas,
347 snd_pcm_uframes_t offset,
348 snd_pcm_uframes_t size,
349 const snd_pcm_channel_area_t *slave_areas,
350 snd_pcm_uframes_t slave_offset,
351 snd_pcm_uframes_t *slave_sizep)
352 {
353 snd_pcm_softvol_t *svol = pcm->private_data;
354 if (size > *slave_sizep)
355 size = *slave_sizep;
356 get_current_volume(svol);
357 if (svol->cchannels == 1)
358 softvol_convert_mono_vol(svol, areas, offset, slave_areas,
359 slave_offset, pcm->channels, size);
360 else
361 softvol_convert_stereo_vol(svol, areas, offset, slave_areas,
362 slave_offset, pcm->channels, size);
363 *slave_sizep = size;
364 return size;
365 }
366
367 static snd_pcm_uframes_t
snd_pcm_softvol_write_areas(snd_pcm_t * pcm,const snd_pcm_channel_area_t * areas,snd_pcm_uframes_t offset,snd_pcm_uframes_t size,const snd_pcm_channel_area_t * slave_areas,snd_pcm_uframes_t slave_offset,snd_pcm_uframes_t * slave_sizep)368 snd_pcm_softvol_write_areas(snd_pcm_t *pcm,
369 const snd_pcm_channel_area_t *areas,
370 snd_pcm_uframes_t offset,
371 snd_pcm_uframes_t size,
372 const snd_pcm_channel_area_t *slave_areas,
373 snd_pcm_uframes_t slave_offset,
374 snd_pcm_uframes_t *slave_sizep)
375 {
376 snd_pcm_softvol_t *svol = pcm->private_data;
377 if (size > *slave_sizep)
378 size = *slave_sizep;
379 /*awalsa_info("cchannels=%d, offset=%u, size:%u, slave_offset=%u, slave_size=%u\n",*/
380 /*svol->cchannels, offset, size, slave_offset, *slave_sizep);*/
381 get_current_volume(svol);
382
383 if (svol->cchannels == 1)
384 softvol_convert_mono_vol(svol, slave_areas, slave_offset,
385 areas, offset, pcm->channels, size);
386 else
387 softvol_convert_stereo_vol(svol, slave_areas, slave_offset,
388 areas, offset, pcm->channels, size);
389 *slave_sizep = size;
390 return size;
391 }
392
softvol_free(snd_pcm_softvol_t * svol)393 static void softvol_free(snd_pcm_softvol_t *svol)
394 {
395 if (svol->plug.gen.close_slave)
396 snd_pcm_close(svol->plug.gen.slave);
397 if (svol->dB_value && svol->dB_value != preset_dB_value)
398 snd_free(svol->dB_value);
399 snd_free(svol);
400 }
401
snd_pcm_softvol_close(snd_pcm_t * pcm)402 static int snd_pcm_softvol_close(snd_pcm_t *pcm)
403 {
404 snd_pcm_softvol_t *svol = pcm->private_data;
405 softvol_free(svol);
406 return 0;
407 }
408
snd_pcm_softvol_hw_refine_cprepare(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)409 static int snd_pcm_softvol_hw_refine_cprepare(snd_pcm_t *pcm,
410 snd_pcm_hw_params_t *params)
411 {
412 int err;
413 snd_pcm_softvol_t *svol = pcm->private_data;
414 snd_interval_t access_mask = { .mask = SND_PCM_ACCBIT_SHM };
415 snd_interval_t format_mask = {
416 .mask =
417 (1ULL << SND_PCM_FORMAT_S16_LE) |
418 (1ULL << SND_PCM_FORMAT_S16_BE) |
419 (1ULL << SND_PCM_FORMAT_S32_LE) |
420 (1ULL << SND_PCM_FORMAT_S32_BE)
421 };
422
423 awalsa_debug("\n");
424 if (svol->sformat != SND_PCM_FORMAT_UNKNOWN) {
425 snd_mask_none(&format_mask);
426 snd_mask_set(&format_mask, (unsigned long)svol->sformat);
427 }
428 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_ACCESS,
429 &access_mask);
430 if (err < 0)
431 return err;
432 err = _snd_pcm_hw_param_set_mask(params, SND_PCM_HW_PARAM_FORMAT,
433 &format_mask);
434 if (err < 0)
435 return err;
436 err = _snd_pcm_hw_param_set_min(params, SND_PCM_HW_PARAM_CHANNELS, 1, 0);
437 if (err < 0)
438 return err;
439 #if 0
440 params->info &= ~(SND_PCM_INFO_MMAP | SND_PCM_INFO_MMAP_VALID);
441 #endif
442 return 0;
443 }
444
snd_pcm_softvol_hw_refine_sprepare(snd_pcm_t * pcm,snd_pcm_hw_params_t * sparams)445 static int snd_pcm_softvol_hw_refine_sprepare(snd_pcm_t *pcm, snd_pcm_hw_params_t *sparams)
446 {
447 snd_pcm_softvol_t *svol = pcm->private_data;
448 snd_interval_t saccess_mask = { .mask = SND_PCM_ACCBIT_MMAP };
449 awalsa_debug("\n");
450 _snd_pcm_hw_params_any(sparams);
451 _snd_pcm_hw_param_set_mask(sparams, SND_PCM_HW_PARAM_ACCESS,
452 &saccess_mask);
453 if (svol->sformat != SND_PCM_FORMAT_UNKNOWN) {
454 _snd_pcm_hw_param_set(sparams, SND_PCM_HW_PARAM_FORMAT,
455 (unsigned long)svol->sformat, 0);
456 }
457 return 0;
458 }
459
460 /*
461 * refine the access mask
462 */
check_access_mask(snd_pcm_hw_params_t * src,snd_pcm_hw_params_t * dst)463 static int check_access_mask(snd_pcm_hw_params_t *src,
464 snd_pcm_hw_params_t *dst)
465 {
466 const snd_interval_t *mask;
467 snd_interval_t smask;
468
469 mask = hw_param_interval_c(src, SND_PCM_HW_PARAM_ACCESS);
470 snd_mask_none(&smask);
471 if (snd_mask_test(mask, (unsigned long)SND_PCM_ACCESS_RW_INTERLEAVED) ||
472 snd_mask_test(mask, (unsigned long)SND_PCM_ACCESS_MMAP_INTERLEAVED)) {
473 snd_mask_set(&smask, (unsigned long)SND_PCM_ACCESS_RW_INTERLEAVED);
474 snd_mask_set(&smask, (unsigned long)SND_PCM_ACCESS_MMAP_INTERLEAVED);
475 }
476 if (snd_mask_test(mask, (unsigned long)SND_PCM_ACCESS_RW_NONINTERLEAVED) ||
477 snd_mask_test(mask, (unsigned long)SND_PCM_ACCESS_MMAP_NONINTERLEAVED)) {
478 snd_mask_set(&smask, (unsigned long)SND_PCM_ACCESS_RW_NONINTERLEAVED);
479 snd_mask_set(&smask, (unsigned long)SND_PCM_ACCESS_MMAP_NONINTERLEAVED);
480 }
481 if (snd_mask_test(mask, (unsigned long)SND_PCM_ACCESS_MMAP_COMPLEX))
482 snd_mask_set(&smask, (unsigned long)SND_PCM_ACCESS_MMAP_COMPLEX);
483
484 return _snd_pcm_hw_param_set_mask(dst, SND_PCM_HW_PARAM_ACCESS, &smask);
485 }
486
snd_pcm_softvol_hw_refine_schange(snd_pcm_t * pcm,snd_pcm_hw_params_t * params,snd_pcm_hw_params_t * sparams)487 static int snd_pcm_softvol_hw_refine_schange(snd_pcm_t *pcm,
488 snd_pcm_hw_params_t *params,
489 snd_pcm_hw_params_t *sparams)
490 {
491 snd_pcm_softvol_t *svol = pcm->private_data;
492 int err;
493 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
494 SND_PCM_HW_PARBIT_RATE |
495 SND_PCM_HW_PARBIT_PERIODS |
496 SND_PCM_HW_PARBIT_PERIOD_SIZE |
497 SND_PCM_HW_PARBIT_PERIOD_TIME |
498 SND_PCM_HW_PARBIT_BUFFER_SIZE |
499 SND_PCM_HW_PARBIT_BUFFER_TIME);
500
501 awalsa_debug("\n");
502 if (svol->sformat == SND_PCM_FORMAT_UNKNOWN)
503 links |= (SND_PCM_HW_PARBIT_FORMAT |
504 SND_PCM_HW_PARBIT_SAMPLE_BITS);
505 err = _snd_pcm_hw_params_refine(sparams, links, params);
506 if (err < 0)
507 return err;
508
509 err = check_access_mask(params, sparams);
510 if (err < 0)
511 return err;
512
513 return 0;
514 }
515
snd_pcm_softvol_hw_refine_cchange(snd_pcm_t * pcm,snd_pcm_hw_params_t * params,snd_pcm_hw_params_t * sparams)516 static int snd_pcm_softvol_hw_refine_cchange(snd_pcm_t *pcm,
517 snd_pcm_hw_params_t *params,
518 snd_pcm_hw_params_t *sparams)
519 {
520 snd_pcm_softvol_t *svol = pcm->private_data;
521 int err;
522 unsigned int links = (SND_PCM_HW_PARBIT_CHANNELS |
523 SND_PCM_HW_PARBIT_RATE |
524 SND_PCM_HW_PARBIT_PERIODS |
525 SND_PCM_HW_PARBIT_PERIOD_SIZE |
526 SND_PCM_HW_PARBIT_PERIOD_TIME |
527 SND_PCM_HW_PARBIT_BUFFER_SIZE |
528 SND_PCM_HW_PARBIT_BUFFER_TIME);
529
530 awalsa_debug("\n");
531 if (svol->sformat == SND_PCM_FORMAT_UNKNOWN)
532 links |= (SND_PCM_HW_PARBIT_FORMAT |
533 SND_PCM_HW_PARBIT_SAMPLE_BITS);
534 err = _snd_pcm_hw_params_refine(params, links, sparams);
535 if (err < 0)
536 return err;
537
538 err = check_access_mask(sparams, params);
539 if (err < 0)
540 return err;
541
542 return 0;
543 }
544
snd_pcm_softvol_hw_refine(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)545 static int snd_pcm_softvol_hw_refine(snd_pcm_t *pcm, snd_pcm_hw_params_t *params)
546 {
547 return snd_pcm_hw_refine_slave(pcm, params,
548 snd_pcm_softvol_hw_refine_cprepare,
549 snd_pcm_softvol_hw_refine_cchange,
550 snd_pcm_softvol_hw_refine_sprepare,
551 snd_pcm_softvol_hw_refine_schange,
552 snd_pcm_generic_hw_refine);
553 }
554
snd_pcm_softvol_hw_params(snd_pcm_t * pcm,snd_pcm_hw_params_t * params)555 static int snd_pcm_softvol_hw_params(snd_pcm_t *pcm, snd_pcm_hw_params_t * params)
556 {
557 snd_pcm_softvol_t *svol = pcm->private_data;
558 snd_pcm_t *slave = svol->plug.gen.slave;
559 #if 0
560 snd_pcm_format_t src_format, dst_format;
561 int err;
562 snd_pcm_hw_params_t *slave_params;
563
564 awalsa_debug("\n");
565
566 snd_pcm_hw_params_alloca(&slave_params);
567 memcpy(slave_params, params, sizeof(snd_pcm_hw_params_t));
568
569 snd_pcm_hw_param_change(slave_params, SND_PCM_HW_PARAM_ACCESS);
570 err = snd_pcm_hw_params_set_access(slave, slave_params, SND_PCM_ACCESS_MMAP_INTERLEAVED);
571 if (err < 0) {
572 awalsa_err("snd_pcm_hw_params_set_access failed\n");
573 return err;
574 }
575
576 err = _snd_pcm_hw_params_internal(slave, slave_params);
577 if (err < 0) {
578 awalsa_err("failed to set slave hw params\n");
579 return err;
580 }
581 /*TODO*/
582 memcpy(params, slave_params, sizeof(snd_pcm_hw_params_t));
583 #else
584 awalsa_debug("\n");
585 int err = snd_pcm_hw_params_slave(pcm, params,
586 snd_pcm_softvol_hw_refine_cchange,
587 snd_pcm_softvol_hw_refine_sprepare,
588 snd_pcm_softvol_hw_refine_schange,
589 snd_pcm_generic_hw_params);
590 if (err < 0)
591 return err;
592 #endif
593
594 if (slave->format != SND_PCM_FORMAT_S16_LE &&
595 slave->format != SND_PCM_FORMAT_S16_BE &&
596 #if 0
597 slave->format != SND_PCM_FORMAT_S24_3LE &&
598 #endif
599 slave->format != SND_PCM_FORMAT_S32_LE &&
600 slave->format != SND_PCM_FORMAT_S32_BE) {
601 awalsa_err("softvol supports only S16_LE, S16_BE, S24_3LE, S32_LE "
602 " or S32_BE");
603 return -EINVAL;
604 }
605 svol->sformat = slave->format;
606 return 0;
607 }
608
snd_pcm_softvol_dump(snd_pcm_t * pcm)609 static void snd_pcm_softvol_dump(snd_pcm_t *pcm)
610 {
611 snd_pcm_softvol_t *svol = pcm->private_data;
612
613 printf("Soft volume PCM\n");
614 printf("Control: %s\n", svol->control->control_name);
615 if (svol->max_val == 1) {
616 printf("boolean\n");
617 } else {
618 printf("min_dB: %g\n", svol->min_dB);
619 printf("max_dB: %g\n", svol->max_dB);
620 printf("resolution: %d\n", svol->max_val + 1);
621 }
622 if (pcm->setup) {
623 printf("Its setup is:\n");
624 snd_pcm_dump_setup(pcm);
625 }
626 printf("Slave: ");
627 snd_pcm_dump(svol->plug.gen.slave);
628 }
629
630 static const snd_pcm_ops_t snd_pcm_softvol_ops = {
631 .close = snd_pcm_softvol_close,
632 /*.info = snd_pcm_generic_info,*/
633 .hw_refine = snd_pcm_softvol_hw_refine,
634 .hw_params = snd_pcm_softvol_hw_params,
635 .hw_free = snd_pcm_generic_hw_free,
636 .sw_params = snd_pcm_generic_sw_params,
637 .channel_info = snd_pcm_generic_channel_info,
638 .dump = snd_pcm_softvol_dump,
639 /*.nonblock = snd_pcm_generic_nonblock,*/
640 /*.async = snd_pcm_generic_async,*/
641 .mmap = snd_pcm_generic_mmap,
642 .munmap = snd_pcm_generic_munmap,
643 .query_chmaps = snd_pcm_generic_query_chmaps,
644 .get_chmap = snd_pcm_generic_get_chmap,
645 .set_chmap = snd_pcm_generic_set_chmap,
646 };
647
softvol_load_control(snd_pcm_t * pcm,snd_pcm_softvol_t * svol,int cchannels,double min_dB,double max_dB,int resolution)648 static int softvol_load_control(snd_pcm_t *pcm, snd_pcm_softvol_t *svol,
649 int cchannels, double min_dB, double max_dB,
650 int resolution)
651 {
652 int ret;
653 unsigned int i;
654 snd_ctl_info_t info;
655
656 svol->max_val = resolution - 1;
657 svol->min_dB = min_dB;
658 svol->max_dB = max_dB;
659
660 if (svol->max_val == 1 || svol->max_dB == ZERO_DB)
661 svol->zero_dB_val = svol->max_val;
662 else if (svol->max_dB < 0)
663 svol->zero_dB_val = 0;
664 else
665 svol->zero_dB_val = (min_dB / (min_dB - max_dB)) *
666 svol->max_val;
667 awalsa_debug("\n");
668 ret = snd_ctl_get(svol->control->card_name, svol->control->control_name, &info);
669 if (ret < 0) {
670 memset(&info, 0, sizeof(snd_ctl_info_t));
671 info.name = svol->control->control_name;
672 info.value = svol->max_val;
673 info.count = cchannels;
674 info.min = 0;
675 info.max = svol->max_val;
676 ret = snd_ctl_add(svol->control->card_name, &info);
677 if (ret < 0) {
678 awalsa_err("create snd ctl failed\n");
679 return -1;
680 }
681 } else {
682 /* check kcontrol range */
683 if (info.max != svol->max_val ||
684 info.min != 0 ||
685 info.count != cchannels) {
686 awalsa_err("exist kcontrol(%s) max:%d,min:%d,count:%d\n"
687 "but now range is max:%d, min:%d, count:%d\n",
688 info.name, info.max, info.min, info.count,
689 svol->max_val, 0, cchannels);
690 return -1;
691 }
692 }
693 awalsa_debug("\n");
694
695 if (min_dB == PRESET_MIN_DB && max_dB == ZERO_DB &&
696 resolution == PRESET_RESOLUTION)
697 svol->dB_value = (unsigned int *)preset_dB_value;
698 else {
699 svol->dB_value = calloc(resolution, sizeof(unsigned int));
700 if (! svol->dB_value) {
701 awalsa_err("cannot allocate dB table\n");
702 return -ENOMEM;
703 }
704 svol->min_dB = min_dB;
705 svol->max_dB = max_dB;
706 for (i = 0; i <= svol->max_val; i++) {
707 double db = svol->min_dB +
708 (i * (svol->max_dB - svol->min_dB)) /
709 svol->max_val;
710 double v = (pow(10.0, db / 20.0) *
711 (double)(1 << VOL_SCALE_SHIFT));
712
713 svol->dB_value[i] = (unsigned int)v;
714 }
715 if (svol->zero_dB_val)
716 svol->dB_value[svol->zero_dB_val] = 65535;
717 }
718 return ret;
719 }
720
_snd_pcm_softvol_open(snd_pcm_t ** pcmp,const snd_pcm_config_t * pcm_config,snd_pcm_stream_t stream,int mode)721 int _snd_pcm_softvol_open(snd_pcm_t **pcmp, const snd_pcm_config_t *pcm_config,
722 snd_pcm_stream_t stream, int mode)
723 {
724 int ret;
725 const snd_pcm_softvol_config_t *svol_config = (const snd_pcm_softvol_config_t *)(pcm_config->config);
726 const snd_pcm_config_t *sconf = NULL;
727 snd_pcm_t *pcm = NULL, *spcm = NULL;
728 snd_pcm_softvol_t *svol = NULL;
729 snd_pcm_format_t sformat = SND_PCM_FORMAT_UNKNOWN;
730 int resolution = PRESET_RESOLUTION;
731 double min_dB = PRESET_MIN_DB;
732 double max_dB = ZERO_DB;
733 int cchannels = 1;
734
735 awalsa_debug("\n");
736 sconf = snd_pcm_config_get_config(svol_config->slave.pcm);
737 if (!sconf) {
738 awalsa_err("can't find softvol slave pcm\n");
739 return -EINVAL;
740 }
741
742 if (svol_config->resolution != 0)
743 resolution = svol_config->resolution;
744 if (svol_config->min_dB != 0)
745 min_dB = svol_config->min_dB;
746 if (svol_config->max_dB != 0)
747 max_dB = svol_config->max_dB;
748 if (svol_config->control.count != 0)
749 cchannels = svol_config->control.count;
750 awalsa_debug("resolution=%d, min_dB=%0.6f, max_dB=%0.6f, cchannels=%d\n",
751 resolution, min_dB, max_dB, cchannels);
752
753 ret = snd_pcm_open_config(&spcm, sconf, stream, mode);
754 if (ret < 0) {
755 awalsa_err("unable to open slave\n");
756 goto err;
757 }
758
759 awalsa_debug("\n");
760
761 svol = snd_malloc(sizeof(snd_pcm_softvol_t));
762 if (!svol) {
763 awalsa_err("no memory\n");
764 ret = -ENOMEM;
765 goto err;
766 }
767
768 snd_pcm_plugin_init(&svol->plug);
769 svol->control = &svol_config->control;
770 svol->sformat = sformat;
771 svol->cchannels = cchannels;
772 svol->plug.read = snd_pcm_softvol_read_areas;
773 svol->plug.write = snd_pcm_softvol_write_areas;
774 svol->plug.undo_read = snd_pcm_plugin_undo_read_generic;
775 svol->plug.undo_write = snd_pcm_plugin_undo_write_generic;
776 svol->plug.gen.slave = spcm;
777 svol->plug.gen.close_slave = 1;
778
779 /* load control */
780 ret = softvol_load_control(spcm, svol, cchannels, min_dB, max_dB, resolution);
781 if (ret < 0) {
782 awalsa_err("load control failed\n");
783 goto err;
784 }
785
786 awalsa_debug("\n");
787 ret = snd_pcm_new(&pcm, SND_PCM_TYPE_SOFTVOL, pcm_config->name, spcm->stream, spcm->mode);
788 /*ret = snd_pcm_new(&pcm, SND_PCM_TYPE_SOFTVOL, pcm_config->name, spcm->stream, mode);*/
789 if (ret < 0) {
790 awalsa_err("failed to new softvol pcm\n");
791 goto err;
792 }
793
794 awalsa_debug("\n");
795 pcm->ops = &snd_pcm_softvol_ops;
796 pcm->fast_ops = &snd_pcm_plugin_fast_ops;
797 pcm->private_data = svol;
798
799 pcm->mmap_shadow = 1;
800 snd_pcm_set_hw_ptr(pcm, &svol->plug.hw_ptr, 0, 0);
801 snd_pcm_set_appl_ptr(pcm, &svol->plug.appl_ptr, 0, 0);
802 awalsa_debug("\n");
803
804 *pcmp = pcm;
805 return 0;
806 err:
807 if (svol)
808 softvol_free(svol);
809 if (pcm)
810 snd_pcm_free(pcm);
811 return ret;
812 }
813