應用WindowsAPI獲得灌音音頻的辦法。本站提示廣大學習愛好者:(應用WindowsAPI獲得灌音音頻的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是應用WindowsAPI獲得灌音音頻的辦法正文
本文實例引見了應用winmm.h停止音頻流的獲得的辦法,詳細步調以下:
1、起首須要包括以下援用對象
#include <Windows.h> #include "mmsystem.h" #pragma comment(lib, "winmm.lib")
2、音頻的獲得須要挪用7個函數
1. waveInGetNumDevs:前往體系中停當的波形聲響輸出裝備的數目
UINT waveInGetNumDevs(VOID);
2. waveInGetDevCaps:檢討指定波形輸出裝備的特征
MMRESULT waveInGetDevCaps( UINT_PTR uDeviceID, LPWAVEINCAPS pwic, UINT cbwic ); //uDeviceID 音頻輸出裝備標識,也能夠為一個翻開的音頻輸出裝備的句柄. // 小我以為假如上一步取得了多個裝備,可以用索引標識每個裝備. // //pwic 對WAVEINCAPS構造體的一個指針,包括裝備的音頻特征. // //cbwic WAVEINCAPS構造體的年夜小,應用sizeof便可. // //MMRESULT 函數履行的成果 // MMSYSERR_NOERROR 表現履行勝利 // MMSYSERR_BADDEVICEID 索引越界 // MMSYSERR_NODRIVER 沒有停當的裝備 // MMSYSERR_NOMEM 不克不及分派或許鎖定內存
引見WAVEINCAPS構造體的寄義:
typedef struct { WORD wMid; //音頻裝備制作約定義的驅動法式標識 WORD wPid; //音頻輸出裝備的產物標識 MMVERSION vDriverVersion; //驅動法式版本號 TCHAR szPname[MAXPNAMELEN];//制作商稱號 DWORD dwFormats; //支撐的格局,拜見MSDN WORD wChannels; //支撐的聲道數 WORD wReserved1; //保存參數 } WAVEINCAPS;
3. waveInOpen:翻開指定的音頻輸出裝備,停止灌音
MMRESULT waveInOpen( LPHWAVEIN phwi, //吸收翻開的音頻輸出裝備標識的HWAVEIN構造的指針 UINT_PTR uDeviceID, //指定一個須要翻開的裝備標識.可使用WAVE_MAPPER選擇一個按指定灌音格局灌音的裝備 LPWAVEFORMATEX pwfx, //一個所需的格局停止灌音的WAVEFORMATEX構造的指針 DWORD_PTR dwCallback, //指向一個回調函數、事宜句柄、窗口句柄、線程標識,對灌音事宜停止處置. DWORD_PTR dwCallbackInstance, //傳給回調機制的參數 DWORD fdwOpen //翻開裝備的辦法標識,指定回調的類型.拜見CSDN );
引見WAVEFORMATEX構造體的寄義:
typedef struct { WORD wFormatTag; //波形聲響的格局,單聲道雙聲道應用WAVE_FORMAT_PCM.當包括在WAVEFORMATEXTENSIBLE構造中時,應用WAVE_FORMAT_EXTENSIBLE. WORD nChannels; //聲道數目 DWORD nSamplesPerSec; //采樣率.wFormatTag為WAVE_FORMAT_PCM時,有8.0kHz,11.025kHz,22.05kHz,和44.1kHz. DWORD nAvgBytesPerSec; //每秒的采樣字節數.經由過程nSamplesPerSec * nChannels * wBitsPerSample / 8盤算 WORD nBlockAlign; //每次采樣的字節數.經由過程nChannels * wBitsPerSample / 8盤算 WORD wBitsPerSample; //采樣位數.wFormatTag為WAVE_FORMAT_PCM時,為8或許16 WORD cbSize; //wFormatTag為WAVE_FORMAT_PCM時,疏忽此參數 } WAVEFORMATEX;
引見dwCallback回調函數格局:
void CALLBACK waveInProc( HWAVEIN hwi, //回調此函數的裝備句柄 UINT uMsg, //波形聲響輸出信息,標識封閉(WIM_CLOSE)、緩沖區滿(WIM_DATA)、翻開(WIM_OPEN). DWORD_PTR dwInstance, //用戶在waveInOpen指定的數據 DWORD_PTR dwParam1, //(LPWAVEHDR)dwParam1,用戶指定的緩沖區 DWORD_PTR dwParam2 );
4. waveInPrepareHeader:為音頻輸出裝備預備一個緩沖區
MMRESULT waveInPrepareHeader( HWAVEIN hwi, //音頻輸出裝備句柄 LPWAVEHDR pwh,//指向WAVEHDR構造的指針,標識預備的緩沖區 UINT cbwh //WAVEHDR構造的年夜小,應用sizeof便可 );
引見WAVEHDR構造:
typedef struct wavehdr_tag { LPSTR lpData; //指向波形格局的緩沖區 DWORD dwBufferLength; //緩沖區的年夜小 DWORD dwBytesRecorded; //以後存儲了若干數據 DWORD_PTR dwUser; //用戶數據 DWORD dwFlags; //為緩沖區供給的信息,在waveInPrepareHeader函數中應用WHDR_PREPARED DWORD dwLoops; //輸入時應用,標識播放次數 struct wavehdr_tag * lpNext;//reserved DWORD_PTR reserved; //reserved } WAVEHDR, *LPWAVEHDR;
5. waveInAddBuffer:將緩沖區發送給裝備,若緩沖區填滿,則不起感化。(參數同上)
MMRESULT waveInAddBuffer( HWAVEIN hwi, LPWAVEHDR pwh, UINT cbwh );
6. waveInStart:開端停止錄制
MMRESULT waveInStart( HWAVEIN hwi //裝備句柄 );
7. waveInClose:封閉裝備
MRESULT waveInClose( HWAVEIN hwi //裝備句柄 );
3、完全實例代碼以下:
//Run.c文件 #include <Windows.h> #include <stdio.h> #include "mmsystem.h" #pragma comment(lib, "winmm.lib") void PlayMusi(); void WaveInitFormat(LPWAVEFORMATEX m_WaveFormat, WORD nCh,DWORD nSampleRate,WORD BitsPerSample); DWORD CALLBACK MicCallback(HWAVEIN hwavein, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2); void RecordWave(); void main() { //PlayMusi(); RecordWave(); while(1); } void RecordWave() { int count = waveInGetNumDevs();//1 printf("\n音頻輸出數目:%d\n",count); WAVEINCAPS waveIncaps; MMRESULT mmResult = waveInGetDevCaps(0,&waveIncaps,sizeof(WAVEINCAPS));//2 printf("\n音頻輸出裝備:%s\n",waveIncaps.szPname); if(MMSYSERR_NOERROR==mmResult) { HWAVEIN phwi; WAVEFORMATEX pwfx; WaveInitFormat(&pwfx,1,8000,8); printf("\n要求翻開音頻輸出裝備"); printf("\n采樣參數:單聲道 8kHz 8bit\n"); mmResult=waveInOpen(&phwi,WAVE_MAPPER,&pwfx,(DWORD)(MicCallback),NULL,CALLBACK_FUNCTION);//3 if(MMSYSERR_NOERROR==mmResult) { WAVEHDR pwh1; char buffer1[10240]; pwh1.lpData=buffer1; pwh1.dwBufferLength=10240; pwh1.dwUser=1; pwh1.dwFlags=0; mmResult=waveInPrepareHeader(phwi,&pwh1,sizeof(WAVEHDR));//4 printf("\n預備緩沖區1"); WAVEHDR pwh2; char buffer2[10240]; pwh2.lpData=buffer2; pwh2.dwBufferLength=10240; pwh2.dwUser=2; pwh2.dwFlags=0; mmResult=waveInPrepareHeader(phwi,&pwh2,sizeof(WAVEHDR));//4 printf("\n預備緩沖區2\n"); if(MMSYSERR_NOERROR==mmResult) { mmResult=waveInAddBuffer(phwi,&pwh1,sizeof(WAVEHDR));//5 printf("\n將緩沖區1參加音頻輸出裝備"); mmResult=waveInAddBuffer(phwi,&pwh2,sizeof(WAVEHDR));//5 printf("\n將緩沖區2參加音頻輸出裝備\n"); if(MMSYSERR_NOERROR==mmResult) { mmResult=waveInStart(phwi);//6 printf("\n要求開端灌音\n"); } } } } } DWORD CALLBACK MicCallback(HWAVEIN hwavein, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2) { switch(uMsg) { case WIM_OPEN: printf("\n裝備曾經翻開...\n"); break; case WIM_DATA: printf("\n緩沖區%d存滿...\n",((LPWAVEHDR)dwParam1)->dwUser); waveInAddBuffer (hwavein, (LPWAVEHDR)dwParam1, sizeof (WAVEHDR)) ; break; case WIM_CLOSE: printf("\n裝備曾經封閉...\n"); break; default: break; } return 0; } void WaveInitFormat(LPWAVEFORMATEX m_WaveFormat, WORD nCh,DWORD nSampleRate,WORD BitsPerSample) { m_WaveFormat->wFormatTag = WAVE_FORMAT_PCM; m_WaveFormat->nChannels = nCh; m_WaveFormat->nSamplesPerSec = nSampleRate; m_WaveFormat->nAvgBytesPerSec = nSampleRate * nCh * BitsPerSample/8; m_WaveFormat->nBlockAlign = m_WaveFormat->nChannels * BitsPerSample/8; m_WaveFormat->wBitsPerSample = BitsPerSample; m_WaveFormat->cbSize = 0; } void PlayMusi() { int error = mciSendString("open C:\\Users\\Angel\\Desktop\\有若干愛可以重來.mp3 alias myDivece", NULL, 0, NULL); if (error == 0) { mciSendString("play myDivece", NULL, 0, NULL); //播放 } }