-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathvisualizer.cpp
More file actions
410 lines (366 loc) · 19.6 KB
/
Copy pathvisualizer.cpp
File metadata and controls
410 lines (366 loc) · 19.6 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/*
Terminal Equalizer - A real-time command line audio visualizer
Copyright (C) 2026 Majock Bim
Copyright (C) 2026 Joe R.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "../inc/ui/visualizer.hpp"
#define AsciiRgb(k, r, g, b) "\033[" #k ";2;" #r ";" #g ";" #b "m"
template <typename _divide_number_t, typename _divide_number1_t>
inline _divide_number_t __cdecl SafeDivide(_In_ _divide_number_t a, _In_ _divide_number1_t b) noexcept
{
if (b == 0) {return(1.00f);} return(_divide_number_t(_divide_number1_t(a)/b));
}
std::string RenderEqualizer::getBraille(unsigned char mask) {
std::string s;
s += (char)0xE2;
s += (char)(0xA0 | (mask >> 6));
s += (char)(0x80 | (mask & 0x3F));
return s;
}
void RenderEqualizer::Display() {
int level;
float temp;
float vol;
while(AudioEngine::Get().IsRunning()) {
vol = AudioEngine::Get().GenVolLevel();
temp = vol * 9.0;
level = (int)temp;
if(vol == 0.0) std::cout << "\r" << " " << " " << std::flush;
else std::cout << "\r" << levels[level] << " " << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
}
void RenderEqualizer::DisplayBuffer() {
std::vector<double> CurrentBuffer = AudioEngine::Get().GetCurrentBuffer();
std::cout << "Size of buffer: " << CurrentBuffer.size() << "\n";
for(int i = 0; i < (int)CurrentBuffer.size(); ++i) {
std::cout << i << ". " << CurrentBuffer[i] << "\n";
}
}
bool __cdecl IsCorrectRow(int row) {
int index = 0;
const int Rows[] = {1, 3, 6, 4, 8, 19, 13, 16, 45, 82, 17, 36};
while (index < (int)(sizeof(Rows) / sizeof(const int))) {
if (row == Rows[index]) return true;
index++;
}
return false;
}
void RenderEqualizer::EnableVisualizer(std::vector<double>& freq, std::vector<double>& wave, std::mutex& magMutex, int sampleRate, JsonFileReader& jsonFileReader) {
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
termWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
termHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
numBins = termWidth * 2;
N_BARS = termWidth / 3;
if (numBins < 1) numBins = 1;
if (N_BARS < 1) N_BARS = 1;
unsigned int frameCount = 0;
std::string frame;
frame.reserve(termWidth * termHeight * 10);
int initialMaxBins = std::max(numBins, N_BARS);
barValues.assign(initialMaxBins, 0.0f);
peakValues.assign(initialMaxBins, 0.0f);
peakDecay.assign(initialMaxBins, 0.0f);
waveValues.assign(initialMaxBins, 0.5f);
// Dynamics
std::vector<float> peakVelocity(initialMaxBins, 0.0f);
std::vector<int> peakHold(initialMaxBins, 0);
std::deque<float> agcPeakWindow;
const int AGC_WINDOW_SIZE = 30;
const float AGC_TARGET_PEAK = 1.0f;
const float MAX_AGC_GAIN = 5.0f;
float frameMaxWave = 0.0f;
const struct ThemeModeManager themeModeManager[] = {
{"Default Mode", Mode0},
{"Mode 1", Mode1},
{"Mode 2", Mode2},
{"Mode 3", Mode3},
{"Mode 4", Mode4},
{"Pink Mode", Mode5},
{"Gradient Mode", Mode6}
};
size_t sizeOfTMM = sizeof(themeModeManager) / sizeof(const struct ThemeModeManager);
enum ThemeMode themeMode = Mode0;
std::cout << "\033[?1049h\033[H\033[2J\033[?25l" << std::flush;
bool themeChanged = true;
static bool lastMState = false;
static bool lastVState = false;
// Preferred Tuning
float rollingMax = 90.0f;
float noiseFloor = 65.0f;
while (AudioEngine::Get().IsRunning()) {
// Theme Switching
size_t index0 = 0;
size_t themesArraySize = jsonFileReader.themes.size();
while (index0 < themesArraySize) {
if (GetAsyncKeyState(jsonFileReader.themes.at(index0).key) & 0x8000) {
std::memcpy(&jsonFileReader.currentTheme, &jsonFileReader.themes.at(index0), sizeof(struct Theme));
themeChanged = true;
break;
}
index0++;
}
if (themeChanged) {
size_t tmIndex = 0;
while (tmIndex < sizeOfTMM) {
if (strcmp(themeModeManager[tmIndex].themeModeName, jsonFileReader.currentTheme.themeMode) == 0) {
themeMode = themeModeManager[tmIndex].themeMode;
break;
}
tmIndex++;
}
themeChanged = false;
N_BARS = (themeMode == Mode5 || themeMode == Mode6) ? (termWidth / 3) : ((termWidth - 1) / 2);
if (N_BARS < 1) N_BARS = 1;
int resizeMaxBins = std::max(numBins, N_BARS);
barValues.resize(resizeMaxBins, 0.0f);
peakValues.resize(resizeMaxBins, 0.0f);
peakVelocity.resize(resizeMaxBins, 0.0f);
peakHold.resize(resizeMaxBins, 0);
waveValues.resize(resizeMaxBins, 0.5f);
}
bool currentMState = (GetAsyncKeyState('M') & 0x8000) != 0;
if (currentMState && !lastMState) {
oscilloscopeMode = !oscilloscopeMode;
}
lastMState = currentMState;
bool currentVState = (GetAsyncKeyState('V') & 0x8000) != 0;
if (currentVState && !lastVState) {
disableVolumeScaling = !disableVolumeScaling;
}
lastVState = currentVState;
if (frameCount % 30 == 0) {
GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
int newWidth = csbi.srWindow.Right - csbi.srWindow.Left + 1;
int newHeight = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
if (newWidth != termWidth || newHeight != termHeight) {
termWidth = newWidth; termHeight = newHeight;
numBins = termWidth * 2;
N_BARS = (themeMode == Mode5 || themeMode == Mode6) ? (termWidth / 3) : ((termWidth - 1) / 2);
int resizeMaxBins = std::max(numBins, N_BARS);
barValues.assign(resizeMaxBins, 0.0f);
peakValues.assign(resizeMaxBins, 0.0f);
peakVelocity.assign(resizeMaxBins, 0.0f);
peakHold.assign(resizeMaxBins, 0);
waveValues.assign(resizeMaxBins, 0.5f);
frame.reserve(termWidth * termHeight * 10);
agcPeakWindow.clear();
std::cout << "\033[2J" << std::flush;
}
}
frameCount++;
float masterVol = disableVolumeScaling ? 1.0f : AudioEngine::Get().GenVolLevel();
float frameMax = 0.0f;
bool hasData = false;
{
std::lock_guard<std::mutex> lock(magMutex);
if (!freq.empty()) {
hasData = true;
if (!oscilloscopeMode) {
for (int i = 0; i < N_BARS; i++) {
// Map bars to 20Hz - 16kHz range (most audio is silent above 16k)
float fLow = 20.0f * std::pow(16000.0f / 20.0f, (float)i / N_BARS);
float fHigh = 20.0f * std::pow(16000.0f / 20.0f, (float)(i + 1) / N_BARS);
double binStart = (double)fLow * (double)(freq.size() - 1) / (sampleRate / 2.0);
double binEnd = (double)fHigh * (double)(freq.size() - 1) / (sampleRate / 2.0);
int iStart = std::max(0, std::min((int)std::floor(binStart), (int)freq.size() - 1));
int iEnd = std::max(0, std::min((int)std::ceil(binEnd), (int)freq.size() - 1));
double pVal = -100.0;
for (int j = iStart; j <= iEnd; j++) {
if (freq[j] > pVal) pVal = freq[j];
}
float tilt = 1.0f + (float)i / (float)N_BARS * 0.4f;
pVal *= tilt;
if (pVal > frameMax) frameMax = (float)pVal;
float target = (float)((pVal - noiseFloor) / (rollingMax - noiseFloor));
target = std::max(0.0f, std::min(1.0f, target * masterVol));
target = std::pow(target, 1.3f);
// Bar Falloff
if (target > barValues[i]) {
barValues[i] = target;
} else {
barValues[i] -= 0.035f;
if (barValues[i] < 0.0f) barValues[i] = 0.0f;
}
// Peak Physics
if (target >= peakValues[i]) {
peakValues[i] = target;
peakVelocity[i] = 0.0f;
peakHold[i] = 12;
} else {
if (peakHold[i] > 0) {
peakHold[i]--;
} else {
peakVelocity[i] += 0.0015f;
peakValues[i] -= peakVelocity[i];
}
}
if (peakValues[i] < barValues[i]) {
peakValues[i] = barValues[i];
peakVelocity[i] = 0.00f;
}
}
// Horizontal spectral smoothing (Edges included)
if (N_BARS > 2) {
barValues[0] = barValues[0] * 0.8f + barValues[1] * 0.2f;
for (int i = 1; i < N_BARS - 1; i++) {
barValues[i] = barValues[i] * 0.6f + (barValues[i-1] + barValues[i+1]) * 0.2f;
}
barValues[N_BARS - 1] = barValues[N_BARS - 1] * 0.8f + barValues[N_BARS - 2] * 0.2f;
}
if (frameMax > rollingMax) {
rollingMax = rollingMax * 0.9f + frameMax * 0.1f;
} else {
rollingMax = rollingMax * 0.999f + frameMax * 0.001f;
}
// dynamically adjust noise floor when volume scaling is disabled
// Remove the lower bound limit of rollingMax when volume scaling is disabled, allowing it to adapt to quieter audio levels.
if (rollingMax < 60.0f && !disableVolumeScaling) rollingMax = 60.0f;
if (disableVolumeScaling) {
noiseFloor = noiseFloor * 0.9f + (frameMax - noiseFloor) * 0.1f;
if (noiseFloor > 65.0f) noiseFloor = 65.0f;
else if (noiseFloor < 0.0f) noiseFloor = 0.0f;
}
else noiseFloor = 65.0f;
} else if (!wave.empty()) {
int offset = 0;
for (int j = 0; j < (int)wave.size() - 1 && j < 500; j++) {
if (wave[j] < 0 && wave[j+1] >= 0) { offset = j; break; }
}
int remainingSamples = (int)wave.size() - offset;
// AGC Calculation by the max wave value in the before 30 frames
agcPeakWindow.push_back(frameMaxWave);
if (agcPeakWindow.size() > AGC_WINDOW_SIZE)
agcPeakWindow.pop_front();
frameMaxWave = 0.0f;
float windowMax = 0.001f;
for (float peak : agcPeakWindow)
if (peak > windowMax) windowMax = peak;
float targetGain = AGC_TARGET_PEAK / windowMax;
if (targetGain > MAX_AGC_GAIN) targetGain = MAX_AGC_GAIN;
for (int i = 0; i < numBins; i++) {
int idx = offset + (int)(i * (float)remainingSamples / numBins);
if (idx >= (int)wave.size()) idx = (int)wave.size() - 1;
float target = (float)wave[idx] * masterVol;
frameMaxWave = std::max(frameMaxWave, std::abs(target));
target *= targetGain;
target = std::max(-1.0f, std::min(1.0f, target * 0.9f));
float targetCentered = (target + 1.0f) * 0.5f;
waveValues[i] = waveValues[i] * 0.4f + targetCentered * 0.6f;
}
}
}
}
if (!hasData) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); continue; }
frame.clear();
int renderHeight = termHeight - 1;
if (renderHeight < 1) renderHeight = 1;
char themeColor[64];
sprintf_s(themeColor, sizeof(themeColor), "\033[38;2;%d;%d;%dm", (int)(jsonFileReader.currentTheme.colorRed * 255), (int)(jsonFileReader.currentTheme.colorGreen * 255), (int)(jsonFileReader.currentTheme.colorBlue * 255));
if (oscilloscopeMode) {
for (int row = 0; row < renderHeight; row++) {
int blockRow = renderHeight - 1 - row;
int dotBase = blockRow * 4;
for (int col = 0; col < termWidth; col++) {
int binL = col * 2; int binR = col * 2 + 1;
unsigned char mask = 0;
auto drawSegment = [&](int bIdx, int mCol) {
float val = waveValues[bIdx];
float prevVal = (bIdx > 0) ? waveValues[bIdx - 1] : val;
int h1 = (int)(prevVal * (renderHeight * 4 - 1));
int h2 = (int)(val * (renderHeight * 4 - 1));
int start = std::min(h1, h2); int end = std::max(h1, h2);
for (int y = start; y <= end; y++) {
if (y >= dotBase && y < dotBase + 4) {
int d = y - dotBase;
if (mCol == 0) {
if (d == 0) mask |= 0x40; else if (d == 1) mask |= 0x04; else if (d == 2) mask |= 0x02; else if (d == 3) mask |= 0x01;
} else {
if (d == 0) mask |= 0x80; else if (d == 1) mask |= 0x20; else if (d == 2) mask |= 0x10; else if (d == 3) mask |= 0x08;
}
}
}
};
drawSegment(binL, 0); if (binR < numBins) drawSegment(binR, 1);
if (mask > 0) { frame += themeColor; frame += getBraille(mask); frame += "\033[0m"; }
else frame += " ";
}
frame += "\033[K";
if (row < renderHeight - 1) frame += '\n';
}
} else if (themeMode == Mode5 || themeMode == Mode6) {
for (int row = 0; row < renderHeight; row++) {
int blockRow = renderHeight - 1 - row;
char currentRowColor[64];
if (themeMode == Mode6) {
float ratio = (float)blockRow / (float)(renderHeight - 1);
int r = (int)(255 * ratio); int g = (int)(255 * (1.0f - ratio));
sprintf_s(currentRowColor, sizeof(currentRowColor), "\033[38;2;%d;%d;%dm", r, g, 0);
} else strcpy_s(currentRowColor, sizeof(currentRowColor), themeColor);
for (int col = 0; col < termWidth; col++) {
int barIdx = col / 3; int colInBar = col % 3;
if (barIdx < N_BARS && colInBar < 2) {
float val = barValues[barIdx];
int h = (int)(val * (renderHeight - 1));
float phVal = peakValues[barIdx];
int ph = (int)(phVal * (renderHeight - 1));
if (blockRow <= h) {
frame += currentRowColor; frame += "█"; frame += "\033[0m";
} else if (blockRow == ph && ph > 0) {
if (themeMode == Mode6) frame += "\033[38;2;128;128;128m";
else frame += currentRowColor;
frame += "─"; frame += "\033[0m";
} else {
frame += " ";
}
} else frame += " ";
}
frame += "\033[K";
if (row < renderHeight - 1) frame += '\n';
}
} else {
const char* UTF8Codes[] = { "─", "│", "─", "│", "┌", "┐", "└", "┘", "├", "┤", "┬", "┴", "┼", "♪", "♫", "♬", "♩", "⣿", "⣶", "⣤", "⣀", "⡇", "#" };
size_t sizeofCodes = sizeof(UTF8Codes) / sizeof(UTF8Codes[0]);
for (int row = renderHeight; row > 0; row--) {
for (int i = 0; i < N_BARS; i++) {
int barHeight = (int)(barValues[i] * renderHeight);
float rowA = float(row) / float(renderHeight);
float rowB = -rowA + 1.0f; float rowC = -fabs(rowA - 0.5f) + 1.0f;
float effect = (themeMode == Mode4) ? barValues[i] * 2.5f : 1.0f;
int color[3] = { (int)((255 * rowA * effect) * jsonFileReader.currentTheme.colorRed), (int)((255 * rowB * effect) * jsonFileReader.currentTheme.colorGreen), (int)((255 * rowC * effect) * jsonFileReader.currentTheme.colorBlue) };
int barTop = 0, barBottom = 0;
if (themeMode == Mode1) { barTop = (renderHeight - barHeight) / 2; barBottom = renderHeight - barTop; }
else if (themeMode == Mode2) { barTop = barHeight; barBottom = barHeight; }
else if (themeMode == ThemeMode::Mode3) { int bh1 = renderHeight - barHeight; barTop = (renderHeight - bh1) / 2; barBottom = renderHeight - barTop; }
char character[2] = {jsonFileReader.currentTheme.customCharacter, '\0'};
char temp[128];
if (themeMode == Mode0) {
if (row <= barHeight) sprintf_s(temp, sizeof(temp), "\033[38;2;%d;%d;%dm%s\033[0m", color[0], color[1], color[2], (jsonFileReader.currentTheme.useRandomCharacters) ? UTF8Codes[rand() % sizeofCodes] : character);
else sprintf_s(temp, sizeof(temp), " ");
} else if (themeMode == Mode1 || themeMode == Mode2 || themeMode == Mode3) {
if (row <= barBottom && row >= barTop) sprintf_s(temp, sizeof(temp), "\033[38;2;%d;%d;%dm%s\033[0m", color[0], color[1], color[2], (jsonFileReader.currentTheme.useRandomCharacters) ? UTF8Codes[rand() % sizeofCodes] : character);
else sprintf_s(temp, sizeof(temp), " ");
} else if (themeMode == Mode4) {
sprintf_s(temp, sizeof(temp), "\033[38;2;%d;%d;%dm%s\033[0m", color[0], color[1], color[2], (jsonFileReader.currentTheme.useRandomCharacters) ? UTF8Codes[rand() % sizeofCodes] : character);
}
frame += temp; frame += " ";
}
frame += "\033[K";
if (row > 1) frame += '\n';
}
}
std::cout << "\033[H" << frame << "\033[J" << std::flush;
std::this_thread::sleep_for(std::chrono::milliseconds(16));
}
std::cout << "\033[2J\033[H\033[?1049l\033[?25h" << std::flush;
}