用于在Windows上捕获声音的API

API for capturing sound on Windows

本文关键字:声音 API Windows 用于      更新时间:2023-10-16

我需要一个c++ API来枚举输入设备并捕获Windows Vista, Windows 7和Windows 8的声音。如果没有通用的API,我可以为不同版本的Windows使用特定于操作系统的API。

我在微软网站上找到了一些参考资料,但我不知道该选什么。你有什么建议吗?

对于waveIn API使用waveInGetNumDevs()和waveInGetDevCaps()。对于核心音频API使用IMMDeviceEnumerator。对于DirectShow,请阅读:http://msdn.microsoft.com/en-us/library/windows/desktop/dd377566(v=vs.85).aspx

这完全取决于架构的其余部分。你必须对捕获的PCM做些什么你可能知道该怎么做。这应该有助于你决定使用什么技术。

看一下BASS库

:

  • 跨平台;
  • 有据可查的,
  • 有很大的支持;
  • 易于使用;
  • 有很多插件;
  • 免费供非商业用途

获取当前存在的记录设备总数:

int a, count=0;
BASS_DEVICEINFO info;
for (a=0; BASS_RecordGetDeviceInfo(a, &info); a++)
    if (info.flags&BASS_DEVICE_ENABLED) // device is enabled
        count++; // count it

以44100hz 16位立体声开始录音:

FILE *file;
...
// the recording callback
BOOL CALLBACK MyRecordingWriter(HRECORD handle, void *buf, DWORD len, void *user)
{
    fwrite(buf, 1, len, file); // write the buffer to the file
    return TRUE; // continue recording
}
...
HRECORD record=BASS_RecordStart(44100, 2, 0, MyRecordingWriter, 0); // start recording

下面的代码使用winapi录制声音并将其保存为。wav文件

#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <Mmsystem.h>
#define ALIAS "random_str"
int main(int argc,char *argv[])
{
    printf("|-----------------------|n" 
           "|Simple Winapi Recorder |n" 
           "|By @systheron          |n" 
           "|-----------------------|n");
    char mci_command[100];
    char ReturnString[300];
    int mci_error;
    sprintf(mci_command, "open new type waveaudio alias %s", ALIAS);
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    // set the time format
    sprintf(mci_command,"set %s time format ms", ALIAS);    // just set time format
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    // start recording
    sprintf(mci_command, "record %s notify", ALIAS);
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    printf("Now recording, get key input to stop...n");
    char c= getc(stdin);
    //stop recording
    sprintf(mci_command,"stop %s", ALIAS);
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    // save the file
    sprintf(mci_command, "save %s %s", ALIAS, "random.wav");
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    // close the device
    sprintf(mci_command,"close %s", ALIAS);
    mci_error = mciSendString(mci_command, ReturnString, sizeof(ReturnString), NULL);
    printf("Recording stopped. Congrat, your file is save as: random.wav. n");
    return 0;
} 

使用g++:

g++ index.cpp -o index.exe -lWinmm

注意:使用-lWinmm手动链接Mmsystem.h