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