| 12
 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
 
 |  
#include <iostream>
#include <ObjBase.h>
#include <mmdeviceapi.h>
#include <Audioclient.h>
#include <Functiondiscoverykeys_devpkey.h>
 
#include <ctime>
#include <fstream>
 
using namespace std;
 
#define TMP_FILE "record.tmp"
 
void writeWAVFile(const char* pathFile, WAVEFORMATEX *wfe, UINT32 sampleSize)
{
	int size;
	char *buffer;
 
	int chunkSize = sampleSize + 36;
	int subChunkSize = 16;
	int audioFormat = 1;
 
	ofstream ofs(pathFile, ios::binary);
	ofs << "RIFF";
	ofs << reinterpret_cast<const char *>(&chunkSize);
	ofs << "WAVE";
	ofs << "fmt ";
	ofs << reinterpret_cast<const char *>(&subChunkSize);
	ofs << reinterpret_cast<const char *>(&audioFormat);
	ofs << reinterpret_cast<const char *>(&wfe->nChannels);
	ofs << reinterpret_cast<const char *>(&wfe->nSamplesPerSec);
	ofs << reinterpret_cast<const char *>(&wfe->nAvgBytesPerSec);
	ofs << reinterpret_cast<const char *>(&wfe->nBlockAlign);
	ofs << reinterpret_cast<const char *>(&wfe->wBitsPerSample);
	ofs << "data";
	ofs << reinterpret_cast<const char *>(&sampleSize);
 
	ifstream iTmp(TMP_FILE, ios::binary);
	iTmp.seekg(0, ios::end);
	size = iTmp.tellg();
	iTmp.seekg(0, ios::beg);
 
	if (size > 0)
	{
		buffer = new char[size + 1];
		buffer[size] = '\0';
		iTmp.read(buffer, size);
		ofs << buffer;
 
		delete[] buffer;
		buffer = NULL;
	}
 
	iTmp.close();
	ofs.close();
 
	remove(TMP_FILE);
}
 
int main(int argc, char *argv[])
{
	// Begin program
	IMMDeviceEnumerator *pEnumerator;
 
	CoInitialize(NULL);
	if (CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator), (void**)&pEnumerator) == S_OK)
	{
		IMMDevice *pDevice;
 
		if (pEnumerator->GetDefaultAudioEndpoint(eCapture, eConsole, &pDevice) == S_OK)
		{
			IAudioClient *pAudioClient;
 
			if (pDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL, NULL, (void**)&pAudioClient) == S_OK)
			{
				WAVEFORMATEX *wfe;
				pAudioClient->GetMixFormat(&wfe);
 
				if (pAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, 0, 10000000, 0, wfe, NULL) == S_OK)
				{
					IAudioCaptureClient *pAudioCapture;
 
					if (pAudioClient->GetService(__uuidof(IAudioCaptureClient), (void**)&pAudioCapture) == S_OK)
					{
						// Constants
						const UINT timeout = (argc >= 3) ? (int)(argv[2] - '0') : 5;
						const char* pathFile = (argc >= 2) ? argv[1] : "record.wav";
 
						// Time
						time_t startTime = time(NULL);
 
						// Stream
						ofstream ofs;
 
						// Buffer capture
						UINT32 packetSize;
						BYTE *buffer;
						UINT32 numFramesToRead;
						DWORD flags;
 
						// Infos capture
						UINT32 sampleSize;
 
						// Start capture
						pAudioClient->Start();
 
						while (true)
						{
							time_t now = time(NULL);
							time_t interval = now - startTime;
 
							if (interval >= timeout) break;
							else
							{
								Sleep(32);
 
								pAudioCapture->GetNextPacketSize(&packetSize);
 
								while (packetSize != 0)
								{
									if (pAudioCapture->GetBuffer(&buffer, &numFramesToRead, &flags, NULL, NULL) == S_OK)
									{
										ofs.open(TMP_FILE, ios::binary | ios::app);
										ofs << buffer;
										ofs.close();
										sampleSize += packetSize;
									}
 
									pAudioCapture->ReleaseBuffer(numFramesToRead);
									pAudioCapture->GetNextPacketSize(&packetSize);
								}
 
								system("CLS");
								cout << timeout - interval << endl;
							}
						}
 
						// Ecrit les données dans le fichier WAV
						writeWAVFile(pathFile, wfe, sampleSize);
 
						pAudioClient->Stop();
						pAudioCapture->Release();
					}
				}
 
				pAudioClient->Release();
			}
 
			pDevice->Release();
		}
 
		pEnumerator->Release();
	}
 
	system("PAUSE");
	return 0;
} |