// cbhechoDlg.cpp : implementation file // #include "stdafx.h" #include "cbhecho.h" #include "cbhechoDlg.h" #include "math.h" #include "findperi.h" #include "realfft.h" #ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif ///////////////////////////////////////////////////////////////////////////// // CAboutDlg dialog used for App About class CAboutDlg : public CDialog { public: CAboutDlg(); // Dialog Data //{{AFX_DATA(CAboutDlg) enum { IDD = IDD_ABOUTBOX }; //}}AFX_DATA // ClassWizard generated virtual function overrides //{{AFX_VIRTUAL(CAboutDlg) protected: virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support //}}AFX_VIRTUAL // Implementation protected: //{{AFX_MSG(CAboutDlg) //}}AFX_MSG DECLARE_MESSAGE_MAP() }; CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD) { //{{AFX_DATA_INIT(CAboutDlg) //}}AFX_DATA_INIT } void CAboutDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CAboutDlg) //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CAboutDlg, CDialog) //{{AFX_MSG_MAP(CAboutDlg) // No message handlers //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCbhechoDlg dialog CCbhechoDlg::CCbhechoDlg(CWnd* pParent /*=NULL*/) : CDialog(CCbhechoDlg::IDD, pParent) { //{{AFX_DATA_INIT(CCbhechoDlg) m_noiselevel = _T(""); m_staticfreq = _T(""); //}}AFX_DATA_INIT // Note that LoadIcon does not require a subsequent DestroyIcon in Win32 m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME); } void CCbhechoDlg::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CCbhechoDlg) DDX_Control(pDX, IDC_SCROLLBAR2, m_freqscroll); DDX_Control(pDX, IDC_SCROLLBAR1, m_scrollbar1); DDX_Text(pDX, IDC_STATICNOISE, m_noiselevel); DDX_Text(pDX, IDC_STATICFREQ, m_staticfreq); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CCbhechoDlg, CDialog) //{{AFX_MSG_MAP(CCbhechoDlg) ON_WM_SYSCOMMAND() ON_WM_PAINT() ON_WM_QUERYDRAGICON() ON_WM_DESTROY() ON_BN_CLICKED(IDC_BUTTONBEGIN, OnButtonbegin) ON_BN_CLICKED(IDC_BUTTONREPLAY, OnButtonreplay) ON_BN_CLICKED(IDC_BUTTONSTOP, OnButtonstop) ON_WM_HSCROLL() ON_WM_VSCROLL() ON_BN_CLICKED(IDC_BUTTONINT, OnButtonint) ON_BN_CLICKED(IDC_BUTTONACU, OnButtonacu) ON_BN_CLICKED(IDC_BUTTONSAVE, OnButtonsave) ON_BN_CLICKED(IDC_BUTTONREAD, OnButtonread) //}}AFX_MSG_MAP ON_MESSAGE(MM_WIM_DATA, OnRecord) END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CCbhechoDlg message handlers BOOL CCbhechoDlg::OnInitDialog() { CDialog::OnInitDialog(); // Add "About..." menu item to system menu. // IDM_ABOUTBOX must be in the system command range. ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX); ASSERT(IDM_ABOUTBOX < 0xF000); CMenu* pSysMenu = GetSystemMenu(FALSE); if (pSysMenu != NULL) { CString strAboutMenu; strAboutMenu.LoadString(IDS_ABOUTBOX); if (!strAboutMenu.IsEmpty()) { pSysMenu->AppendMenu(MF_SEPARATOR); pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu); } } // Set the icon for this dialog. The framework does this automatically // when the application's main window is not a dialog SetIcon(m_hIcon, TRUE); // Set big icon SetIcon(m_hIcon, FALSE); // Set small icon // TODO: Add extra initialization here record = new cbhrecord; analysis = new cbhanalysis; m_scrollbar1.SetScrollRange(0,512); m_freqscroll.SetScrollRange(0,999); wave = new double[8192]; wavesize = 1000; for(int i=0;i<1000;i++) wave[i]=i; currentpos=0; currentfreq=0; peaknumber = 8; peaks = new double[100]; for(i=0; ieffect){ CRect rect; GetClientRect(&rect); CRect rect1(rect.left, rect.top+rect.Height()/2, rect.left-10+rect.Width()/2, rect.bottom); CRect rect2(rect.left+rect.Width()/2+10, rect.top+rect.Height()/2, rect.right, rect.bottom); /* double buf[128]; for(int i=0; i<129; i++) buf[i] = sin(6.28*i/64); */ CPaintDC dc(this); // device context for painting // drawcurve(dc, rect1, buf, 128); // drawcurve(dc, rect2, buf, 128); drawcurve(dc, rect1, wave, 1000, currentfreq, peaknumber, peaks); drawcurve(dc, rect2, analysis->fbuffer, analysis->tmsize, currentpos); } } } // The system calls this to obtain the cursor to display while the user drags // the minimized window. HCURSOR CCbhechoDlg::OnQueryDragIcon() { return (HCURSOR) m_hIcon; } void CCbhechoDlg::OnRecord(WPARAM wp, LPARAM lp) { if(havestoped) return; if(record->resycle(lp)) { // makewave(record->tbuffer, record->rsize); myplaysound(record->tbuffer, record->rsize); analysis->calculate(record->tbuffer, record->rsize); dofft(0); // Invalidate(); // PlaySound("my.wav",NULL,SND_FILENAME); // record->start(m_hWnd); havestoped = 1; return; } m_noiselevel.Format("声音电平:%f",record->noiselevel); UpdateData(FALSE); } void CCbhechoDlg::OnDestroy() { CDialog::OnDestroy(); // TODO: Add your message handler code here delete record; delete analysis; delete []wave; delete []peaks; } void CCbhechoDlg::OnButtonbegin() { // TODO: Add your control notification handler code here if(!havestoped) return; havestoped = 0; record->start(m_hWnd); } void CCbhechoDlg::OnButtonreplay() { // TODO: Add your control notification handler code here if(!havestoped) return; if(!record->rsize) return; myplaysound(record->tbuffer, record->rsize); /* int i; for(i=0;i<4096*64;i++) { record->tbuffer[i] = (unsigned int)(127*sin(6.28*1000.0*(1/44100.0)*i))+128; } for(i=4096*64;i<4096*128;i++) { record->tbuffer[i] = (unsigned int)(127*sin(6.28*500.0*(1/44100.0)*i))+128; } record->rsize = record->tbsize = 4096*128; analysis->calculate(record->tbuffer,record->rsize); m_noiselevel.Format("numberis:%d,%f",analysis->tmsize,analysis->fbuffer[3]); UpdateData(FALSE); analysis->effect = 1; Invalidate();*/ } void CCbhechoDlg::OnButtonstop() { // TODO: Add your control notification handler code here record->stop(); } void CCbhechoDlg::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // TODO: Add your message handler code here and/or call default if(pScrollBar->GetDlgCtrlID()==IDC_SCROLLBAR1) { int nCurrentPos=pScrollBar->GetScrollPos(); int ii=nCurrentPos; int j=0; switch(nSBCode) { case SB_THUMBPOSITION: pScrollBar->SetScrollPos(nPos); ii=nPos; j=1; break; case SB_LINERIGHT: pScrollBar->SetScrollPos(nCurrentPos+1); ii=nCurrentPos+1; j=1; break; case SB_LINELEFT: pScrollBar->SetScrollPos(nCurrentPos-1); ii=nCurrentPos-1; j=1; break; } if(j==1){ if(ii<0)ii=0; if(ii>508)ii=508; dofft(ii); } } CDialog::OnHScroll(nSBCode, nPos, pScrollBar); } void CCbhechoDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar) { // TODO: Add your message handler code here and/or call default if(pScrollBar->GetDlgCtrlID()==IDC_SCROLLBAR2) { int nCurrentPos=pScrollBar->GetScrollPos(); int ii=nCurrentPos; int j=0; switch(nSBCode) { case SB_THUMBPOSITION: pScrollBar->SetScrollPos(nPos); ii=nPos; j=1; break; case SB_LINEUP: pScrollBar->SetScrollPos(nCurrentPos-1); ii=nCurrentPos-1; j=1; break; case SB_LINEDOWN: pScrollBar->SetScrollPos(nCurrentPos+1); ii=nCurrentPos+1; j=1; break; } if(j==1){ currentfreq = ii; double a; a = peakvalue(wave, ii); m_staticfreq.Format("频率:%d, 值:%f, 峰度:%f", ii,wave[ii],a); UpdateData(FALSE); Invalidate(); } } CDialog::OnVScroll(nSBCode, nPos, pScrollBar); } void CCbhechoDlg::dofft(int ii) { currentpos=ii; double * pr; int n,k,i; pr = new double[16384]; for(i=0; i<16384; i++) pr[i]=0; for(i=0; i<4096; i++) pr[i]=(double)(record->tbuffer[i+ii*1024]-128); realfftm(pr,16384); peaknumber = findpeaks(pr, peaks); double smax = 0; k=0; for(i=0;i<8192;i++){ wave[i]=pr[i]; if(smax=40) { smax+=wave[k-1]*wave[k-1]+wave[k+1]*wave[k+1]; smax/=3; for(i=0;i<=1000;i++) s+=wave[i]*wave[i]; s/=1000; smax/=s; } else smax=0; double freq; freq = getfreq(peaknumber, peaks); m_noiselevel.Format("%d,%d,%d,%f,测得频率:%f",ii,analysis->tmsize,analysis->noise[ii],smax,freq); analysis->effect = 1; delete []pr; double a; a = peakvalue(wave, ii); m_staticfreq.Format("频率:%d, 值:%f, 峰度:%f", ii,wave[ii],a); UpdateData(FALSE); Invalidate(); } void CCbhechoDlg::OnButtonint() { // TODO: Add your control notification handler code here for(int i=0; itmsize; i++) analysis->fbuffer[i]=(double)(int)(analysis->fbuffer[i]); Invalidate(); } void CCbhechoDlg::OnButtonacu() { // TODO: Add your control notification handler code here if(currentpos > analysis->tmsize) return; searchfreq(wave, analysis->fbuffer[currentpos]); Invalidate(); } void CCbhechoDlg::OnButtonsave() { // TODO: Add your control notification handler code here static char szFilter[]="Chart Files (*.dat)|*.dat|All Files (*.*)|*.*||"; CFileDialog dlg(FALSE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL); if(dlg.DoModal()!=IDOK) return; CFile f(dlg.GetPathName(),CFile::modeCreate | CFile::modeWrite); // CFile f("d:\\temp.dat",CFile::modeCreate | CFile::modeWrite); // f.Write(wave,1000*sizeof(double)); f.Write(record->tbuffer, record->rsize); f.Close(); } void CCbhechoDlg::OnButtonread() { // TODO: Add your control notification handler code here static char szFilter[]="Chart Files (*.dat)|*.dat|All Files (*.*)|*.*||"; CFileDialog dlg(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, NULL); if(dlg.DoModal()!=IDOK) return; CFileStatus status; if(CFile::GetStatus(dlg.GetPathName(), status)){ CFile f(dlg.GetPathName(), CFile::modeRead); if(status.m_sizetbsize) record->rsize = status.m_size; else record->rsize = record->tbsize; f.Read(record->tbuffer, record->rsize); f.Close(); myplaysound(record->tbuffer, record->rsize); analysis->calculate(record->tbuffer, record->rsize); dofft(0); } }