-
Windows WaveIn 录音
- 设置采集音频格式
- WAVEFORMATEX waveform;
- waveform.wFormatTag = WAVE_FORMAT_PCM;
- waveform.nSamplesPerSec = 8000;
- waveform.wBitsPerSample = 16;
- waveform.nChannels = 1;
- waveform.nAvgBytesPerSec = 16000;
- waveform.nBlockAlign = 2;
- waveform.cbSize = 0;
- 提前准备好的录音数据缓存
- m_pRecoderBuf = new unsigned char[20 * 1024];
- m_recoderNum = 20;
- m_recoderBufLen = 20 * 1024;
- m_pWaveHdr = new WAVEHDR[20];
- memset(m_pRecoderBuf, 0, 20 * 1024);
- memset(m_pWaveHdr, 0, sizeof(WAVEHDR) * 20);
- 打开录音设备
- MMRESULT mmres = waveInOpen(&m_hWaveIn, WAVE_MAPPER, &waveform, (DWORD_PTR)WaveInProc, (DWORD_PTR)this, CALLBACK_FUNCTION );
-
- if(mmres != MMSYSERR_NOERROR)
- {
-
- return;
- }
WAVE_MAPPER表示系统会自己寻找合适的录音设备。
- 准备数据空间
- for (int i = 0; i < m_recoderNum; ++i)
- {
- m_pWaveHdr[i].lpData = (char*)m_pRecoderBuf + i * 1024;
- m_pWaveHdr[i].dwBufferLength = 1024;
- mmres = waveInPrepareHeader(m_hWaveIn, &m_pWaveHdr[i], sizeof(WAVEHDR));
- mmres = waveInAddBuffer(m_hWaveIn, &m_pWaveHdr[i], sizeof(WAVEHDR));
- }
- 开始录音并打开写文件
- mmres = waveInStart(m_hWaveIn);
-
- SYSTEMTIME systemtime;
- GetLocalTime(&systemtime);
- char filename[256];
- sprintf_s(filename, 256, "%d-%d-%d_%d_%d_%d.wav", systemtime.wYear, systemtime.wMonth, systemtime.wDay, systemtime.wHour, systemtime.wMinute, systemtime.wSecond);
- m_pWavFile = wav_write_open(filename, 8000, 16, 1);
- 要提前定义好数据接收回调函数,因为我们用的是CALLBACK_FUNCTION
- static BOOL CALLBACK WaveInProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2);
- BOOL WaveInProcImpl(HWAVEOUT hwo, UINT uMsg, DWORD dwParam1, DWORD dwParam2);
函数里的处理是:
- BOOL CAudioFileConvertDlg::WaveInProc(HWAVEOUT hwo, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
- {
- CAudioFileConvertDlg* pPlayer = (CAudioFileConvertDlg*)dwInstance;
-
- return pPlayer->WaveInProcImpl(hwo, uMsg, dwParam1, dwParam2);
- }
-
- BOOL CAudioFileConvertDlg::WaveInProcImpl(HWAVEOUT hwo, UINT uMsg, DWORD dwParam1, DWORD dwParam2)
- {
-
- if(uMsg == WIM_DATA)
- {
- LPWAVEHDR pHdr = (LPWAVEHDR) dwParam1;
-
- MMRESULT mmres = waveInUnprepareHeader (m_hWaveIn, pHdr, sizeof(WAVEHDR));
-
- if (NULL != m_pWavFile)
- {
- wav_write_data(m_pWavFile, (unsigned char *)(pHdr->lpData), pHdr->dwBytesRecorded);
- }
-
- mmres = waveInPrepareHeader(m_hWaveIn, pHdr, sizeof(WAVEHDR));
- mmres = waveInAddBuffer(m_hWaveIn, pHdr, sizeof(WAVEHDR));
- }
-
- return TRUE;
- }
- 关闭录音并关闭wav写文件
- MMRESULT mmres = waveInStop(m_hWaveIn);
- mmres = waveInClose(m_hWaveIn);
- wav_write_close(m_pWavFile);
- if (NULL != m_pRecoderBuf)
- {
- delete []m_pRecoderBuf;
- m_pRecoderBuf = NULL;
- }
-
- if (NULL != m_pWaveHdr)
- {
- delete []m_pWaveHdr;
- m_pWaveHdr = NULL;
- }
-
相关阅读:
Photoshop色阶、曲线命令图解和编程实现(附源码)
苹果Mac OS X快捷键大全
lucene Field部分参数设置含义
图片管理下载逻辑
Lucene.Net无障碍学习和使用:索引篇 (转)
Firebug 调试器开发中的12个技巧
Lucene.Net 多线程操作建议(转)
如何在ASP.NET中下载文件
Lucene.Net 2.3.1开发介绍 —— 简介 (转)
JS打开图片另存为对话框 (转)
-
原文地址:https://www.cnblogs.com/qiengo/p/6437308.html
Copyright © 2020-2023
润新知