2

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

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

4

3 回答 3

3

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

这一切都取决于架构的其余部分。你必须对捕获的 PCM 做一些事情,你可能知道是什么。这应该可以帮助您决定使用什么技术。

于 2013-06-12T09:02:20.703 回答
1

看看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
于 2013-06-12T09:20:10.393 回答
1

下面的代码使用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

于 2021-02-05T01:23:57.637 回答