forked from EliasOenal/multimon-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
win32_soundin.c
93 lines (86 loc) · 2.57 KB
/
win32_soundin.c
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
#include <Windows.h>
#include <stdio.h>
#define BUFFER_LEN_IN_MS 20
#define BUFFERS 50
int SAMPLES_PER_BUFFER;
WAVEFORMATEX g_WavFmt = {0};
HWAVEIN hWavIn;
void process_buffer(float *float_buf, short *short_buf, unsigned int len);
int Overlap;
float *fbuf;
void CALLBACK waveInProc(HWAVEIN hwi,UINT uMsg,DWORD dwInstance,DWORD dwParam1,DWORD dwParam2)
{
WAVEHDR* pWaveHdr;
SHORT *sp;
switch(uMsg)
{
case MM_WIM_DATA:
pWaveHdr = ((WAVEHDR*)dwParam1 );
sp = (SHORT*)pWaveHdr->lpData;
int i;
for (i=0; i<SAMPLES_PER_BUFFER; i++)
{
fbuf[i+Overlap] = sp[i] /32768.0;
}
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
process_buffer(fbuf, sp, SAMPLES_PER_BUFFER);
memcpy(&fbuf[0],&fbuf[SAMPLES_PER_BUFFER],sizeof(float)*Overlap);
break;
case MM_WIM_OPEN:
break;
case MM_WIM_CLOSE:
break;
}
}
void input_sound(unsigned int sample_rate, unsigned int overlap, const char *ifname)
{
Overlap = overlap;
SAMPLES_PER_BUFFER = (BUFFER_LEN_IN_MS / 1000.0)*sample_rate;
fbuf = (float*)malloc(sizeof(float)*(SAMPLES_PER_BUFFER+Overlap));
hWavIn=0;
g_WavFmt.wFormatTag = WAVE_FORMAT_PCM;
g_WavFmt.nChannels = 1;
g_WavFmt.nSamplesPerSec = sample_rate;
g_WavFmt.wBitsPerSample = 16;
g_WavFmt.nBlockAlign = g_WavFmt.nChannels * g_WavFmt.wBitsPerSample / 8;
g_WavFmt.nAvgBytesPerSec = g_WavFmt.nChannels * g_WavFmt.wBitsPerSample / 8 * g_WavFmt.nSamplesPerSec;
g_WavFmt.cbSize = 0;
if (!waveInGetNumDevs())
{
printf("Default audio device not found!\n");
exit(0);
}
DWORD ret = waveInOpen(&hWavIn, WAVE_MAPPER, &g_WavFmt, 0, 0, WAVE_FORMAT_QUERY);
if (MMSYSERR_NOERROR != ret)
{
printf("Unsupported audio format.\n");
exit(0);
}
ret = waveInOpen(&hWavIn, WAVE_MAPPER, &g_WavFmt,(DWORD_PTR) &waveInProc,0, CALLBACK_FUNCTION);
WAVEHDR *headers[BUFFERS];
int i;
for (i=0; i< BUFFERS; i++)
{
WAVEHDR *WavHdr = (WAVEHDR*)malloc(sizeof(WAVEHDR));
headers[i]=WavHdr;
void* pBuffer = malloc(SAMPLES_PER_BUFFER*sizeof(SHORT));
WavHdr->lpData = (LPSTR)pBuffer;
WavHdr->dwBufferLength = SAMPLES_PER_BUFFER*sizeof(SHORT);
WavHdr->dwBytesRecorded = 0;
WavHdr->dwUser = 0;
WavHdr->dwFlags = 0;
WavHdr->dwLoops = 1;
WavHdr->lpNext = 0;
WavHdr->reserved = 0;
MMRESULT x1= waveInPrepareHeader(hWavIn,WavHdr,sizeof(WAVEHDR));
MMRESULT x2= waveInAddBuffer(hWavIn, WavHdr, sizeof(WAVEHDR));
}
MMRESULT x3=waveInStart(hWavIn);
MMTIME MMTime = {0};
//Do nothing...till user gets tired of us and kills the app.
while(1)
{
Sleep(100);
}
waveInReset(hWavIn);
}