资源简介
利用windows api采集麦克风音频并播放。
代码片段和文件信息
#include “stdafx.h“
#include
#include
#include
#pragma comment(lib “winmm.lib“)
#define BUFFER_SIZE (44100*16*2/8*5) // 录制声音长度
#define FRAGMENT_SIZE 1024 // 缓存区大小
#define FRAGMENT_NUM 4 // 缓存区个数
static unsigned char buffer[BUFFER_SIZE] = { 0 };
static int buf_count = 0;
// 函数定义
void CALLBACK waveInProc(HWAVEIN hwi
UINT uMsg
DWORD_PTR dwInstance
DWORD_PTR dwParam1
DWORD_PTR dwParam2);
void CALLBACK waveOutProc(HWAVEOUT hwo
UINT uMsg
DWORD_PTR dwInstance
DWORD_PTR dwParam1
DWORD_PTR dwParam2);
// 入口
int main()
{
/* 录音 */
// Device
int nReturn = waveInGetNumDevs();
printf(“输入设备数目:%d\n“ nReturn);
for (int i = 0; i {
WAVEINCAPS wic;
waveInGetDevCaps(i &wic sizeof(WAVEINCAPS));
printf(“#%d\t设备名:%s\n“ i wic.szPname);
}
// open
HWAVEIN hWaveIn;
WAVEFORMATEX wavform;
wavform.wFormatTag = WAVE_FORMAT_PCM;
wavform.nChannels = 2;
wavform.nSamplesPerSec = 44100;
wavform.nAvgBytesPerSec = 44100 * 16 * 2 / 8;
wavform.nBlockAlign = 4;
wavform.wBitsPerSample = 16;
wavform.cbSize = 0;
waveInOpen(&hWaveIn WAVE_MAPPER &wavform (DWORD_PTR)waveInProc 0 CALLBACK_FUNCTION);
WAVEINCAPS wic;
waveInGetDevCaps((UINT_PTR)hWaveIn &wic sizeof(WAVEINCAPS));
printf(“打开的输入设备:%s\n“ wic.szPname);
// prepare buffer
static WAVEHDR wh[FRAGMENT_NUM];
for (int i = 0; i {
wh[i].lpData = new char[FRAGMENT_SIZE];
wh[i].dwBufferLength = FRAGMENT_SIZE;
wh[i].dwBytesRecorded = 0;
wh[i].dwUser = NULL;
wh[i].dwFlags = 0;
wh[i].dwLoops = 1;
wh[i].lpNext = NULL;
wh[i].reserved = 0;
waveInPrepareHeader(hWaveIn &wh[i] sizeof(WAVEHDR));
waveInAddBuffer(hWaveIn &wh[i] sizeof(WAVEHDR));
}
// record
printf(“Start to Record...\n“);
buf_count = 0;
waveInStart(hWaveIn);
while (buf_count < BUFFER_SIZE)
{
Sleep(1);
}
printf(“Record Over!\n\n“);
// clean
waveInStop(hWaveIn);
waveInReset(hWaveIn);
for (int i = 0; i {
waveInUnprepareHeader(hWaveIn &wh[i] sizeof(WAVEHDR));
delete wh[i].lpData;
}
waveInClose(hWaveIn);
system(“pause“);
printf(“\n“);
/* 放音 */
// Device
nReturn = waveOutGetNumDevs();
printf(“\n输出设备数目:%d\n“ nReturn);
for (int i = 0; i {
WAVEOUTCAPS woc;
waveOutGetDevCaps(i &woc sizeof(WAVEOUTCAPS));
printf(“#%d\t设备名:%s\n“ i wic.szPname);
}
// open
HWAVEOUT hWaveOut;
waveOutOpen(&hWaveOut WAVE_MAPPER &wavform (DWORD_PTR)waveOutProc 0 CALLBACK_FUNCTION);
WAVEOUTCAPS woc;
waveOutGetDevCaps((UINT_PTR)hWaveOut &woc sizeof(WAVEOUTCAPS));
printf(“打开的输出设备:%s\n“ wic.szPname);
// prepare buffer
WAVEHDR wavhdr;
wavhdr.lpData = (LPSTR)buffer;
wavhdr.dwBufferLength = BUFFER_SIZE;
wavhdr.dwFlags = 0;
wavhdr.dwLoops = 0;
waveOutPrepareHeader(
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 4139 2016-09-01 10:28 AudioCapture-WindowsApi\AudioCapture-WindowsApi\AudioCapture-WindowsApi.cpp
文件 4566 2016-09-01 10:31 AudioCapture-WindowsApi\AudioCapture-WindowsApi\AudioCapture-WindowsApi.vcxproj
文件 1330 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi\AudioCapture-WindowsApi.vcxproj.filters
文件 1965 2016-09-05 18:34 AudioCapture-WindowsApi\AudioCapture-WindowsApi\Debug\AudioCapture-WindowsApi.Build.CppClean.log
文件 94 2016-09-05 18:34 AudioCapture-WindowsApi\AudioCapture-WindowsApi\Debug\AudioCapture-WindowsApi.log
文件 1622 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi\ReadMe.txt
文件 229 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi\stdafx.cpp
文件 234 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi\stdafx.h
文件 236 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi\targetver.h
文件 1015 2016-09-01 10:24 AudioCapture-WindowsApi\AudioCapture-WindowsApi.sln
..A..H. 23040 2016-09-05 18:34 AudioCapture-WindowsApi\AudioCapture-WindowsApi.v12.suo
目录 0 2016-09-05 18:34 AudioCapture-WindowsApi\AudioCapture-WindowsApi\Debug\AudioCap.7B50A768.tlog
目录 0 2016-09-05 18:34 AudioCapture-WindowsApi\AudioCapture-WindowsApi\Debug
目录 0 2016-09-01 10:28 AudioCapture-WindowsApi\AudioCapture-WindowsApi
目录 0 2016-09-05 18:34 AudioCapture-WindowsApi\Debug
目录 0 2016-09-05 18:34 AudioCapture-WindowsApi
----------- --------- ---------- ----- ----
38470 16
相关资源
- 直接将麦克风的声音直接输出到音箱
- 索尼SONY VPCM125JC网卡驱动程序 v6.0.14
- 音频测试文件pcmmp3aacamrg711ag711u等多种
- ffmpeg之pcm转AAC
- 使用FFmpeg采集摄像头图像和麦克风音
- 最简单的基于FFMPEG的音频编码器 1.1
- mp3文件转成pcm格式
- 易语言PCM文件转换源码
- 声卡麦克风重采样
- 内存H264+PCM发布rtmp.rar
- 从声卡麦克风捕捉声音
- stm32F103与PCM5102A与Helix软解播放MP3
- QQPCMgr.7z
- PcModWin4.0操作指南
- Microphone Array Signal Processing
- libmad mp3软解码
- pcm-g711a-采集器-播放器 源代码
- PCM时分复用电路的设计与仿真
- PcMODWin40
- 音频采集编码pcm到aac
- PCM编码实验系统的设计和实现
- 实验1:PCM5102播放SD卡音乐STM32F103ZET
- Verilog编写的采集PCM1750串行输出的代码
- PCM格式音乐文件音频文件
- pcm格式文件资源集合
- pcm音频文件
- 语音信号频带传输通信系统仿真——
- FFmpeg解码MP4分别播放YUV视频和PCM音频
- 树莓派麦克风模块—Adafruit I2S MEMS M
- Nokia PCM 培训教程
评论
共有 条评论