Mircea Ispas
Mircea Ispas

Reputation: 20780

API for capturing sound on Windows

I need a C++ API to enumerate input devices and capture sound for Windows Vista, Windows 7 and Windows 8. If there is no common API I can use OS specific API for different versions of Windows.

I found some references on Microsoft site, but I don't know what to chose. What do you recommend?

Upvotes: 2

Views: 10291

Answers (3)

A5H1Q
A5H1Q

Reputation: 624

Below code uses winapi to record sound and save it as .wav file

#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;
} 


Using g++ :

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

Note: use the -lWinmm to link Mmsystem.h manually

Upvotes: 1

Andrey Volk
Andrey Volk

Reputation: 3549

Take a look at the BASS library.

It is:

  • cross-platform;
  • well documented;
  • has a great support;
  • easy to use;
  • has lots of plugins;
  • free for non-commercial use.

Get the total number of recording devices currently present:

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

Start recording at 44100hz 16-bit stereo:

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

Upvotes: 1

user1764961
user1764961

Reputation: 693

For waveIn API use waveInGetNumDevs() and waveInGetDevCaps(). For Core Audio API use IMMDeviceEnumerator. For DirectShow read this: http://msdn.microsoft.com/en-us/library/windows/desktop/dd377566(v=vs.85).aspx

It all depends on the rest of the architecture. You have to do something with the captured PCM and you probably know what. That should help you decide what technology to use.

Upvotes: 3

Related Questions