I'm reading documentation such as Capturing a stream or Loopback recording, but I can't find a good reproducible example (with includes, build instructions, etc.) about how to record chunks from a Loopback audio device (sometimes called "What you hear", "Stereo Mix") with Windows WASAPI.
Would you have a simple reproducible example showing how to record audio chunks in a loop from a WASAPI device, in C++?
Here is a similar (working) example in Python:
import soundcard as sc # installed with: pip install soundcard
lb = sc.all_microphones(include_loopback=True)[0]
with lb.recorder(samplerate=44100) as mic:
while True:
data = mic.record(numframes=None)
print(data) # chunks of audio data (448 samples x 2 channels as an array by default)
This is an example of loopback-mode audio capturing.
Based on documents Capturing a stream, make some editions pointed out by Loopback recording as follows:
Missed part in the documents:
CopyData()
and write file functions (WriteWaveHeader()
andFinishWaveFile()
). The following show examples for those functions implementations. Refer to blog Sample - WASAPI loopback capture (record what you hear) for more detailed information.Call
WriteWaveHeader
beforepAudioClient->Start()
. CallFinishWaveFile
afterpAudioClient->Stop()
.As a result, it will record about 10 seconds audio playing on your Windows.
UPDATE #1:
Compile command:
UPDATE #2: