22.2.4 数字录音机——WaveInXXX波形输入设备函数的使用
(1)录音常用的API
①waveInOpen(打开一个音频输入设备)
②waveInPrepareHeader(为一个即将在waveInAddBuffer中调用的输入缓冲区准备头部)
③waveInAddBuffer(添加一个输入用的数据缓冲区)
④waveInStart(开始录音)
⑤waveInReset(重置输入设备以便重新录音或关闭录音)
⑥waveInClose(关闭音频输入设备)等几个。
(2)windows waveform方式实现录音要通过这么几步:(注意顺序!!)
①打开录音设备——waveInOpen()函数
注意,调用之前要填写好wav头信息(包含采样率、采样位数等);还要设置好接收录音消息的窗口句柄。
②准备好录音缓存空间——waveInPrepareHeader()函数
这一步为了准备好将要送入录音设备的缓存,以便之后可以供之使用。一般至少需要准备两块缓存。因为录音不能间断,当一块填满时没有时间等待你去送入下一块缓存,所以必须提前就准备好。(注意这两个缓存使用的先后顺序遵循FIFO原则)
③将缓存送入录音设备——waveInAddBuffer()函数
将缓存送入录音设备,供之存入已录下的音频。开始录音时,应至少送入两块不同的缓存,即调用两次这个函数。之后为了不致录音产生间断,应保证至少有一块缓存在录音时为空,以备衔接。
④开始录音——waveInStart()函数
当以上的工作都准备好时,便可调用此函数开始录音了。一旦调用,录音设备便立即录音并存入已经送来的缓存块内,当被送来的有多个缓存块时,按照FIFO的原则向缓存块内存入录音数据。如果是在利用回调函数来接收录音消息时,此函数执行之后可以执行一个while()循环,来等待录音设备录音。为了减少CPU使用率,可以在循环中加人sleepex(x,TRUE),x单位ms,TRUE必须要有。每个缓冲块存满时,将发送一个MM_WIM_DATA消息。
⑤停止录音,关闭设备——依次调用以下这些函数,来结束录音。
waveInStop: 停止录音
waveInReset:复位。 //会发送MM_WIM_DATA消息
waveInClose:关闭设备(想结束录音时,最好在这函数前调用一下waveInReset)
(3)录音时的消息通知
①MM_WIM_OPEN
当执行waveInOpen函数时,并产生这个消息信号。代表录音设备已经打开。在这里可以自己设定一些操作,也可以没有操作。
②MM_WIM_DATA
当每块缓存块填满时或调用waveInReset函数时,产生这个消息,在这个消息中,应当完成这样的工作,以便录音连续进行:
A、将存满的缓存块处理,例如存入文件,或送入其他设备;
B、向录音设备送入新的缓存块;录音设备任何时刻应当拥有不少于2个的缓存块,以保证录音不间断性。
③MM_WIM_CLOSE
当调用waveInclose函数时,会产生这个消息,代表录音设备关闭成功。这次回调函数调用中,可以执行相应的一些关闭文件保存信息等等的操作,自定义。
【Record1程序】
/*---------------------------------------------------- RECORD1.C —— Waveform Audio Recorder (c) Charles Petzold,1998 ----------------------------------------------------*/ #include <Windows.h> #include "resource.h" #pragma comment(lib,"WINMM.LIB") #define INP_BUFFER_SIZE 16384 //16K,即16*1024 BOOL CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM); TCHAR szAppName[] = TEXT("Record1"); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInStance, PSTR szCmdLine, int iCmdShow) { if (-1 == DialogBox(hInstance,TEXT("Record"),NULL,DlgProc)) { MessageBox(NULL, TEXT("This program requires Windows NT!"), szAppName, MB_ICONERROR); } return 0; } void ReverseMemory(BYTE* pBuffer, int iLength) { BYTE b; int i; for (i = 0; i <iLength /2; i++) { b = pBuffer[i]; pBuffer[i] = pBuffer[iLength - 1 - i]; pBuffer[iLength - 1 - i] = b; } } BOOL CALLBACK DlgProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) { RECT rect; static HWAVEIN hWaveIn; static HWAVEOUT hWaveOut; static PWAVEHDR pWaveHdr1, pWaveHdr2; static PBYTE pBuffer1, pBuffer2, pSaveBuffer,pNewBuffer; static TCHAR szMemError[] = TEXT("Error allocating memory!"); static TCHAR szOpenError[] = TEXT("Error opening waveform audio!"); static WAVEFORMATEX waveform; static BOOL bRecording, bPlaying, bReverse, bPaused,bEnding,bTerminating; static DWORD dwDataLength, dwRepetitions = 1; switch (message) { case WM_INITDIALOG: GetWindowRect(hwnd, &rect); SetWindowPos(hwnd, NULL, (GetSystemMetrics(SM_CXSCREEN) - rect.right + rect.left) / 2, (GetSystemMetrics(SM_CYSCREEN) - rect.bottom + rect.top) / 2, rect.right- rect.left,rect.bottom-rect.top,SWP_SHOWWINDOW); //分配两个波形头结构 pWaveHdr1 = malloc(sizeof(WAVEHDR)); //WaveIn(Out)PrepareHeader、WaveInAddBuffe等函数的参数 pWaveHdr2 = malloc(sizeof(WAVEHDR)); //分配“保存缓冲区”(用来保存声音数据的缓冲区) pSaveBuffer = malloc(1); //1个字节,以后可根据需要录制的声音长度重新realloc来改变缓冲区的大小 return TRUE; case WM_COMMAND: switch (LOWORD(wParam)) { /*-----------------------------录制按钮过程--------------------------------------*/ case IDC_RECORD_BEG: //分配16K的声音数据缓冲区 //一般至少需要准备两块缓存。因为录音不能间断,当一块填满时没有时间等待你去送入下一块缓存,所以必须提前就准备好 pBuffer1 = malloc(INP_BUFFER_SIZE); pBuffer2 = malloc(INP_BUFFER_SIZE); if (!pBuffer1 || !pBuffer2) { if (pBuffer1) free(pBuffer1); if (pBuffer2) free(pBuffer2); MessageBeep(MB_ICONEXCLAMATION); MessageBox(hwnd, szMemError, szAppName, MB_ICONEXCLAMATION | MB_OK); return TRUE; } //打开波形输入设备 waveform.wFormatTag = WAVE_FORMAT_PCM; waveform.nChannels = 1; waveform.nSamplesPerSec = 11025; //采样率:11025Hz waveform.nAvgBytesPerSec = 11025; //每秒数据传输的字节数=nBlockAlign*nSamplesPerSec waveform.nBlockAlign = 1; //块对齐 = nChannels*wBitsPerSample/8(字节) waveform.wBitsPerSample = 8; //采样大小,即声音的振幅(强度) waveform.cbSize = 0; //PCM,必须为0 if (MMSYSERR_NOERROR !=waveInOpen(&hWaveIn, WAVE_MAPPER, &waveform, (DWORD)hwnd, 0,CALLBACK_WINDOW)) //发送MM_WIM_OPEN消息 { free(pBuffer1); free(pBuffer2); MessageBeep(MB_ICONEXCLAMATION); MessageBox(hwnd, szOpenError, szAppName, MB_ICONEXCLAMATION | MB_OK); return TRUE; } //创建并准备波形头结构 pWaveHdr1->lpData = pBuffer1; pWaveHdr1->dwBufferLength = INP_BUFFER_SIZE; //缓冲区的大小,当该数据超过该值是会发送MM_WIM_DATA消息 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 = pBuffer2; pWaveHdr2->dwBufferLength = INP_BUFFER_SIZE; //缓冲区的大小,当该数据超过该值是会发送MM_WIM_DATA消息 pWaveHdr2->dwBytesRecorded = 0; //己经录制的字节数 pWaveHdr2->dwUser = 0; pWaveHdr2->dwFlags = 0; pWaveHdr2->dwLoops = 1; pWaveHdr2->lpNext = NULL; //次数 pWaveHdr2->reserved = 0; waveInPrepareHeader(hWaveIn, pWaveHdr2, sizeof(WAVEHDR)); return TRUE; /*---------------------------录制结束按钮过程------------------------------------*/ case IDC_RECORD_END: //重置波形输入设备,所有等待录音的缓冲区标志为己完成,当前位置设为第0个缓冲区 //即清掉尚在等待录音的缓冲区,然后将正在录音的缓冲区中数据发送MM_WIM_DATA给用户处理 bEnding = TRUE; waveInReset(hWaveIn); //送MM_WIM_DATA消息 return TRUE; /*-----------------------------播放按钮过程--------------------------------------*/ case IDC_PLAY_BEG: //打开波形输出设备 waveform.wFormatTag = WAVE_FORMAT_PCM; waveform.nChannels = 1; waveform.nSamplesPerSec = 11025;//采样率:11025Hz waveform.nAvgBytesPerSec = 11025;//每秒数据传输的字节数=nBlockAlign*nSamplesPerSec waveform.nBlockAlign = 1; //块对齐 = nChannels*wBitsPerSample/8(字节) waveform.wBitsPerSample = 8; //采样大小,即声音的振幅(强度) waveform.cbSize = 0; //PCM,必须为0 if (MMSYSERR_NOERROR != waveOutOpen(&hWaveOut, WAVE_MAPPER, &waveform, (DWORD)hwnd, 0, CALLBACK_WINDOW)) //发送MM_WOM_OPEN消息 { MessageBeep(MB_ICONEXCLAMATION); MessageBox(hwnd, szOpenError, szAppName, MB_ICONEXCLAMATION | MB_OK); } return TRUE; /*-----------------------------暂停按钮过程--------------------------------------*/ case IDC_PLAY_PAUSE: //暂停或重启音频输出设备 if (!bPaused) { waveOutPause(hWaveOut); SetDlgItemText(hwnd, IDC_PLAY_PAUSE, TEXT("Resume")); bPaused = TRUE; } else { waveOutRestart(hWaveOut); SetDlgItemText(hwnd, IDC_PLAY_PAUSE, TEXT("Pause")); bPaused = FALSE; } return TRUE; /*----------------------------停止播放按钮过程------------------------------------*/ case IDC_PLAY_END: //复位音频输出设备,为关闭做准备 bEnding = TRUE; waveOutReset(hWaveOut); //会发送MM_WOM_DONE消息 return TRUE; /*----------------------------反转播放按钮过程------------------------------------*/ case IDC_PLAY_REV: //反转“保存缓冲区”中的数据,并倒播音频 bReverse = TRUE; ReverseMemory(pSaveBuffer, dwDataLength); SendMessage(hwnd, WM_COMMAND, IDC_PLAY_BEG, 0); //发送消息,开始播放 return TRUE; /*----------------------------重复播放按钮过程------------------------------------*/ case IDC_PLAY_REP: //将播放次数设备为无限次数 dwRepetitions = -1; SendMessage(hwnd, WM_COMMAND, IDC_PLAY_BEG, 0); //发送消息,开始播放 return TRUE; /*----------------------------快进播放按钮过程------------------------------------*/ case IDC_PLAY_SPEED: //打开音频输出设备,并快播(2倍速度) waveform.wFormatTag = WAVE_FORMAT_PCM; waveform.nChannels = 1; waveform.nSamplesPerSec = 22050;//2倍的录音时的采样率(11025Hz) waveform.nAvgBytesPerSec = 22050;//每秒数据传输的字节数=nBlockAlign*nSamplesPerSec waveform.nBlockAlign = 1; //块对齐 = nChannels*wBitsPerSample/8(字节) waveform.wBitsPerSample = 8; //采样大小,即声音的振幅(强度) waveform.cbSize = 0; //PCM,必须为0 if (MMSYSERR_NOERROR != waveOutOpen(&hWaveOut, 0, &waveform, (DWORD)hwnd, 0, CALLBACK_WINDOW)) { MessageBeep(MB_ICONEXCLAMATION); MessageBox(hwnd, szOpenError, szAppName, MB_ICONEXCLAMATION | MB_OK); } return TRUE; } break; /*----------录制函数的消息,共3个(MM_WIM_OPEN、MM_WIM_DATA、MM_WIM_CLOSE)------------*/ case MM_WIM_OPEN: //将“保存缓冲区”缩小为1个字节 pSaveBuffer = realloc(pSaveBuffer, 1); //启动或禁用按钮组 EnableWindow(GetDlgItem(hwnd, IDC_RECORD_BEG), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_RECORD_END), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_BEG), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_PAUSE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_END), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REV), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REP), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_SPEED), FALSE); SetFocus(GetDlgItem(hwnd, IDC_RECORD_END)); //向波形输入设备增加缓冲区 waveInAddBuffer(hWaveIn, pWaveHdr1, sizeof(WAVEHDR)); waveInAddBuffer(hWaveIn, pWaveHdr2, sizeof(WAVEHDR)); //开始采样 bRecording = TRUE; bEnding = FALSE; dwDataLength = 0; //录音开始,录音设备便立即录音并存入已经送来的缓存块内,当被送来的有多个缓存块时, //按照FIFO的原则向缓存块内存入录音数据 waveInStart(hWaveIn); return TRUE; case MM_WIM_DATA: //当指定16K缓冲区满时会收到该消息 //新“保存缓冲区”大小 = 原大小+新录制的大小 pNewBuffer = realloc(pSaveBuffer, dwDataLength + ((PWAVEHDR)lParam)->dwBytesRecorded); if (pNewBuffer == NULL) { waveInClose(hWaveIn); //发送MM_WIM_CLOSE消息 MessageBeep(MB_ICONEXCLAMATION); MessageBox(hwnd, szMemError, szAppName, MB_ICONEXCLAMATION | MB_OK); return TRUE; } pSaveBuffer = pNewBuffer; //将新录制的声音数据拷贝到新缓冲区后面。注意上面的realloc会保存原有的数据 CopyMemory(pSaveBuffer + dwDataLength, ((PWAVEHDR)lParam)->lpData, ((PWAVEHDR)lParam)->dwBytesRecorded); dwDataLength += ((PWAVEHDR)lParam)->dwBytesRecorded; if (bEnding) //按了“End”按钮来结束录制时 { waveInClose(hWaveIn); return TRUE; } //将当前使用的缓冲区作为新的缓冲区,发送给输入设备(该缓冲区将被重新覆盖录音) //注意即使不发送新的缓冲区给输入设备,录音也还在进行,只是数据被丢弃了。 waveInAddBuffer(hWaveIn, (PWAVEHDR)lParam, sizeof(WAVEHDR)); return TRUE; case MM_WIM_CLOSE: //释放缓冲区 waveInUnprepareHeader(hWaveIn, pWaveHdr1, sizeof(WAVEHDR)); waveInUnprepareHeader(hWaveIn, pWaveHdr2, sizeof(WAVEHDR)); free(pBuffer1); free(pBuffer2); //启用或禁用相关的按钮 EnableWindow(GetDlgItem(hwnd, IDC_RECORD_BEG), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_RECORD_END), FALSE); SetFocus(GetDlgItem(hwnd, IDC_RECORD_BEG)); if (dwDataLength>0) { EnableWindow(GetDlgItem(hwnd, IDC_PLAY_BEG), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_PAUSE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_END), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REV), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REP), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_SPEED), TRUE); } bRecording = FALSE; if (bTerminating) //如果是按下了关闭按钮 SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0L); return TRUE; /*----------播放函数的消息,共3个(MM_WOM_OPEN、MM_WOM_DONE、MM_WOM_CLOSE)------------*/ case MM_WOM_OPEN: //启用或禁用相应按钮 EnableWindow(GetDlgItem(hwnd, IDC_RECORD_BEG), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_RECORD_END), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_BEG), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_PAUSE), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_END), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REV), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REP), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_SPEED), FALSE); SetFocus(GetDlgItem(hwnd, IDC_PLAY_END)); //设置wav头信息 pWaveHdr1->lpData = 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; //准备并写入输出设备,播放完会发送MM_WOM_DONE消息 waveOutPrepareHeader(hWaveOut, pWaveHdr1, sizeof(WAVEHDR)); waveOutWrite(hWaveOut, pWaveHdr1, sizeof(WAVEHDR)); //播放开始 bEnding = FALSE; bPlaying = TRUE; return TRUE; case MM_WOM_DONE: waveOutUnprepareHeader(hWaveOut, pWaveHdr1, sizeof(WAVEHDR)); waveOutClose(hWaveOut); //发送MM_WOM_CLOSE消息 return TRUE; case MM_WOM_CLOSE: //启用或禁用相应按钮 EnableWindow(GetDlgItem(hwnd, IDC_RECORD_BEG), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_RECORD_END), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_BEG), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_PAUSE), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_END), FALSE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REV), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_REP), TRUE); EnableWindow(GetDlgItem(hwnd, IDC_PLAY_SPEED), TRUE); SetFocus(GetDlgItem(hwnd, IDC_PLAY_BEG)); SetDlgItemText(hwnd, IDC_PLAY_PAUSE, TEXT("Pause")); bPaused = FALSE; dwRepetitions = 1; bPlaying = FALSE; if (bReverse) //如果以前是倒播的,现在重新恢复过来 { ReverseMemory(pSaveBuffer, dwDataLength); bReverse = FALSE; } if (bTerminating) //如果是按了“关闭”按钮 SendMessage(hwnd, WM_SYSCOMMAND, SC_CLOSE, 0L); return TRUE; case WM_SYSCOMMAND: switch (LOWORD(wParam)) { case SC_CLOSE: if (bRecording) { bTerminating = TRUE; bEnding = TRUE; waveInReset(hWaveIn); //发送MM_WIM_DATA消息 return TRUE; } if (bPlaying) { bTerminating = TRUE; bEnding = TRUE; waveOutReset(hWaveOut); //发送MM_WOM_CLOSE消息 return TRUE; } if (pWaveHdr1) free(pWaveHdr1); if (pWaveHdr2) free(pWaveHdr2); if(pSaveBuffer) free(pSaveBuffer); EndDialog(hwnd, 0); return TRUE; } break; } return FALSE; }
//resource.h
//{{NO_DEPENDENCIES}} // Microsoft Visual C++ 生成的包含文件。 // 供 Record1.rc 使用 // #define IDC_RECORD_BEG 1001 #define IDC_RECORD_END 1002 #define IDC_PLAY_BEG 1003 #define IDC_PLAY_PAUSE 1004 #define IDC_PLAY_END 1005 #define IDC_PLAY_REV 1006 #define IDC_PLAY_REP 1007 #define IDC_PLAY_SPEED 1008 // Next default values for new objects // #ifdef APSTUDIO_INVOKED #ifndef APSTUDIO_READONLY_SYMBOLS #define _APS_NEXT_RESOURCE_VALUE 102 #define _APS_NEXT_COMMAND_VALUE 40001 #define _APS_NEXT_CONTROL_VALUE 1002 #define _APS_NEXT_SYMED_VALUE 101 #endif #endif
//record.rc
// Microsoft Visual C++ generated resource script. // #include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // // Generated from the TEXTINCLUDE 2 resource. // #include "winres.h" ///////////////////////////////////////////////////////////////////////////// #undef APSTUDIO_READONLY_SYMBOLS ///////////////////////////////////////////////////////////////////////////// // 中文(简体,中国) resources #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS) LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED #ifdef APSTUDIO_INVOKED ///////////////////////////////////////////////////////////////////////////// // // TEXTINCLUDE // 1 TEXTINCLUDE BEGIN "resource.h " END 2 TEXTINCLUDE BEGIN "#include ""winres.h"" " "