1 /*- 2 * Copyright (c) 2008-2009 Ariff Abdullah <ariff@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 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 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * feeder_eq: Parametric (compile time) Software Equalizer. Though accidental, 29 * it proves good enough for educational and general consumption. 30 * 31 * "Cookbook formulae for audio EQ biquad filter coefficients" 32 * by Robert Bristow-Johnson <rbj@audioimagination.com> 33 * - http://www.musicdsp.org/files/Audio-EQ-Cookbook.txt 34 */ 35 36 #ifdef _KERNEL 37 #ifdef HAVE_KERNEL_OPTION_HEADERS 38 #include "opt_snd.h" 39 #endif 40 #include <dev/sound/pcm/sound.h> 41 #include <dev/sound/pcm/pcm.h> 42 #include "feeder_if.h" 43 44 #define SND_USE_FXDIV 45 #include "snd_fxdiv_gen.h" 46 47 SND_DECLARE_FILE("$FreeBSD$"); 48 #endif 49 50 #include "feeder_eq_gen.h" 51 52 #define FEEDEQ_LEVELS \ 53 (((FEEDEQ_GAIN_MAX - FEEDEQ_GAIN_MIN) * \ 54 (FEEDEQ_GAIN_DIV / FEEDEQ_GAIN_STEP)) + 1) 55 56 #define FEEDEQ_L2GAIN(v) \ 57 ((int)min(((v) * FEEDEQ_LEVELS) / 100, FEEDEQ_LEVELS - 1)) 58 59 #define FEEDEQ_PREAMP_IPART(x) (abs(x) >> FEEDEQ_GAIN_SHIFT) 60 #define FEEDEQ_PREAMP_FPART(x) (abs(x) & FEEDEQ_GAIN_FMASK) 61 #define FEEDEQ_PREAMP_SIGNVAL(x) ((x) < 0 ? -1 : 1) 62 #define FEEDEQ_PREAMP_SIGNMARK(x) (((x) < 0) ? '-' : '+') 63 64 #define FEEDEQ_PREAMP_IMIN -192 65 #define FEEDEQ_PREAMP_IMAX 192 66 #define FEEDEQ_PREAMP_FMIN 0 67 #define FEEDEQ_PREAMP_FMAX 9 68 69 #define FEEDEQ_PREAMP_INVALID INT_MAX 70 71 #define FEEDEQ_IF2PREAMP(i, f) \ 72 ((abs(i) << FEEDEQ_GAIN_SHIFT) | \ 73 (((abs(f) / FEEDEQ_GAIN_STEP) * FEEDEQ_GAIN_STEP) & \ 74 FEEDEQ_GAIN_FMASK)) 75 76 #define FEEDEQ_PREAMP_MIN \ 77 (FEEDEQ_PREAMP_SIGNVAL(FEEDEQ_GAIN_MIN) * \ 78 FEEDEQ_IF2PREAMP(FEEDEQ_GAIN_MIN, 0)) 79 80 #define FEEDEQ_PREAMP_MAX \ 81 (FEEDEQ_PREAMP_SIGNVAL(FEEDEQ_GAIN_MAX) * \ 82 FEEDEQ_IF2PREAMP(FEEDEQ_GAIN_MAX, 0)) 83 84 #define FEEDEQ_PREAMP_DEFAULT FEEDEQ_IF2PREAMP(0, 0) 85 86 #define FEEDEQ_PREAMP2IDX(v) \ 87 ((int32_t)((FEEDEQ_GAIN_MAX * (FEEDEQ_GAIN_DIV / \ 88 FEEDEQ_GAIN_STEP)) + (FEEDEQ_PREAMP_SIGNVAL(v) * \ 89 FEEDEQ_PREAMP_IPART(v) * (FEEDEQ_GAIN_DIV / \ 90 FEEDEQ_GAIN_STEP)) + (FEEDEQ_PREAMP_SIGNVAL(v) * \ 91 (FEEDEQ_PREAMP_FPART(v) / FEEDEQ_GAIN_STEP)))) 92 93 static int feeder_eq_exact_rate = 0; 94 95 #ifdef _KERNEL 96 static char feeder_eq_presets[] = FEEDER_EQ_PRESETS; 97 SYSCTL_STRING(_hw_snd, OID_AUTO, feeder_eq_presets, CTLFLAG_RD, 98 &feeder_eq_presets, 0, "compile-time eq presets"); 99 100 SYSCTL_INT(_hw_snd, OID_AUTO, feeder_eq_exact_rate, CTLFLAG_RWTUN, 101 &feeder_eq_exact_rate, 0, "force exact rate validation"); 102 #endif 103 104 struct feed_eq_info; 105 106 typedef void (*feed_eq_t)(struct feed_eq_info *, uint8_t *, uint32_t); 107 108 struct feed_eq_tone { 109 intpcm_t o1[SND_CHN_MAX]; 110 intpcm_t o2[SND_CHN_MAX]; 111 intpcm_t i1[SND_CHN_MAX]; 112 intpcm_t i2[SND_CHN_MAX]; 113 int gain; 114 }; 115 116 struct feed_eq_info { 117 struct feed_eq_tone treble; 118 struct feed_eq_tone bass; 119 struct feed_eq_coeff *coeff; 120 feed_eq_t biquad; 121 uint32_t channels; 122 uint32_t rate; 123 uint32_t align; 124 int32_t preamp; 125 int state; 126 }; 127 128 #if !defined(_KERNEL) && defined(FEEDEQ_ERR_CLIP) 129 #define FEEDEQ_ERR_CLIP_CHECK(t, v) do { \ 130 if ((v) < PCM_S32_MIN || (v) > PCM_S32_MAX) \ 131 errx(1, "\n\n%s(): ["#t"] Sample clipping: %jd\n", \ 132 __func__, (intmax_t)(v)); \ 133 } while (0) 134 #else 135 #define FEEDEQ_ERR_CLIP_CHECK(...) 136 #endif 137 138 #define FEEDEQ_CLAMP(v) (((v) > PCM_S32_MAX) ? PCM_S32_MAX : \ 139 (((v) < PCM_S32_MIN) ? PCM_S32_MIN : \ 140 (v))) 141 142 #define FEEDEQ_DECLARE(SIGN, BIT, ENDIAN) \ 143 static void \ 144 feed_eq_biquad_##SIGN##BIT##ENDIAN(struct feed_eq_info *info, \ 145 uint8_t *dst, uint32_t count) \ 146 { \ 147 struct feed_eq_coeff_tone *treble, *bass; \ 148 intpcm64_t w; \ 149 intpcm_t v; \ 150 uint32_t i, j; \ 151 int32_t pmul, pshift; \ 152 \ 153 pmul = feed_eq_preamp[info->preamp].mul; \ 154 pshift = feed_eq_preamp[info->preamp].shift; \ 155 \ 156 if (info->state == FEEDEQ_DISABLE) { \ 157 j = count * info->channels; \ 158 dst += j * PCM_##BIT##_BPS; \ 159 do { \ 160 dst -= PCM_##BIT##_BPS; \ 161 v = _PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \ 162 v = ((intpcm64_t)pmul * v) >> pshift; \ 163 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, v); \ 164 } while (--j != 0); \ 165 \ 166 return; \ 167 } \ 168 \ 169 treble = &(info->coeff[info->treble.gain].treble); \ 170 bass = &(info->coeff[info->bass.gain].bass); \ 171 \ 172 do { \ 173 i = 0; \ 174 j = info->channels; \ 175 do { \ 176 v = _PCM_READ_##SIGN##BIT##_##ENDIAN(dst); \ 177 v <<= 32 - BIT; \ 178 v = ((intpcm64_t)pmul * v) >> pshift; \ 179 \ 180 w = (intpcm64_t)v * treble->b0; \ 181 w += (intpcm64_t)info->treble.i1[i] * treble->b1; \ 182 w += (intpcm64_t)info->treble.i2[i] * treble->b2; \ 183 w -= (intpcm64_t)info->treble.o1[i] * treble->a1; \ 184 w -= (intpcm64_t)info->treble.o2[i] * treble->a2; \ 185 info->treble.i2[i] = info->treble.i1[i]; \ 186 info->treble.i1[i] = v; \ 187 info->treble.o2[i] = info->treble.o1[i]; \ 188 w >>= FEEDEQ_COEFF_SHIFT; \ 189 FEEDEQ_ERR_CLIP_CHECK(treble, w); \ 190 v = FEEDEQ_CLAMP(w); \ 191 info->treble.o1[i] = v; \ 192 \ 193 w = (intpcm64_t)v * bass->b0; \ 194 w += (intpcm64_t)info->bass.i1[i] * bass->b1; \ 195 w += (intpcm64_t)info->bass.i2[i] * bass->b2; \ 196 w -= (intpcm64_t)info->bass.o1[i] * bass->a1; \ 197 w -= (intpcm64_t)info->bass.o2[i] * bass->a2; \ 198 info->bass.i2[i] = info->bass.i1[i]; \ 199 info->bass.i1[i] = v; \ 200 info->bass.o2[i] = info->bass.o1[i]; \ 201 w >>= FEEDEQ_COEFF_SHIFT; \ 202 FEEDEQ_ERR_CLIP_CHECK(bass, w); \ 203 v = FEEDEQ_CLAMP(w); \ 204 info->bass.o1[i] = v; \ 205 \ 206 v >>= 32 - BIT; \ 207 _PCM_WRITE_##SIGN##BIT##_##ENDIAN(dst, v); \ 208 dst += PCM_##BIT##_BPS; \ 209 i++; \ 210 } while (--j != 0); \ 211 } while (--count != 0); \ 212 } 213 214 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 215 FEEDEQ_DECLARE(S, 16, LE) 216 FEEDEQ_DECLARE(S, 32, LE) 217 #endif 218 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 219 FEEDEQ_DECLARE(S, 16, BE) 220 FEEDEQ_DECLARE(S, 32, BE) 221 #endif 222 #ifdef SND_FEEDER_MULTIFORMAT 223 FEEDEQ_DECLARE(S, 8, NE) 224 FEEDEQ_DECLARE(S, 24, LE) 225 FEEDEQ_DECLARE(S, 24, BE) 226 FEEDEQ_DECLARE(U, 8, NE) 227 FEEDEQ_DECLARE(U, 16, LE) 228 FEEDEQ_DECLARE(U, 24, LE) 229 FEEDEQ_DECLARE(U, 32, LE) 230 FEEDEQ_DECLARE(U, 16, BE) 231 FEEDEQ_DECLARE(U, 24, BE) 232 FEEDEQ_DECLARE(U, 32, BE) 233 #endif 234 235 #define FEEDEQ_ENTRY(SIGN, BIT, ENDIAN) \ 236 { \ 237 AFMT_##SIGN##BIT##_##ENDIAN, \ 238 feed_eq_biquad_##SIGN##BIT##ENDIAN \ 239 } 240 241 242 static const struct { 243 uint32_t format; 244 feed_eq_t biquad; 245 } feed_eq_biquad_tab[] = { 246 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 247 FEEDEQ_ENTRY(S, 16, LE), 248 FEEDEQ_ENTRY(S, 32, LE), 249 #endif 250 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 251 FEEDEQ_ENTRY(S, 16, BE), 252 FEEDEQ_ENTRY(S, 32, BE), 253 #endif 254 #ifdef SND_FEEDER_MULTIFORMAT 255 FEEDEQ_ENTRY(S, 8, NE), 256 FEEDEQ_ENTRY(S, 24, LE), 257 FEEDEQ_ENTRY(S, 24, BE), 258 FEEDEQ_ENTRY(U, 8, NE), 259 FEEDEQ_ENTRY(U, 16, LE), 260 FEEDEQ_ENTRY(U, 24, LE), 261 FEEDEQ_ENTRY(U, 32, LE), 262 FEEDEQ_ENTRY(U, 16, BE), 263 FEEDEQ_ENTRY(U, 24, BE), 264 FEEDEQ_ENTRY(U, 32, BE) 265 #endif 266 }; 267 268 #define FEEDEQ_BIQUAD_TAB_SIZE \ 269 ((int32_t)(sizeof(feed_eq_biquad_tab) / sizeof(feed_eq_biquad_tab[0]))) 270 271 static struct feed_eq_coeff * 272 feed_eq_coeff_rate(uint32_t rate) 273 { 274 uint32_t spd, threshold; 275 int i; 276 277 if (rate < FEEDEQ_RATE_MIN || rate > FEEDEQ_RATE_MAX) 278 return (NULL); 279 280 /* 281 * Not all rates are supported. Choose the best rate that we can to 282 * allow 'sloppy' conversion. Good enough for naive listeners. 283 */ 284 for (i = 0; i < FEEDEQ_TAB_SIZE; i++) { 285 spd = feed_eq_tab[i].rate; 286 threshold = spd + ((i < (FEEDEQ_TAB_SIZE - 1) && 287 feed_eq_tab[i + 1].rate > spd) ? 288 ((feed_eq_tab[i + 1].rate - spd) >> 1) : 0); 289 if (rate == spd || 290 (feeder_eq_exact_rate == 0 && rate <= threshold)) 291 return (feed_eq_tab[i].coeff); 292 } 293 294 return (NULL); 295 } 296 297 int 298 feeder_eq_validrate(uint32_t rate) 299 { 300 301 if (feed_eq_coeff_rate(rate) != NULL) 302 return (1); 303 304 return (0); 305 } 306 307 static void 308 feed_eq_reset(struct feed_eq_info *info) 309 { 310 uint32_t i; 311 312 for (i = 0; i < info->channels; i++) { 313 info->treble.i1[i] = 0; 314 info->treble.i2[i] = 0; 315 info->treble.o1[i] = 0; 316 info->treble.o2[i] = 0; 317 info->bass.i1[i] = 0; 318 info->bass.i2[i] = 0; 319 info->bass.o1[i] = 0; 320 info->bass.o2[i] = 0; 321 } 322 } 323 324 static int 325 feed_eq_setup(struct feed_eq_info *info) 326 { 327 328 info->coeff = feed_eq_coeff_rate(info->rate); 329 if (info->coeff == NULL) 330 return (EINVAL); 331 332 feed_eq_reset(info); 333 334 return (0); 335 } 336 337 static int 338 feed_eq_init(struct pcm_feeder *f) 339 { 340 struct feed_eq_info *info; 341 feed_eq_t biquad_op; 342 int i; 343 344 if (f->desc->in != f->desc->out) 345 return (EINVAL); 346 347 biquad_op = NULL; 348 349 for (i = 0; i < FEEDEQ_BIQUAD_TAB_SIZE && biquad_op == NULL; i++) { 350 if (AFMT_ENCODING(f->desc->in) == feed_eq_biquad_tab[i].format) 351 biquad_op = feed_eq_biquad_tab[i].biquad; 352 } 353 354 if (biquad_op == NULL) 355 return (EINVAL); 356 357 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); 358 if (info == NULL) 359 return (ENOMEM); 360 361 info->channels = AFMT_CHANNEL(f->desc->in); 362 info->align = info->channels * AFMT_BPS(f->desc->in); 363 364 info->rate = FEEDEQ_RATE_MIN; 365 info->treble.gain = FEEDEQ_L2GAIN(50); 366 info->bass.gain = FEEDEQ_L2GAIN(50); 367 info->preamp = FEEDEQ_PREAMP2IDX(FEEDEQ_PREAMP_DEFAULT); 368 info->state = FEEDEQ_UNKNOWN; 369 370 info->biquad = biquad_op; 371 372 f->data = info; 373 374 return (feed_eq_setup(info)); 375 } 376 377 static int 378 feed_eq_set(struct pcm_feeder *f, int what, int value) 379 { 380 struct feed_eq_info *info; 381 382 info = f->data; 383 384 switch (what) { 385 case FEEDEQ_CHANNELS: 386 if (value < SND_CHN_MIN || value > SND_CHN_MAX) 387 return (EINVAL); 388 info->channels = (uint32_t)value; 389 info->align = info->channels * AFMT_BPS(f->desc->in); 390 feed_eq_reset(info); 391 break; 392 case FEEDEQ_RATE: 393 if (feeder_eq_validrate(value) == 0) 394 return (EINVAL); 395 info->rate = (uint32_t)value; 396 if (info->state == FEEDEQ_UNKNOWN) 397 info->state = FEEDEQ_ENABLE; 398 return (feed_eq_setup(info)); 399 break; 400 case FEEDEQ_TREBLE: 401 case FEEDEQ_BASS: 402 if (value < 0 || value > 100) 403 return (EINVAL); 404 if (what == FEEDEQ_TREBLE) 405 info->treble.gain = FEEDEQ_L2GAIN(value); 406 else 407 info->bass.gain = FEEDEQ_L2GAIN(value); 408 break; 409 case FEEDEQ_PREAMP: 410 if (value < FEEDEQ_PREAMP_MIN || value > FEEDEQ_PREAMP_MAX) 411 return (EINVAL); 412 info->preamp = FEEDEQ_PREAMP2IDX(value); 413 break; 414 case FEEDEQ_STATE: 415 if (!(value == FEEDEQ_BYPASS || value == FEEDEQ_ENABLE || 416 value == FEEDEQ_DISABLE)) 417 return (EINVAL); 418 info->state = value; 419 feed_eq_reset(info); 420 break; 421 default: 422 return (EINVAL); 423 break; 424 } 425 426 return (0); 427 } 428 429 static int 430 feed_eq_free(struct pcm_feeder *f) 431 { 432 struct feed_eq_info *info; 433 434 info = f->data; 435 if (info != NULL) 436 free(info, M_DEVBUF); 437 438 f->data = NULL; 439 440 return (0); 441 } 442 443 static int 444 feed_eq_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, 445 uint32_t count, void *source) 446 { 447 struct feed_eq_info *info; 448 uint32_t j; 449 uint8_t *dst; 450 451 info = f->data; 452 453 /* 454 * 3 major states: 455 * FEEDEQ_BYPASS - Bypass entirely, nothing happened. 456 * FEEDEQ_ENABLE - Preamp+biquad filtering. 457 * FEEDEQ_DISABLE - Preamp only. 458 */ 459 if (info->state == FEEDEQ_BYPASS) 460 return (FEEDER_FEED(f->source, c, b, count, source)); 461 462 dst = b; 463 count = SND_FXROUND(count, info->align); 464 465 do { 466 if (count < info->align) 467 break; 468 469 j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source), 470 info->align); 471 if (j == 0) 472 break; 473 474 info->biquad(info, dst, j); 475 476 j *= info->align; 477 dst += j; 478 count -= j; 479 480 } while (count != 0); 481 482 return (dst - b); 483 } 484 485 static struct pcm_feederdesc feeder_eq_desc[] = { 486 { FEEDER_EQ, 0, 0, 0, 0 }, 487 { 0, 0, 0, 0, 0 } 488 }; 489 490 static kobj_method_t feeder_eq_methods[] = { 491 KOBJMETHOD(feeder_init, feed_eq_init), 492 KOBJMETHOD(feeder_free, feed_eq_free), 493 KOBJMETHOD(feeder_set, feed_eq_set), 494 KOBJMETHOD(feeder_feed, feed_eq_feed), 495 KOBJMETHOD_END 496 }; 497 498 FEEDER_DECLARE(feeder_eq, NULL); 499 500 static int32_t 501 feed_eq_scan_preamp_arg(const char *s) 502 { 503 int r, i, f; 504 size_t len; 505 char buf[32]; 506 507 bzero(buf, sizeof(buf)); 508 509 /* XXX kind of ugly, but works for now.. */ 510 511 r = sscanf(s, "%d.%d", &i, &f); 512 513 if (r == 1 && !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX)) { 514 snprintf(buf, sizeof(buf), "%c%d", 515 FEEDEQ_PREAMP_SIGNMARK(i), abs(i)); 516 f = 0; 517 } else if (r == 2 && 518 !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX || 519 f < FEEDEQ_PREAMP_FMIN || f > FEEDEQ_PREAMP_FMAX)) 520 snprintf(buf, sizeof(buf), "%c%d.%d", 521 FEEDEQ_PREAMP_SIGNMARK(i), abs(i), f); 522 else 523 return (FEEDEQ_PREAMP_INVALID); 524 525 len = strlen(s); 526 if (len > 2 && strcasecmp(s + len - 2, "dB") == 0) 527 strlcat(buf, "dB", sizeof(buf)); 528 529 if (i == 0 && *s == '-') 530 *buf = '-'; 531 532 if (strcasecmp(buf + ((*s >= '0' && *s <= '9') ? 1 : 0), s) != 0) 533 return (FEEDEQ_PREAMP_INVALID); 534 535 while ((f / FEEDEQ_GAIN_DIV) > 0) 536 f /= FEEDEQ_GAIN_DIV; 537 538 return (((i < 0 || *buf == '-') ? -1 : 1) * FEEDEQ_IF2PREAMP(i, f)); 539 } 540 541 #ifdef _KERNEL 542 static int 543 sysctl_dev_pcm_eq(SYSCTL_HANDLER_ARGS) 544 { 545 struct snddev_info *d; 546 struct pcm_channel *c; 547 struct pcm_feeder *f; 548 int err, val, oval; 549 550 d = oidp->oid_arg1; 551 if (!PCM_REGISTERED(d)) 552 return (ENODEV); 553 554 PCM_LOCK(d); 555 PCM_WAIT(d); 556 if (d->flags & SD_F_EQ_BYPASSED) 557 val = 2; 558 else if (d->flags & SD_F_EQ_ENABLED) 559 val = 1; 560 else 561 val = 0; 562 PCM_ACQUIRE(d); 563 PCM_UNLOCK(d); 564 565 oval = val; 566 err = sysctl_handle_int(oidp, &val, 0, req); 567 568 if (err == 0 && req->newptr != NULL && val != oval) { 569 if (!(val == 0 || val == 1 || val == 2)) { 570 PCM_RELEASE_QUICK(d); 571 return (EINVAL); 572 } 573 574 PCM_LOCK(d); 575 576 d->flags &= ~(SD_F_EQ_ENABLED | SD_F_EQ_BYPASSED); 577 if (val == 2) { 578 val = FEEDEQ_BYPASS; 579 d->flags |= SD_F_EQ_BYPASSED; 580 } else if (val == 1) { 581 val = FEEDEQ_ENABLE; 582 d->flags |= SD_F_EQ_ENABLED; 583 } else 584 val = FEEDEQ_DISABLE; 585 586 CHN_FOREACH(c, d, channels.pcm.busy) { 587 CHN_LOCK(c); 588 f = chn_findfeeder(c, FEEDER_EQ); 589 if (f != NULL) 590 (void)FEEDER_SET(f, FEEDEQ_STATE, val); 591 CHN_UNLOCK(c); 592 } 593 594 PCM_RELEASE(d); 595 PCM_UNLOCK(d); 596 } else 597 PCM_RELEASE_QUICK(d); 598 599 return (err); 600 } 601 602 static int 603 sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS) 604 { 605 struct snddev_info *d; 606 struct pcm_channel *c; 607 struct pcm_feeder *f; 608 int err, val, oval; 609 char buf[32]; 610 611 d = oidp->oid_arg1; 612 if (!PCM_REGISTERED(d)) 613 return (ENODEV); 614 615 PCM_LOCK(d); 616 PCM_WAIT(d); 617 val = d->eqpreamp; 618 bzero(buf, sizeof(buf)); 619 (void)snprintf(buf, sizeof(buf), "%c%d.%ddB", 620 FEEDEQ_PREAMP_SIGNMARK(val), FEEDEQ_PREAMP_IPART(val), 621 FEEDEQ_PREAMP_FPART(val)); 622 PCM_ACQUIRE(d); 623 PCM_UNLOCK(d); 624 625 oval = val; 626 err = sysctl_handle_string(oidp, buf, sizeof(buf), req); 627 628 if (err == 0 && req->newptr != NULL) { 629 val = feed_eq_scan_preamp_arg(buf); 630 if (val == FEEDEQ_PREAMP_INVALID) { 631 PCM_RELEASE_QUICK(d); 632 return (EINVAL); 633 } 634 635 PCM_LOCK(d); 636 637 if (val != oval) { 638 if (val < FEEDEQ_PREAMP_MIN) 639 val = FEEDEQ_PREAMP_MIN; 640 else if (val > FEEDEQ_PREAMP_MAX) 641 val = FEEDEQ_PREAMP_MAX; 642 643 d->eqpreamp = val; 644 645 CHN_FOREACH(c, d, channels.pcm.busy) { 646 CHN_LOCK(c); 647 f = chn_findfeeder(c, FEEDER_EQ); 648 if (f != NULL) 649 (void)FEEDER_SET(f, FEEDEQ_PREAMP, val); 650 CHN_UNLOCK(c); 651 } 652 653 } 654 655 PCM_RELEASE(d); 656 PCM_UNLOCK(d); 657 } else 658 PCM_RELEASE_QUICK(d); 659 660 return (err); 661 } 662 663 void 664 feeder_eq_initsys(device_t dev) 665 { 666 struct snddev_info *d; 667 const char *preamp; 668 char buf[64]; 669 670 d = device_get_softc(dev); 671 672 if (!(resource_string_value(device_get_name(dev), device_get_unit(dev), 673 "eq_preamp", &preamp) == 0 && 674 (d->eqpreamp = feed_eq_scan_preamp_arg(preamp)) != 675 FEEDEQ_PREAMP_INVALID)) 676 d->eqpreamp = FEEDEQ_PREAMP_DEFAULT; 677 678 if (d->eqpreamp < FEEDEQ_PREAMP_MIN) 679 d->eqpreamp = FEEDEQ_PREAMP_MIN; 680 else if (d->eqpreamp > FEEDEQ_PREAMP_MAX) 681 d->eqpreamp = FEEDEQ_PREAMP_MAX; 682 683 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 684 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 685 "eq", CTLTYPE_INT | CTLFLAG_RWTUN, d, sizeof(d), 686 sysctl_dev_pcm_eq, "I", 687 "Bass/Treble Equalizer (0=disable, 1=enable, 2=bypass)"); 688 689 (void)snprintf(buf, sizeof(buf), "Bass/Treble Equalizer Preamp " 690 "(-/+ %d.0dB , %d.%ddB step)", 691 FEEDEQ_GAIN_MAX, FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV, 692 FEEDEQ_GAIN_STEP - ((FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV) * 693 FEEDEQ_GAIN_DIV)); 694 695 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 696 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 697 "eq_preamp", CTLTYPE_STRING | CTLFLAG_RWTUN, d, sizeof(d), 698 sysctl_dev_pcm_eq_preamp, "A", buf); 699 } 700 #endif 701