精通
英语
和
开源
,
擅长
开发
与
培训
,
胸怀四海
第一信赖
锐英源精品开源心得,转载请注明出处:锐英源,www.wisestudy.cn,孙老师作品,联系电话13803810136。
本文介绍播放器均衡器的代码实现,此代码实现混音功能。DSP专指混音器的属性调整以实现不同的效果。Bass是个音频库,有很好播放效果。
本文介绍的均衡器代码简单易用,如果想对频带进行更多控制,向类里添加更多的Bandpass过滤器。默认值需要一些间接调用,在Bass内的使用方法如下:
1、声明
Equalizer MyEqualizer ;
2、在DSP回调里传递缓冲和长度到Equalizer.Run函数里
MyEqualizer.Run(buffer,length);
有音频原始数据缓冲指针,就可以修改数据,实现定制效果。
3、Run调用修改Gain
MyEqualizer.SetGain( low , mid , high );
gain以db计量。
class Bandpass { private: float PI; int freq; // center freq in Hz int freq_s; // Sampling Freq. float res; // resonance [0 - 1] (minimum - maximum) float sf; // scaling factor float xOld[3]; float yOld[3]; float a0,a1,a2,b1,b2; // Filter coefficients void Add2Old( float input, float* dest ); short Clip(float input); public: Bandpass(int samplerate); Bandpass(); void Reset(); void Run(void* buf ,unsigned int length); void Set(int freq,float g, float q,int samplerate); }; class Equalizer { private: Bandpass Band1; // Low Bandpass Bandpass Band2; // Mid Bandpass Bandpass Band3; // Hi Bandpass public: void SetGain(float Again_low, float Again_mid, float Again_high); void Run(void* buf ,unsigned int length); void Reset(); void Update(); int samplerate; float gain_low,gain_mid,gain_high; int freq_low,freq_mid,freq_high; float q_low,q_mid,q_high; Equalizer(); }; #include#include "Equalizer.h" Bandpass::Bandpass() { sf = 1 / 32768.0; PI = 3.141592654; Reset(); } void Bandpass::Add2Old(float input, float* dest) { for (int i=2;i > 0;i--) { dest[i] = dest[i-1]; } dest[0] = input; } Bandpass::Bandpass(int samplerate) { sf = 1 / 32768.0 ; PI = 3.141592654; Reset(); Set(22050.0,0.0,0.0,samplerate); } void Bandpass::Set(int freq,float g, float q,int samplerate) { freq_s = samplerate; float a,b,c,e; float Wn,Wp; float gain=g/6.6; if (freq > freq_s/2) freq = freq_s/2 ; Wn=1/(2*PI*freq); Wp=(Wn/tan(Wn/(2*freq_s))); a=(Wn*Wn*Wp*Wp); b=(3+gain)*Wn*Wp*q; c=(3-gain)*Wn*Wp*q; e=1.0; b2 = (e-c+a)/(a+c+e); a2 = (e-b+a)/(a+c+e); b1 = 2*(e-a)/(a+c+e); a1 = 2*(e-a)/(a+c+e); a0 = (a+b+e)/(a+c+e); } void Bandpass::Run(void* buf ,unsigned int length) { short *d = (short*) buf; float out, input ; for (;length;length-=4,d+=2) { input = (float)d[0]; Add2Old( sf * input - b1*xOld[0] - b2*xOld[1] , xOld ); out = a0*xOld[0] + a1*xOld[1] + a2*xOld[2]; d[0] = Clip( out /sf ); input = (float)d[1]; Add2Old( sf * input - b1*yOld[0] - b2*yOld[1], yOld ); out = a0*yOld[0] + a1*yOld[1] + a2*yOld[2]; d[1] = Clip( out /sf ); } } void Bandpass::Reset() { for (int i=0;i<3 ;i++) { xOld[i]=0.0; yOld[i]=0.0; } } short Bandpass::Clip(float input) { if (input < -32768) input = -32768; else if (input > 32767) input = 32767; return (short)input; } /* Equalizer ------------------------------------------------------------*/ Equalizer::Equalizer() { // Default values ---------------------- freq_low = 100; freq_mid = 6000; freq_high = 12000; q_low = q_mid = q_high = 0.5; gain_low = gain_mid = gain_high = 0.0001; samplerate = 44100; // -------------------------------------- Reset(); Update(); } void Equalizer::Reset() { Band1.Reset(); Band2.Reset(); Band3.Reset(); } void Equalizer::Run(void* buf ,unsigned int length) { Band3.Run(buf,length); Band2.Run(buf,length); Band1.Run(buf,length); } void Equalizer::SetGain(float Again_low,float Again_mid, float Again_high) { gain_high = Again_high ; gain_mid = Again_mid ; gain_low = Again_low ; Update(); } void Equalizer::Update() { Band1.Set(freq_low,gain_low,q_low,samplerate); Band2.Set(freq_mid,gain_mid,q_mid,samplerate); Band3.Set(freq_high,gain_high,q_high,samplerate); }