-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
175 lines (142 loc) · 4.3 KB
/
main.cpp
File metadata and controls
175 lines (142 loc) · 4.3 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
#include <portaudio.h>
#include <iostream>
#include <cmath>
#include <deque>
#include <algorithm>
#include <mutex>
#include <atomic>
#include <csignal>
#include <numeric>
#include <thread>
const int SAMPLE_RATE = 48000;
const int FRAMES_PER_BUFFER = 512;
const int CHANNELS = 2;
const double DB_RANGE_MIN = -120.0;
const double DB_RANGE_MAX = 0.0;
const int BAR_WIDTH = 120;
const int SMOOTHING_WINDOW = 15;
std::mutex data_mutex;
std::deque<double> rms_history;
std::atomic<bool> running(true);
std::atomic<double> peak_db(DB_RANGE_MIN);
const char *GREEN = "\033[32m";
const char *YELLOW = "\033[33m";
const char *RED = "\033[31m";
const char *RESET = "\033[0m";
void signal_handler([[maybe_unused]] int signal) {
running = false;
}
static int audio_callback(
const void *input,
void *output,
unsigned long frame_count,
const PaStreamCallbackTimeInfo *time_info,
PaStreamCallbackFlags status_flags,
void *user_data) {
(void) output;
(void) time_info;
(void) status_flags;
(void) user_data;
const auto *in = (const float *) input;
double sum = 0.0;
for (unsigned i = 0; i < frame_count; i++) {
double sample = (in[i * CHANNELS] + in[i * CHANNELS + 1]) / 2.0;
sum += sample * sample;
}
double rms = sqrt(sum / frame_count);
double db = 20.0 * log10(std::max(rms, 1e-12));
db = std::max(db, DB_RANGE_MIN);
{
std::lock_guard<std::mutex> lock(data_mutex);
rms_history.push_back(db);
if (rms_history.size() > SMOOTHING_WINDOW) {
rms_history.pop_front();
}
if (db > peak_db) {
peak_db = db;
}
}
return paContinue;
}
void draw_vu_meter() {
while (running) {
double db;
double peak;
std::deque<double> history;
{
std::lock_guard<std::mutex> lock(data_mutex);
history = rms_history;
peak = peak_db;
}
if (!history.empty()) {
db = std::accumulate(history.begin(), history.end(), 0.0) / history.size();
} else {
db = DB_RANGE_MIN;
}
double scaled = (db - DB_RANGE_MIN) / (DB_RANGE_MAX - DB_RANGE_MIN);
int filled = static_cast<int>(scaled * BAR_WIDTH);
filled = std::clamp(filled, 0, BAR_WIDTH);
std::string bar = "[";
for (int i = 0; i < BAR_WIDTH; ++i) {
double level = DB_RANGE_MIN + (DB_RANGE_MAX - DB_RANGE_MIN) * (i / static_cast<double>(BAR_WIDTH));
const char *color = GREEN;
if (level >= -18.0) color = YELLOW;
if (level >= -6.0) color = RED;
bar += (i < filled) ? color + std::string("█") : " ";
}
bar += std::string(RESET) + "]";
std::cout << "\r\033[K"
<< bar
<< " " << db << " dB"
<< " | PEAK: " << peak << " dB"
<< std::flush;
static auto last_peak_reset = std::chrono::steady_clock::now();
auto now = std::chrono::steady_clock::now();
if (std::chrono::duration_cast<std::chrono::seconds>(now - last_peak_reset).count() >= 2) {
peak_db = db;
last_peak_reset = now;
}
usleep(50000);
}
}
int main() {
std::signal(SIGINT, signal_handler);
PaError err = Pa_Initialize();
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
return 1;
}
PaStream *stream;
err = Pa_OpenDefaultStream(
&stream,
CHANNELS,
0,
paFloat32,
SAMPLE_RATE,
FRAMES_PER_BUFFER,
audio_callback,
nullptr
);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
Pa_Terminate();
return 1;
}
err = Pa_StartStream(stream);
if (err != paNoError) {
std::cerr << "PortAudio error: " << Pa_GetErrorText(err) << std::endl;
Pa_CloseStream(stream);
Pa_Terminate();
return 1;
}
std::thread vu_thread(draw_vu_meter);
while (running) {
Pa_Sleep(100);
}
Pa_StopStream(stream);
Pa_CloseStream(stream);
Pa_Terminate();
vu_thread.join();
std::cout << "\nExit" << std::endl;
return 0;
}