如何用MFC制作音乐波形

已经用MFC制作了一个简单的音乐播放器,现在想添加一个类似千千静听那个播放波形的模块不知哪位大虾可以指导一下?小弟感激不尽。做实时的频谱分析也可以~~~~~~急!!!... 已经用MFC制作了一个简单的音乐播放器,现在想添加一个类似 千千静听 那个播放波形的模块 不知哪位大虾可以指导一下?小弟感激不尽。
做实时的频谱分析也可以~~~~~~急!!!
展开
 我来答
_GlacialSpike
推荐于2016-06-20 · TA获得超过336个赞
知道答主
回答量:94
采纳率:0%
帮助的人:112万
展开全部
我做过,但播放模块使用的是Direct Sound。
频谱分析需要用FFT变换,给你提供一下我的代码(c++)
/*==============================================================
快速傅立叶变换 模块
================================================================*/
inline unsigned int NumberOfBitsNeeded( unsigned int p_nSamples )
{

int i;

if( p_nSamples < 2 )
{
return 0;
}

for ( i=0; ; i++ )
{
if( p_nSamples & (1 << i) ) return i;
}

}
inline unsigned int ReverseBits(unsigned int p_nIndex, unsigned int p_nBits)
{

unsigned int i, rev;

for(i=rev=0; i < p_nBits; i++)
{
rev = (rev << 1) | (p_nIndex & 1);
p_nIndex >>= 1;
}

return rev;

}
inline double Index_to_frequency(unsigned int p_nBaseFreq, unsigned int p_nSamples, unsigned int p_nIndex)
{

if(p_nIndex >= p_nSamples)
{
return 0.0;
}
else if(p_nIndex <= p_nSamples/2)
{
return ( (double)p_nIndex / (double)p_nSamples * p_nBaseFreq );
}
else
{
return ( -(double)(p_nSamples-p_nIndex) / (double)p_nSamples * p_nBaseFreq );
}

}
void FFT (unsigned int p_nSamples, bool p_bInverseTransform, double *p_lpRealIn, double *p_lpImagIn, double *p_lpRealOut, double *p_lpImagOut)
{

if(!p_lpRealIn || !p_lpRealOut || !p_lpImagOut) return;

unsigned int NumBits;
unsigned int i, j, k, n;
unsigned int BlockSize, BlockEnd;

double angle_numerator = 2.0 * PI;
double tr, ti;

if( p_bInverseTransform ) angle_numerator = -angle_numerator;

NumBits = NumberOfBitsNeeded ( p_nSamples );

for( i=0; i < p_nSamples; i++ )
{
j = ReverseBits ( i, NumBits );
p_lpRealOut[j] = p_lpRealIn[i];
p_lpImagOut[j] = (p_lpImagIn == NULL) ? 0.0 : p_lpImagIn[i];
}

BlockEnd = 1;
for( BlockSize = 2; BlockSize <= p_nSamples; BlockSize <<= 1 )
{
double delta_angle = angle_numerator / (double)BlockSize;
double sm2 = sin ( -2 * delta_angle );
double sm1 = sin ( -delta_angle );
double cm2 = cos ( -2 * delta_angle );
double cm1 = cos ( -delta_angle );
double w = 2 * cm1;
double ar[3], ai[3];

for( i=0; i < p_nSamples; i += BlockSize )
{

ar[2] = cm2;
ar[1] = cm1;

ai[2] = sm2;
ai[1] = sm1;

for ( j=i, n=0; n < BlockEnd; j++, n++ )
{

ar[0] = w*ar[1] - ar[2];
ar[2] = ar[1];
ar[1] = ar[0];

ai[0] = w*ai[1] - ai[2];
ai[2] = ai[1];
ai[1] = ai[0];

k = j + BlockEnd;
tr = ar[0]*p_lpRealOut[k] - ai[0]*p_lpImagOut[k];
ti = ar[0]*p_lpImagOut[k] + ai[0]*p_lpRealOut[k];

p_lpRealOut[k] = p_lpRealOut[j] - tr;
p_lpImagOut[k] = p_lpImagOut[j] - ti;

p_lpRealOut[j] += tr;
p_lpImagOut[j] += ti;

}
}

BlockEnd = BlockSize;

}

if( p_bInverseTransform )
{
double denom = (double)p_nSamples;

for ( i=0; i < p_nSamples; i++ )
{
p_lpRealOut[i] /= denom;
p_lpImagOut[i] /= denom;
}
}

}
/*===================================================================

=====================================================================*/
如果你是做软件的,这些应该看得懂,
还有个使用的实例,我做的声音变调函数:
//*************************音调变换*****************************************

void __stdcall Sd72_Pitch(char *in_fn,//输入文件名
char *in_outfn,//输出文件名
long in_s,//开始位置(单位:采样)
long in_l,//处理长度(单位:采样)
double in_v)//变调幅度((1,1.8]升调,[0.6,1)降调)
{
jFile fp,dp;
fp=in_fn;
dp=in_outfn;
dp.toend();
unsigned long filelength=dp.len()+in_l*2;
long l,i,j=0,k,m=2;
double a=in_v,ca=(abs(a-1)>0.3)?0.85:0.75,w,winpower=(a>1)?1:2,at=(a>1)?0.5:2;
l=pow(2,13);
long cl,el,jl=1;
cl=(long)(ca*l);
el=l-cl;

short *e=(short*)malloc(sizeof(short)*l*m);
short *be=(short*)malloc(sizeof(short)*l*m);
short *f=(short*)malloc(sizeof(short)*l*m);
short *bf=(short*)malloc(sizeof(short)*l*m);
double *rin=(double*)malloc(sizeof(double)*l*m);
double *iin=(double*)malloc(sizeof(double)*l*m);
double *rout=(double*)malloc(sizeof(double)*l*m);
double *iout=(double*)malloc(sizeof(double)*l*m);

fp.pointto(in_s*2-1);

fp.in(0,e,sizeof(short)*l);
memcpy(be,e,sizeof(short)*l);
////
ZeroMemory(rin,sizeof(double)*l*m);
ZeroMemory(iin,sizeof(double)*l*m);
ZeroMemory(rout,sizeof(double)*l*m);
ZeroMemory(iout,sizeof(double)*l*m);

w=2*PI/l;
for(i=0;i<l;i++)
rin[i]=(double)e[i]*pow(0.5*(cos(w*(i-l*0.5+0.5))+1),winpower);//余弦窗

C_DoEvents();
FFT(l,false,rin,iin,rout,iout);
C_DoEvents();

ZeroMemory(&rout[(long)(l*0.5)],sizeof(double)*l*0.5);
ZeroMemory(&iout[(long)(l*0.5)],sizeof(double)*l*0.5);
ZeroMemory(rin,sizeof(double)*l*m);
ZeroMemory(iin,sizeof(double)*l*m);

if(a>1 && a<=1.8)
{
FcZero(rout,l*0.5,rin,(l*0.5)*a);
FcZero(iout,l*0.5,iin,(l*0.5)*a);
}

if(a>=0.6 && a<1)
{
FcTo(rout,l*0.5,rin,(l*0.5)*a);
FcTo(iout,l*0.5,iin,(l*0.5)*a);
}

ZeroMemory(rout,sizeof(double)*l*m);
ZeroMemory(iout,sizeof(double)*l*m);

C_DoEvents();
FFT(l*m,true,rin,iin,rout,iout);
C_DoEvents();

FcTo(rout,l*m,iout,l);//iout出

for(i=0;i<l;i++)
f[i]=(short)(iout[i]*at);
////
memcpy(bf,f,sizeof(short)*l);

while(jl==1)
{
fp.in(0,&e[cl],sizeof(short)*el);
memcpy(e,&be[el],sizeof(short)*cl);
memcpy(be,e,sizeof(short)*l);
////
ZeroMemory(rin,sizeof(double)*l*m);
ZeroMemory(iin,sizeof(double)*l*m);
ZeroMemory(rout,sizeof(double)*l*m);
ZeroMemory(iout,sizeof(double)*l*m);

w=2*PI/l;
for(i=0;i<l;i++)
rin[i]=(double)e[i]*pow(0.5*(cos(w*(i-l*0.5+0.5))+1),winpower);//余弦窗

C_DoEvents();
FFT(l,false,rin,iin,rout,iout);
C_DoEvents();

ZeroMemory(&rout[(long)(l*0.5)],sizeof(double)*l*0.5);
ZeroMemory(&iout[(long)(l*0.5)],sizeof(double)*l*0.5);
ZeroMemory(rin,sizeof(double)*l*m);
ZeroMemory(iin,sizeof(double)*l*m);

if(a>1 && a<=1.8)
{
FcZero(rout,l*0.5,rin,(l*0.5)*a);
FcZero(iout,l*0.5,iin,(l*0.5)*a);
}

if(a>=0.6 && a<1)
{
FcTo(rout,l*0.5,rin,(l*0.5)*a);
FcTo(iout,l*0.5,iin,(l*0.5)*a);
}

ZeroMemory(rout,sizeof(double)*l*m);
ZeroMemory(iout,sizeof(double)*l*m);

C_DoEvents();
FFT(l,true,rin,iin,rout,iout);
C_DoEvents();

for(i=0;i<l;i++)
f[i]=(short)(rout[i]*at);
////
for(k=0;k<cl;k++)
{
f[k]=f[k]+bf[k+el];// ^_^ 重叠叠加,使声音变平滑
}
dp.out(0,bf,sizeof(short)*el);
in_l-=el;
if(in_l<=0)
jl=0;
memcpy(bf,f,sizeof(short)*l);
}
fp.leave(NULL,JFILE_NORMAL);
dp.leave(NULL,filelength);
}
//*********************************************************************************

如果不懂,等我高考完了可以和我探讨探讨,QQ:994373259
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
匿名用户
2009-04-28
展开全部
听说是要进行FFT变换~~~不过这变换有点复杂,之前我也想过做类似这种效果……
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 1条折叠回答
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式