锐英源软件
第一信赖

精通

英语

开源

擅长

开发

培训

胸怀四海 

第一信赖

当前位置:锐英源 / 开源技术 / 结合Dsp使用的均衡器代码
服务方向
人工智能数据处理
人工智能培训
kaldi数据准备
小语种语音识别
语音识别标注
语音识别系统
语音识别转文字
kaldi开发技术服务
软件开发
运动控制卡上位机
机械加工软件
软件开发培训
Java 安卓移动开发
VC++
C#软件
汇编和破解
驱动开发
联系方式
固话:0371-63888850
手机:138-0381-0136
Q Q:396806883
微信:ryysoft

锐英源精品开源心得,转载请注明出处:锐英源,www.wisestudy.cn,孙老师作品,联系电话13803810136。


结合Dsp使用的均衡器代码


介绍

本文介绍播放器均衡器的代码实现,此代码实现混音功能。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);
}
友情链接
版权所有 Copyright(c)2004-2021 锐英源软件
公司注册号:410105000449586 豫ICP备08007559号 最佳分辨率 1024*768
地址:郑州大学北校区院(文化路97号院)内