-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
146 lines (121 loc) · 3.13 KB
/
main.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
#include "stream.h"
#include "fft.hpp"
#include <ncurses.h>
#include <vector>
#include <thread>
#include <atomic>
#include <algorithm>
const int MinSampleCount = 32;
const int MaxSampleCount = 1 << 16;
const int FrequencyChangeStep = 100;
const int ThresholdChangeStep = 100;
const int SampleRate = 44100;
short buffer[MaxSampleCount];
double amplitudes[MaxSampleCount];
int main() {
initscr();
raw();
noecho();
timeout(100);
int min_frequency = 0;
int max_frequency = 20000;
int threshold = 1000;
int sample_count = 1 << 15;
std::thread sample_thread;
bool running = true;
std::atomic<bool> auto_sample(false);
while(running) {
clear();
printw("Threshold: %d\n", threshold);
printw("Frequency: %d - %d Hz\n", min_frequency, max_frequency);
printw("Sample count: %d\n", sample_count);
printw("=====================\n");
std::vector<std::pair<double, double>> frequencies;
for(int i = 0; i < sample_count / 2; ++i) {
double freq = (double) SampleRate / sample_count * i;
if(freq < min_frequency || freq > max_frequency)
continue;
double amp = amplitudes[i] * 2 / sample_count;
if(amp < threshold)
continue;
frequencies.push_back({amp, freq});
}
std::sort(frequencies.begin(), frequencies.end(), [](auto x, auto y) { return x.first > y.first; });
for(auto& f : frequencies) printw("%lfHz\n", f.second);
if(auto_sample) {
attron(A_BOLD);
mvprintw(3, 3, " AUTO Sampling ");
attroff(A_BOLD);
}
refresh();
switch(getch()) {
case 'q': // quit
running = false;
break;
case 's': // make a sample
if(auto_sample) break;
attron(A_BOLD);
mvprintw(3, 3, " Sampling ... ");
attroff(A_BOLD);
refresh();
open_stream();
read_stream(buffer, sizeof(short) * sample_count);
free_stream();
fft_get_amplitudes(buffer, sample_count, amplitudes);
break;
case 'S':
if(auto_sample) {
auto_sample = false;
sample_thread.join();
free_stream();
} else {
auto_sample = true;
open_stream();
sample_thread = std::thread([&] {
while(auto_sample) {
read_stream(buffer, sizeof(short) * sample_count);
fft_get_amplitudes(buffer, sample_count, amplitudes);
}
});
}
break;
case 'z':
if(min_frequency - FrequencyChangeStep >= 0)
min_frequency -= FrequencyChangeStep;
break;
case 'x':
if(min_frequency + FrequencyChangeStep < max_frequency)
min_frequency += FrequencyChangeStep;
break;
case 'c':
if(max_frequency - FrequencyChangeStep > min_frequency)
max_frequency -= FrequencyChangeStep;
break;
case 'v':
if(max_frequency + FrequencyChangeStep <= 20000)
max_frequency += FrequencyChangeStep;
break;
case '[':
if(threshold - ThresholdChangeStep >= 0)
threshold -= ThresholdChangeStep;
break;
case ']':
threshold += ThresholdChangeStep;
break;
case 'w':
if(sample_count > MinSampleCount)
sample_count >>= 1;
break;
case 'e':
if(sample_count < MaxSampleCount)
sample_count <<= 1;
break;
}
}
if(auto_sample) {
auto_sample = false;
sample_thread.join();
free_stream();
}
endwin();
}