// ************定义全局变量*************
#define NUM_SAMPLES 1024
HWAVEIN hWaveIn;
WAVEFORMATEX waveform;
short g_samples[NUM_SAMPLES];
//位深=16,每个采样点两个字节
WAVEHDR waveHeader;
int zong = 0;
CWnd* pWnd; //picture控件句柄
HWND hWnd;
HDC hdc;
HDC hdcmen;//内存DC
HPEN hpen;
HPEN oldhpen;
RECT rect;
HBRUSH hbr;
HBRUSH oldhbr;
HANDLE mutex;//互斥锁句柄
int beisu ;//纵向缩小倍数
int beisu1;//横向放大倍数倍数
int nReturn;
WAVEINCAPS* pwic;
int suoyin = 0;//麦克风ID
int width = 0, height = 0;
CRect rectPicture;//picture控件客户区坐标
// TODO: 在此添加额外的初始化代码
// 设置音频格式
waveform.wFormatTag = WAVE_FORMAT_PCM;
waveform.nChannels = 1; // 声道数
waveform.nSamplesPerSec = 44100; // 采样频率
waveform.wBitsPerSample = 16; // 位深
waveform.nBlockAlign = waveform.nChannels * (waveform.wBitsPerSample / 8);
waveform.nAvgBytesPerSec = waveform.nSamplesPerSec * waveform.nBlockAlign;
waveform.cbSize = 0;
//创建并准备缓冲区
waveHeader.lpData = (LPSTR)g_samples;
waveHeader.dwBufferLength = NUM_SAMPLES * sizeof(short);
waveHeader.dwBytesRecorded = 0;
waveHeader.dwUser = 0;
waveHeader.dwFlags = 0;
waveHeader.dwLoops = 0;
pWnd = GetDlgItem(IDC_SOUND); //获取picture控件句柄
hWnd = ::GetDlgItem(GetSafeHwnd(), IDC_SOUND);
hdc = ::GetDC(hWnd);
hdcmen = ::CreateCompatibleDC(hdc);//内存DC
hpen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));//创建画笔
oldhpen= (HPEN)SelectObject(hdcmen, hpen);
::GetClientRect(hWnd, &rect);//获取picture控件客户区的坐标
hbr = CreateSolidBrush(RGB(255, 255, 255));
oldhbr =(HBRUSH)SelectObject(hdcmen, hbr);
FillRect(hdcmen, &rect, hbr);//绘制并填充矩形
myspin.SetRange32(0, 1000); //调节的范围
myspin.SetBase(10); //十进制
myspin.SetBuddy(GetDlgItem(IDC_EDIT1)); //设置关联的编辑框
myspin.SetPos(200);//设置当前值
beisu = 200;
beisu1 = 6;
int nReturn = waveInGetNumDevs();//获取系统中存在的波形音频输入设备数
if (nReturn==0) {
AfxMessageBox(_T("请插入麦克风!"));
return 0;
}
pwic = new WAVEINCAPS[nReturn];
for (int i = 0; i < nReturn; i++) {
MMRESULT mmresult = waveInGetDevCaps(i, (pwic+i), sizeof(WAVEINCAPS)); //获取给定的波形音频输入设备的信息
m_cmbRec.AddString(pwic->szPname);
//m_cmbRec下拉列表框变量名
}
m_cmbRec.SetCurSel(0);
suoyin= m_cmbRec.GetCurSel();//获取选中项的索引号
pWnd->GetClientRect(&rectPicture);//获取picture控件客户区坐标
width = rectPicture.Width();
height = rectPicture.Height();
//代码
void CMFCyinpinDlg::OnBnClickedBtnStart()
{
//MMRESULT mm=waveInOpen(&hWaveIn, WAVE_MAPPER, &waveform, (DWORD)this->m_hWnd, NULL, CALLBACK_WINDOW);//打开录音设备
MMRESULT mm = waveInOpen(&hWaveIn, suoyin, &waveform, (DWORD)this->m_hWnd, NULL, CALLBACK_WINDOW);//打开录音设备
if (mm != MMSYSERR_NOERROR) {
AfxMessageBox(_T("打开失败"));
return;
}
waveInPrepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));//准备缓冲区
waveInAddBuffer(hWaveIn, &waveHeader, sizeof(WAVEHDR));// 添加缓冲区到音频输入设备
waveInStart(hWaveIn);//启动输入设备
}
void CMFCyinpinDlg::OnBnClickedBtnStop()
{
waveInReset(hWaveIn);//停止录音设备
waveInUnprepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));//去除缓冲区的准备
ReleaseMutex(mutex);//释放线程控制权
}
UINT func1(LPVOID pParam) //线程函数
{
FillRect(hdcmen, &rect, hbr);//绘制并填充矩形
int t = 1;//第一个点不能画直线
short* g = (short*)pParam;
DWORD d = WaitForSingleObject(mutex, INFINITE);
for (int i = 0; i < zong / sizeof(short); i++) {
if (t == 1) {
//移到起始点
::MoveToEx(hdcmen, i * beisu1, *(g + i) / beisu + 150, NULL);
t = 0;
}
else {
LineTo(hdcmen, i* beisu1, *(g + i)/beisu+150);//绘制直线
}
}
BOOL b =BitBlt(hdc, 0, 0, width, height, hdcmen, 0, 0, SRCCOPY);//把源DC拷贝到目标DC
return 0;
}
afx_msg LRESULT CMFCyinpinDlg::OnMM_WIM_DATA(WPARAM wParam, LPARAM lParam)
{
WAVEHDR* pWaveHdr = (WAVEHDR*)lParam;
memcpy(g_samples, pWaveHdr->lpData, pWaveHdr->dwBytesRecorded);//复制音频数据
zong = pWaveHdr->dwBytesRecorded;
CWinThread* pp = AfxBeginThread(func1, (short*)(g_samples), 0, 0, 0, NULL);//创建工作线程
waveInAddBuffer(hWaveIn, pWaveHdr, sizeof(WAVEHDR)); // 继续录制下一段音频数据
return 0;
}
afx_msg LRESULT CMFCyinpinDlg::OnMM_WIM_CLOSE(WPARAM wParam, LPARAM lParam)
{
return 0;
}
void CMFCyinpinDlg::OnDeltaposSpin1(NMHDR* pNMHDR, LRESULT* pResult)
{
LPNMUPDOWN pNMUpDown = reinterpret_cast<LPNMUPDOWN>(pNMHDR);
// TODO: 在此添加控件通知处理程序代码
beisu = myspin.GetPos(); //获取当前值
*pResult = 0;
}