1#!/usr/bin/awk -f 2# 3# Copyright (c) 2007-2009 Ariff Abdullah <ariff@FreeBSD.org> 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: --- 10 unchanged lines hidden (view full) --- 19# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25# SUCH DAMAGE. 26# |
27# $FreeBSD: head/sys/tools/sound/feeder_rate_mkfilter.awk 195378 2009-07-05 18:15:06Z ariff $ |
28# 29 30# 31# FIR filter design by windowing method. This might become one of the 32# funniest joke I've ever written due to too many tricks being applied to 33# ensure maximum precision (well, in fact this is already have the same 34# precision granularity compared to its C counterpart). Nevertheless, it's 35# working, precise, dynamically tunable based on "presets". --- 345 unchanged lines hidden (view full) --- 381 382 if (alen > 0 && a[1] == "OVERSAMPLING_FACTOR") { 383 if (alen != 2) 384 return (-1); 385 init_drift(floor(a[2])); 386 return (-1); 387 } 388 |
389 if (alen > 0 && a[1] == "COEFFICIENT_BIT") { 390 if (alen != 2) 391 return (-1); 392 init_coeff_bit(floor(a[2])); 393 return (-1); 394 } 395 396 if (alen > 0 && a[1] == "ACCUMULATOR_BIT") { 397 if (alen != 2) 398 return (-1); 399 init_accum_bit(floor(a[2])); 400 return (-1); 401 } 402 403 if (alen > 0 && a[1] == "INTERPOLATOR") { 404 if (alen != 2) 405 return (-1); 406 init_coeff_interpolator(toupper(a[2])); 407 return (-1); 408 } 409 |
410 if (alen == 1 || alen == 2) { 411 if (a[1] == "NYQUIST_HOVER") { 412 i = 1.0 * a[2]; 413 Z_NYQUIST_HOVER = (i > 0.0 && i < 1.0) ? i : 0.0; 414 return (-1); 415 } 416 i = 1; 417 if (alen == 1) { --- 46 unchanged lines hidden (view full) --- 464 i = 1.0 * a[i]; 465 Popts["rolloff"] = rolloff_round(i); 466 467 return (0); 468} 469 470function genscale(bit, s1, s2, scale) 471{ |
472 if ((bit + Z_COEFF_SHIFT) > Z_ACCUMULATOR_BIT) 473 s1 = Z_COEFF_SHIFT - \ 474 (32 - (Z_ACCUMULATOR_BIT - Z_COEFF_SHIFT)); 475 else 476 s1 = Z_COEFF_SHIFT - (32 - bit); 477 |
478 s2 = Z_SHIFT + (32 - bit); 479 480 if (s1 == 0) 481 scale = "v"; 482 else if (s1 < 0) 483 scale = sprintf("(v) << %d", abs(s1)); 484 else 485 scale = sprintf("(v) >> %d", s1); --- 62 unchanged lines hidden (view full) --- 548 Z_DRIFT_SHIFT = xdrift; 549 Z_DRIFT_ONE = shl(1, Z_DRIFT_SHIFT); 550 551 Z_SHIFT = Z_FULL_SHIFT - Z_DRIFT_SHIFT; 552 Z_ONE = shl(1, Z_SHIFT); 553 Z_MASK = Z_ONE - 1; 554} 555 |
556function init_coeff_bit(cbit, xcbit) 557{ 558 xcbit = floor(cbit); 559 560 if (Z_COEFF_SHIFT != 0) { 561 if (xcbit != Z_COEFF_SHIFT) 562 printf("#error Z_COEFF_SHIFT reinitialize!\n"); 563 return; 564 } 565 566 # 567 # Initialize dynamic range of coefficients. 568 # 569 if (xcbit < 1) 570 xcbit = 1; 571 else if (xcbit > 30) 572 xcbit = 30; 573 574 Z_COEFF_SHIFT = xcbit; 575 Z_COEFF_ONE = shl(1, Z_COEFF_SHIFT); 576} 577 578function init_accum_bit(accbit, xaccbit) 579{ 580 xaccbit = floor(accbit); 581 582 if (Z_ACCUMULATOR_BIT != 0) { 583 if (xaccbit != Z_ACCUMULATOR_BIT) 584 printf("#error Z_ACCUMULATOR_BIT reinitialize!\n"); 585 return; 586 } 587 588 # 589 # Initialize dynamic range of accumulator. 590 # 591 if (xaccbit > 64) 592 xaccbit = 64; 593 else if (xaccbit < 32) 594 xaccbit = 32; 595 596 Z_ACCUMULATOR_BIT = xaccbit; 597} 598 599function init_coeff_interpolator(interp) 600{ 601 # 602 # Validate interpolator type. 603 # 604 if (interp == "ZOH" || interp == "LINEAR" || \ 605 interp == "QUADRATIC" || interp == "HERMITE" || \ 606 interp == "BSPLINE" || interp == "OPT32X" || \ 607 interp == "OPT16X" || interp == "OPT8X" || \ 608 interp == "OPT4X" || interp == "OPT2X") 609 Z_COEFF_INTERPOLATOR = interp; 610} 611 |
612BEGIN { 613 I0_EPSILON = 1e-21; 614 M_PI = atan2(0.0, -1.0); 615 616 INT32_MAX = 1 + ((shl(1, 30) - 1) * 2); 617 INT32_MIN = -1 - INT32_MAX; 618 619 Z_COEFF_OFFSET = 5; 620 |
621 Z_ACCUMULATOR_BIT_DEFAULT = 58; 622 Z_ACCUMULATOR_BIT = 0; 623 |
624 Z_FULL_SHIFT = 30; 625 Z_FULL_ONE = shl(1, Z_FULL_SHIFT); 626 |
627 Z_COEFF_SHIFT_DEFAULT = 30; 628 Z_COEFF_SHIFT = 0; 629 Z_COEFF_ONE = 0; |
630 |
631 Z_COEFF_INTERPOLATOR = 0; 632 |
633 Z_INTERP_COEFF_SHIFT = 24; 634 Z_INTERP_COEFF_ONE = shl(1, Z_INTERP_COEFF_SHIFT); 635 636 Z_LINEAR_FULL_SHIFT = Z_FULL_SHIFT; 637 Z_LINEAR_FULL_ONE = shl(1, Z_LINEAR_FULL_SHIFT); 638 Z_LINEAR_SHIFT = 8; 639 Z_LINEAR_UNSHIFT = Z_LINEAR_FULL_SHIFT - Z_LINEAR_SHIFT; 640 Z_LINEAR_ONE = shl(1, Z_LINEAR_SHIFT) --- 62 unchanged lines hidden (view full) --- 703 imp["quality"] = 2; 704 for (i = 1; i < ARGC; i++) { 705 if (filter_parse(ARGV[i]) == 0) { 706 beta = Popts["beta"]; 707 nmult = Popts["nmult"]; 708 rolloff = Popts["rolloff"]; 709 if (Z_DRIFT_SHIFT == -1) 710 init_drift(Z_DRIFT_SHIFT_DEFAULT); |
711 if (Z_COEFF_SHIFT == 0) 712 init_coeff_bit(Z_COEFF_SHIFT_DEFAULT); 713 if (Z_ACCUMULATOR_BIT == 0) 714 init_accum_bit(Z_ACCUMULATOR_BIT_DEFAULT); |
715 ztab[imp["quality"] - 2] = \ 716 mkfilter(imp, nmult, rolloff, beta, Z_DRIFT_ONE); 717 imp["quality"]++; 718 } 719 } 720 721 printf("\n"); 722 # --- 115 unchanged lines hidden (view full) --- 838 # (Z_SCALE_ONE > INT32_MAX) ? "U" : ""); 839 printf("\n"); 840 printf("#define Z_SCALE_CAST(s)\t\t((uint32_t)(s))\n"); 841 genscale(8); 842 genscale(16); 843 genscale(24); 844 genscale(32); 845 printf("\n"); |
846 printf("#define Z_ACCUMULATOR_BIT\t%d\n\n", Z_ACCUMULATOR_BIT) 847 for (i = 8; i <= 32; i += 8) { 848 gbit = ((i + Z_COEFF_SHIFT) > Z_ACCUMULATOR_BIT) ? \ 849 (i - (Z_ACCUMULATOR_BIT - Z_COEFF_SHIFT)) : 0; 850 printf("#define Z_GUARD_BIT_%d\t\t%d\n", i, gbit); 851 if (gbit == 0) 852 printf("#define Z_NORM_%d(v)\t\t(v)\n\n", i); 853 else 854 printf("#define Z_NORM_%d(v)\t\t" \ 855 "((v) >> Z_GUARD_BIT_%d)\n\n", i, i); 856 } 857 printf("\n"); |
858 printf("#define Z_LINEAR_FULL_ONE\t0x%08xU\n", Z_LINEAR_FULL_ONE); 859 printf("#define Z_LINEAR_SHIFT\t\t%d\n", Z_LINEAR_SHIFT); 860 printf("#define Z_LINEAR_UNSHIFT\t%d\n", Z_LINEAR_UNSHIFT); 861 printf("#define Z_LINEAR_ONE\t\t0x%08x\n", Z_LINEAR_ONE); 862 printf("\n"); 863 printf("#ifdef SND_PCM_64\n"); 864 genlerp(8, 1); 865 genlerp(16, 1); --- 8 unchanged lines hidden (view full) --- 874 printf("\n"); 875 printf("#define Z_QUALITY_ZOH\t\t0\n"); 876 printf("#define Z_QUALITY_LINEAR\t1\n"); 877 printf("#define Z_QUALITY_SINC\t\t%d\n", \ 878 floor((length(ztab) - 1) / 2) + 2); 879 printf("\n"); 880 printf("#define Z_QUALITY_MIN\t\t0\n"); 881 printf("#define Z_QUALITY_MAX\t\t%d\n", length(ztab) + 1); |
882 if (Z_COEFF_INTERPOLATOR != 0) 883 printf("\n#define Z_COEFF_INTERP_%s\t1\n", \ 884 Z_COEFF_INTERPOLATOR); |
885 printf("\n/*\n * smallest: %.32f\n * largest: %.32f\n *\n", \ 886 smallest, largest); 887 printf(" * z_unshift=%d, z_interp_shift=%d\n *\n", \ 888 Z_UNSHIFT, Z_INTERP_SHIFT); 889 v = shr(Z_ONE - 1, Z_UNSHIFT) * abs(largest_interp); 890 printf(" * largest interpolation multiplication: %d\n */\n", v); 891 if (v < INT32_MIN || v > INT32_MAX) { 892 printf("\n#ifndef SND_FEEDER_RATE_HP\n"); 893 printf("#error interpolation overflow, please reduce" \ 894 " Z_INTERP_SHIFT\n"); 895 printf("#endif\n"); 896 } 897 898 printf("\n#endif /* !_FEEDER_RATE_GEN_H_ */\n"); 899} |