#include #include #inc..."/>

使用WASAPI随机打开默认麦克风并录音

2023-11-24 49 0
// mictest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Shlwapi.h>
#include <MMDeviceAPI.h>
#include <AudioClient.h>

#define SAFE_RELEASE(punk)  \
              if ((punk) != NULL)  \
                { (punk)->Release(); (punk) = NULL; }

void MicTest(ULONGLONG EndTieme)
{
    IMMDeviceEnumerator* deviceEnumerator = NULL;
    HRESULT hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&deviceEnumerator));
    if (FAILED(hr)) 
    {
        printf("Unable to retrieve CoCreateInstance %x\n", hr);
        return;
    }

    IAudioClient* _AudioClient;
    IAudioCaptureClient* _CaptureClient;
    IMMDevice* _Device;

    hr = deviceEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, &_Device);
    if (FAILED(hr))
    {
        printf("Unable to retrieve device %x\n", hr);
        return;
    }


    HANDLE _AudioSamplesReadyEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if (_AudioSamplesReadyEvent == NULL)
    {
        printf("Unable to create samples ready event: %d.\n", GetLastError());
        return;
    }

    hr = _Device->Activate(__uuidof(IAudioClient), CLSCTX_INPROC_SERVER, NULL, reinterpret_cast<void**>(&_AudioClient));
    if (FAILED(hr))
    {
        printf("Unable to activate audio client: %x.\n", hr);
        return;
    }

    WAVEFORMATEX* _MixFormat;
    hr = _AudioClient->GetMixFormat(&_MixFormat);
    if (FAILED(hr))
    {
        printf("Unable to get mix format on audio client: %x.\n", hr);
        return ;
    }

    REFERENCE_TIME hnsDefaultDevicePeriod;
    hr = _AudioClient->GetDevicePeriod(&hnsDefaultDevicePeriod, NULL);
    if (FAILED(hr)) 
    {
        return ;
    }

    hr = _AudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,AUDCLNT_STREAMFLAGS_EVENTCALLBACK, hnsDefaultDevicePeriod, hnsDefaultDevicePeriod, _MixFormat, NULL);
    if (FAILED(hr))
    {
        printf("Unable to initialize audio client: %x.\n", hr);
        return ;
    }

    UINT32              _BufferSize;
    hr = _AudioClient->GetBufferSize(&_BufferSize);
    if (FAILED(hr))
    {
        printf("Unable to get audio client buffer: %x. \n", hr);
        return ;
    }

    hr = _AudioClient->SetEventHandle(_AudioSamplesReadyEvent);
    if (FAILED(hr))
    {
        printf("Unable to set ready event: %x.\n", hr);
        return ;
    }

    hr = _AudioClient->GetService(IID_PPV_ARGS(&_CaptureClient));
    if (FAILED(hr))
    {
        printf("Unable to get new capture client: %x.\n", hr);
        return ;
    }

    hr = _AudioClient->Reset();
    if (FAILED(hr))
    {
        printf("Unable to get new capture client: %x.\n", hr);
        return ;
    }

    hr = _AudioClient->Start();
    if (FAILED(hr))
    {
        printf("Unable to get new capture client: %x.\n", hr);
        return ;
    }

    while (GetTickCount64() < EndTieme)
    {
        DWORD waitResult = WaitForSingleObject(_AudioSamplesReadyEvent, INFINITE);
        BYTE* pData;
        UINT32 framesAvailable;
        DWORD  flags;

        hr = _CaptureClient->GetBuffer(&pData, &framesAvailable, &flags, NULL, NULL);
        if (SUCCEEDED(hr))
        {
            hr = _CaptureClient->ReleaseBuffer(framesAvailable);
            if (FAILED(hr))
            {
                printf("Unable to release capture buffer: %x!\n", hr);
            }
        }
    }

    hr = _AudioClient->Stop();

    CloseHandle(_AudioSamplesReadyEvent);
    SAFE_RELEASE(deviceEnumerator);
    SAFE_RELEASE(_Device);
    SAFE_RELEASE(_AudioClient);
    SAFE_RELEASE(_CaptureClient);

}
int main()
{
    CoInitializeEx(NULL, COINIT_MULTITHREADED);
    for(int i=0;;i++)
    {
        ULONG range = rand() % 100000;
        ULONGLONG EndTime = GetTickCount64() + range;
        printf("%d %dms test\n", i, range);
        MicTest(EndTime);

    }
    return 0;
}

0 篇笔记 写笔记

使用COREAPI获取默认音频属性wstring DeviceAPOInfo::getDefaultDevice(bool input, int role){ wstring result; IMMDeviceEnumerator* enumerator = NULL; ......
APO与其他音频过滤框架(如VST)的区别在于,APO是Windows音频引擎使用的过滤框架。VST:Virtual Studio TechnologyWindows音频引擎是Windows音频堆栈的核心组件。它的作用是在单个应用程序音频流和硬件音频设备之间架起桥梁。因此,它可以处理各种任务......
// mictest.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。//#include #include #include #inc......
要则试麦克风重复打开关闭的功能,如果手动也太麻烦了。为了让打开关闭的时间更随机一点,使用WASAPI做了这么一个小功能。#include #include #include #inc......
OBS插件模块WASAPI用于实现Windows音频的。其源文件目录位于:obs-studio-masterpluginswin-wasapi源文件有:CMakeLists.txtdata //资源文件目录enum-wasapi.cppenum-wasapi.hppplugin-m......
obs_module_load是OBS插件模块必须实现的函数,其对应于结构体obs_module中load函数指针: bool (*load)(void); //函数名必须为obs_module_loadWASAPI的插件函数代码如下:bool obs_module_load(void){......
在 Windows 操作系统中,处理音频缓冲的主要组件包括以下几个:其中各个模块的DLL依赖关系如下:音频各个模块之间的关系图如下: 核心音频 API 包括在 Audioses.dll 和 Mmdevapi.dll 用户模式系统模块中实现的 MMDevice API、WASAPI、DeviceTo......
取消
感谢您的支持,我会继续努力的!
扫码支持
扫码打赏,你说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

您的支持,是我们前进的动力!