CWaveFile.CPP
上传用户:btjhwmj
上传日期:2007-03-02
资源大小:7k
文件大小:12k
- //CWaveViewCtrl.CPP
- #include "stdafx.h"
- #include "CWaveFile.h"
- static const IID IID_DirectSound= {0x279AFA83, 0x4981, 0x11CE, 0xA5, 0x21, 0x00, 0x20, 0xAF, 0x0B, 0xE5, 0x60};
- UINT TimerID;
- IMPLEMENT_SERIAL(CWaveViewCtrl,CObject,VERSIONABLE_SCHEMA|2)
- UINT DrawThread(LPVOID pParam);
- //Message Map
- BEGIN_MESSAGE_MAP( CWaveViewCtrl, CWnd)
- //{{AFX_MSG_MAP(CWaveEditorView)
- ON_WM_LBUTTONDOWN()
- ON_WM_LBUTTONUP()
- ON_WM_MOUSEMOVE()
- ON_WM_KILLFOCUS()
- ON_WM_SETFOCUS()
- ON_WM_CREATE()
- ON_WM_TIMER()
- ON_WM_PAINT()
- ON_WM_SIZE()
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- //<<<<<<<Constructors>>>>>>>>>
- CWaveViewCtrl::CWaveViewCtrl()
- {
- m_selpoint=-1;
- m_shiftcaret=false;
- DSound=NULL;
- DSoundBuff=NULL;
- isComm=false;
- m_MouseSel=false;
- m_PBP.x=m_PBP.y=NULL;
- }
- //******************************
- //<<<<<<<<<<<Overrides>>>>>>>>>>>>
- BOOL CWaveViewCtrl::Create(DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext)
- {
- pContext = NULL;
- static CString className = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW);
- return CWnd::CreateEx(WS_EX_CLIENTEDGE | WS_EX_STATICEDGE,
- className, NULL, dwStyle,
- rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
- pParentWnd->GetSafeHwnd(), (HMENU) nID);
- }
- int CWaveViewCtrl::OnCreate( LPCREATESTRUCT lpCreateStruct )
- {
- if (CWnd::OnCreate(lpCreateStruct) == -1)
- return -1;
- m_selpoint=-1;
- // hWnd=GetSafeHwnd();
- GetClientRect(&rect);
- // CreateCaret(true);
- return 0;
- }
- void CWaveViewCtrl::Serialize( CArchive& ar )
- {
- CObject::Serialize(ar);
- if(ar.IsLoading ())
- {
- CFile *file;
- char* groupID;
- file=ar.GetFile();
- HANDLE m_hMap=::CreateFileMapping((HANDLE)file->m_hFile,NULL,PAGE_READONLY,0,0,NULL);
- LPVOID lpvFile=::MapViewOfFile(m_hMap,FILE_MAP_READ,0,0,0);
-
- int p=0;
-
- m_pRiff=(RIFF*)lpvFile;
- p+=sizeof(*m_pRiff);
- if(memcmp(m_pRiff->ID ,"RIFF",sizeof(m_pRiff->ID))==0) //for reading file heder difinetion
- {
- groupID=(char*)lpvFile+p;p+=4;
- if(memcmp(groupID ,"WAVE",sizeof(groupID))!=0)
- {
- AfxThrowArchiveException(CArchiveException::badIndex);
- }
- }
- else
- AfxThrowArchiveException(CArchiveException::badIndex);
-
- m_pRiff=(RIFF*)((BYTE*)lpvFile+p);
- p+=sizeof(*m_pRiff);
- while(memcmp(m_pRiff->ID ,"fmt ",sizeof(m_pRiff->ID))) //for reading format chunk
- {
- p+=m_pRiff->Size;
- m_pRiff=(RIFF*)((BYTE*)lpvFile+p);
- p+=sizeof(*m_pRiff);
-
- }
-
- m_pfch=(fchunk*)((BYTE*)lpvFile+p);
-
- if(m_pfch->wFormatTag ==1)
- {
- do{
- p+=m_pRiff->Size;
- m_pRiff=(RIFF*)((BYTE*)lpvFile+p);
- p+=sizeof(*m_pRiff);
-
- }while(memcmp(m_pRiff->ID ,"data",sizeof(m_pRiff->ID)));
- m_data=(BYTE*)lpvFile+p;
- isComm=true;
- m_MouseSel=true;
- }
- }
-
- }
- void CWaveViewCtrl::OnLButtonDown( UINT nFlags, CPoint point )
- {
- CWnd::OnLButtonDown(nFlags,point );
- if(!m_MouseSel)return;
- if(m_selpoint!=-1)
- {
- m_selpoint=-1;
- OnPaint();
- }
- m_selpoint=point.x ;
- ::SetCaretPos(point.x,26);
- }
- void CWaveViewCtrl::OnMouseMove( UINT nFlags, CPoint point )
- {
- CWnd::OnMouseMove(nFlags,point );
- if(!m_MouseSel)return;
- int i=-1;
- if(nFlags==MK_LBUTTON &&m_selpoint!=-1)
- {
- CDC *dc=GetDC();
-
- CPoint old=GetCaretPos();
- CPen pe(PS_NULL,0,COLORREF(0));
- CPen* oldp =dc->SelectObject (&pe);
- dc->SetROP2(R2_NOT);
-
- if(point.x <old.x )
- if(!m_shiftcaret)
- {
- i=0;
- m_shiftcaret=true;
- }
- else
- i=1;
-
- if(point.x >old.x )
- if(m_shiftcaret)
- {
- i=0;
- m_shiftcaret=false;
- }
- else
- i=-1;
-
- if(point.x!=m_selpoint)
- {
- HideCaret();
- dc->Rectangle (old.x+i,26,point.x,rect.Width ()-26);
- ::SetCaretPos(point.x,26);
- ShowCaret();
- }
- else
- dc->Rectangle (m_selpoint+i,26,old.x,rect.Width ()-26);
-
- dc->SelectObject (oldp);
- ReleaseDC (dc);
- }
- }
- void CWaveViewCtrl::OnLButtonUp( UINT nFlags, CPoint point )
- {
- m_shiftcaret=false;
- }
- void CWaveViewCtrl::OnSize( UINT nType, int cx, int cy )
- {
- CRect rec;
- CPoint caret;
- caret=GetCaretPos();
-
- GetClientRect(&rec);
-
- if(m_selpoint!=-1)
- {
- TRACE("%d",m_selpoint);
- int pre1=(int)(((float)m_selpoint/rect.Width ())*100);
- m_selpoint=(int)((((float)rec.Width()*pre1)/100)+1);
- }
- int pre=(int)(((float)caret.x/rect.Width ())*100);
- int pos=(int)((float)(rec.Width()*pre)/100);
-
- rect=rec;
- if(isComm)
- {
- CreateCaret(true);
- ::SetCaretPos(pos+1,26);
- ShowCaret();
- }
- CWnd::OnSize (nType,rect.Width (),rect.Height());
- }
- //*******************************
- //<<<<<<<<<<<Implimentations>>>>>>>>>>>>>>>>
- CWaveViewCtrl::~CWaveViewCtrl()
- {
- /*if(isComm) //for free memory
- VirtualFree(m_data,m_pRiff->Size,MEM_DECOMMIT);
- VirtualFree(m_data,m_pRiff->Size,MEM_RELEASE); */
- UnmapViewOfFile(lpvFile);
- isComm=!isComm;
-
- DestroyCaret();
- SelectObject(m_MDC.GetSafeHdc (),m_pOldBitmap);
- if(DSound!=NULL)
- {
- if(DSoundBuff)
- DSoundBuff->Release ();
- DSound->Release() ;
- // DSound=NULL;DSoundBuff=NULL;
- }
- }
- void CWaveViewCtrl::OnPaint()
- {
- CPaintDC DC(this);
- if(isComm==false) return;
- /*::fch=fch;
- ::Riff=Riff;
- ::m_data =m_data;
- ::pDC=&m_MDC;
- ::hBitmap=&m_bitmap;
- ::hOldBitmap=m_pBitmap;
- ::memrect=&memrec;*/
-
- DrawTimeRuler(&DC);
- if (m_MDC.m_hDC ==NULL)
- {
- AfxBeginThread(DrawThread,(LPVOID)this);
- }
- else
- DrawThread((LPVOID)this);
-
- if(m_selpoint!=-1)
- OnMouseMove( MK_LBUTTON, CPoint(m_selpoint,0) );
- }
- void CWaveViewCtrl::DrawTimeRuler(CPaintDC* PaintDC)
- {
- CFont font,*oldf;
- int sp=rect.Width ()/10;
- int w=sp-sp/2;
- font.CreateFont((int)(w*0.33),w/8,0,0,FW_BOLD,1,0,0,0,1,OUT_DEFAULT_PRECIS,PROOF_QUALITY,FF_ROMAN,NULL );
- oldf=PaintDC->SelectObject(&font);
-
- int ss=(m_pRiff->Size/m_pfch->awAvgBytesPerSec)/10;
- ss=ss==0?ss=1:ss;
-
- int sc=0;
- for(int i=0 ;i<=10;i++)
- {
- int r=sp*i;
- PaintDC->Rectangle(r,25,r+1,12);
- int t[3];
- t[1]=sc/60;
- t[2]=t[1]/60;
- t[1]=t[1]-(t[2]*60);
- t[0]=sc-(t[2]*60+t[1])*60;
- char str[]="