rlm@0: /*- rlm@0: * Copyright (c) 2005 Boris Mikhaylov rlm@0: * rlm@0: * Permission is hereby granted, free of charge, to any person obtaining rlm@0: * a copy of this software and associated documentation files (the rlm@0: * "Software"), to deal in the Software without restriction, including rlm@0: * without limitation the rights to use, copy, modify, merge, publish, rlm@0: * distribute, sublicense, and/or sell copies of the Software, and to rlm@0: * permit persons to whom the Software is furnished to do so, subject to rlm@0: * the following conditions: rlm@0: * rlm@0: * The above copyright notice and this permission notice shall be rlm@0: * included in all copies or substantial portions of the Software. rlm@0: * rlm@0: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, rlm@0: * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF rlm@0: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. rlm@0: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY rlm@0: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, rlm@0: * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE rlm@0: * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. rlm@0: */ rlm@0: rlm@0: #include "config.h" rlm@0: rlm@0: #include rlm@0: rlm@0: #include "bs2b.h" rlm@0: rlm@0: #ifndef M_PI rlm@0: #define M_PI 3.14159265358979323846 rlm@0: #endif rlm@0: rlm@0: /* Single pole IIR filter. rlm@0: * O[n] = a0*I[n] + a1*I[n-1] + b1*O[n-1] rlm@0: */ rlm@0: rlm@0: /* Lowpass filter */ rlm@0: #define lo_filter(in, out_1) (bs2b->a0_lo*(in) + bs2b->b1_lo*(out_1)) rlm@0: rlm@0: /* Highboost filter */ rlm@0: #define hi_filter(in, in_1, out_1) (bs2b->a0_hi*(in) + bs2b->a1_hi*(in_1) + bs2b->b1_hi*(out_1)) rlm@0: rlm@0: /* Set up all data. */ rlm@0: static void init(struct bs2b *bs2b) rlm@0: { rlm@0: double Fc_lo, Fc_hi; rlm@0: double G_lo, G_hi; rlm@0: double x; rlm@0: rlm@0: if ((bs2b->srate > 192000) || (bs2b->srate < 2000)) rlm@0: bs2b->srate = BS2B_DEFAULT_SRATE; rlm@0: rlm@0: switch(bs2b->level) rlm@0: { rlm@0: case BS2B_LOW_CLEVEL: /* Low crossfeed level */ rlm@0: Fc_lo = 360.0; rlm@0: Fc_hi = 501.0; rlm@0: G_lo = 0.398107170553497; rlm@0: G_hi = 0.205671765275719; rlm@0: break; rlm@0: rlm@0: case BS2B_MIDDLE_CLEVEL: /* Middle crossfeed level */ rlm@0: Fc_lo = 500.0; rlm@0: Fc_hi = 711.0; rlm@0: G_lo = 0.459726988530872; rlm@0: G_hi = 0.228208484414988; rlm@0: break; rlm@0: rlm@0: case BS2B_HIGH_CLEVEL: /* High crossfeed level (virtual speakers are closer to itself) */ rlm@0: Fc_lo = 700.0; rlm@0: Fc_hi = 1021.0; rlm@0: G_lo = 0.530884444230988; rlm@0: G_hi = 0.250105790667544; rlm@0: break; rlm@0: rlm@0: case BS2B_LOW_ECLEVEL: /* Low easy crossfeed level */ rlm@0: Fc_lo = 360.0; rlm@0: Fc_hi = 494.0; rlm@0: G_lo = 0.316227766016838; rlm@0: G_hi = 0.168236228897329; rlm@0: break; rlm@0: rlm@0: case BS2B_MIDDLE_ECLEVEL: /* Middle easy crossfeed level */ rlm@0: Fc_lo = 500.0; rlm@0: Fc_hi = 689.0; rlm@0: G_lo = 0.354813389233575; rlm@0: G_hi = 0.187169483835901; rlm@0: break; rlm@0: rlm@0: default: /* High easy crossfeed level */ rlm@0: bs2b->level = BS2B_HIGH_ECLEVEL; rlm@0: rlm@0: Fc_lo = 700.0; rlm@0: Fc_hi = 975.0; rlm@0: G_lo = 0.398107170553497; rlm@0: G_hi = 0.205671765275719; rlm@0: break; rlm@0: } /* switch */ rlm@0: rlm@0: /* $fc = $Fc / $s; rlm@0: * $d = 1 / 2 / pi / $fc; rlm@0: * $x = exp(-1 / $d); rlm@0: */ rlm@0: rlm@0: x = exp(-2.0 * M_PI * Fc_lo / bs2b->srate); rlm@0: bs2b->b1_lo = x; rlm@0: bs2b->a0_lo = G_lo * (1.0 - x); rlm@0: rlm@0: x = exp(-2.0 * M_PI * Fc_hi / bs2b->srate); rlm@0: bs2b->b1_hi = x; rlm@0: bs2b->a0_hi = 1.0 - G_hi * (1.0 - x); rlm@0: bs2b->a1_hi = -x; rlm@0: rlm@0: bs2b->gain = 1.0 / (1.0 - G_hi + G_lo); rlm@0: } /* init */ rlm@0: rlm@0: /* Exported functions. rlm@0: * See descriptions in "bs2b.h" rlm@0: */ rlm@0: rlm@0: void bs2b_set_level(struct bs2b *bs2b, int level) rlm@0: { rlm@0: if(level == bs2b->level) rlm@0: return; rlm@0: bs2b->level = level; rlm@0: init(bs2b); rlm@0: } /* bs2b_set_level */ rlm@0: rlm@0: int bs2b_get_level(struct bs2b *bs2b) rlm@0: { rlm@0: return bs2b->level; rlm@0: } /* bs2b_get_level */ rlm@0: rlm@0: void bs2b_set_srate(struct bs2b *bs2b, int srate) rlm@0: { rlm@0: if (srate == bs2b->srate) rlm@0: return; rlm@0: bs2b->srate = srate; rlm@0: init(bs2b); rlm@0: } /* bs2b_set_srate */ rlm@0: rlm@0: int bs2b_get_srate(struct bs2b *bs2b) rlm@0: { rlm@0: return bs2b->srate; rlm@0: } /* bs2b_get_srate */ rlm@0: rlm@0: void bs2b_clear(struct bs2b *bs2b) rlm@0: { rlm@0: int loopv = sizeof(bs2b->last_sample); rlm@0: rlm@0: while (loopv) rlm@0: { rlm@0: ((char *)&bs2b->last_sample)[--loopv] = 0; rlm@0: } rlm@0: } /* bs2b_clear */ rlm@0: rlm@0: int bs2b_is_clear(struct bs2b *bs2b) rlm@0: { rlm@0: int loopv = sizeof(bs2b->last_sample); rlm@0: rlm@0: while (loopv) rlm@0: { rlm@0: if (((char *)&bs2b->last_sample)[--loopv] != 0) rlm@0: return 0; rlm@0: } rlm@0: return 1; rlm@0: } /* bs2b_is_clear */ rlm@0: rlm@0: void bs2b_cross_feed(struct bs2b *bs2b, float *sample) rlm@0: { rlm@0: /* Lowpass filter */ rlm@0: bs2b->last_sample.lo[0] = lo_filter(sample[0], bs2b->last_sample.lo[0]); rlm@0: bs2b->last_sample.lo[1] = lo_filter(sample[1], bs2b->last_sample.lo[1]); rlm@0: rlm@0: /* Highboost filter */ rlm@0: bs2b->last_sample.hi[0] = hi_filter(sample[0], bs2b->last_sample.asis[0], bs2b->last_sample.hi[0]); rlm@0: bs2b->last_sample.hi[1] = hi_filter(sample[1], bs2b->last_sample.asis[1], bs2b->last_sample.hi[1]); rlm@0: bs2b->last_sample.asis[0] = sample[0]; rlm@0: bs2b->last_sample.asis[1] = sample[1]; rlm@0: rlm@0: /* Crossfeed */ rlm@0: sample[0] = bs2b->last_sample.hi[0] + bs2b->last_sample.lo[1]; rlm@0: sample[1] = bs2b->last_sample.hi[1] + bs2b->last_sample.lo[0]; rlm@0: rlm@0: /* Bass boost cause allpass attenuation */ rlm@0: sample[0] *= bs2b->gain; rlm@0: sample[1] *= bs2b->gain; rlm@0: rlm@0: /* Clipping of overloaded samples */ rlm@0: #if 0 rlm@0: if (sample[0] > 1.0) rlm@0: sample[0] = 1.0; rlm@0: if (sample[0] < -1.0) rlm@0: sample[0] = -1.0; rlm@0: if (sample[1] > 1.0) rlm@0: sample[1] = 1.0; rlm@0: if (sample[1] < -1.0) rlm@0: sample[1] = -1.0; rlm@0: #endif rlm@0: } /* bs2b_cross_feed */