-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoopbackCapture.cpp
More file actions
378 lines (331 loc) · 13.7 KB
/
LoopbackCapture.cpp
File metadata and controls
378 lines (331 loc) · 13.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include <windows.h>
#include <mmdeviceapi.h>
#include <audioclient.h>
#include <avrt.h>
#include <fstream>
#include <iostream>
#include <cstdint>
#include <string>
#include <direct.h> // For _getcwd
#include <vector> // Added for std::vector
#pragma comment(lib, "ole32.lib")
#pragma comment(lib, "avrt.lib")
// WAV file header structure
struct WAVHeader {
char riff[4] = {'R', 'I', 'F', 'F'};
uint32_t chunkSize;
char wave[4] = {'W', 'A', 'V', 'E'};
char fmt[4] = {'f', 'm', 't', ' '};
uint32_t subchunk1Size = 16;
uint16_t audioFormat = 1; // PCM
uint16_t numChannels;
uint32_t sampleRate;
uint32_t byteRate;
uint16_t blockAlign;
uint16_t bitsPerSample;
char data[4] = {'d', 'a', 't', 'a'};
uint32_t dataSize;
};
// Function to write WAV header
void WriteWAVHeader(std::ofstream& outFile, WAVEFORMATEX* pwfx, uint32_t dataSize) {
WAVHeader header;
header.numChannels = pwfx->nChannels;
header.sampleRate = pwfx->nSamplesPerSec;
header.bitsPerSample = pwfx->wBitsPerSample;
header.byteRate = pwfx->nAvgBytesPerSec;
header.blockAlign = pwfx->nBlockAlign;
header.dataSize = dataSize;
header.chunkSize = 36 + dataSize;
outFile.write(reinterpret_cast<const char*>(&header), sizeof(WAVHeader));
}
// Function to get full path of output file
std::string GetOutputPath(const std::string& fileName) {
char currentDir[MAX_PATH];
if (_getcwd(currentDir, MAX_PATH) != nullptr) {
return std::string(currentDir) + "\\" + fileName;
}
return fileName; // Fallback to just the filename
}
int main() {
// Print current directory
char currentDir[MAX_PATH];
if (_getcwd(currentDir, MAX_PATH) != nullptr) {
std::cout << "Current directory: " << currentDir << std::endl;
}
HRESULT hr;
hr = CoInitialize(nullptr);
if (FAILED(hr)) {
std::cerr << "CoInitialize failed: " << std::hex << hr << std::endl;
return -1;
}
IMMDeviceEnumerator* pEnumerator = nullptr;
hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), nullptr, CLSCTX_ALL,
__uuidof(IMMDeviceEnumerator), (void**)&pEnumerator);
if (FAILED(hr)) {
std::cerr << "CoCreateInstance failed: " << std::hex << hr << std::endl;
CoUninitialize();
return -1;
}
IMMDevice* pDevice = nullptr;
hr = pEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &pDevice);
pEnumerator->Release();
if (FAILED(hr)) {
std::cerr << "GetDefaultAudioEndpoint failed: " << std::hex << hr << std::endl;
CoUninitialize();
return -1;
}
IAudioClient* pAudioClient = nullptr;
hr = pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, nullptr, (void**)&pAudioClient);
pDevice->Release();
if (FAILED(hr)) {
std::cerr << "Device Activate failed: " << std::hex << hr << std::endl;
CoUninitialize();
return -1;
}
WAVEFORMATEX* pwfx = nullptr;
hr = pAudioClient->GetMixFormat(&pwfx);
if (FAILED(hr)) {
std::cerr << "GetMixFormat failed: " << std::hex << hr << std::endl;
pAudioClient->Release();
CoUninitialize();
return -1;
}
// Print audio format information for debugging
std::cout << "Audio Format: " << std::endl;
std::cout << " Format Tag: " << pwfx->wFormatTag << std::endl;
std::cout << " Channels: " << pwfx->nChannels << std::endl;
std::cout << " Sample Rate: " << pwfx->nSamplesPerSec << std::endl;
std::cout << " Bits Per Sample: " << pwfx->wBitsPerSample << std::endl;
std::cout << " Block Align: " << pwfx->nBlockAlign << std::endl;
std::cout << " Bytes Per Second: " << pwfx->nAvgBytesPerSec << std::endl;
// Check if we need to convert the format to PCM
// WAVE_FORMAT_IEEE_FLOAT = 3
bool isFloat = (pwfx->wFormatTag == 3);
// If format is extensible, check if it's float
if (pwfx->wFormatTag == WAVE_FORMAT_EXTENSIBLE && pwfx->cbSize >= 22) {
// Extensible format
WAVEFORMATEXTENSIBLE* pwfxext = reinterpret_cast<WAVEFORMATEXTENSIBLE*>(pwfx);
// Check first 4 bytes of GUID to see if it's float format
// KSDATAFORMAT_SUBTYPE_IEEE_FLOAT first DWORD is 0x00000003
DWORD formatType = pwfxext->SubFormat.Data1;
isFloat = (formatType == 3);
}
// If format is not PCM, create a compatible PCM format
bool customFormat = false;
if (pwfx->wFormatTag != WAVE_FORMAT_PCM) {
// Try to find a compatible PCM format
std::cout << "Not a standard PCM format, trying to find compatible format..." << std::endl;
// Create a PCM format with the same number of channels and sample rate
WAVEFORMATEX pcmFormat;
pcmFormat.wFormatTag = WAVE_FORMAT_PCM;
pcmFormat.nChannels = pwfx->nChannels;
pcmFormat.nSamplesPerSec = pwfx->nSamplesPerSec;
pcmFormat.wBitsPerSample = 16; // Standard 16-bit PCM
pcmFormat.nBlockAlign = pcmFormat.nChannels * (pcmFormat.wBitsPerSample / 8);
pcmFormat.nAvgBytesPerSec = pcmFormat.nSamplesPerSec * pcmFormat.nBlockAlign;
pcmFormat.cbSize = 0;
// Use the new format
CoTaskMemFree(pwfx);
pwfx = (WAVEFORMATEX*)CoTaskMemAlloc(sizeof(WAVEFORMATEX));
memcpy(pwfx, &pcmFormat, sizeof(WAVEFORMATEX));
customFormat = true;
std::cout << "Using custom PCM format:" << std::endl;
std::cout << " Channels: " << pwfx->nChannels << std::endl;
std::cout << " Sample Rate: " << pwfx->nSamplesPerSec << std::endl;
std::cout << " Bits Per Sample: " << pwfx->wBitsPerSample << std::endl;
}
hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_LOOPBACK,
0, 0, pwfx, nullptr);
if (FAILED(hr)) {
std::cerr << "AudioClient Initialize failed: " << std::hex << hr << std::endl;
if (customFormat) {
std::cout << "Failed with custom format, trying original format..." << std::endl;
CoTaskMemFree(pwfx);
hr = pAudioClient->GetMixFormat(&pwfx);
if (SUCCEEDED(hr)) {
hr = pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
AUDCLNT_STREAMFLAGS_LOOPBACK,
0, 0, pwfx, nullptr);
}
}
// If still failing, exit
if (FAILED(hr)) {
CoTaskMemFree(pwfx);
pAudioClient->Release();
CoUninitialize();
return -1;
}
}
IAudioCaptureClient* pCaptureClient = nullptr;
hr = pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pCaptureClient);
if (FAILED(hr)) {
std::cerr << "GetService failed: " << std::hex << hr << std::endl;
pAudioClient->Release();
CoTaskMemFree(pwfx);
CoUninitialize();
return -1;
}
hr = pAudioClient->Start();
if (FAILED(hr)) {
std::cerr << "AudioClient Start failed: " << std::hex << hr << std::endl;
pCaptureClient->Release();
pAudioClient->Release();
CoTaskMemFree(pwfx);
CoUninitialize();
return -1;
}
// Use full path for output file
std::string outputPath = GetOutputPath("output.wav");
std::cout << "Will save to: " << outputPath << std::endl;
std::ofstream outFile(outputPath, std::ios::binary);
if (!outFile) {
std::cerr << "Failed to open " << outputPath << " for writing." << std::endl;
std::cerr << "Error code: " << GetLastError() << std::endl;
pAudioClient->Stop();
pCaptureClient->Release();
pAudioClient->Release();
CoTaskMemFree(pwfx);
CoUninitialize();
return -1;
}
// Write placeholder for WAV header
WAVHeader placeholderHeader = {};
outFile.write(reinterpret_cast<const char*>(&placeholderHeader), sizeof(WAVHeader));
if (!outFile) {
std::cerr << "Failed to write WAV header placeholder" << std::endl;
outFile.close();
pAudioClient->Stop();
pCaptureClient->Release();
pAudioClient->Release();
CoTaskMemFree(pwfx);
CoUninitialize();
return -1;
}
UINT32 packetLength = 0;
BYTE* pData;
DWORD flags;
UINT32 numFrames;
uint32_t totalDataSize = 0;
bool isRecording = true;
std::cout << "Recording... Press Ctrl+C to stop." << std::endl;
std::cout << "If Ctrl+C doesn't work, close this window after a few seconds." << std::endl;
// Setup signal handler to gracefully exit on Ctrl+C
SetConsoleCtrlHandler([](DWORD ctrlType) -> BOOL {
if (ctrlType == CTRL_C_EVENT) {
return TRUE; // We handled the event
}
return FALSE;
}, TRUE);
// Record for 30 seconds maximum (as a safety measure)
DWORD startTime = GetTickCount();
DWORD maxDuration = 30 * 1000; // 30 seconds in milliseconds
while (isRecording && (GetTickCount() - startTime < maxDuration)) {
hr = pCaptureClient->GetNextPacketSize(&packetLength);
if (FAILED(hr)) {
std::cerr << "GetNextPacketSize failed: " << std::hex << hr << std::endl;
break;
}
if (packetLength == 0) {
Sleep(10);
continue;
}
hr = pCaptureClient->GetBuffer(&pData, &numFrames, &flags, nullptr, nullptr);
if (FAILED(hr)) {
std::cerr << "GetBuffer failed: " << std::hex << hr << std::endl;
break;
}
if (flags & AUDCLNT_BUFFERFLAGS_SILENT) {
// Silent data, fill with zeros
std::vector<BYTE> silentData(numFrames * pwfx->nBlockAlign, 0);
outFile.write(reinterpret_cast<const char*>(silentData.data()), silentData.size());
totalDataSize += silentData.size();
} else {
// Handle float audio format conversion if needed
if (isFloat && pwfx->wBitsPerSample == 32) {
float* floatData = reinterpret_cast<float*>(pData);
UINT32 numSamples = numFrames * pwfx->nChannels;
std::vector<int16_t> pcmData(numSamples);
// Convert float (-1.0 to 1.0) to int16 (-32768 to 32767)
for (UINT32 i = 0; i < numSamples; i++) {
float sample = floatData[i];
// Clamp the float value between -1.0 and 1.0
if (sample > 1.0f) sample = 1.0f;
if (sample < -1.0f) sample = -1.0f;
// Convert to int16
pcmData[i] = static_cast<int16_t>(sample * 32767.0f);
}
// Write the converted PCM data
outFile.write(reinterpret_cast<const char*>(pcmData.data()), numSamples * sizeof(int16_t));
totalDataSize += numSamples * sizeof(int16_t);
} else {
// Just write the original data if it's already PCM
UINT32 bytesToWrite = numFrames * pwfx->nBlockAlign;
outFile.write(reinterpret_cast<const char*>(pData), bytesToWrite);
totalDataSize += bytesToWrite;
}
if (!outFile) {
std::cerr << "Failed to write audio data!" << std::endl;
break;
}
}
hr = pCaptureClient->ReleaseBuffer(numFrames);
if (FAILED(hr)) {
std::cerr << "ReleaseBuffer failed: " << std::hex << hr << std::endl;
break;
}
// Check if file is still good
if (!outFile) {
std::cerr << "Output file error occurred!" << std::endl;
break;
}
}
// Stop recording
hr = pAudioClient->Stop();
if (FAILED(hr)) {
std::cerr << "AudioClient Stop failed: " << std::hex << hr << std::endl;
}
// Make sure we have valid data
if (totalDataSize > 0 && outFile) {
// Write the actual WAV header now that we know the data size
outFile.seekp(0, std::ios::beg);
if (!outFile) {
std::cerr << "Failed to seek to beginning of file!" << std::endl;
} else {
WriteWAVHeader(outFile, pwfx, totalDataSize);
if (!outFile) {
std::cerr << "Failed to write WAV header!" << std::endl;
}
}
} else {
std::cerr << "No audio data was captured or file error occurred!" << std::endl;
}
outFile.flush();
outFile.close();
pCaptureClient->Release();
pAudioClient->Release();
CoTaskMemFree(pwfx);
CoUninitialize();
std::cout << "Recording stopped. Total data size: " << totalDataSize << " bytes" << std::endl;
std::cout << "Saved to " << outputPath << std::endl;
// Test if the file is accessible and valid
std::ifstream testFile(outputPath, std::ios::binary);
if (testFile) {
WAVHeader testHeader;
testFile.read(reinterpret_cast<char*>(&testHeader), sizeof(WAVHeader));
if (testFile) {
std::cout << "WAV file successfully created and verified." << std::endl;
std::cout << " RIFF header: " << testHeader.riff[0] << testHeader.riff[1]
<< testHeader.riff[2] << testHeader.riff[3] << std::endl;
std::cout << " File size: " << (testHeader.chunkSize + 8) << " bytes" << std::endl;
std::cout << " Channels: " << testHeader.numChannels << std::endl;
std::cout << " Sample rate: " << testHeader.sampleRate << " Hz" << std::endl;
} else {
std::cerr << "Created file appears to be invalid - couldn't read header." << std::endl;
}
testFile.close();
} else {
std::cerr << "Unable to open the created file for verification." << std::endl;
}
return 0;
}