-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
84 lines (67 loc) · 1.94 KB
/
example.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
#include <iostream>
#include <iomanip>
#include <cstddef>
#include <cmath>
#include <chrono>
#include <thread>
#include <ncurses.h>
#include "OpenALDataFetcher.h"
#include "Spectrum.h"
const size_t fft_size = 4096;
const size_t sample_rate = 44100;
const size_t num_bars = 50;
void draw_bars(float data[], uint start_x, uint start_y, uint height, size_t width) {
for (size_t x = 0; x < width; x++) {
size_t bar_height = round(pow(data[x], 2.f) * height);
for (size_t y = 0; y < height; y++) {
mvaddch(height - y + start_y, x + start_x, y <= bar_height ? '|' : ' ');
}
}
}
void start_curses() {
initscr();
cbreak();
noecho();
nodelay(stdscr, TRUE);
curs_set(0);
}
void stop_curses() {
endwin();
}
int main() {
float audio_data[fft_size];
OpenALDataFetcher audio_fetcher(
sample_rate,
fft_size,
[](const std::vector<std::string> &list) {
stop_curses();
for (size_t i = 0; i < list.size(); ++i) {
std::cout << i << ": " << list[i] << std::endl;
}
size_t device_id = 0;
std::cout << "Enter a device number to use: ";
std::cin >> device_id;
std::cout << "Using device #" << device_id << std::endl << std::endl;
start_curses();
return device_id;
}
);
Spectrum spec(fft_size);
spec.UseLinearNormalisation(1, num_bars);
float bar_data[num_bars];
while (true) {
int ch = getch();
if (ch == 'q')
break;
audio_fetcher.UpdateData();
audio_fetcher.GetData(audio_data);
spec.Update(audio_data);
spec.GetData(20, 5000, sample_rate, bar_data, num_bars);
draw_bars(bar_data, 0, 0, 30, num_bars);
refresh();
std::this_thread::sleep_for(std::chrono::milliseconds(20));
}
stop_curses();
std::cout << "stop" << std::endl;
return 0;
}