2 * Copyright (c) 2005-2009 Ariff Abdullah <ariff@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 /* feeder_volume, a long 'Lost Technology' rather than a new feature. */
30 #ifdef HAVE_KERNEL_OPTION_HEADERS
33 #include <dev/sound/pcm/sound.h>
34 #include <dev/sound/pcm/pcm.h>
35 #include "feeder_if.h"
38 #include "snd_fxdiv_gen.h"
40 SND_DECLARE_FILE("$FreeBSD$");
43 typedef void (*feed_volume_t)(int *, int *, uint32_t, uint8_t *, uint32_t);
45 #define FEEDVOLUME_CALC8(s, v) (SND_VOL_CALC_SAMPLE((intpcm_t) \
47 #define FEEDVOLUME_CALC16(s, v) SND_VOL_CALC_SAMPLE((intpcm_t)(s), v)
48 #define FEEDVOLUME_CALC24(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
49 #define FEEDVOLUME_CALC32(s, v) SND_VOL_CALC_SAMPLE((intpcm64_t)(s), v)
51 #define FEEDVOLUME_DECLARE(SIGN, BIT, ENDIAN) \
53 feed_volume_##SIGN##BIT##ENDIAN(int *vol, int *matrix, \
54 uint32_t channels, uint8_t *dst, uint32_t count) \
60 dst += count * PCM_##BIT##_BPS * channels; \
64 dst -= PCM_##BIT##_BPS; \
66 x = PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \
67 v = FEEDVOLUME_CALC##BIT(x, vol[matrix[i]]); \
68 x = PCM_CLAMP_##SIGN##BIT(v); \
69 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, x); \
71 } while (--count != 0); \
74 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
75 FEEDVOLUME_DECLARE(S, 16, LE)
76 FEEDVOLUME_DECLARE(S, 32, LE)
78 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
79 FEEDVOLUME_DECLARE(S, 16, BE)
80 FEEDVOLUME_DECLARE(S, 32, BE)
82 #ifdef SND_FEEDER_MULTIFORMAT
83 FEEDVOLUME_DECLARE(S, 8, NE)
84 FEEDVOLUME_DECLARE(S, 24, LE)
85 FEEDVOLUME_DECLARE(S, 24, BE)
86 FEEDVOLUME_DECLARE(U, 8, NE)
87 FEEDVOLUME_DECLARE(U, 16, LE)
88 FEEDVOLUME_DECLARE(U, 24, LE)
89 FEEDVOLUME_DECLARE(U, 32, LE)
90 FEEDVOLUME_DECLARE(U, 16, BE)
91 FEEDVOLUME_DECLARE(U, 24, BE)
92 FEEDVOLUME_DECLARE(U, 32, BE)
95 struct feed_volume_info {
96 uint32_t bps, channels;
100 int matrix[SND_CHN_MAX];
103 #define FEEDVOLUME_ENTRY(SIGN, BIT, ENDIAN) \
105 AFMT_##SIGN##BIT##_##ENDIAN, \
106 feed_volume_##SIGN##BIT##ENDIAN \
109 static const struct {
112 } feed_volume_info_tab[] = {
113 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
114 FEEDVOLUME_ENTRY(S, 16, LE),
115 FEEDVOLUME_ENTRY(S, 32, LE),
117 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT)
118 FEEDVOLUME_ENTRY(S, 16, BE),
119 FEEDVOLUME_ENTRY(S, 32, BE),
121 #ifdef SND_FEEDER_MULTIFORMAT
122 FEEDVOLUME_ENTRY(S, 8, NE),
123 FEEDVOLUME_ENTRY(S, 24, LE),
124 FEEDVOLUME_ENTRY(S, 24, BE),
125 FEEDVOLUME_ENTRY(U, 8, NE),
126 FEEDVOLUME_ENTRY(U, 16, LE),
127 FEEDVOLUME_ENTRY(U, 24, LE),
128 FEEDVOLUME_ENTRY(U, 32, LE),
129 FEEDVOLUME_ENTRY(U, 16, BE),
130 FEEDVOLUME_ENTRY(U, 24, BE),
131 FEEDVOLUME_ENTRY(U, 32, BE)
135 #define FEEDVOLUME_TAB_SIZE ((int32_t) \
136 (sizeof(feed_volume_info_tab) / \
137 sizeof(feed_volume_info_tab[0])))
140 feed_volume_init(struct pcm_feeder *f)
142 struct feed_volume_info *info;
143 struct pcmchan_matrix *m;
147 if (f->desc->in != f->desc->out ||
148 AFMT_CHANNEL(f->desc->in) > SND_CHN_MAX)
151 for (i = 0; i < FEEDVOLUME_TAB_SIZE; i++) {
152 if (AFMT_ENCODING(f->desc->in) ==
153 feed_volume_info_tab[i].format) {
154 info = malloc(sizeof(*info), M_DEVBUF,
159 info->bps = AFMT_BPS(f->desc->in);
160 info->channels = AFMT_CHANNEL(f->desc->in);
161 info->apply = feed_volume_info_tab[i].apply;
162 info->volume_class = SND_VOL_C_PCM;
163 info->state = FEEDVOLUME_ENABLE;
166 m = feeder_matrix_default_channel_map(info->channels);
168 free(info, M_DEVBUF);
172 ret = feeder_volume_apply_matrix(f, m);
174 free(info, M_DEVBUF);
184 feed_volume_free(struct pcm_feeder *f)
186 struct feed_volume_info *info;
190 free(info, M_DEVBUF);
198 feed_volume_set(struct pcm_feeder *f, int what, int value)
200 struct feed_volume_info *info;
201 struct pcmchan_matrix *m;
208 case FEEDVOLUME_CLASS:
209 if (value < SND_VOL_C_BEGIN || value > SND_VOL_C_END)
211 info->volume_class = value;
213 case FEEDVOLUME_CHANNELS:
214 if (value < SND_CHN_MIN || value > SND_CHN_MAX)
216 m = feeder_matrix_default_channel_map(value);
219 ret = feeder_volume_apply_matrix(f, m);
221 case FEEDVOLUME_STATE:
222 if (!(value == FEEDVOLUME_ENABLE || value == FEEDVOLUME_BYPASS))
235 feed_volume_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b,
236 uint32_t count, void *source)
238 struct feed_volume_info *info;
240 int i, *vol, *matrix;
244 * Fetch filter data operation.
248 if (info->state == FEEDVOLUME_BYPASS)
249 return (FEEDER_FEED(f->source, c, b, count, source));
251 vol = c->volume[SND_VOL_C_VAL(info->volume_class)];
252 matrix = info->matrix;
255 * First, let see if we really need to apply gain at all.
260 if (vol[matrix[--i]] != SND_VOL_FLAT) {
266 /* Nope, just bypass entirely. */
268 return (FEEDER_FEED(f->source, c, b, count, source));
271 align = info->bps * info->channels;
277 j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source),
282 info->apply(vol, matrix, info->channels, dst, j);
288 } while (count != 0);
293 static struct pcm_feederdesc feeder_volume_desc[] = {
294 { FEEDER_VOLUME, 0, 0, 0, 0 },
298 static kobj_method_t feeder_volume_methods[] = {
299 KOBJMETHOD(feeder_init, feed_volume_init),
300 KOBJMETHOD(feeder_free, feed_volume_free),
301 KOBJMETHOD(feeder_set, feed_volume_set),
302 KOBJMETHOD(feeder_feed, feed_volume_feed),
306 FEEDER_DECLARE(feeder_volume, NULL);
311 * feeder_volume_apply_matrix(): For given matrix map, apply its configuration
312 * to feeder_volume matrix structure. There are
313 * possibilites that feeder_volume be inserted
314 * before or after feeder_matrix, which in this
315 * case feeder_volume must be in a good terms
316 * with _current_ matrix.
319 feeder_volume_apply_matrix(struct pcm_feeder *f, struct pcmchan_matrix *m)
321 struct feed_volume_info *info;
324 if (f == NULL || f->desc == NULL || f->desc->type != FEEDER_VOLUME ||
325 f->data == NULL || m == NULL || m->channels < SND_CHN_MIN ||
326 m->channels > SND_CHN_MAX)
331 for (i = 0; i < (sizeof(info->matrix) / sizeof(info->matrix[0])); i++) {
333 info->matrix[i] = m->map[i].type;
335 info->matrix[i] = SND_CHN_T_FL;
338 info->channels = m->channels;