9.14.2007

錄音 : Multimedia library

錄音 : 就是把 mic 的輸入 save成WAVE檔。
使用 Multimedia library,也就是 mciSendCommand( ) 這個function。
這個funciton 跟IoControl 類似,都是用command id + command structure 來傳遞資料。
所以很麻煩的每個動作都要準備好作為argument 的structure。

先 Open:
mciOpen.dwCallback = 0;
mciOpen.wDeviceID = 1;
mciOpen.lpstrDeviceType = TEXT("waveaudio");
mciOpen.lpstrElementName = "";
mciOpen.lpstrAlias = NULL;
dwError = mciSendCommand(0, MCI_OPEN, MCI_WAIT|MCI_OPEN_TYPE|MCI_OPEN_ELEMENT,
(DWORD)(LPMCI_OPEN_PARMS) &mciOpen);
if( dwError != 0 )
{
rtnMsg = "Open record device error; ";
return 0;
}
//save the Device ID
wRecordDeviceID = mciOpen.wDeviceID;
open 好的id 放到 wRecordDeviceID。 ( 這個以後作為對 錄音device 的control handle)。
然後,設定sample 格式
    DWORD dwFlags = MCI_WAVE_SET_FORMATTAG |
MCI_WAVE_SET_BLOCKALIGN |
MCI_WAVE_SET_CHANNELS |
MCI_WAVE_SET_BITSPERSAMPLE |
MCI_WAVE_SET_SAMPLESPERSEC |
MCI_WAVE_SET_AVGBYTESPERSEC|
MCI_WAVE_INPUT;
mciSet.wInput = 0; //recordsource index
mciSet.wFormatTag = WAVE_FORMAT_PCM;
mciSet.nChannels = 2;
mciSet.wBitsPerSample = 16; //8 or 16
mciSet.nBlockAlign = 4;
mciSet.nSamplesPerSec = 8000;
mciSet.nAvgBytesPerSec = mciSet.nSamplesPerSec * mciSet.nBlockAlign;
dwError = mciSendCommand(wRecordDeviceID, MCI_SET, dwFlags,
(DWORD)(LPVOID)&mciSet);
if (dwError != 0)
{
rtnMsg = "Not support specific record id; ";
return 0;
}
然後...
下command 開始錄音了..
    mciRecord.dwCallback = NULL;
mciRecord.dwFrom = 0;
mciRecord.dwTo = 0;
dwError = mciSendCommand(wRecordDeviceID, MCI_RECORD, MCI_NOTIFY,
(DWORD) (LPMCI_PLAY_PARMS)&mciRecord);
if(dwError != 0)
{
rtnMsg = _T("Not support specific record format; ");
}
然後,等等等...錄夠了..
叫他存起來:
    mciSave.dwCallback = 0;
mciSave.lpfilename = "TTTTT.WAV";
if( wRecordDeviceID != 0 )
{
mciGeneric.dwCallback = 0;
mciSendCommand(wRecordDeviceID, MCI_STOP ,MCI_WAIT ,
(DWORD) (LPMCI_PLAY_PARMS)&mciGeneric);
mciSendCommand(wRecordDeviceID, MCI_SAVE ,MCI_WAIT|MCI_SAVE_FILE,
(DWORD) (LPMCI_PLAY_PARMS)&mciSave );
mciSendCommand(wRecordDeviceID, MCI_CLOSE,MCI_WAIT ,
(DWORD) (LPMCI_PLAY_PARMS)&mciGeneric);
wRecordDeviceID = 0;
}
這樣就把錄到的資料放到 "TTTTT.WAV"這個file理了..

multimedia library 要include mmsystem.h
並且要 link Winmm.lib,否則會找不到 mciSendCommand( ) 這個function.
這個function 是假設你的pc 只有一個音效卡,在opendevice 時,直接指定 mciOpen.wDeviceID = 1 。

還有,要設定是用MIC還是LineIn 的話,在 control panel -system -hardware - audio 的


很愚蠢的,是"音量"那一個button,會出現右邊那個dialog.

沒有留言: