2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 /* feeder_volume, a long 'Lost Technology' rather than a new feature. */
32 #ifdef HAVE_KERNEL_OPTION_HEADERS
35 #include <dev/sound/pcm/sound.h>
36 #include <dev/sound/pcm/pcm.h>
37 #include "feeder_if.h"
40 #include "snd_fxdiv_gen.h"
42 SND_DECLARE_FILE("$FreeBSD$");
45 typedef void (*feed_volume_t)(int *, int *, uint32_t, uint8_t *, uint32_t);
47 #define FEEDVOLUME_CALC8(s, v) (SND_VOL_CALC_SAMPLE((intpcm_t) \
49 #define FEEDVOLUME_CALC16(s, v) SND_VOL_CALC_SAMPLE((intpcm_t)(s), v)
50 #define FEEDVOLUME_CALC24(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
51 #define FEEDVOLUME_CALC32(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
53 #define FEEDVOLUME_DECLARE(SIGN, BIT, ENDIAN) \
55 feed_volume_##SIGN##BIT##ENDIAN(int *vol, int *matrix, \
56 uint32_t channels, uint8_t *dst, uint32_t count) \
62 dst += count * PCM_##BIT##_BPS * channels; \
66 dst -= PCM_##BIT##_BPS; \
68 x = PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \
69 v = FEEDVOLUME_CALC##BIT(x, vol[matrix[i]]); \
70 x = PCM_CLAMP_##SIGN##BIT(v); \
71 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x); \
73 } while (--count != 0); \
76 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
77 FEEDVOLUME_DECLARE(S, 16, LE)
78 FEEDVOLUME_DECLARE(S, 32, LE)
80 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
81 FEEDVOLUME_DECLARE(S, 16, BE)
82 FEEDVOLUME_DECLARE(S, 32, BE)
84 #ifdef SND_FEEDER_MULTIFORMAT
85 FEEDVOLUME_DECLARE(S, 8, NE)
86 FEEDVOLUME_DECLARE(S, 24, LE)
87 FEEDVOLUME_DECLARE(S, 24, BE)
88 FEEDVOLUME_DECLARE(U, 8, NE)
89 FEEDVOLUME_DECLARE(U, 16, LE)
90 FEEDVOLUME_DECLARE(U, 24, LE)
91 FEEDVOLUME_DECLARE(U, 32, LE)
92 FEEDVOLUME_DECLARE(U, 16, BE)
93 FEEDVOLUME_DECLARE(U, 24, BE)
94 FEEDVOLUME_DECLARE(U, 32, BE)
97 struct feed_volume_info {
98 uint32_t bps, channels;
102 int matrix[SND_CHN_MAX];
105 #define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN) \
107 AFMT_##SIGN##BIT##_##ENDIAN, \
108 feed_volume_##SIGN##BIT##ENDIAN \
111 static const struct {
114 } feed_volume_info_tab[] = {
115 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
116 FEEDVOLUME_ENTRY(S, 16, LE),
117 FEEDVOLUME_ENTRY(S, 32, LE),
119 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
120 FEEDVOLUME_ENTRY(S, 16, BE),
121 FEEDVOLUME_ENTRY(S, 32, BE),
123 #ifdef SND_FEEDER_MULTIFORMAT
124 FEEDVOLUME_ENTRY(S, 8, NE),
125 FEEDVOLUME_ENTRY(S, 24, LE),
126 FEEDVOLUME_ENTRY(S, 24, BE),
127 FEEDVOLUME_ENTRY(U, 8, NE),
128 FEEDVOLUME_ENTRY(U, 16, LE),
129 FEEDVOLUME_ENTRY(U, 24, LE),
130 FEEDVOLUME_ENTRY(U, 32, LE),
131 FEEDVOLUME_ENTRY(U, 16, BE),
132 FEEDVOLUME_ENTRY(U, 24, BE),
133 FEEDVOLUME_ENTRY(U, 32, BE)
137 #define FEEDVOLUME_TAB_SIZE ((int32_t) \
138 (sizeof(feed_volume_info_tab) / \
139 sizeof(feed_volume_info_tab[0])))
142 feed_volume_init(struct pcm_feeder *f)
144 struct feed_volume_info *info;
145 struct pcmchan_matrix *m;
149 if (f->desc->in != f->desc->out ||
150 AFMT_CHANNEL(f->desc->in) > SND_CHN_MAX)
153 for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
154 if (AFMT_ENCODING(f->desc->in) ==
155 feed_volume_info_tab[i].format) {
156 info = malloc(sizeof(*info), M_DEVBUF,
161 info->bps = AFMT_BPS(f->desc->in);
162 info->channels = AFMT_CHANNEL(f->desc->in);
163 info->apply = feed_volume_info_tab[i].apply;
164 info->volume_class = SND_VOL_C_PCM;
165 info->state = FEEDVOLUME_ENABLE;
168 m = feeder_matrix_default_channel_map(info->channels);
170 free(info, M_DEVBUF);
174 ret = feeder_volume_apply_matrix(f, m);
176 free(info, M_DEVBUF);
186 feed_volume_free(struct pcm_feeder *f)
188 struct feed_volume_info *info;
192 free(info, M_DEVBUF);
200 feed_volume_set(struct pcm_feeder *f, int what, int value)
202 struct feed_volume_info *info;
203 struct pcmchan_matrix *m;
210 case FEEDVOLUME_CLASS:
211 if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
213 info->volume_class = value;
215 case FEEDVOLUME_CHANNELS:
216 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
218 m = feeder_matrix_default_channel_map(value);
221 ret = feeder_volume_apply_matrix(f, m);
223 case FEEDVOLUME_STATE:
224 if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
237 feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
238 uint32_t count, void *source)
240 struct feed_volume_info *info;
242 int i, *vol, *matrix;
246 * Fetch filter data operation.
250 if (info->state == FEEDVOLUME_BYPASS)
251 return (FEEDER_FEED(f->source, c, b, count, source));
253 vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
254 matrix = info->matrix;
257 * First, let see if we really need to apply gain at all.
262 if (vol[matrix[--i]] != SND_VOL_FLAT) {
268 /* Nope, just bypass entirely. */
270 return (FEEDER_FEED(f->source, c, b, count, source));
273 align = info->bps * info->channels;
279 j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
284 info->apply(vol, matrix, info->channels, dst, j);
290 } while (count != 0);
295 static struct pcm_feederdesc feeder_volume_desc[] = {
296 { FEEDER_VOLUME, 0, 0, 0, 0 },
300 static kobj_method_t feeder_volume_methods[] = {
301 KOBJMETHOD(feeder_init, feed_volume_init),
302 KOBJMETHOD(feeder_free, feed_volume_free),
303 KOBJMETHOD(feeder_set, feed_volume_set),
304 KOBJMETHOD(feeder_feed, feed_volume_feed),
308 FEEDER_DECLARE(feeder_volume, NULL);
313 * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
314 * to feeder_volume matrix structure. There are
315 * possibilites that feeder_volume be inserted
316 * before or after feeder_matrix, which in this
317 * case feeder_volume must be in a good terms
318 * with _current_ matrix.
321 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
323 struct feed_volume_info *info;
326 if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME ||
327 f->data == NULL || m == NULL || m->channels < SND_CHN_MIN ||
328 m->channels > SND_CHN_MAX)
333 for (i = 0; i < (sizeof(info->matrix) / sizeof(info->matrix[0])); i++) {
335 info->matrix[i] = m->map[i].type;
337 info->matrix[i] = SND_CHN_T_FL;
340 info->channels = m->channels;