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

📄 armtestdlg.cpp

📁 自己写的一个基于eMbedded Visual C++的音频采集以及频谱分析小程序。 包括录音
💻 CPP
📖 第 1 页 / 共 2 页
字号:
void CArmtestDlg::OnMM_WIM_OPEN(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	SetTimer(1,100,NULL);
	bRecording=TRUE;


//	TRACE("MM_WIM_OPEN\n");
	
	
}


void CArmtestDlg::OnMM_WIM_DATA(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
	// Reallocate save buffer memory
	
	//////////////////////////////////////////////////////////////////////////
	
	pNewBuffer = (PBYTE)realloc (pSaveBuffer, dwDataLength +
		((PWAVEHDR) lParam)->dwBytesRecorded) ;
	
	if (pNewBuffer == NULL)
	{
		waveInClose (hWaveIn) ;
		MessageBeep (MB_ICONEXCLAMATION) ;
		AfxMessageBox(_T("erro memory"));
		return ;
	}
	
	pSaveBuffer = pNewBuffer ;
	//////////////////////////////////////////////////////////////////////////
	
	CopyMemory (pSaveBuffer + dwDataLength, ((PWAVEHDR) lParam)->lpData,
		((PWAVEHDR) lParam)->dwBytesRecorded) ;
	
	dwDataLength += ((PWAVEHDR) lParam)->dwBytesRecorded ;
	
	if (bEnding)
	{
		waveInClose (hWaveIn) ;
		return ;
	}






/////////////////
	//分配了一个中间数组temp和数组指针pAraay,用来连接FFT的接口
	
	float temp[NUMBER_OF_SAMPLES];
	int i=0;
	char * pArray = ((PWAVEHDR) lParam)->lpData; //////pSaveBuffer不可改动,所以另加指针指向缓冲区。
	for(i=1;i<NUMBER_OF_SAMPLES;i++)
	{
		temp[i-1]=(unsigned float)*(pArray++);  //这里要取无符号传递!!否则有翻卷现象
	}
	fft.ComplexFFT(temp,NUMBER_OF_SAMPLES,SAMPLE_RATE,1);

//////////////////
	//Graphic
	ShowWave(g_pDC,&g_Rect,((PWAVEHDR) lParam)->lpData,((PWAVEHDR) lParam)->dwBytesRecorded);
	//ShowSpectrum
	ShowSpectrum(s_pDC,&s_Rect);
	
	//这里前台输出FFT参数	
	CArmtestDlg::OnFftRate();
	CArmtestDlg::OnFreq();
	
	

		
	// Send out a new buffer
	
	waveInAddBuffer (hWaveIn, (PWAVEHDR) lParam, sizeof (WAVEHDR)) ;
	TRACE(_T("done input data\n"));

	return ;

	
}
void CArmtestDlg::OnMM_WIM_CLOSE(UINT wParam, LONG lParam) 
{
	// TODO: Add your message handler code here and/or call default
//	KillTimer(1);
	TRACE(_T("MM_WIM_CLOSE\n"));
	if (0==dwDataLength) 
	{
		return;
	}
	

	waveInUnprepareHeader (hWaveIn, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveInUnprepareHeader (hWaveIn, pWaveHdr2, sizeof (WAVEHDR)) ;
	
	free (pBuffer1) ;
	free (pBuffer2) ;
	
	if (dwDataLength > 0)
	{
		//enable play
		((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
		((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	}
	bRecording = FALSE ;
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	
	
	
	
	return ;
	
}


void CArmtestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	CDialog::OnClose();
	if (bRecording) 
	{
		bTerminating=TRUE;
		bEnding=TRUE;
		waveInReset(hWaveIn);
		TRACE(_T("waveInReset\n"));
		Sleep(500);
		//return CWinThread::ExitInstance();
	}
	if (bPlaying) 
	{
		bTerminating=TRUE;
		bEnding=TRUE;
		waveOutReset(hWaveOut);
		Sleep(500);
		//return CWinThread::ExitInstance();
	}
	free (pWaveHdr1) ;
	free (pWaveHdr2) ;
	free (pSaveBuffer) ;
	
}

void CArmtestDlg::OnPlayStart() 
{
	// TODO: Add your control notification handler code here
		if (bPlaying) 
	{
		waveOutReset(hWaveOut);
	}

	//open waveform audio for output
	waveform.wFormatTag		=	WAVE_FORMAT_PCM;
	waveform.nChannels		=	1;
	waveform.nSamplesPerSec	=	RATE;
	waveform.nAvgBytesPerSec=	RATE;
	waveform.nBlockAlign	=	1;
	waveform.wBitsPerSample	=	NBITS;
	waveform.cbSize			=	0;
	
	
	if (waveOutOpen(&hWaveOut,WAVE_MAPPER,&waveform,(DWORD)this->m_hWnd,NULL,CALLBACK_WINDOW)) 
	{
		MessageBeep(MB_ICONEXCLAMATION);
		AfxMessageBox(_T("Audio output erro"));
	}
	

	return ;
}



void CArmtestDlg::OnPlayPause() 
{
	// TODO: Add your control notification handler code here
		if (!bPlaying) 
	{
		return;
	}
	if (!bPaused) 
	{
		waveOutPause(hWaveOut);
		bPaused = TRUE;
	}
	else
	{
		waveOutRestart(hWaveOut);
		bPaused=FALSE;
	}	
	return ;
	
}



void CArmtestDlg::OnPlayStop() 
{
	// TODO: Add your control notification handler code here
		if (!bPlaying) {
		return ;
	}
	bEnding=TRUE;
	waveOutReset(hWaveOut);
	return ;

}



void CArmtestDlg::OnMM_WOM_OPEN(UINT wParam, LONG lParam)
{

	TRACE(_T("open MM_WOM_OPEN\n"));
	// Set up header
	
	pWaveHdr1->lpData          = (LPSTR)pSaveBuffer ;
	pWaveHdr1->dwBufferLength  = dwDataLength ;
	pWaveHdr1->dwBytesRecorded = 0 ;
	pWaveHdr1->dwUser          = 0 ;
	pWaveHdr1->dwFlags         = WHDR_BEGINLOOP | WHDR_ENDLOOP ;
	pWaveHdr1->dwLoops         = dwRepetitions ;
	pWaveHdr1->lpNext          = NULL ;
	pWaveHdr1->reserved        = 0 ;
	
	// Prepare and write
	
	waveOutPrepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutWrite (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	
	bEnding = FALSE ;
	bPlaying = TRUE ;
	
	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(TRUE);
	

	
	
}

void CArmtestDlg::OnMM_WOM_DONE(UINT wParam, LONG lParam){

	TRACE(_T("open MM_WOM_DONE\n"));
	waveOutUnprepareHeader (hWaveOut, pWaveHdr1, sizeof (WAVEHDR)) ;
	waveOutClose (hWaveOut) ;
	
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	
	
	return  ;
	
}
void CArmtestDlg::OnMM_WOM_CLOSE(UINT wParam, LONG lParam)
{
	TRACE(_T("open MM_WOM_CLOSE\n"));
	bPaused = FALSE ;
	dwRepetitions = 1 ;
	bPlaying = FALSE ;	

	((CWnd *)(this->GetDlgItem(IDC_REC_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_REC_STOP)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_START)))->EnableWindow(TRUE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_PAUSE)))->EnableWindow(FALSE);
	((CWnd *)(this->GetDlgItem(IDC_PLAY_STOP)))->EnableWindow(FALSE);
	return ;
}





void CArmtestDlg::OnChannel() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strCHANNEL.Format(_T("%.0f"),channel);
	UpdateData(false);
}

void CArmtestDlg::OnCollect() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strCOLLECT.Format(_T("%.0f"),collect);
	UpdateData(false);
}



void ShowWave(CDC *pDC,CRect *pRect,LPSTR pData,long len)
{
	int ox,oy;
	int a;
	long i,k;

	pDC->FillSolidRect(pRect,RGB(255,255,255));  

	ox=pRect->left;   ////CRect继承tagRECT
	oy=pRect->top+pRect->Height()/2;  ////Height()是窗口的高度??!!
	k=pRect->Width() > len ? len : pRect->Width(); 

	pDC->MoveTo(ox,oy); 
	for(i=0;i<k;i++)
	{
		a=(unsigned char)pData[i];  ////信号幅度
		float b=128-a;
		pDC->LineTo(ox+i,oy+b);  ////表示划线,线的起点是当前位置,终点为以x为横坐标,以y为纵坐标的点
	}	

}

void ShowSpectrum(CDC *pDC,CRect *pRect)
{	
	int ox,oy,x_temp,y_temp;
	pDC->FillSolidRect(pRect,RGB(255,255,255));

//	ox=pRect->left;   ////CRect继承tagRECT
//	oy=pRect->top+pRect->Height()/2;  ////Height()是窗口的高度??!!

	
	for(ox=0;ox<s_Rect.right;ox++)
	{
		////信号幅度
		x_temp=(ox*(SAMPLE_RATE/2))/s_Rect.right;       //采样率从/坐标长度
		y_temp=2*((int)((s_Rect.bottom*(pow(fft.vector[2*x_temp],2)+pow(fft.vector[2*x_temp+1],2)))/((double)pow(fft.vector[2*fft.fundamental_frequency],2)+pow(fft.vector[2*fft.fundamental_frequency+1],2))));
		pDC->MoveTo(ox,s_Rect.bottom);
		oy=s_Rect.bottom-y_temp;
		pDC->LineTo(ox,oy);
	}	
}

void CArmtestDlg::OnFftRate() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strFFT_RATE.Format(_T("%d"),SAMPLE_RATE);
	UpdateData(false);
}

void CArmtestDlg::OnFreq() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	m_strFREQ.Format(_T("%d"),fft.fundamental_frequency);
	UpdateData(false);
}

⌨️ 快捷键说明

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