📄 hxeqengine.c
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: HXEQEngine.c,v 1.2.2.3 2004/07/09 01:49:47 hubbe Exp $ * * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved. * * The contents of this file, and the files included with this file, * are subject to the current version of the RealNetworks Public * Source License (the "RPSL") available at * http://www.helixcommunity.org/content/rpsl unless you have licensed * the file under the current version of the RealNetworks Community * Source License (the "RCSL") available at * http://www.helixcommunity.org/content/rcsl, in which case the RCSL * will apply. You may also obtain the license terms directly from * RealNetworks. You may not use this file except in compliance with * the RPSL or, if you have a valid RCSL with RealNetworks applicable * to this file, the RCSL. Please see the applicable RPSL or RCSL for * the rights, obligations and limitations governing use of the * contents of the file. * * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL") in which case the provisions of the GPL are applicable * instead of those above. If you wish to allow use of your version of * this file only under the terms of the GPL, and not to allow others * to use your version of this file under the terms of either the RPSL * or RCSL, indicate your decision by deleting the provisions above * and replace them with the notice and other provisions required by * the GPL. If you do not delete the provisions above, a recipient may * use your version of this file under the terms of any one of the * RPSL, the RCSL or the GPL. * * This file is part of the Helix DNA Technology. RealNetworks is the * developer of the Original Code and owns the copyrights in the * portions it created. * * This file, and the files included with this file, is distributed * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET * ENJOYMENT OR NON-INFRINGEMENT. * * Technology Compatibility Kit Test Suite(s) Location: * http://www.helixcommunity.org/content/tck * * Contributor(s): * * ***** END LICENSE BLOCK ***** *//* * DESIGN: * Uses separate filter set for each sampling rate. * 10-Band with centers at { 31 62 125 250 500 1k 2k 4k 8k 16k } * Filter Q = 1.4 * * Each filter is a 2nd order IIR bandpass of the form: * y[n] = gain * alpha * (x[n] - x[n-2]) + gamma * y[n-1] + beta * y[n-2]; * * and the final output is: * out[n] = x[n] + y1[n] + y2[n] + ... yk[n] * * PERFORMANCE: * Approx 8% of PPro-200 at 44kHz stereo. * */#include <stdlib.h>#include "HXEQEngine.h"#include <string.h>//#include "pnheap.h"#ifdef _DEBUG#undef PN_THIS_FILE //static char PN_THIS_FILE[] = __FILE__;#endif//static char pgmrname[] = "1998 Ken Cooke, 1999 Greg Conklin";/* supported sampling rates */static int const ratetab[EQ_NRATES] = { 8000, 11025, 16000, 22050, 32000, 44100, 48000 };/* gain of order 1 IIR low-pass echo filters for reverb */static float const echoFiltCoeff_tab[EQ_NRATES] = {0.3f, 0.25f, 0.2f, 0.2f, 0.15f, 0.1f, 0.1f};/* number of active bands for each rate */static int const nbandtab[EQ_NRATES] = { EQ_NBANDS_8000, EQ_NBANDS_11025, EQ_NBANDS_16000, EQ_NBANDS_22050, EQ_NBANDS_32000, EQ_NBANDS_44100, EQ_NBANDS_48000};/* * Set of filter coefficients for each rate. * Generate with Matlab scripts. */static float const filtertab[EQ_NRATES][3*EQ_MAXBANDS] = /* alpha gamma beta */{{ /* 8000Hz */ 8.710134894088783e-003f, 1.981982598970183e+000f, -9.825797302118224e-001f, 1.727115128250412e-002f, 1.963089974998454e+000f, -9.654576974349918e-001f, 3.396593280788152e-002f, 1.922760884501598e+000f, -9.320681343842368e-001f, 6.577590621415974e-002f, 1.832487552730858e+000f, -8.684481875716803e-001f, 1.239690282256030e-001f, 1.617815066411626e+000f, -7.520619435487940e-001f, 2.240723323636952e-001f, 1.085148862569372e+000f, -5.518553352726097e-001f, 3.864411084133348e-001f, -1.406063057473513e-001f, -2.271177831733304e-001f, 2.890250750139612e-001f, -1.314230783298363e+000f, -4.219498499720776e-001f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, { /* 11025Hz */ 6.335343685943684e-003f, 1.987014148482799e+000f, -9.873293126281128e-001f, 1.259142257160317e-002f, 1.973564483979041e+000f, -9.748171548567936e-001f, 2.487362573947438e-002f, 1.945305160724163e+000f, -9.502527485210514e-001f, 4.856981461880283e-002f, 1.883562934010661e+000f, -9.028603707623942e-001f, 9.285864973861380e-002f, 1.740868886457108e+000f, -8.142827005227723e-001f, 1.714210738310449e-001f, 1.391591652958285e+000f, -6.571578523379102e-001f, 3.017645835264713e-001f, 5.363990129056221e-001f, -3.964708329470574e-001f, 4.522456271235510e-001f, -9.408631837096566e-001f, -9.550874575289810e-002f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,}, { /* 16000Hz */ 4.374033365920349e-003f, 1.991101994699731e+000f, -9.912519332681594e-001f, 8.710134894088783e-003f, 1.981982598970183e+000f, -9.825797302118224e-001f, 1.727115128250412e-002f, 1.963089974998454e+000f, -9.654576974349918e-001f, 3.396593280788152e-002f, 1.922760884501598e+000f, -9.320681343842368e-001f, 6.577590621415974e-002f, 1.832487552730858e+000f, -8.684481875716803e-001f, 1.239690282256030e-001f, 1.617815066411626e+000f, -7.520619435487940e-001f, 2.240723323636952e-001f, 1.085148862569372e+000f, -5.518553352726097e-001f, 3.864411084133348e-001f, -1.406063057473513e-001f, -2.271177831733304e-001f, 2.890250750139612e-001f, -1.314230783298363e+000f, -4.219498499720776e-001f, 0.0f, 0.0f, 0.0f,}, { /* 22050Hz */ 3.177705835768263e-003f, 1.993565546144875e+000f, -9.936445883284634e-001f, 6.335343685943684e-003f, 1.987014148482799e+000f, -9.873293126281128e-001f, 1.259142257160317e-002f, 1.973564483979041e+000f, -9.748171548567936e-001f, 2.487362573947438e-002f, 1.945305160724163e+000f, -9.502527485210514e-001f, 4.856981461880283e-002f, 1.883562934010661e+000f, -9.028603707623942e-001f, 9.285864973861380e-002f, 1.740868886457108e+000f, -8.142827005227723e-001f, 1.714210738310449e-001f, 1.391591652958285e+000f, -6.571578523379102e-001f, 3.017645835264713e-001f, 5.363990129056221e-001f, -3.964708329470574e-001f, 4.522456271235510e-001f, -9.408631837096566e-001f, -9.550874575289810e-002f, 0.0f, 0.0f, 0.0f,}, { /* 32000Hz */ 2.191799705892117e-003f, 1.995578833613303e+000f, -9.956164005882160e-001f, 4.374033365920349e-003f, 1.991101994699731e+000f, -9.912519332681594e-001f, 8.710134894088783e-003f, 1.981982598970183e+000f, -9.825797302118224e-001f, 1.727115128250412e-002f, 1.963089974998454e+000f, -9.654576974349918e-001f, 3.396593280788152e-002f, 1.922760884501598e+000f, -9.320681343842368e-001f, 6.577590621415974e-002f, 1.832487552730858e+000f, -8.684481875716803e-001f, 1.239690282256030e-001f, 1.617815066411626e+000f, -7.520619435487940e-001f, 2.240723323636952e-001f, 1.085148862569372e+000f, -5.518553352726097e-001f, 3.864411084133348e-001f, -1.406063057473513e-001f, -2.271177831733304e-001f, 2.890250750139612e-001f, -1.314230783298363e+000f, -4.219498499720776e-001f,}, { /* 44100Hz */ 1.591377371402913e-003f, 1.996797453216610e+000f, -9.968172452571942e-001f, 3.177705835768263e-003f, 1.993565546144875e+000f, -9.936445883284634e-001f, 6.335343685943684e-003f, 1.987014148482799e+000f, -9.873293126281128e-001f, 1.259142257160317e-002f, 1.973564483979041e+000f, -9.748171548567936e-001f, 2.487362573947438e-002f, 1.945305160724163e+000f, -9.502527485210514e-001f, 4.856981461880283e-002f, 1.883562934010661e+000f, -9.028603707623942e-001f, 9.285864973861380e-002f, 1.740868886457108e+000f, -8.142827005227723e-001f, 1.714210738310449e-001f, 1.391591652958285e+000f, -6.571578523379102e-001f, 3.017645835264713e-001f, 5.363990129056221e-001f, -3.964708329470574e-001f, 4.522456271235510e-001f, -9.408631837096566e-001f, -9.550874575289810e-002f,}, { /* 48000Hz */ 1.462266838385334e-003f, 1.997058757668317e+000f, -9.970754663232294e-001f, 2.920269686957624e-003f, 1.994092723730942e+000f, -9.941594606260845e-001f, 5.823582515269456e-003f, 1.988086666872636e+000f, -9.883528349694609e-001f, 1.158012040231685e-002f, 1.975781286236746e+000f, -9.768397591953663e-001f, 2.289816913170989e-002f, 1.950018799529792e+000f, -9.542036617365804e-001f, 4.479454859684075e-002f, 1.894055158922016e+000f, -9.104109028063185e-001f, 8.591998299109292e-002f, 1.765685036799309e+000f, -8.281600340178141e-001f, 1.594237125483447e-001f, 1.453270522628865e+000f, -6.811525749033107e-001f, 2.823330993858340e-001f, 6.828979361551111e-001f, -4.353338012283320e-001f, 4.818841328568499e-001f, -8.590373446961327e-001f, -3.623173428630035e-002f,}};/* * Lookup table for pow(10.0, i/(8.0 * 20.0)), -144 <= i <= 144. */static float const powertab[EQ_MAXGAIN-EQ_MINGAIN+1] = { 0.1258925412f, 0.1277173798f, 0.1295686698f, 0.1314467946f, 0.1333521432f, 0.1352851103f, 0.1372460961f, 0.1392355068f, 0.1412537545f, 0.1433012570f, 0.1453784386f, 0.1474857293f, 0.1496235656f, 0.1517923903f, 0.1539926526f, 0.1562248081f, 0.1584893192f, 0.1607866549f, 0.1631172909f, 0.1654817100f, 0.1678804018f, 0.1703138632f, 0.1727825981f, 0.1752871178f, 0.1778279410f, 0.1804055940f, 0.1830206106f, 0.1856735325f, 0.1883649089f, 0.1910952975f, 0.1938652636f, 0.1966753809f, 0.1995262315f, 0.2024184057f, 0.2053525026f, 0.2083291299f, 0.2113489040f, 0.2144124503f, 0.2175204034f, 0.2206734069f, 0.2238721139f, 0.2271171867f, 0.2304092976f, 0.2337491283f, 0.2371373706f, 0.2405747261f, 0.2440619068f, 0.2475996349f, 0.2511886432f, 0.2548296748f, 0.2585234840f, 0.2622708356f, 0.2660725060f, 0.2699292823f, 0.2738419634f, 0.2778113597f, 0.2818382931f, 0.2859235978f, 0.2900681199f, 0.2942727176f, 0.2985382619f, 0.3028656361f, 0.3072557365f, 0.3117094724f, 0.3162277660f, 0.3208115533f, 0.3254617835f, 0.3301794198f, 0.3349654392f, 0.3398208329f, 0.3447466066f, 0.3497437804f, 0.3548133892f, 0.3599564831f, 0.3651741273f, 0.3704674022f, 0.3758374043f, 0.3812852457f, 0.3868120546f, 0.3924189758f, 0.3981071706f, 0.4038778168f, 0.4097321098f, 0.4156712620f, 0.4216965034f, 0.4278090820f, 0.4340102636f, 0.4403013327f, 0.4466835922f, 0.4531583638f, 0.4597269885f, 0.4663908269f, 0.4731512590f, 0.4800096849f, 0.4869675252f, 0.4940262208f, 0.5011872336f, 0.5084520469f, 0.5158221651f, 0.5232991147f, 0.5308844442f, 0.5385797247f, 0.5463865499f, 0.5543065366f, 0.5623413252f, 0.5704925797f, 0.5787619883f, 0.5871512638f, 0.5956621435f, 0.6042963902f, 0.6130557921f, 0.6219421634f, 0.6309573445f, 0.6401032025f, 0.6493816316f, 0.6587945534f, 0.6683439176f, 0.6780317017f, 0.6878599123f, 0.6978305849f, 0.7079457844f, 0.7182076058f, 0.7286181745f, 0.7391796466f, 0.7498942093f, 0.7607640819f, 0.7717915156f, 0.7829787942f, 0.7943282347f, 0.8058421878f, 0.8175230379f, 0.8293732045f, 0.8413951416f, 0.8535913393f, 0.8659643234f, 0.8785166564f, 0.8912509381f, 0.9041698059f, 0.9172759354f, 0.9305720409f, 0.9440608763f, 0.9577452351f, 0.9716279516f, 0.9857119009f, 1.0000000000f, 1.0144952081f, 1.0292005272f, 1.0441190030f, 1.0592537252f, 1.0746078283f, 1.0901844924f, 1.1059869434f, 1.1220184543f, 1.1382823453f, 1.1547819847f, 1.1715207898f, 1.1885022274f, 1.2057298145f, 1.2232071190f, 1.2409377608f, 1.2589254118f, 1.2771737976f, 1.2956866975f, 1.3144679458f, 1.3335214322f, 1.3528511028f, 1.3724609610f, 1.3923550682f, 1.4125375446f, 1.4330125702f, 1.4537843856f, 1.4748572928f, 1.4962356561f, 1.5179239032f, 1.5399265261f, 1.5622480815f, 1.5848931925f, 1.6078665491f, 1.6311729092f, 1.6548170999f, 1.6788040181f, 1.7031386317f, 1.7278259805f, 1.7528711776f, 1.7782794100f, 1.8040559401f, 1.8302061063f, 1.8567353246f, 1.8836490895f, 1.9109529750f, 1.9386526360f, 1.9667538093f, 1.9952623150f, 2.0241840574f, 2.0535250265f, 2.0832912990f, 2.1134890398f, 2.1441245032f, 2.1752040340f, 2.2067340691f, 2.2387211386f, 2.2711718673f, 2.3040929761f, 2.3374912832f, 2.3713737057f, 2.4057472609f, 2.4406190680f, 2.4759963492f, 2.5118864315f, 2.5482967480f, 2.5852348396f, 2.6227083565f, 2.6607250598f, 2.6992928232f, 2.7384196343f, 2.7781135966f, 2.8183829313f, 2.8592359783f, 2.9006811987f, 2.9427271762f, 2.9853826189f, 3.0286563611f, 3.0725573653f, 3.1170947236f, 3.1622776602f, 3.2081155328f, 3.2546178350f, 3.3017941977f, 3.3496543916f, 3.3982083289f, 3.4474660657f, 3.4974378037f, 3.5481338923f, 3.5995648314f, 3.6517412725f, 3.7046740221f, 3.7583740429f, 3.8128524566f, 3.8681205463f, 3.9241897585f, 3.9810717055f, 4.0387781682f, 4.0973210981f, 4.1567126200f, 4.2169650343f, 4.2780908199f, 4.3401026364f, 4.4030133272f, 4.4668359215f, 4.5315836376f, 4.5972698853f, 4.6639082688f, 4.7315125896f, 4.8000968491f, 4.8696752517f, 4.9402622077f, 5.0118723363f, 5.0845204686f, 5.1582216507f, 5.2329911468f, 5.3088444423f, 5.3857972471f, 5.4638654988f, 5.5430653661f, 5.6234132519f, 5.7049257970f, 5.7876198835f, 5.8715126379f, 5.9566214353f, 6.0429639024f, 6.1305579215f, 6.2194216341f, 6.3095734448f, 6.4010320247f, 6.4938163158f, 6.5879455344f, 6.6834391757f, 6.7803170172f, 6.8785991231f, 6.9783058486f, 7.0794578438f, 7.1820760583f, 7.2861817451f, 7.3917964656f, 7.4989420933f, 7.6076408193f, 7.7179151559f, 7.8297879419f, 7.9432823472f};/* * table of echo parameters for reverb */static float const feedbackgain_tab[13] = { 0.000f, 0.040f, 0.060f, 0.070f, 0.075f, 0.080f, 0.084f, 0.087f, 0.089f, 0.091f, 0.093f, 0.094f, 0.095f};/* * table of echo delays for a given room size. * each row is a different roomsize. units are millisecs. */static int const delay_tab[13][REVERB_NUM_ECHOS] = { { 60, 63, 71, 83, 92,103,122,136,150,171}, { 70, 78, 92,102,114,127,142,157,176,195}, { 80, 89,105,117,130,145,161,180,200,223}, { 90,100,112,131,146,163,182,202,225,251}, {100,111,131,146,163,181,202,225,250,279}, {112,125,147,163,182,203,226,252,281,313}, {123,139,164,182,203,226,252,281,313,349}, {123,158,176,197,219,257,287,320,356,397}, {121,162,199,223,248,277,308,343,404,450}, {121,162,191,237,271,315,341,413,478,489}, {121,163,201,243,317,385,421,453,478,489}, {121,169,217,251,331,415,441,463,478,489}, {169,237,311,361,415,431,441,463,478,489},};/* * Fast, rounded float to long. */#ifdef _M_IX86__inline longRoundFtoL(float f) { long l; __asm fld f __asm fistp l return l;}#else#define RoundFtoL(f) ((f) < 0.0 ? (f) - 0.5 : (f) + 0.5 )#endif/* * Create an EQ instance. * * Allocates memory. * Filter state is cleared. * Gains are initialized to flat response. * Reverb is reset and turned off. * * Returns a valid EQ, or NULL on error. */EQPTREQInit(int samprate, int nchans){ EQPTR eq; int ratecode, i, chan; int hist_length; /* find rate code */ ratecode = -1; for (i = 0; i < EQ_NRATES; i++) { if (ratetab[i] == samprate) ratecode = i; } if (ratecode == -1) return NULL; /* invalid rate */ /* create EQSTATE struct */ eq = (EQPTR) malloc(sizeof(EQSTATE)); if (!eq) return NULL; /* malloc failed */ /* fill in params */ eq->ratecode = ratecode; eq->nchans = nchans; eq->nbands = nbandtab[ratecode]; eq->coeff = filtertab[ratecode]; eq->pwrLevelMeasure = 4096*4096.f; eq->autopregain = 0; eq->itlbuf = (float *) malloc (sizeof(float) * 2 * EQ_MAXSAMPS); if (!(eq->itlbuf)) return NULL; /* init history to zero */ for (chan = 0; chan < EQ_MAXCHANS; chan++) { eq->ihist[chan][0] = 0.0f; eq->ihist[chan][1] = 0.0f; eq->mean[chan] = 0.0f; for (i = 0; i < 2 * EQ_MAXBANDS; i++) eq->ohist[chan][i] = 0.0f; } /* init gains to flat response */ eq->pregain = 1.0f; for (i = 0; i < EQ_MAXBANDS; i++) eq->filtgain[i] = 0.0f; /* allocate enough memory for EQ_HIST_DURATION seconds of data */ hist_length = (int)(samprate * nchans * REVERB_MAX_ECHO_DELAY / 1000) + EQ_MAXSAMPS * nchans + 100; eq->eqhist = NULL; eq->eqhist = (float *)malloc(sizeof(float) * hist_length); if (!(eq->eqhist)) return NULL; else { for (i = 0; i < hist_length; i++) eq->eqhist[i] = 0.0f; } eq->eqhist_head = eq->eqhist; eq->eqhist_end = eq->eqhist + (hist_length) - (EQ_MAXSAMPS * nchans); /* reset reverb settings */ for (i = 0; i < REVERB_NUM_ECHOS; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -