⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filterdlg.cpp

📁 能够读取wav格式的音频文件
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// filterDlg.cpp : implementation file
//

#include "stdafx.h"
#include "filter.h"
#include "filterDlg.h"
#include "complex.h"

#include <windef.h>
#include <math.h>
#include <mmsystem.h>
#include <afxwin.h>
#pragma comment (lib,"WINMM.LIB")
const double pi=3.1416;

#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()

/////////////////////////////////////////////////////////////////////////////
// CFilterDlg dialog

CFilterDlg::CFilterDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CFilterDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CFilterDlg)
	m_num1 = 0.0;
	m_num2 = 0.0;
	m_num3 = 0;
	m_file = _T("");
	m_filterkind = -1;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CFilterDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CFilterDlg)
	DDX_Text(pDX, IDC_EDIT1, m_num1);
	DDX_Text(pDX, IDC_EDIT2, m_num2);
	DDX_Text(pDX, IDC_EDIT3, m_num3);
	DDX_Text(pDX, IDC_EDIT4, m_file);
	DDX_Radio(pDX, IDC_RADIO1, m_filterkind);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CFilterDlg, CDialog)
	//{{AFX_MSG_MAP(CFilterDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO3, OnRadio3)
	ON_BN_CLICKED(IDC_RADIO4, OnRadio4)
	ON_BN_CLICKED(IDC_BTN_BROUSE, OnBtnBrouse)
	ON_BN_CLICKED(IDC_BTN_PRO, OnBtnPro)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CFilterDlg message handlers

BOOL CFilterDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.
	(CWnd *)(this->GetDlgItem (IDC_EDIT1))->EnableWindow(FALSE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT2))->EnableWindow(FALSE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT3))->EnableWindow(FALSE);

	(CWnd *)(this->GetDlgItem (IDC_COMBO1))->EnableWindow(FALSE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO2))->EnableWindow(FALSE);
	((CComboBox*)GetDlgItem(IDC_COMBO1))->SetCurSel(0);
	((CComboBox*)GetDlgItem(IDC_COMBO2))->SetCurSel(0);

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CFilterDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CFilterDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CFilterDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CFilterDlg::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	(CWnd *)(this->GetDlgItem (IDC_EDIT1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT2))->EnableWindow(FALSE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO2))->EnableWindow(TRUE);	
}

void CFilterDlg::OnRadio2() 
{
	// TODO: Add your control notification handler code here
	(CWnd *)(this->GetDlgItem (IDC_EDIT1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT2))->EnableWindow(FALSE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO2))->EnableWindow(TRUE);	
}

void CFilterDlg::OnRadio3() 
{
	// TODO: Add your control notification handler code here
	(CWnd *)(this->GetDlgItem (IDC_EDIT1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT2))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO2))->EnableWindow(TRUE);	
}

void CFilterDlg::OnRadio4() 
{
	// TODO: Add your control notification handler code here
	(CWnd *)(this->GetDlgItem (IDC_EDIT1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_EDIT2))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO1))->EnableWindow(TRUE);
	(CWnd *)(this->GetDlgItem (IDC_COMBO2))->EnableWindow(TRUE);	
}

void CFilterDlg::OnBtnBrouse() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	CFileDialog fdlg(TRUE);
	fdlg.DoModal();
	m_file=fdlg.GetPathName ();

	{
	CSignal thesignal;
	CFile file;
	CFileException e;
	long temp, buflong;
	short t;

	if(!file.Open(m_file, CFile::modeRead, &e))	//打开文件
	{
		MessageBox("cannot open file");
		exit(-2);;
	}

	file.Read (&temp, sizeof(long));		//读取标志'RIFF'
	if(temp != 0x46464952)//'RIFF'
	{
		MessageBox("not a riff file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//文件长度
	buflong = temp;
	if((DWORD)(buflong + 8) != file.GetLength ())
	{
		MessageBox("file length wrong");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取标志'WAVE'
	if(temp != 0x45564157)//'WAVE'
	{
		MessageBox("not a .wav file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取标志'FMT '
	if(temp != 0x20746d66)//'FMT '
	{
		MessageBox("not a fmt file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取FMT 长度

	file.Read (&t, 2);						//读取Format Tag
	if(t != WAVE_FORMAT_PCM)//format tag
	{
		MessageBox("something wrong");
		exit(-2);;
	}

	thesignal.wFormatTag = t;									//Format Tag
	file.Read (&(thesignal.nChannels), 2);					//channel number,通道数
	file.Read (&(thesignal.nSamplesPerSec), 4);				//sample per sec, 抽样率
	this->m_num3=thesignal.nSamplesPerSec;
	}
	UpdateData(FALSE);
	return;	
}

void CFilterDlg::OnBtnPro() 
{
	// TODO: Add your control notification handler code here
	UpdateData(TRUE);
	//取得运算点数信息

	int index;
	Cnumber thenum;
	index=((CComboBox *)GetDlgItem(IDC_COMBO1))->GetCurSel();
	switch(index)
	{
	case 0:
		thenum.filternum=32;
		thenum.fftnum=64;
		thenum.signalnum=32;
		break;
	case 1:
		thenum.filternum=64;
		thenum.signalnum=64;
		thenum.fftnum=128;
		break;
	case 2:
		thenum.filternum=128;
		thenum.signalnum=128;
		thenum.fftnum=256;
		break;
	case 3:
		thenum.filternum=257;
		thenum.signalnum=255;
		thenum.fftnum=512;
		break;
	case 4:
		thenum.filternum=512;
		thenum.signalnum=512;
		thenum.fftnum=1024;
		break;
	default:
		MessageBox("something wrong");
		return;
		break;
	}
	//取得运算点数完毕,存入thenum


	//取得音频文件及相关信息
	CSignal thesignal;
	getsignal(thesignal,thenum);
	//取得音频文件及相关信息完毕
	


	//取得理想滤波器类型并生成
	//const double pi=3.1416;
	double *wfil=new double[thenum.filternum];
	switch(m_filterkind)
	{
	case 0:
		wlowpass(wfil,thenum.filternum,2*pi*m_num1/thesignal.nSamplesPerSec);
		break;
	case 1:
		whighpass(wfil,thenum.filternum,2*pi*m_num1/thesignal.nSamplesPerSec);
		break;
	case 2:
		wbandpass(wfil,thenum.filternum,2*pi*m_num1/thesignal.nSamplesPerSec,2*pi*m_num2/thesignal.nSamplesPerSec);
		break;
	case 3:
		wbandstop(wfil,thenum.filternum,2*pi*m_num1/thesignal.nSamplesPerSec,2*pi*m_num2/thesignal.nSamplesPerSec);
		break;
	default:
		break;
	}
	//取得理想滤波器类型并生成完毕,存入wfil

	//取得窗体类型并生成
	int filterkind;
	filterkind=((CComboBox *)GetDlgItem(IDC_COMBO2))->GetCurSel();
	double *wwin=new double[thenum.filternum];
	wwindow(wwin,thenum.filternum,filterkind);
	//取得窗体类型并生成完毕,存入wwin

	//生成加窗后的滤波器
	Ccomplex *thefilter=new Ccomplex[thenum.fftnum];
	{
		UINT i;
		for(i=0;i<thenum.fftnum;i++)
			thefilter[i].set(0,0);
	}
	UINT FillterCreatCounter;
	for(FillterCreatCounter=0;FillterCreatCounter<thenum.filternum;FillterCreatCounter++)
		thefilter[FillterCreatCounter].set(wfil[FillterCreatCounter]*wwin[FillterCreatCounter],0);
	delete [] wwin;
	delete [] wfil;		//删除wfil和wwin
	//生成加窗后的滤波器,存入thefilter

	//信号通过滤波器
	processing(thesignal,thefilter,thenum);
	//信号通过滤波器完毕

	this->DrawSignal(thesignal);
	//写回信号完毕
	//exit(0);
	delete []thefilter;
}


void CFilterDlg::getsignal(CSignal & thesignal, Cnumber &thenum)
{
	CFile file;
	CFileException e;
	long temp, buflong;
	short t;

	if(!file.Open(m_file, CFile::modeRead, &e))	//打开文件
	{
		MessageBox("cannot open file");
		exit(-2);;
	}

	file.Read (&temp, sizeof(long));		//读取标志'RIFF'
	if(temp != 0x46464952)//'RIFF'
	{
		MessageBox("not a riff file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//文件长度
	buflong = temp;
	if((DWORD)(buflong + 8) != file.GetLength ())
	{
		MessageBox("file length wrong");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取标志'WAVE'
	if(temp != 0x45564157)//'WAVE'
	{
		MessageBox("not a .wav file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取标志'FMT '
	if(temp != 0x20746d66)//'FMT '
	{
		MessageBox("not a fmt file");
		exit(-2);;
	}

	file.Read (&temp, 4);					//读取FMT 长度

	file.Read (&t, 2);						//读取Format Tag
	if(t != WAVE_FORMAT_PCM)//format tag
	{
		MessageBox("something wrong");
		exit(-2);;
	}

	thesignal.wFormatTag = t;									//Format Tag
	file.Read (&(thesignal.nChannels), 2);					//channel number,通道数
	file.Read (&(thesignal.nSamplesPerSec), 4);				//sample per sec, 抽样率		
	file.Read (&(thesignal.nAvgBytesPerSec), 4);				//bytes per sec, 字节/秒
	file.Read (&(thesignal.nBlockAlign), 2);					//block align,
	file.Read (&(thesignal.wBitsPerSample), 2);				//bits per sample, 采样位数

	if(temp == 18)
	{
		file.Read (&(thesignal.cbSize), 2);
	}
	if(temp > 18)
	{
		file.Seek(temp - 18, CFile::current);
	}

	file.Read (&temp, 4);					//读取标志'data'
	if(temp != 0x61746164)//'data'
	{
		MessageBox("not a data file");
		exit(-2);;
	}


	file.Read (&temp, 4);					//读取数据的长度

	thesignal.dwDatalength = temp;

	thesignal.m_pSavebuffer=new BYTE[thesignal.dwDatalength+thenum.fftnum];
	thesignal.m_pAfter=new BYTE[thesignal.dwDatalength+thenum.fftnum];
	file.Read (thesignal.m_pSavebuffer,thesignal.dwDatalength);
	
	DWORD counter;
	for(counter=0;counter<thesignal.dwDatalength;counter++)
		thesignal.m_pAfter[counter]=0;

	file.Close ();
	thesignal.m_pcurbuffer = thesignal.m_pSavebuffer ;
	this->setkind(thesignal);
	return;



}


//fft
void CFilterDlg::fft(Ccomplex a[], UINT l)
{
 Ccomplex u,w,t;
 UINT n=l;
 UINT nv2,nm1,k,le,lei,ip;
 UINT i,j,m;
 double tmp;

 // 倒位序
 nv2=n/2;
 nm1=n-1;
 j=0;
 for(i=0;i<nm1;i++)
	{if(i<j)
		{t=a[j];
		 a[j]=a[i];
		 a[i]=t;
		}
	 k=nv2;
	 while(k<=j)
		{j-=k;
		 k=k/2;
		}
	 j+=k;
	}
  //递推计算
 double R=log10(l)/log10(2)+0.1;
 for(m=1;m<=R;m++)
	{le=(UINT)pow(2,m);
	 lei=le/2;
	 
	 u.set(1,0);
	 tmp=pi/lei;
	 w.set(cos(tmp),-sin(tmp));

	 for(j=0;j<lei;j++)
		{for(i=j;i<n;i+=le)
			{ip=i+lei;
			 t=a[ip]*u;
			 a[ip]=a[i]-t;
			 a[i]=a[i]+t;
			}
		 u=u*w;
		}
	 }
 return;
}


//ifft
void CFilterDlg::ifft(Ccomplex a[], UINT l)
{
 Ccomplex u,w,t;
 UINT n=l;
 UINT nv2,nm1,k,le,lei,ip;
 UINT i,j=0,m;
 double tmp;

 //倒位序
 nv2=n/2;
 nm1=n-1;
 j=0;
 for(i=0;i<nm1;i++)
	{if(i<j)
		{t=a[j];
		 a[j]=a[i];
		 a[i]=t;
		}
	 k=nv2;
	 while(k<=j)
		{j-=k;
		 k=k/2;
		}
	 j+=k;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -