-
Notifications
You must be signed in to change notification settings - Fork 0
/
level.cpp
153 lines (127 loc) · 4.77 KB
/
level.cpp
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
#include <iostream>
#include <Windows.h>
#include <vector>
#include <cmath>
#include <mmeapi.h>
#include "Unit1.h"
#include "Unit2.h"
using namespace std;
short int waveIn[4410];
float GetBufferData(short* buffer);
int scaleBufferData(float dB);
int level() {
const int NUMPTS = 4410;
int sampleRate = 44100;
HWAVEIN hWaveIn;
MMRESULT result;
WAVEFORMATEX pFormat;
pFormat.wFormatTag = WAVE_FORMAT_PCM;
pFormat.nChannels = 1;
pFormat.nSamplesPerSec = sampleRate;
pFormat.nAvgBytesPerSec = sampleRate * 2;
pFormat.nBlockAlign = 2;
pFormat.wBitsPerSample = 16;
pFormat.cbSize = 0;
result = waveInOpen(&hWaveIn, WAVE_MAPPER, &pFormat, 0L, 0L, WAVE_FORMAT_DIRECT);
if (result == MMSYSERR_ALLOCATED) {
Application->MessageBox(L"In function waveInOpen: Specified resource is already allocated.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_BADDEVICEID) {
Application->MessageBox(L"In function waveInOpen: Specified device identifier is out of range.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NODRIVER) {
Application->MessageBox(L"In function waveInOpen: No device driver is present.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NOMEM) {
Application->MessageBox(L"In function waveInOpen: Unable to allocate or lock memory.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == WAVERR_BADFORMAT) {
Application->MessageBox(L"In function waveInOpen: Attempted to open with an unsupported waveform-audio format.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
WAVEHDR WaveInHdr;
WaveInHdr.lpData = (LPSTR)waveIn;
WaveInHdr.dwBufferLength = NUMPTS * 2;
WaveInHdr.dwBytesRecorded = 0;
WaveInHdr.dwUser = 0L;
WaveInHdr.dwFlags = 0L;
WaveInHdr.dwLoops = 0L;
waveInPrepareHeader(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
result = waveInAddBuffer(hWaveIn, &WaveInHdr, sizeof(WAVEHDR));
if (result == MMSYSERR_INVALHANDLE) {
Application->MessageBox(L"In function waveInAddBuffer: Specified device handle is invalid.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NODRIVER) {
Application->MessageBox(L"In function waveInAddBuffer: No device driver is present.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NOMEM) {
Application->MessageBox(L"In function waveInAddBuffer: Unable to allocate or lock memory.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == WAVERR_UNPREPARED) {
Application->MessageBox(L"In function waveInAddBuffer: The buffer pointed to by the pwh parameter hasn't been prepared.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
result = waveInStart(hWaveIn);
if (result == MMSYSERR_INVALHANDLE) {
Application->MessageBox(L"In function waveInStart: Specified device handle is invalid.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NODRIVER) {
Application->MessageBox(L"In function waveInStart: No device driver is present.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
if (result == MMSYSERR_NOMEM) {
Application->MessageBox(L"In function waveInStart: Unable to allocate or lock memory.", L"Error", MB_OK | MB_ICONERROR);
myThread->Suspend();
Application->Terminate();
}
Sleep(100);
while ((WaveInHdr.dwFlags & WHDR_DONE) == 0)
Sleep(10);
waveInClose(hWaveIn);
return scaleBufferData(GetBufferData(waveIn));
}
float GetBufferData(short* buffer) {
float dB = 0;
long long sum = 0;
for (int i = 0; i < 4410; i++) {
sum += abs(buffer[i]);
}
float avg = sum / 4410;
short max = 0;
for (int i = 0; i < 4410; i++) {
if (abs(buffer[i]) > max) {
max = abs(buffer[i]);
}
}
avg = (float)max;
dB = (avg > 0) ? 20 * log10(avg / 32768) : -1000000;
return dB;
}
int scaleBufferData(float dB) {
double REFERENCE = 32768;
double MIN_DB = 20 * log10(1.0 / REFERENCE);
double MAX_DB = 0;
int nPos = (int)(((double)dB - MIN_DB) * 100 / (MAX_DB - MIN_DB));
if (nPos < 0)
return 0;
return nPos;
}