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

📄 armtestdlg.cpp

📁 自己写的一个基于eMbedded Visual C++的音频采集以及频谱分析小程序。 包括录音
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// armtestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "armtest.h"
#include "armtestDlg.h"
#include "Fourier.h"
#include <math.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif







/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

	CFourier fft;  ///构造对象 fft
	
	float channel,collect;
	
//////////////////
	//Graphic

	//waveform
	CDC *g_pDC=NULL;
	CRect g_Rect;

	//spectrum
	CDC	*s_pDC=NULL;
	CRect s_Rect;
		

	





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

/////////////////////////////////////////////////////////////////////////////
// CArmtestDlg dialog

CArmtestDlg::CArmtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CArmtestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CArmtestDlg)
	m_strFFT_RATE = _T("");
	m_strCOLLECT = _T("");
	m_strFREQ = _T("");
	m_strCHANNEL = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CArmtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CArmtestDlg)
	DDX_Text(pDX, IDC_FFT_RATE, m_strFFT_RATE);
	DDX_Text(pDX, IDC_COLLECT, m_strCOLLECT);
	DDX_Text(pDX, IDC_FREQ, m_strFREQ);
	DDX_Text(pDX, IDC_CHANNEL, m_strCHANNEL);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CArmtestDlg, CDialog)
	//{{AFX_MSG_MAP(CArmtestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_REC_START, OnRecStart)
	ON_BN_CLICKED(IDC_REC_STOP, OnRecStop)
	ON_MESSAGE(MM_WIM_OPEN,OnMM_WIM_OPEN)
	ON_MESSAGE(MM_WIM_DATA,OnMM_WIM_DATA)
	ON_MESSAGE(MM_WIM_CLOSE,OnMM_WIM_CLOSE)
	ON_MESSAGE(MM_WOM_OPEN,OnMM_WOM_OPEN)
	ON_MESSAGE(MM_WOM_DONE,OnMM_WOM_DONE)
	ON_MESSAGE(MM_WOM_CLOSE,OnMM_WOM_CLOSE)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_PLAY_START, OnPlayStart)
	ON_BN_CLICKED(IDC_PLAY_PAUSE, OnPlayPause)
	ON_BN_CLICKED(IDC_PLAY_STOP, OnPlayStop)
	ON_BN_CLICKED(IDC_CHANNEL, OnChannel)
	ON_BN_CLICKED(IDC_COLLECT, OnCollect)
	ON_BN_CLICKED(IDC_FFT_RATE, OnFftRate)
	ON_BN_CLICKED(IDC_FREQ, OnFreq)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CArmtestDlg message handlers

BOOL CArmtestDlg::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

	//allocate memory for wave header
	pWaveHdr1=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	pWaveHdr2=reinterpret_cast<PWAVEHDR>(malloc(sizeof(WAVEHDR)));
	
	//allocate memory for save buffer
	pSaveBuffer = reinterpret_cast<PBYTE>(malloc(1));
	

////////////////
	//Graphic

	//waveform
	CWnd *pWnd=GetDlgItem(IDC_WAVEFORM);
	g_pDC=pWnd->GetDC();
	pWnd->GetWindowRect(&g_Rect);
	pWnd->ScreenToClient(&g_Rect);	

	//spectrum
	CWnd *pWnd2=GetDlgItem(IDC_SPECTRUM);
	s_pDC=pWnd2->GetDC();
	pWnd2->GetWindowRect(&s_Rect);
	pWnd2->ScreenToClient(&s_Rect);	




	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CArmtestDlg::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 CArmtestDlg::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 CArmtestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}



void CArmtestDlg::OnRecStart() 
{
	// TODO: Add your control notification handler code here
	
	// TODO: Add your control notification handler code here
	//allocate buffer memory  缓冲有两个,循环使用???
	pBuffer1=(PBYTE)malloc(INP_BUFFER_SIZE);
	pBuffer2=(PBYTE)malloc(INP_BUFFER_SIZE);

	if (!pBuffer1 || !pBuffer2) 
	if (!pBuffer1 ) 
	{
		if (pBuffer1) 
		
			free(pBuffer1);
		
		if (pBuffer2) 
		
		free(pBuffer2);
		
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("Memory error!"));
		return ;
	}
	
	//open waveform audio for input
	////设置录音格式
	
	waveform.wFormatTag      =	WAVE_FORMAT_PCM;		//数据格式,一般为WAVE_FORMAT_PCM即脉冲编码
	waveform.nChannels       =	1;						//声道
	waveform.nSamplesPerSec  =	RATE;					//采样率
	waveform.nAvgBytesPerSec =	RATE;					//每秒数据量
	waveform.nBlockAlign     =	1;						//
	waveform.wBitsPerSample  =	NBITS;						//采样位数
	waveform.cbSize			 =	0;



	//在这里将采集参数输出到前台

	channel =	waveform.nChannels;
	collect	=	waveform.nSamplesPerSec;
	CArmtestDlg::OnChannel();
	CArmtestDlg::OnCollect();



	////设置缓冲
	if (waveInOpen(&hWaveIn,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) 
	{
		free(pBuffer1);
		free(pBuffer2);
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("Audio can not be open!"));
	}
	pWaveHdr1->lpData			=	(LPSTR)pBuffer1;		//指向缓冲区的指针
	pWaveHdr1->dwBufferLength	=	INP_BUFFER_SIZE;		//缓冲区大小
	pWaveHdr1->dwBytesRecorded	=	0;						//录音时指明缓冲区中的数据量
	pWaveHdr1->dwUser			=	0;						//用户数据
	pWaveHdr1->dwFlags			=	0;						//缓冲区信息的标志
	pWaveHdr1->dwLoops			=	1;						//循环播放次数
	pWaveHdr1->lpNext			=	NULL;						
	pWaveHdr1->reserved			=	0;
	
	waveInPrepareHeader(hWaveIn,pWaveHdr1,sizeof(WAVEHDR));
	

	
	pWaveHdr2->lpData			=	(LPSTR)pBuffer2;
	pWaveHdr2->dwBufferLength	=	INP_BUFFER_SIZE;
	pWaveHdr2->dwBytesRecorded	=	0;
	pWaveHdr2->dwUser			=	0;
	pWaveHdr2->dwFlags			=	0;
	pWaveHdr2->dwLoops			=	1;
	pWaveHdr2->lpNext			=	NULL;
	pWaveHdr2->reserved			=	0;
	
	waveInPrepareHeader(hWaveIn,pWaveHdr2,sizeof(WAVEHDR));

	
	//////////////////////////////////////////////////////////////////////////
	pSaveBuffer = (PBYTE)realloc (pSaveBuffer, 1) ;


	
	// Add the buffers
	
	waveInAddBuffer (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveInAddBuffer (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
	
	// Begin sampling
	
	bRecording		= TRUE ;
	bEnding			= FALSE ;
	dwDataLength	= 0 ;
	waveInStart (hWaveIn) ;
}



void CArmtestDlg::OnRecStop() 
{
	// TODO: Add your control notification handler code here
		TRACE(_T("rec stop \n"));
	bEnding=TRUE;
	//Sleep(1500);

	
	waveInReset(hWaveIn);
}



⌨️ 快捷键说明

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