【问题标题】:API for capturing sound on Windows用于在 Windows 上捕获声音的 API
【发布时间】:2013-06-12 08:42:57
【问题描述】:

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

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

【问题讨论】:

  • 嗯,这取决于你想使用什么:waveIn API、Core Audio API(在 XP 上不可用)、DirectShow?
  • @user1764961 我不想使用特定的东西。现在我处于“研究”阶段,我需要选择一个,但我不知道该选择什么。
  • Steinberg 的 ASIO API 是否超出或无法解决问题。

标签: c++ windows audio


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    看看BASS library

    它是:

    • 跨平台;
    • well documented;
    • 有很大的支持;
    • 易于使用;
    • 有很多插件;
    • 非商业用途免费

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

    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
    

    【讨论】:

    • BASS 库不是免费的
    【解决方案3】:

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

    【讨论】:

      猜你喜欢
      • 2015-03-15
      • 2013-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      相关资源
      最近更新 更多