// WaveView.cpp : implementation file
//
#include "stdafx.h"
#include "Define.h"
#include "WaveView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CWaveView
IMPLEMENT_DYNCREATE(CWaveView, CView)
CWaveView::CWaveView()
{
//各个画笔的颜色
m_crBkground = RGB(0, 0, 0);
m_crPosLine = RGB(255, 255, 255);
m_crCurPosLine=RGB(255, 255, 0);
m_crMidLine = RGB(0, 0, 255);
m_crWaveForm = RGB(24, 243, 173);
//波形显示控制
WaveSetVShowRange(30000);
WaveSetShowRange(0, 0);
WaveSetMark(0, 0);
WaveSetHScale(1);
WaveSetVScale(1);
m_bOnMark = FALSE;
//波形播放/录制控制
m_bOnPlay = FALSE;
m_bOnRecord= FALSE;
//合成控制
m_crSepStart = RGB(0, 100, 255);
m_crSepEnd = RGB(225, 100, 155);
m_bShowSep = FALSE;
m_bOnWaveDrag = FALSE;
m_bOnSepDrag = FALSE;
}
CWaveView::~CWaveView()
{
//清空位图
m_bmpWaveForm.DeleteObject();
}
BEGIN_MESSAGE_MAP(CWaveView, CView)
//{{AFX_MSG_MAP(CWaveView)
ON_WM_CREATE()
ON_WM_SIZE()
ON_WM_ERASEBKGND()
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONDBLCLK()
ON_MESSAGE(WM_WAVECHANGED,OnWaveChanged)
ON_MESSAGE(MM_WOM_DONE, OnWaveDone)
ON_WM_TIMER()
ON_WM_DESTROY()
ON_WM_RBUTTONDOWN()
ON_WM_RBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CWaveView diagnostics
#ifdef _DEBUG
void CWaveView::AssertValid() const
{
CView::AssertValid();
}
void CWaveView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CWaveView message handlers
///////////////////////////////////////////////////////////////////////////////////
//
// 用于语音波形显示控制
//
///////////////////////////////////////////////////////////////////////////////////
void CWaveView::WaveSetMark(long lStart, long lEnd)
{
//ASSERT(lStart>=0 && lStart<=m_tWaveShowRange.end && lEnd>=0 && lEnd<=m_tWaveShowRange.end);
ASSERT(lStart>=0 && lEnd>=0);
m_tWaveMark.start = lStart;
m_tWaveMark.end = lEnd;
}
void CWaveView::WaveGetMark(long *plStart, long *plEnd)
{
if( m_tWaveMark.start<m_tWaveMark.end )
{
*plStart = m_tWaveMark.start;
*plEnd = m_tWaveMark.end;
}
else
{
*plStart = m_tWaveMark.end;
*plEnd = m_tWaveMark.start;
}
}
void CWaveView::WaveGetShowRange(long *plStart, long *plEnd)
{
*plStart = m_tWaveShowRange.start;
*plEnd = m_tWaveShowRange.end;
}
void CWaveView::WaveSetShowRange(long lStart, long lEnd)
{
m_tWaveShowRange.start = lStart;
m_tWaveShowRange.end = lEnd;
}
void CWaveView::WaveSetHScale(float fHScale)
{
m_fWaveHScale = fHScale;
}
float CWaveView::WaveGetHScale()
{
return m_fWaveHScale;
}
void CWaveView::WaveSetVScale(float fVScale)
{
m_fWaveVScale = fVScale;
}
float CWaveView::WaveGetVScale()
{
return m_fWaveVScale;
}
long CWaveView::WaveGetLength()
{
return m_WaveFile.GetWaveLength();
}
int CWaveView::WaveGetChannels()
{
return m_WaveFile.GetChannels();
}
BOOL CWaveView::WaveGetLeftChannelShow()
{
return TRUE;
}
BOOL CWaveView::WaveGetRightChannelShow()
{
return m_WaveFile.GetChannels()==2;
}
void CWaveView::WaveSetLeftChannelShow(BOOL bShow)
{
}
void CWaveView::WaveSetRightChannelShow()
{
}
byte* CWaveView::WaveGetChannelData(int nChannel)
{
return m_WaveFile.GetChannelData(nChannel);
}
byte* CWaveView::WaveGetWaveData()
{
return m_WaveFile.GetWaveData();
}
void CWaveView::WaveSetVShowRange(float fRange)
{
m_fWaveVShowRange = fRange;
}
float CWaveView::WaveGetVShowRange()
{
return m_fWaveVShowRange;
}
int CWaveView::WaveGetBitsPerSample()
{
return m_WaveFile.GetBitsPerSample();
}
int CWaveView::WaveGetSamplesPerSec()
{
return m_WaveFile.GetSamplesPerSec();
}
int CWaveView::WaveChangePosToShow(long pos)
{
long ShowStart,ShowEnd;
WaveGetShowRange(&ShowStart,&ShowEnd);
float fScale = WaveGetHScale();
return (int)((float)(pos-ShowStart)/fScale);
}
///////////////////////////////////////////////////////////////////////////////////
//
// 用于语音波形播放控制
//
///////////////////////////////////////////////////////////////////////////////////
//
// WaveCurrentPosChange - 改变当前播放位置显示 orgpos -> pos
//
void CWaveView::WaveCurrentPosChange(int orgpos, int pos)
{
DrawPosLine(orgpos);
DrawPosLine(pos);
}
//
// WavePlay - 播放波形
//
BOOL CWaveView::WavePlay()
{
byte* pBuffer;
int nChannel;
if( WaveGetLeftChannelShow() )
{
if( WaveGetRightChannelShow() )
{ //播放两个声道的数据
pBuffer = WaveGetWaveData();
nChannel = WaveGetChannels();
}
else
{ //播放左声道的数据
pBuffer = WaveGetChannelData(0);
nChannel = 1;
}
}
else
{
if( WaveGetRightChannelShow() )
{ //播放右声道的数据
pBuffer = WaveGetChannelData(1);
nChannel = 1;
}
else
{ //没有任何数据,直接退出
pBuffer = NULL;
}
}
if( pBuffer )
{
long BufLen;
long MarkStart,MarkEnd,ShowStart,ShowEnd;
WaveGetMark(&MarkStart,&MarkEnd);
WaveGetShowRange(&ShowStart,&ShowEnd);
if(MarkEnd!=MarkStart)
BufLen=MarkEnd-MarkStart;
else
BufLen=ShowEnd-MarkStart;
if(BufLen==0)
return FALSE;
//保存位置(开始播放,播放)
m_lPlayStartPos = MarkStart;
m_lPlayPos = MarkStart;
//开始播放
MarkStart *= WaveGetBitsPerSample()/8*nChannel;
BufLen *= WaveGetBitsPerSample()/8*nChannel;
m_WavePlay.Open(m_hWnd, WaveGetSamplesPerSec(), nChannel, WaveGetBitsPerSample());
m_WavePlay.PlayData(pBuffer+MarkStart, BufLen);
m_bOnPlay = TRUE;
DrawPosLine(WaveChangePosToShow(m_lPlayStartPos));
SetTimer(TIMER_WAVE_PLAY, TIME_LONG_WAVE_PLAY, NULL);
return TRUE;
}
else
return FALSE;
}
//
// WaveRecord - 录制波形
//
BOOL CWaveView::WaveRecord()
{
return FALSE;
}
//
// WaveStop - 停止播放
//
BOOL CWaveView::WaveStop()
{
if( m_bOnRecord )
{
m_WavePlay.WaveInGetPosition();
m_WavePlay.InStop();
m_WavePlay.InClose();
int Bits = WaveGetBitsPerSample()/8*WaveGetChannels();
long WavePos = m_WavePlay.GetRecordBytes()/Bits;
m_WaveFile.SetWaveLength(WavePos);
//必须重新分配声道数据
m_WaveFile.FillChannelData();
m_bOnRecord = FALSE;
//显示波形
WaveSetShowRange(0,WavePos);
SendMessage(WM_WAVECHANGED, 0, 0);
Invalidate();
UpdateWindow();
return TRUE;
}
else if( m_bOnPlay )
{
m_WavePlay.Stop();
m_WavePlay.Close();
return TRUE;
}
return FALSE;
}
//
// WavePause - 暂停播放/录制
//
BOOL CWaveView::WavePause()
{
if( m_bOnPlay )
{
m_WavePlay.Pause();
}
return FALSE;
}
//
// OnWaveDone - 播放/录制完成(响应MM_WOM_DONE消息)
//
long CWaveView::OnWaveDone(WPARAM,LPARAM)
{
KillTimer(TIMER_WAVE_PLAY);
if( m_bOnPlay )
{
//停止播放
m_WavePlay.Stop();
m_WavePlay.Close();
m_bOnPlay = FALSE;
//显示当前播放位置
long orgplaypos = m_lPlayPos;
int orgshowpos = WaveChangePosToShow(orgplaypos);
long curpos = m_lPlayStartPos;
int curshowpos = WaveChangePosToShow(curpos);
WaveCurrentPosChange(orgshowpos, curshowpos);
DrawPosLine(curshowpos);
return TRUE;
}
else if( m_bOnRecord )
{
m_WavePlay.WaveInGetPosition();
m_WavePlay.InStop();
m_WavePlay.InClose();
int Bits = WaveGetBitsPerSample()/8*WaveGetChannels();
long WavePos=m_WavePlay.GetRecordBytes()/Bits;
m_WaveFile.SetWaveLength(WavePos);
//必须重新分配声道数据
m_WaveFile.FillChannelData();
m_bOnRecord = FALSE;
//显示波形
WaveSetShowRange(0,WavePos);
SendMessage(WM_WAVECHANGED, 0, 0);
Invalidate();
UpdateWindow();
}
return 0;
}
//
// OnTimer - 响应时钟消息
// TIMER_WAVE_PLAY 用于改变当前波形播
- 1
- 2
- 3
- 4
- 5
- 6
前往页