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 #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 static const struct { 242 uint32_t format; 243 feed_eq_t biquad; 244 } feed_eq_biquad_tab[] = { 245 #if BYTE_ORDER == LITTLE_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 246 FEEDEQ_ENTRY(S, 16, LE), 247 FEEDEQ_ENTRY(S, 32, LE), 248 #endif 249 #if BYTE_ORDER == BIG_ENDIAN || defined(SND_FEEDER_MULTIFORMAT) 250 FEEDEQ_ENTRY(S, 16, BE), 251 FEEDEQ_ENTRY(S, 32, BE), 252 #endif 253 #ifdef SND_FEEDER_MULTIFORMAT 254 FEEDEQ_ENTRY(S, 8, NE), 255 FEEDEQ_ENTRY(S, 24, LE), 256 FEEDEQ_ENTRY(S, 24, BE), 257 FEEDEQ_ENTRY(U, 8, NE), 258 FEEDEQ_ENTRY(U, 16, LE), 259 FEEDEQ_ENTRY(U, 24, LE), 260 FEEDEQ_ENTRY(U, 32, LE), 261 FEEDEQ_ENTRY(U, 16, BE), 262 FEEDEQ_ENTRY(U, 24, BE), 263 FEEDEQ_ENTRY(U, 32, BE) 264 #endif 265 }; 266 267 #define FEEDEQ_BIQUAD_TAB_SIZE \ 268 ((int32_t)(sizeof(feed_eq_biquad_tab) / sizeof(feed_eq_biquad_tab[0]))) 269 270 static struct feed_eq_coeff * 271 feed_eq_coeff_rate(uint32_t rate) 272 { 273 uint32_t spd, threshold; 274 int i; 275 276 if (rate < FEEDEQ_RATE_MIN || rate > FEEDEQ_RATE_MAX) 277 return (NULL); 278 279 /* 280 * Not all rates are supported. Choose the best rate that we can to 281 * allow 'sloppy' conversion. Good enough for naive listeners. 282 */ 283 for (i = 0; i < FEEDEQ_TAB_SIZE; i++) { 284 spd = feed_eq_tab[i].rate; 285 threshold = spd + ((i < (FEEDEQ_TAB_SIZE - 1) && 286 feed_eq_tab[i + 1].rate > spd) ? 287 ((feed_eq_tab[i + 1].rate - spd) >> 1) : 0); 288 if (rate == spd || 289 (feeder_eq_exact_rate == 0 && rate <= threshold)) 290 return (feed_eq_tab[i].coeff); 291 } 292 293 return (NULL); 294 } 295 296 int 297 feeder_eq_validrate(uint32_t rate) 298 { 299 300 if (feed_eq_coeff_rate(rate) != NULL) 301 return (1); 302 303 return (0); 304 } 305 306 static void 307 feed_eq_reset(struct feed_eq_info *info) 308 { 309 uint32_t i; 310 311 for (i = 0; i < info->channels; i++) { 312 info->treble.i1[i] = 0; 313 info->treble.i2[i] = 0; 314 info->treble.o1[i] = 0; 315 info->treble.o2[i] = 0; 316 info->bass.i1[i] = 0; 317 info->bass.i2[i] = 0; 318 info->bass.o1[i] = 0; 319 info->bass.o2[i] = 0; 320 } 321 } 322 323 static int 324 feed_eq_setup(struct feed_eq_info *info) 325 { 326 327 info->coeff = feed_eq_coeff_rate(info->rate); 328 if (info->coeff == NULL) 329 return (EINVAL); 330 331 feed_eq_reset(info); 332 333 return (0); 334 } 335 336 static int 337 feed_eq_init(struct pcm_feeder *f) 338 { 339 struct feed_eq_info *info; 340 feed_eq_t biquad_op; 341 int i; 342 343 if (f->desc->in != f->desc->out) 344 return (EINVAL); 345 346 biquad_op = NULL; 347 348 for (i = 0; i < FEEDEQ_BIQUAD_TAB_SIZE && biquad_op == NULL; i++) { 349 if (AFMT_ENCODING(f->desc->in) == feed_eq_biquad_tab[i].format) 350 biquad_op = feed_eq_biquad_tab[i].biquad; 351 } 352 353 if (biquad_op == NULL) 354 return (EINVAL); 355 356 info = malloc(sizeof(*info), M_DEVBUF, M_NOWAIT | M_ZERO); 357 if (info == NULL) 358 return (ENOMEM); 359 360 info->channels = AFMT_CHANNEL(f->desc->in); 361 info->align = info->channels * AFMT_BPS(f->desc->in); 362 363 info->rate = FEEDEQ_RATE_MIN; 364 info->treble.gain = FEEDEQ_L2GAIN(50); 365 info->bass.gain = FEEDEQ_L2GAIN(50); 366 info->preamp = FEEDEQ_PREAMP2IDX(FEEDEQ_PREAMP_DEFAULT); 367 info->state = FEEDEQ_UNKNOWN; 368 369 info->biquad = biquad_op; 370 371 f->data = info; 372 373 return (feed_eq_setup(info)); 374 } 375 376 static int 377 feed_eq_set(struct pcm_feeder *f, int what, int value) 378 { 379 struct feed_eq_info *info; 380 381 info = f->data; 382 383 switch (what) { 384 case FEEDEQ_CHANNELS: 385 if (value < SND_CHN_MIN || value > SND_CHN_MAX) 386 return (EINVAL); 387 info->channels = (uint32_t)value; 388 info->align = info->channels * AFMT_BPS(f->desc->in); 389 feed_eq_reset(info); 390 break; 391 case FEEDEQ_RATE: 392 if (feeder_eq_validrate(value) == 0) 393 return (EINVAL); 394 info->rate = (uint32_t)value; 395 if (info->state == FEEDEQ_UNKNOWN) 396 info->state = FEEDEQ_ENABLE; 397 return (feed_eq_setup(info)); 398 break; 399 case FEEDEQ_TREBLE: 400 case FEEDEQ_BASS: 401 if (value < 0 || value > 100) 402 return (EINVAL); 403 if (what == FEEDEQ_TREBLE) 404 info->treble.gain = FEEDEQ_L2GAIN(value); 405 else 406 info->bass.gain = FEEDEQ_L2GAIN(value); 407 break; 408 case FEEDEQ_PREAMP: 409 if (value < FEEDEQ_PREAMP_MIN || value > FEEDEQ_PREAMP_MAX) 410 return (EINVAL); 411 info->preamp = FEEDEQ_PREAMP2IDX(value); 412 break; 413 case FEEDEQ_STATE: 414 if (!(value == FEEDEQ_BYPASS || value == FEEDEQ_ENABLE || 415 value == FEEDEQ_DISABLE)) 416 return (EINVAL); 417 info->state = value; 418 feed_eq_reset(info); 419 break; 420 default: 421 return (EINVAL); 422 break; 423 } 424 425 return (0); 426 } 427 428 static int 429 feed_eq_free(struct pcm_feeder *f) 430 { 431 struct feed_eq_info *info; 432 433 info = f->data; 434 if (info != NULL) 435 free(info, M_DEVBUF); 436 437 f->data = NULL; 438 439 return (0); 440 } 441 442 static int 443 feed_eq_feed(struct pcm_feeder *f, struct pcm_channel *c, uint8_t *b, 444 uint32_t count, void *source) 445 { 446 struct feed_eq_info *info; 447 uint32_t j; 448 uint8_t *dst; 449 450 info = f->data; 451 452 /* 453 * 3 major states: 454 * FEEDEQ_BYPASS - Bypass entirely, nothing happened. 455 * FEEDEQ_ENABLE - Preamp+biquad filtering. 456 * FEEDEQ_DISABLE - Preamp only. 457 */ 458 if (info->state == FEEDEQ_BYPASS) 459 return (FEEDER_FEED(f->source, c, b, count, source)); 460 461 dst = b; 462 count = SND_FXROUND(count, info->align); 463 464 do { 465 if (count < info->align) 466 break; 467 468 j = SND_FXDIV(FEEDER_FEED(f->source, c, dst, count, source), 469 info->align); 470 if (j == 0) 471 break; 472 473 info->biquad(info, dst, j); 474 475 j *= info->align; 476 dst += j; 477 count -= j; 478 479 } while (count != 0); 480 481 return (dst - b); 482 } 483 484 static struct pcm_feederdesc feeder_eq_desc[] = { 485 { FEEDER_EQ, 0, 0, 0, 0 }, 486 { 0, 0, 0, 0, 0 } 487 }; 488 489 static kobj_method_t feeder_eq_methods[] = { 490 KOBJMETHOD(feeder_init, feed_eq_init), 491 KOBJMETHOD(feeder_free, feed_eq_free), 492 KOBJMETHOD(feeder_set, feed_eq_set), 493 KOBJMETHOD(feeder_feed, feed_eq_feed), 494 KOBJMETHOD_END 495 }; 496 497 FEEDER_DECLARE(feeder_eq, NULL); 498 499 static int32_t 500 feed_eq_scan_preamp_arg(const char *s) 501 { 502 int r, i, f; 503 size_t len; 504 char buf[32]; 505 506 bzero(buf, sizeof(buf)); 507 508 /* XXX kind of ugly, but works for now.. */ 509 510 r = sscanf(s, "%d.%d", &i, &f); 511 512 if (r == 1 && !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX)) { 513 snprintf(buf, sizeof(buf), "%c%d", 514 FEEDEQ_PREAMP_SIGNMARK(i), abs(i)); 515 f = 0; 516 } else if (r == 2 && 517 !(i < FEEDEQ_PREAMP_IMIN || i > FEEDEQ_PREAMP_IMAX || 518 f < FEEDEQ_PREAMP_FMIN || f > FEEDEQ_PREAMP_FMAX)) 519 snprintf(buf, sizeof(buf), "%c%d.%d", 520 FEEDEQ_PREAMP_SIGNMARK(i), abs(i), f); 521 else 522 return (FEEDEQ_PREAMP_INVALID); 523 524 len = strlen(s); 525 if (len > 2 && strcasecmp(s + len - 2, "dB") == 0) 526 strlcat(buf, "dB", sizeof(buf)); 527 528 if (i == 0 && *s == '-') 529 *buf = '-'; 530 531 if (strcasecmp(buf + ((*s >= '0' && *s <= '9') ? 1 : 0), s) != 0) 532 return (FEEDEQ_PREAMP_INVALID); 533 534 while ((f / FEEDEQ_GAIN_DIV) > 0) 535 f /= FEEDEQ_GAIN_DIV; 536 537 return (((i < 0 || *buf == '-') ? -1 : 1) * FEEDEQ_IF2PREAMP(i, f)); 538 } 539 540 #ifdef _KERNEL 541 static int 542 sysctl_dev_pcm_eq(SYSCTL_HANDLER_ARGS) 543 { 544 struct snddev_info *d; 545 struct pcm_channel *c; 546 struct pcm_feeder *f; 547 int err, val, oval; 548 549 d = oidp->oid_arg1; 550 if (!PCM_REGISTERED(d)) 551 return (ENODEV); 552 553 PCM_LOCK(d); 554 PCM_WAIT(d); 555 if (d->flags & SD_F_EQ_BYPASSED) 556 val = 2; 557 else if (d->flags & SD_F_EQ_ENABLED) 558 val = 1; 559 else 560 val = 0; 561 PCM_ACQUIRE(d); 562 PCM_UNLOCK(d); 563 564 oval = val; 565 err = sysctl_handle_int(oidp, &val, 0, req); 566 567 if (err == 0 && req->newptr != NULL && val != oval) { 568 if (!(val == 0 || val == 1 || val == 2)) { 569 PCM_RELEASE_QUICK(d); 570 return (EINVAL); 571 } 572 573 PCM_LOCK(d); 574 575 d->flags &= ~(SD_F_EQ_ENABLED | SD_F_EQ_BYPASSED); 576 if (val == 2) { 577 val = FEEDEQ_BYPASS; 578 d->flags |= SD_F_EQ_BYPASSED; 579 } else if (val == 1) { 580 val = FEEDEQ_ENABLE; 581 d->flags |= SD_F_EQ_ENABLED; 582 } else 583 val = FEEDEQ_DISABLE; 584 585 CHN_FOREACH(c, d, channels.pcm.busy) { 586 CHN_LOCK(c); 587 f = feeder_find(c, FEEDER_EQ); 588 if (f != NULL) 589 (void)FEEDER_SET(f, FEEDEQ_STATE, val); 590 CHN_UNLOCK(c); 591 } 592 593 PCM_RELEASE(d); 594 PCM_UNLOCK(d); 595 } else 596 PCM_RELEASE_QUICK(d); 597 598 return (err); 599 } 600 601 static int 602 sysctl_dev_pcm_eq_preamp(SYSCTL_HANDLER_ARGS) 603 { 604 struct snddev_info *d; 605 struct pcm_channel *c; 606 struct pcm_feeder *f; 607 int err, val, oval; 608 char buf[32]; 609 610 d = oidp->oid_arg1; 611 if (!PCM_REGISTERED(d)) 612 return (ENODEV); 613 614 PCM_LOCK(d); 615 PCM_WAIT(d); 616 val = d->eqpreamp; 617 bzero(buf, sizeof(buf)); 618 (void)snprintf(buf, sizeof(buf), "%c%d.%ddB", 619 FEEDEQ_PREAMP_SIGNMARK(val), FEEDEQ_PREAMP_IPART(val), 620 FEEDEQ_PREAMP_FPART(val)); 621 PCM_ACQUIRE(d); 622 PCM_UNLOCK(d); 623 624 oval = val; 625 err = sysctl_handle_string(oidp, buf, sizeof(buf), req); 626 627 if (err == 0 && req->newptr != NULL) { 628 val = feed_eq_scan_preamp_arg(buf); 629 if (val == FEEDEQ_PREAMP_INVALID) { 630 PCM_RELEASE_QUICK(d); 631 return (EINVAL); 632 } 633 634 PCM_LOCK(d); 635 636 if (val != oval) { 637 if (val < FEEDEQ_PREAMP_MIN) 638 val = FEEDEQ_PREAMP_MIN; 639 else if (val > FEEDEQ_PREAMP_MAX) 640 val = FEEDEQ_PREAMP_MAX; 641 642 d->eqpreamp = val; 643 644 CHN_FOREACH(c, d, channels.pcm.busy) { 645 CHN_LOCK(c); 646 f = feeder_find(c, FEEDER_EQ); 647 if (f != NULL) 648 (void)FEEDER_SET(f, FEEDEQ_PREAMP, val); 649 CHN_UNLOCK(c); 650 } 651 } 652 653 PCM_RELEASE(d); 654 PCM_UNLOCK(d); 655 } else 656 PCM_RELEASE_QUICK(d); 657 658 return (err); 659 } 660 661 void 662 feeder_eq_initsys(device_t dev) 663 { 664 struct snddev_info *d; 665 const char *preamp; 666 char buf[64]; 667 668 d = device_get_softc(dev); 669 670 if (!(resource_string_value(device_get_name(dev), device_get_unit(dev), 671 "eq_preamp", &preamp) == 0 && 672 (d->eqpreamp = feed_eq_scan_preamp_arg(preamp)) != 673 FEEDEQ_PREAMP_INVALID)) 674 d->eqpreamp = FEEDEQ_PREAMP_DEFAULT; 675 676 if (d->eqpreamp < FEEDEQ_PREAMP_MIN) 677 d->eqpreamp = FEEDEQ_PREAMP_MIN; 678 else if (d->eqpreamp > FEEDEQ_PREAMP_MAX) 679 d->eqpreamp = FEEDEQ_PREAMP_MAX; 680 681 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 682 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 683 "eq", CTLTYPE_INT | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, d, 684 sizeof(d), sysctl_dev_pcm_eq, "I", 685 "Bass/Treble Equalizer (0=disable, 1=enable, 2=bypass)"); 686 687 (void)snprintf(buf, sizeof(buf), "Bass/Treble Equalizer Preamp " 688 "(-/+ %d.0dB , %d.%ddB step)", 689 FEEDEQ_GAIN_MAX, FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV, 690 FEEDEQ_GAIN_STEP - ((FEEDEQ_GAIN_STEP / FEEDEQ_GAIN_DIV) * 691 FEEDEQ_GAIN_DIV)); 692 693 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 694 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 695 "eq_preamp", CTLTYPE_STRING | CTLFLAG_RWTUN | CTLFLAG_MPSAFE, 696 d, sizeof(d), sysctl_dev_pcm_eq_preamp, "A", buf); 697 } 698 #endif 699