| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#include <alsa/asoundlib.h> |
| 22 |
#include <alsa/pcm_external.h> |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
#ifdef I_AM_POWERFUL |
| 27 |
#define RINGBUF_SIZE (1 << 9) |
| 28 |
#else |
| 29 |
#define RINGBUF_SIZE (1 << 7) |
| 30 |
#endif |
| 31 |
#define RINGBUF_MASK (RINGBUF_SIZE - 1) |
| 32 |
|
| 33 |
struct vdownmix_tap { |
| 34 |
int delay; |
| 35 |
int weight; |
| 36 |
}; |
| 37 |
|
| 38 |
#define MAX_TAPS 30 |
| 39 |
|
| 40 |
struct vdownmix_filter { |
| 41 |
int taps; |
| 42 |
struct vdownmix_tap tap[MAX_TAPS]; |
| 43 |
}; |
| 44 |
|
| 45 |
typedef struct { |
| 46 |
snd_pcm_extplug_t ext; |
| 47 |
int channels; |
| 48 |
unsigned int curpos; |
| 49 |
short rbuf[RINGBUF_SIZE][5]; |
| 50 |
} snd_pcm_vdownmix_t; |
| 51 |
|
| 52 |
static const struct vdownmix_filter tap_filters[5] = { |
| 53 |
{ |
| 54 |
#ifdef I_AM_POWERFUL |
| 55 |
18, |
| 56 |
#else |
| 57 |
14, |
| 58 |
#endif |
| 59 |
{{ 0, 0xfffffd0a }, |
| 60 |
{ 1, 0x41d }, |
| 61 |
{ 2, 0xffffe657 }, |
| 62 |
{ 3, 0x6eb5 }, |
| 63 |
{ 4, 0xffffe657 }, |
| 64 |
{ 5, 0x41d }, |
| 65 |
{ 6, 0xfffffd0a }, |
| 66 |
{ 71, 0xffffff1c }, |
| 67 |
{ 72, 0x12e }, |
| 68 |
{ 73, 0xfffff81a }, |
| 69 |
{ 74, 0x24de }, |
| 70 |
{ 75, 0xfffff81a }, |
| 71 |
{ 76, 0x12e }, |
| 72 |
{ 77, 0xffffff1c }, |
| 73 |
{ 265, 0xfffffc65 }, |
| 74 |
{ 266, 0xee1 }, |
| 75 |
{ 267, 0xfffffc65 }, |
| 76 |
{ 395, 0x46a }}, |
| 77 |
}, |
| 78 |
|
| 79 |
{ |
| 80 |
#ifdef I_AM_POWERFUL |
| 81 |
17, |
| 82 |
#else |
| 83 |
10, |
| 84 |
#endif |
| 85 |
{{ 8, 0xcf }, |
| 86 |
{ 9, 0xa7b }, |
| 87 |
{ 10, 0xcd7 }, |
| 88 |
{ 11, 0x5b3 }, |
| 89 |
{ 12, 0x859 }, |
| 90 |
{ 13, 0xaf }, |
| 91 |
{ 80, 0x38b }, |
| 92 |
{ 81, 0x454 }, |
| 93 |
{ 82, 0x218 }, |
| 94 |
{ 83, 0x2c1 }, |
| 95 |
{ 268, 0x58b }, |
| 96 |
{ 275, 0xc2 }, |
| 97 |
{ 397, 0xbd }, |
| 98 |
{ 398, 0x1e8 }, |
| 99 |
{ 506, 0xfffffeac }, |
| 100 |
{ 507, 0x636 }, |
| 101 |
{ 508, 0xfffffeac }}, |
| 102 |
}, |
| 103 |
|
| 104 |
{ |
| 105 |
#ifdef I_AM_POWERFUL |
| 106 |
11, |
| 107 |
#else |
| 108 |
1, |
| 109 |
#endif |
| 110 |
{{ 3, 0x4000 }, |
| 111 |
{ 125, 0x12a }, |
| 112 |
{ 126, 0xda1 }, |
| 113 |
{ 127, 0x12a }, |
| 114 |
{ 193, 0xfffffed3 }, |
| 115 |
{ 194, 0xdb9 }, |
| 116 |
{ 195, 0xfffffed3 }, |
| 117 |
{ 454, 0x10a }, |
| 118 |
{ 483, 0xfffffe97 }, |
| 119 |
{ 484, 0x698 }, |
| 120 |
{ 485, 0xfffffe97 }}, |
| 121 |
}, |
| 122 |
|
| 123 |
{ |
| 124 |
#ifdef I_AM_POWERFUL |
| 125 |
25, |
| 126 |
#else |
| 127 |
10, |
| 128 |
#endif |
| 129 |
{{ 5, 0x1cb }, |
| 130 |
{ 6, 0x9c5 }, |
| 131 |
{ 7, 0x117e }, |
| 132 |
{ 8, 0x200 }, |
| 133 |
{ 9, 0x533 }, |
| 134 |
{ 10, 0x1c6 }, |
| 135 |
{ 11, 0x167 }, |
| 136 |
{ 12, 0x5ff }, |
| 137 |
{ 13, 0x425 }, |
| 138 |
{ 14, 0xd9 }, |
| 139 |
{ 128, 0x247 }, |
| 140 |
{ 129, 0x5e1 }, |
| 141 |
{ 130, 0xb7 }, |
| 142 |
{ 131, 0x122 }, |
| 143 |
{ 135, 0x10a }, |
| 144 |
{ 200, 0x1b6 }, |
| 145 |
{ 201, 0xa7 }, |
| 146 |
{ 202, 0x188 }, |
| 147 |
{ 203, 0x1d9 }, |
| 148 |
{ 445, 0xffffff44 }, |
| 149 |
{ 446, 0x5e2 }, |
| 150 |
{ 447, 0xffffff44 }, |
| 151 |
{ 484, 0xffffff51 }, |
| 152 |
{ 485, 0x449 }, |
| 153 |
{ 486, 0xffffff51 }}, |
| 154 |
}, |
| 155 |
|
| 156 |
{ |
| 157 |
#ifdef I_AM_POWERFUL |
| 158 |
21, |
| 159 |
#else |
| 160 |
7, |
| 161 |
#endif |
| 162 |
{{ 0, 0xfffffdee }, |
| 163 |
{ 1, 0x28b }, |
| 164 |
{ 2, 0xffffed1e }, |
| 165 |
{ 3, 0x6336 }, |
| 166 |
{ 4, 0xffffed1e }, |
| 167 |
{ 5, 0x28b }, |
| 168 |
{ 6, 0xfffffdee }, |
| 169 |
{ 51, 0xffffff2c }, |
| 170 |
{ 52, 0x105 }, |
| 171 |
{ 53, 0xfffff86b }, |
| 172 |
{ 54, 0x27d9 }, |
| 173 |
{ 55, 0xfffff86b }, |
| 174 |
{ 56, 0x105 }, |
| 175 |
{ 57, 0xffffff2c }, |
| 176 |
{ 333, 0xfffffd69 }, |
| 177 |
{ 334, 0xb2f }, |
| 178 |
{ 335, 0xfffffd69 }, |
| 179 |
{ 339, 0xdf }, |
| 180 |
{ 340, 0x168 }, |
| 181 |
{ 342, 0xa6 }, |
| 182 |
{ 343, 0xba }}, |
| 183 |
}, |
| 184 |
}; |
| 185 |
|
| 186 |
static const int tap_index[5][2] = { |
| 187 |
|
| 188 |
{ 0, 1 }, |
| 189 |
|
| 190 |
{ 1, 0 }, |
| 191 |
|
| 192 |
{ 2, 3 }, |
| 193 |
|
| 194 |
{ 3, 2 }, |
| 195 |
|
| 196 |
{ 4, 4 }, |
| 197 |
}; |
| 198 |
|
| 199 |
static inline void *area_addr(const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset) |
| 200 |
{ |
| 201 |
unsigned int bitofs = area->first + area->step * offset; |
| 202 |
return (char *) area->addr + bitofs / 8; |
| 203 |
} |
| 204 |
|
| 205 |
static inline unsigned int area_step(const snd_pcm_channel_area_t *area) |
| 206 |
{ |
| 207 |
return area->step / 8; |
| 208 |
} |
| 209 |
|
| 210 |
static snd_pcm_sframes_t |
| 211 |
vdownmix_transfer(snd_pcm_extplug_t *ext, |
| 212 |
const snd_pcm_channel_area_t *dst_areas, |
| 213 |
snd_pcm_uframes_t dst_offset, |
| 214 |
const snd_pcm_channel_area_t *src_areas, |
| 215 |
snd_pcm_uframes_t src_offset, |
| 216 |
snd_pcm_uframes_t size) |
| 217 |
{ |
| 218 |
snd_pcm_vdownmix_t *mix = (snd_pcm_vdownmix_t *)ext; |
| 219 |
short *src[mix->channels], *ptr[2]; |
| 220 |
unsigned int src_step[mix->channels], step[2]; |
| 221 |
int i, ch, curpos, p, idx; |
| 222 |
int acc[2]; |
| 223 |
int fr; |
| 224 |
|
| 225 |
ptr[0] = area_addr(dst_areas, dst_offset); |
| 226 |
step[0] = area_step(dst_areas) / 2; |
| 227 |
ptr[1] = area_addr(dst_areas + 1, dst_offset); |
| 228 |
step[1] = area_step(dst_areas + 1) / 2; |
| 229 |
for (ch = 0; ch < mix->channels; ch++) { |
| 230 |
const snd_pcm_channel_area_t *src_area = &src_areas[ch]; |
| 231 |
src[ch] = area_addr(src_area, src_offset); |
| 232 |
src_step[ch] = area_step(src_area) / 2; |
| 233 |
} |
| 234 |
curpos = mix->curpos; |
| 235 |
fr = size; |
| 236 |
while (fr--) { |
| 237 |
acc[0] = acc[1] = 0; |
| 238 |
for (ch = 0; ch < mix->channels; ch++) { |
| 239 |
mix->rbuf[curpos][ch] = *src[ch]; |
| 240 |
for (idx = 0; idx < 2; idx++) { |
| 241 |
int f = tap_index[ch][idx]; |
| 242 |
const struct vdownmix_filter *filter; |
| 243 |
filter = &tap_filters[f]; |
| 244 |
for (i = 0; i < filter->taps; i++) { |
| 245 |
p = (curpos + RINGBUF_SIZE - filter->tap[i].delay) |
| 246 |
& RINGBUF_MASK; |
| 247 |
acc[idx] += mix->rbuf[p][ch] * filter->tap[i].weight; |
| 248 |
} |
| 249 |
} |
| 250 |
src[ch] += src_step[ch]; |
| 251 |
} |
| 252 |
for (idx = 0; idx < 2; idx++) { |
| 253 |
acc[idx] >>= 14; |
| 254 |
if (acc[idx] < -32768) |
| 255 |
*ptr[idx] = -32768; |
| 256 |
else if (acc[idx] > 32767) |
| 257 |
*ptr[idx] = 32767; |
| 258 |
else |
| 259 |
*ptr[idx] = acc[idx]; |
| 260 |
ptr[idx] += step[idx]; |
| 261 |
} |
| 262 |
curpos = (curpos + 1) & RINGBUF_MASK; |
| 263 |
} |
| 264 |
mix->curpos = curpos; |
| 265 |
return size; |
| 266 |
} |
| 267 |
|
| 268 |
|
| 269 |
static int vdownmix_init(snd_pcm_extplug_t *ext) |
| 270 |
{ |
| 271 |
snd_pcm_vdownmix_t *mix = (snd_pcm_vdownmix_t *)ext; |
| 272 |
mix->channels = ext->channels; |
| 273 |
if (mix->channels > 5) |
| 274 |
mix->channels = 5; |
| 275 |
mix->curpos = 0; |
| 276 |
memset(mix->rbuf, 0, sizeof(mix->rbuf)); |
| 277 |
return 0; |
| 278 |
} |
| 279 |
|
| 280 |
#if SND_PCM_EXTPLUG_VERSION >= 0x10002 |
| 281 |
static unsigned int chmap[6] = { |
| 282 |
SND_CHMAP_FL, SND_CHMAP_FR, |
| 283 |
SND_CHMAP_RL, SND_CHMAP_RR, |
| 284 |
SND_CHMAP_FC, SND_CHMAP_LFE, |
| 285 |
}; |
| 286 |
|
| 287 |
static snd_pcm_chmap_query_t **vdownmix_query_chmaps(snd_pcm_extplug_t *ext ATTRIBUTE_UNUSED) |
| 288 |
{ |
| 289 |
snd_pcm_chmap_query_t **maps; |
| 290 |
int i; |
| 291 |
|
| 292 |
maps = calloc(4, sizeof(void *)); |
| 293 |
if (!maps) |
| 294 |
return NULL; |
| 295 |
for (i = 0; i < 3; i++) { |
| 296 |
snd_pcm_chmap_query_t *p; |
| 297 |
p = maps[i] = calloc(i + 4 + 2, sizeof(int)); |
| 298 |
if (!p) { |
| 299 |
snd_pcm_free_chmaps(maps); |
| 300 |
return NULL; |
| 301 |
} |
| 302 |
p->type = SND_CHMAP_TYPE_FIXED; |
| 303 |
p->map.channels = i + 4; |
| 304 |
memcpy(p->map.pos, chmap, (i + 4) * sizeof(int)); |
| 305 |
} |
| 306 |
return maps; |
| 307 |
} |
| 308 |
|
| 309 |
static snd_pcm_chmap_t *vdownmix_get_chmap(snd_pcm_extplug_t *ext) |
| 310 |
{ |
| 311 |
snd_pcm_chmap_t *map; |
| 312 |
|
| 313 |
if (ext->channels < 4 || ext->channels > 6) |
| 314 |
return NULL; |
| 315 |
map = malloc((ext->channels + 1) * sizeof(int)); |
| 316 |
if (!map) |
| 317 |
return NULL; |
| 318 |
map->channels = ext->channels; |
| 319 |
memcpy(map->pos, chmap, ext->channels * sizeof(int)); |
| 320 |
return map; |
| 321 |
} |
| 322 |
#endif |
| 323 |
|
| 324 |
static const snd_pcm_extplug_callback_t vdownmix_callback = { |
| 325 |
.transfer = vdownmix_transfer, |
| 326 |
.init = vdownmix_init, |
| 327 |
|
| 328 |
#if SND_PCM_EXTPLUG_VERSION >= 0x10002 |
| 329 |
.query_chmaps = vdownmix_query_chmaps, |
| 330 |
.get_chmap = vdownmix_get_chmap, |
| 331 |
#endif |
| 332 |
}; |
| 333 |
|
| 334 |
SND_PCM_PLUGIN_DEFINE_FUNC(vdownmix) |
| 335 |
{ |
| 336 |
snd_config_iterator_t i, next; |
| 337 |
snd_pcm_vdownmix_t *mix; |
| 338 |
snd_config_t *sconf = NULL; |
| 339 |
int err; |
| 340 |
|
| 341 |
snd_config_for_each(i, next, conf) { |
| 342 |
snd_config_t *n = snd_config_iterator_entry(i); |
| 343 |
const char *id; |
| 344 |
if (snd_config_get_id(n, &id) < 0) |
| 345 |
continue; |
| 346 |
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0) |
| 347 |
continue; |
| 348 |
if (strcmp(id, "slave") == 0) { |
| 349 |
sconf = n; |
| 350 |
continue; |
| 351 |
} |
| 352 |
SNDERR("Unknown field %s", id); |
| 353 |
return -EINVAL; |
| 354 |
} |
| 355 |
|
| 356 |
if (! sconf) { |
| 357 |
SNDERR("No slave configuration for vdownmix pcm"); |
| 358 |
return -EINVAL; |
| 359 |
} |
| 360 |
|
| 361 |
mix = calloc(1, sizeof(*mix)); |
| 362 |
if (mix == NULL) |
| 363 |
return -ENOMEM; |
| 364 |
|
| 365 |
mix->ext.version = SND_PCM_EXTPLUG_VERSION; |
| 366 |
mix->ext.name = "Vdownmix Plugin"; |
| 367 |
mix->ext.callback = &vdownmix_callback; |
| 368 |
mix->ext.private_data = mix; |
| 369 |
|
| 370 |
err = snd_pcm_extplug_create(&mix->ext, name, root, sconf, stream, mode); |
| 371 |
if (err < 0) { |
| 372 |
free(mix); |
| 373 |
return err; |
| 374 |
} |
| 375 |
|
| 376 |
|
| 377 |
snd_pcm_extplug_set_param_minmax(&mix->ext, SND_PCM_EXTPLUG_HW_CHANNELS, |
| 378 |
4, 6); |
| 379 |
snd_pcm_extplug_set_slave_param(&mix->ext, SND_PCM_EXTPLUG_HW_CHANNELS, 2); |
| 380 |
snd_pcm_extplug_set_param(&mix->ext, SND_PCM_EXTPLUG_HW_FORMAT, |
| 381 |
SND_PCM_FORMAT_S16); |
| 382 |
snd_pcm_extplug_set_slave_param(&mix->ext, SND_PCM_EXTPLUG_HW_FORMAT, |
| 383 |
SND_PCM_FORMAT_S16); |
| 384 |
|
| 385 |
*pcmp = mix->ext.pcm; |
| 386 |
return 0; |
| 387 |
} |
| 388 |
|
| 389 |
SND_PCM_PLUGIN_SYMBOL(vdownmix); |