使用 Windows Core Audio APs 进行 Loopback Recording 并生成 WAV 文件
参考文档
COM Coding Practices
Audio File Format Specifications
Core Audio APIs
Loopback Recording
#include <iostream>
#include <fstream>
#include <vector>
#include <mmdeviceapi.h>
#include <combaseapi.h>
#include <atlbase.h>
#include <Functiondiscoverykeys_devpkey.h>
#include <Audioclient.h>
#include <Audiopolicy.h>
// 利用RAII手法,自动调用 CoUninitialize
class CoInitializeGuard {
public:
CoInitializeGuard()
{
_hr = CoInitializeEx(nullptr, COINIT::COINIT_MULTITHREADED);
}
~CoInitializeGuard()
{
if (_hr == S_OK || _hr == S_FALSE) {
CoUninitialize();
}
}
HRESULT result() const { return _hr; }
private:
HRESULT _hr;
};
constexpr inline void exit_on_failed(HRESULT hr);
void printEndpoints(CComPtr<IMMDeviceCollection> pColletion);
std::string wchars_to_mbs(const wchar_t* s);
int main()
{
HRESULT hr{};
CoInitializeGuard coInitializeGuard;
exit_on_failed(coInitializeGuard.result());
// COM 对象都用 CComPtr 包装,会自动调用 Release
// COM 接口分配的堆变量用 CComHeapPtr 包装,会自动调用 CoTaskMemFree
CComPtr<IMMDeviceEnumerator> pEnumerator;
hr = pEnumerator.CoCreateInstance(__uuidof(MMDeviceEnumerator));
exit_on_failed(hr);
// 打印所有可用的音频设备
//CComPtr<IMMDeviceCollection> pColletion;
//hr = pEnumerator->EnumAudioEndpoints(eRender, DEVICE_STATE_ACTIVE, &pColletion);
//exit_on_failed(hr);
//printEndpoints(pColletion);
// 使用默认的 Audio Endpoint,eRender 表示音频播放设备,而不是录音设备
CComPtr<IMMDevice> pEndpoint;
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pEndpoint);
exit_on_failed(hr);
// 打印出播放设备的名字,可能包含中文
CComPtr<IPropertyStore> pProps;
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
exit_on_failed(hr);
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
exit_on_failed(hr);
std::cout << "select audio endpoint: " << wchars_to_mbs(varName.pwszVal) << std::endl;
PropVariantClear(&varName);
// 由 IMMDevice 对象 得到 IAudioClient 对象
CComPtr<IAudioClient> pAudioClient;
hr = pEndpoint->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&pAudioClient);
exit_on_failed(hr);
// 获得音频播放设备格式信息
CComHeapPtr<WAVEFORMATEX> pDeviceFormat;
pAudioClient->GetMixFormat(&pDeviceFormat);
constexpr int REFTIMES_PER_SEC = 10000000; // 1 reference_time = 100ns
constexpr int REFTIMES_PER_MILLISEC = 10000;
// 初始化 IAudioClient 对象
const REFERENCE_TIME hnsRequestedDuration = 2 * REFTIMES_PER_SEC; // 1s
hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, AUDCLNT_STREAMFLAGS_LOOPBACK, hnsRequestedDuration, 0, pDeviceFormat, nullptr);
exit_on_failed(hr);
// 获得缓冲区大小
UINT32 bufferFrameCount{};
hr = pAudioClient->GetBufferSize(&bufferFrameCount);
exit_on_failed(hr);
// 由 IAudioClient 对象 得到 IAudioCaptureClient 对象,也就是将音频播放设备视为录音设备
CComPtr<IAudioCaptureClient> pCaptureClient;
hr = pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pCaptureClient);
exit_on_failed(hr);
// 开始录音
hr = pAudioClient->Start();
exit_on_failed(hr);
const REFERENCE_TIME hnsActualDuration = (long long)REFTIMES_PER_SEC * bufferFrameCount / pDeviceFormat->nSamplesPerSec;
std::ofstream ofile("./out.wav", std::ios::binary);
if (!ofile) {
exit(-1);
}
// 写入各种 header 信息
constexpr UINT32 sizePlaceholder{};
// master RIFF chunk
ofile.write("RIFF", 4);
ofile.write((const char*)&sizePlaceholder, 4);
ofile.write("WAVE", 4);
// 12
// fmt chunk
ofile.write("fmt ", 4);
UINT32 fmt_ckSize = sizeof(WAVEFORMATEX) + pDeviceFormat->cbSize;
ofile.write((const char*)&fmt_ckSize, 4);
{
auto p = pDeviceFormat.Detach();
ofile.write((const char*)p, fmt_ckSize);
pDeviceFormat.Attach(p);
}
// 8 + fmt_ckSize
// fact chunk
bool has_fact_chunt = pDeviceFormat->wFormatTag != WAVE_FORMAT_PCM;
if (has_fact_chunt) {
ofile.write("fact", 4);
UINT32 fact_ckSize = 4;
ofile.write((const char*)&fact_ckSize, 4);
DWORD dwSampleLength{};
ofile.write((const char*)&dwSampleLength, 4);
}
// 12
// data chunk
ofile.write("data", 4);
ofile.write((const char*)&sizePlaceholder, 4);
UINT32 data_ckSize = 0; // samples data 的大小
UINT32 frame_count = 0; // 帧数
constexpr int max_duration = 60; // 录制 60s
int seconds{}; // 已经录制的时间
time_t t_begin = time(NULL);
//UINT32
do {
// 睡眠一定时间,防止CPU占用率高
Sleep(9);
BYTE* pData{}; // samples 数据
UINT32 numFramesAvailable{}; // 缓冲区有多少帧
DWORD dwFlags{};
hr = pCaptureClient->GetBuffer(&pData, &numFramesAvailable, &dwFlags, NULL, NULL);
exit_on_failed(hr);
int frame_bytes = pDeviceFormat->nChannels * pDeviceFormat->wBitsPerSample / 8;
int count = numFramesAvailable * frame_bytes;
ofile.write((const char*)pData, count);
data_ckSize += count;
frame_count += numFramesAvailable;
seconds = frame_count / pDeviceFormat->nSamplesPerSec;
std::cout << "numFramesAvailable: " << numFramesAvailable << " seconds: " << seconds << std::endl;
hr = pCaptureClient->ReleaseBuffer(numFramesAvailable);
exit_on_failed(hr);
} while (seconds < max_duration);
// 检测实际花了多久,实际时间 - max_duration = 延迟
time_t t_end = time(NULL);
std::cout << "use wall clock: " << t_end - t_begin << "s" << std::endl;
if (data_ckSize % 2) {
ofile.put(0);
++data_ckSize;
}
UINT32 wave_ckSize = 4 + (8 + fmt_ckSize) + (8 + data_ckSize);
ofile.seekp(4);
ofile.write((const char*)&wave_ckSize, 4);
if (has_fact_chunt) {
ofile.seekp(12 + (8 + fmt_ckSize) + 8);
ofile.write((const char*)&frame_count, 4);
}
ofile.seekp(12 + (8 + fmt_ckSize) + 12 + 4);
ofile.write((const char*)&data_ckSize, 4);
ofile.close();
//所有 COM 对象和 Heap 都会自动释放
}
void printEndpoints(CComPtr<IMMDeviceCollection> pColletion)
{
HRESULT hr{};
UINT count{};
hr = pColletion->GetCount(&count);
exit_on_failed(hr);
for (UINT i = 0; i < count; ++i) {
CComPtr<IMMDevice> pEndpoint;
hr = pColletion->Item(i, &pEndpoint);
exit_on_failed(hr);
CComHeapPtr<WCHAR> pwszID;
hr = pEndpoint->GetId(&pwszID);
exit_on_failed(hr);
CComPtr<IPropertyStore> pProps;
hr = pEndpoint->OpenPropertyStore(STGM_READ, &pProps);
exit_on_failed(hr);
PROPVARIANT varName;
PropVariantInit(&varName);
hr = pProps->GetValue(PKEY_Device_FriendlyName, &varName);
exit_on_failed(hr);
std::cout << wchars_to_mbs(varName.pwszVal) << std::endl;
PropVariantClear(&varName);
}
}
constexpr inline void exit_on_failed(HRESULT hr) {
if (FAILED(hr)) {
exit(-1);
}
}
// 汉字会有编码问题,全部转成窄字符
std::string wchars_to_mbs(const wchar_t* src)
{
UINT cp = GetACP();
int ccWideChar = (int)wcslen(src);
int n = WideCharToMultiByte(cp, 0, src, ccWideChar, 0, 0, 0, 0);
std::vector<char> buf(n);
WideCharToMultiByte(cp, 0, src, ccWideChar, buf.data(), (int)buf.size(), 0, 0);
std::string dst(buf.data(), buf.size());
return dst;
}
使用 Windows Core Audio APs 进行 Loopback Recording 并生成 WAV 文件的更多相关文章
- windows core audio apis
这个播放流程有一次当初不是很理解,做个记录,代码中的中文部分,原文档是有解释的:To move a stream of rendering data through the endpoint buff ...
- windows&lunix下node.js实现模板化生成word文件
最近在做了一个小程序!里面有个功能就是根据用户提交的数据,自动生成一份word文档返回给用户.我也是第一次做这功能,大概思路就是先自己弄一份word模板,后台接受小程序发过来的数据,再根据这些数据将相 ...
- Core Audio(一)
Core Audio APIs core audio apis是vista之后引入的,不使用与之前的windows版本:core audio apis提供访问endpoint devices,比如耳机 ...
- Core Audio 在Vista/Win7上实现
应用范围:Vista / win7, 不支持XP 1. 关于Windows Core Auido APIs 在Windowss Vista及Windows 7操作系统下,微软为应用程序提供了一套新的音 ...
- Core Audio(二)
用户模式音频组件 在windows vista中,core audio apis充当用户模式音频子系统的基础,core audio apis作为用户模式系统组件的一个thin layer,它用来将用户 ...
- Windows 7上安装Microsoft Loopback Adapter(微软环回网卡)
Oracle 安装过程中,先决条件检查遇到如下错误: 正在检查网络配置要求... 检查完成.此次检查的总体结果为: 失败 <<<< 问题: 安装检测到系统的主 IP 地址是 ...
- 使用Core Audio实现VoIP通用音频模块
最近一直在做iOS音频技术相关的项目,由于单项直播SDK,互动直播SDK(iOS/Mac),短视频SDK,都会用到音频技术,因此在这里收集三个SDK的音频技术需求,开发一个通用的音频模块用于三个SDK ...
- 重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作
原文:重新想象 Windows 8 Store Apps (24) - 文件系统: Application Data 中的文件操作, Package 中的文件操作, 可移动存储中的文件操作 [源码下载 ...
- windows下Android利用ant自动编译、修改配置文件、批量多渠道,打包生成apk文件
原创文章,转载请注明:http://www.cnblogs.com/ycxyyzw/p/4535459.html android 程序打包成apk,如果在是命令行方式,一般都要经过如下步骤: 1.用a ...
随机推荐
- MySQL为什么"错误"选择代价更大的索引
欢迎来到 GreatSQL社区分享的MySQL技术文章,如有疑问或想学习的内容,可以在下方评论区留言,看到后会进行解答 MySQL优化器索引选择迷思. 高鹏(八怪)对本文亦有贡献. 1. 问题描述 群 ...
- Windows 查看端口占用并关闭
在启动服务的时候,可能会遇到端口被占用的情况. 这时候就需要知道哪个服务占用了这个端口,并将其关闭. 然后再启动服务就不会存在端口占用了. 这里以 Tomcat 的默认端口 8080 为例. 打开命令 ...
- 如何在CSS中使用变量
前言 CSS变量(官方称为自定义属性)是用户定义的值,它可以在你的代码库中设置一次并多次使用.它们使管理颜色.字体.大小和动画值变得更加容易,并确保整个web应用的一致性. 举个例子,你可以将品牌颜色 ...
- linux 3.10 一个扇区异常可能引发的hung
最近遇到一例3.10内核的crash: [ 4109.682163] "echo 0 > /proc/sys/kernel/hung_task_timeout_secs" d ...
- 【Django】DRF开发中的一些技巧记录
问题记录 问题1:信号没有按预期触发 描述 编写了信号函数后,并没有如预期一般在必要时候触发,函数如下: @receiver(signals.post_save, sender=Prometheus) ...
- SpringMVC 06: 日期类型的变量的注入和显示
日期处理和日期显示 日期处理 此时SpringMVC的项目配置和SpringMVC博客集中(指SpringMVC 02)配置相同 日期处理分为单个日期处理和类中全局日期处理 单个日期处理: 使用@Da ...
- openstack中Nova组件简解
一.Nova组件概述 计算节点通过Nova Computer进行虚拟机创建,通过libvirt调用kvm创建虚拟机,nova之间通信通过rabbitMQ队列进行通信. Nova位于Openstack架 ...
- 原生JavaScript对【DOM元素】的操作——增、删、改、查
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- this的指向问题、bind/call/apply改变this指向
this的指向问题 全局作用域下的this指向 无论是否是严格模式,全局作用域下的this始终指向window 函数内部的this 严格模式下: function test() { 'use stri ...
- Ubuntu22.04 安装配置流水账
前两天为了测一个CH340的bug, 装了三遍20.04. bug解决完, 心想反正也要重新装各种软件, 不如直接装22.04吧. 把涉及的安装记录一下方便将来参考. 制作启动U盘 在Ubuntu网站 ...