19,468
社区成员
发帖
与我相关
我的任务
分享
//-----------------------------------------------------------------------------
// 绘制声音波形
// pBuffer PCM数据
// dwSize PCM数据长度
// dwPos 数据开始的位置 为了使MP3解码一帧,输出一帧波形图, POS就是表示当前帧在MP3中的位置
//-----------------------------------------------------------------------------
void CWaveBar::DrawWaveGraph(BYTE* pBuffer, DWORD dwSize, DWORD dwPos)
{
iframecount++; //第几帧,,这个函数做为回调函数用,
if(dwPos>m_dwDataSize) m_dwDataSize=dwPos;
if(m_dwDataSize>=m_dwSize && m_dwSize==0) return;
if(m_MDC.m_hDC !=NULL) //
{
CPen penLine,penWhite,penCursor,*oldpen; //各种COLOR的PEN
penLine.CreatePen (PS_SOLID,0,RGB(0,90,180));
penWhite.CreatePen (PS_SOLID,1,RGB(255,255,255));
penCursor.CreatePen (PS_SOLID,1,RGB(255,0,0));
oldpen=m_MDC.SelectObject (&penLine);
DWORD size, perSize, totalSize;
size=m_wfx.wBitsPerSample ==16?dwSize/2:dwSize;
perSize=m_wfx.wBitsPerSample ==16?m_dwDataSize/2:m_dwDataSize;
totalSize=m_wfx.wBitsPerSample ==16?m_dwSize/2:m_dwSize;
int b=0;
int time=m_dwSize/m_wfx.nAvgBytesPerSec ;
b=time<10 ? memrec.Width()-((memrec.Width()/10)*time):0;// if the time<10 calculate the wave width
int pps=(memrec.Width ()*15)/time;
int n= m_wfx.nSamplesPerSec /pps;
int yy=memrec.Height ()/4;
int h=yy;
short sample;
int register x=0;
int nLastX = int(((float)(perSize)/totalSize)*(memrec.Width()-b));
int px;
int Index=nLastX/10;
int nLastY = yy;
for(x=0;x<(long)size;x=x+n) //画第1 channel 的波形
{
//难道下面两句就是FFT变换的算法么????
sample=m_wfx.wBitsPerSample ==8?( (*(pBuffer+x)-128)*h)/128:((*((short*)pBuffer+x))*h)/(65535/2);
px = int(((float)(perSize+x)/totalSize)*(memrec.Width()-b));
int py = yy-sample;
m_MDC.SelectObject (&penLine);
m_MDC.MoveTo(nLastX, nLastY);
m_MDC.LineTo (px, py);
nLastX = px;
nLastY = py;
}
if(m_wfx.nChannels>1) // 如果是立体专用
{
yy= yy+h+h/2;
m_MDC.SelectObject (&penLine);
m_MDC.MoveTo (int(((float)(perSize)/totalSize)*(memrec.Width()-b)),yy); // move to the orgin
for( x=1;x<=(long)size;x=x+n) //画第二通道的波形
{
//难道下面两句就是FFT变换的算法么????
sample=m_wfx.wBitsPerSample ==8?( (*(pBuffer+x)-128)*h)/128:((*((short*)pBuffer+x))*h)/(65535/2);
px=int(((float)(perSize+x)/totalSize)*(memrec.Width()-b)) ;
m_MDC.LineTo (px,yy-sample);
}
}
m_MDC.SelectObject (oldpen);
if (Index<1000 && m_OffsetIndex[Index]==0)
{
m_OffsetIndex[Index]=g_pMp3Current-g_pMp3Begin ;
if (Index>0)
{
//画向屏
int x1,x2 ;
x1= (float)((Index-1)*10) / m_fZoom- m_XAmount ;
x2= (float)(Index*10) / m_fZoom- m_XAmount ;
CDC *pDc = GetDC();
CRect rc;
GetClientRect(&rc);
StretchBlt(pDc->m_hDC,x1,WAVE_SCALE_TOP,x2-x1,rc.Height()-WAVE_SCALE_TOP,m_MDC.GetSafeHdc(),
(Index-1)*10,0,10,memrec.Height(),SRCCOPY);
pDc->DeleteDC();
}
}
}
m_dwDataSize += dwSize;
}