📄 equalization.cpp
字号:
else if(slider >= EQBANDS31) slider = EQBANDS31 - 1; } } } // maEqualizerSetLevelsvoid maEqualizeBlock(void* pcmblock, unsigned long pcmblocksize){// return; eq31EqualizeBlock(pcmblock, pcmblocksize);} // maEqualizeBlock// from eq-xmms;#define EQ_MAX_BANDS EQBANDS10#define EQ_CHANNELS 2void init_iir();/* Global configuration structure */EQConfig eqcfg;/* History for two filters */sXYData data_history[EQ_MAX_BANDS][EQ_CHANNELS];sXYData data_history2[EQ_MAX_BANDS][EQ_CHANNELS];/* Coefficients */sIIRCoefficients *iir_cf;/* Gain for each band values should be between -0.2 and 1.0 */float gain[EQ_MAX_BANDS][EQ_CHANNELS];int round_trick(float floatvalue_to_round);int eq31EqualizeBlock(void *pcmblock, unsigned long pcmblocksize){ gpointer ppcmblock = pcmblock; if(!eqcfg.equalizer_active || !eqcfg.bits16 /* || (g_state.insamplerate != 44100 && !(g_sets.eqflags & MAEQADAPTRATE))*/ ) return (int)pcmblocksize; return iir(&ppcmblock, (int)pcmblocksize); } // eq31EqualizeBlockint eq31EqualizerSetLevel(int band, double factor){ gain[band][0] = gain[band][1] = -0.2f + ((float)factor / 5.125903437963185f); // (1.2 * factor) / 6.151084125555822; return 0; } // eq31EqualizerSetLevelint eq31Init(int bands, int bits16, int extra){ eqcfg.band_num = bands; eqcfg.equalizer_active = 1; eqcfg.extra_filtering = extra; eqcfg.bits16 = bits16; init_iir(); return 0; } // eq31Initint round_trick(float floatvalue_to_round){ float floattmp; int rounded_value; floattmp = (int) 0x00FD8000L + (floatvalue_to_round); rounded_value = *(int*)(&floattmp) - (int)0x4B7D8000L; if(rounded_value != (short) rounded_value ) rounded_value = ( rounded_value >> 31 ) ^ 0x7FFF; return rounded_value;}magint g_eqi = 0, g_eqj = 2, g_eqk = 1;/* Init the filter */void init_iir(){ if(eqcfg.band_num == EQBANDS31) { iir_cf = iir_cf31; } else { iir_cf = iir_cf10; }// iir_cf = iir_cf2; /* Zero the history arrays */ memset(data_history, 0, sizeof(sXYData) * EQ_MAX_BANDS * EQ_CHANNELS); memset(data_history2, 0, sizeof(sXYData) * EQ_MAX_BANDS * EQ_CHANNELS);}int iir(gpointer *d, magint length){ gint16 *data = (gint16 *) *d; /* Indexes for the history arrays * These have to be kept between calls to this function * hence they are static */ //n static magint i = 0, j = 2, k = 1; magint index, band, channel, ic; magint tempgint, halflength; float out[EQ_CHANNELS], pcm[EQ_CHANNELS]; g_eqi = 0; g_eqj = 2; g_eqk = 1; /** * IIR filter equation is * y[n] = 2 * (alpha*(x[n]-x[n-2]) + gamma*y[n-1] - beta*y[n-2]) * * NOTE: The 2 factor was introduced in the coefficients to save * a multiplication * * This algorithm cascades two filters to get nice filtering * at the expense of extra CPU cycles */ /* 16bit, 2 bytes per sample, so divide by two the length of * the buffer (length is in bytes) */ halflength = (length >> 1); for(index = 0; index < halflength; index+=2) { /* For each channel */ for(channel = 0; channel < EQ_CHANNELS; channel++) { ic = index + channel; /* No need to scale when processing the PCM with the filter */ pcm[channel] = data[ic]; /* Preamp gain */ //n pcm[channel] *= preamp[channel]; out[channel] = 0; /* For each band */ for(band = 0; band < eqcfg.band_num; band++) { /* Store Xi(n) */ data_history[band][channel].x[g_eqi] = pcm[channel]; /* Calculate and store Yi(n) */ data_history[band][channel].y[g_eqi] = ( /* = alpha * [x(n)-x(n-2)] */ iir_cf[band].alpha * ( data_history[band][channel].x[g_eqi] - data_history[band][channel].x[g_eqk]) /* + gamma * y(n-1) */ + iir_cf[band].gamma * data_history[band][channel].y[g_eqj] /* - beta * y(n-2) */ - iir_cf[band].beta * data_history[band][channel].y[g_eqk] ); /* * The multiplication by 2.0 was 'moved' into the coefficients to save * CPU cycles here */ /* Apply the gain */ out[channel] += data_history[band][channel].y[g_eqi] * gain[band][channel]; // * 2.0; } /* For each band */ if(eqcfg.extra_filtering) { /* Filter the sample again */ for(band = 0; band < eqcfg.band_num; band++) { /* Store Xi(n) */ data_history2[band][channel].x[g_eqi] = out[channel]; /* Calculate and store Yi(n) */ data_history2[band][channel].y[g_eqi] = ( /* y(n) = alpha * [x(n)-x(n-2)] */ iir_cf[band].alpha * (data_history2[band][channel].x[g_eqi] - data_history2[band][channel].x[g_eqk]) /* + gamma * y(n-1) */ + iir_cf[band].gamma * data_history2[band][channel].y[g_eqj] /* - beta * y(n-2) */ - iir_cf[band].beta * data_history2[band][channel].y[g_eqk] ); /* Apply the gain */ out[channel] += data_history2[band][channel].y[g_eqi]*gain[band][channel]; } /* For each band */ } /* Volume stuff Scale down original PCM sample and add it to the filters output. This substitutes the multiplication by 0.25 */ out[channel] += (data[ic] >> 2); /* Round and convert to integer */ tempgint = round_trick(out[channel]); /* Limit the output */ if(tempgint < -32768) data[ic] = -32768; else if(tempgint > 32767) data[ic] = 32767; else data[ic] = (gint16)tempgint; } /* For each channel */ g_eqi++; g_eqj++; g_eqk++; /* Wrap around the indexes */ if(g_eqi == 3) g_eqi = 0; else if(g_eqj == 3) g_eqj = 0; else g_eqk = 0; }/* For each pair of samples */ return length;}#ifdef __cplusplus}#endif// eof
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -