-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (111 loc) · 3.76 KB
/
Copy pathmain.cpp
File metadata and controls
134 lines (111 loc) · 3.76 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
#include <iostream>
#include <cmath>
#include <vector>
#include <windows.h>
#include <mmsystem.h>
#include <thread>
#include <atomic>
#include <conio.h>
#pragma comment(lib, "winmm.lib")
#define SAMPLE_RATE 44100
#define NUM_CHANNELS 1
#define BITS_PER_SAMPLE 16
#define BUFFER_SIZE 44100
std::atomic<bool> running(true);
std::atomic<bool> paused(false);
double calculateRMS(const std::vector<int>& samples) {
double sum = 0.0;
for (int sample : samples) {
sum += sample * sample;
}
return std::sqrt(sum / samples.size());
}
double rmsToDecibels(double rms) {
const double reference = 1.0;
if (rms == 0) {
return -std::numeric_limits<double>::infinity();
}
return 20 * std::log10(rms / reference);
}
void handleInput() {
while (running) {
int ch = _getch();
if (ch == 27) {
running = false;
} else if (ch == 13) {
paused = !paused;
if (paused) {
std::cout << "\rPAUSED " << std::flush;
}
}
}
}
void CALLBACK waveInProc(
HWAVEIN hwi, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
if (uMsg == WIM_DATA) {
WAVEHDR* pWaveHdr = (WAVEHDR*)dwParam1;
std::vector<short> buffer((short*)pWaveHdr->lpData, (short*)pWaveHdr->lpData + pWaveHdr->dwBytesRecorded / sizeof(short));
if (!paused) {
std::vector<int> audioSamples(buffer.begin(), buffer.end());
double rms = calculateRMS(audioSamples);
double decibels = rmsToDecibels(rms);
std::cout << "\rCurrent Sound Level: " << decibels << " dB " << std::flush;
}
waveInAddBuffer(hwi, pWaveHdr, sizeof(WAVEHDR));
}
}
int main() {
HWAVEIN hWaveIn;
WAVEFORMATEX waveFormat;
WAVEHDR waveHeader;
std::vector<short> buffer(BUFFER_SIZE);
MMRESULT result;
waveFormat.wFormatTag = WAVE_FORMAT_PCM;
waveFormat.nChannels = NUM_CHANNELS;
waveFormat.nSamplesPerSec = SAMPLE_RATE;
waveFormat.wBitsPerSample = BITS_PER_SAMPLE;
waveFormat.nBlockAlign = (NUM_CHANNELS * BITS_PER_SAMPLE) / 8;
waveFormat.nAvgBytesPerSec = SAMPLE_RATE * waveFormat.nBlockAlign;
waveFormat.cbSize = 0;
result = waveInOpen(&hWaveIn, WAVE_MAPPER, &waveFormat, (DWORD_PTR)waveInProc, 0, CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to open audio input device." << std::endl;
return 1;
}
waveHeader.lpData = reinterpret_cast<LPSTR>(buffer.data());
waveHeader.dwBufferLength = BUFFER_SIZE * sizeof(short);
waveHeader.dwBytesRecorded = 0;
waveHeader.dwFlags = 0;
waveHeader.dwLoops = 0;
result = waveInPrepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to prepare header." << std::endl;
return 1;
}
result = waveInAddBuffer(hWaveIn, &waveHeader, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to add buffer." << std::endl;
return 1;
}
result = waveInStart(hWaveIn);
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to start recording." << std::endl;
return 1;
}
std::thread inputThread(handleInput);
inputThread.join();
result = waveInStop(hWaveIn);
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to stop recording." << std::endl;
}
result = waveInUnprepareHeader(hWaveIn, &waveHeader, sizeof(WAVEHDR));
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to unprepare header." << std::endl;
}
result = waveInClose(hWaveIn);
if (result != MMSYSERR_NOERROR) {
std::cerr << "Failed to close audio input device." << std::endl;
}
return 0;
}