-
Notifications
You must be signed in to change notification settings - Fork 0
/
channel.cpp
64 lines (57 loc) · 1.68 KB
/
channel.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
#include "channel.h"
#include "fft.h"
#include <cmath>
static Channel logScale(const Channel& input) {
// scales amplitude to log_10
Channel output(input.size(), 0.0);
for (size_t i=0; i<input.size(); i++) {
auto val = std::abs(input[i]) + 1;
assert(val >= 1);
output[i] = std::log10(val);
}
return output;
}
Channel::Channel() :
std::vector<float>(SAMPLES_PER_FRAME),
m_minY(-1.0),
m_maxY(1.0)
{}
Channel::Channel(size_t size, float value) : std::vector<float>(size, value) {}
int Channel::render(SDL_Renderer* renderer, uint8_t r, uint8_t g, uint8_t b) const {
size_t err;
Channel result = fft(*this);
Channel scaled = logScale(result);
for (size_t i=0; i<scaled.size(); i++) {
float point = scaled[i];
float minX = 0.0;
float maxX = static_cast<float>(scaled.size());
size_t x = static_cast<size_t>(std::round((
(static_cast<float>(i) - minX) / (maxX - minX)
) * SCREEN_WIDTH));
size_t y = (SCREEN_HEIGHT - static_cast<size_t>(std::round(
point * static_cast<float>(SCREEN_HEIGHT)
))) - 20;
if ((err = SDL_SetRenderDrawColor(renderer, r, g, b, 150)) != 0) {
return err;
};
if ((err = SDL_RenderDrawPoint(renderer, x, y)) != 0) {
return err;
}
if ((err = SDL_SetRenderDrawColor(renderer, r, g, b, 75)) != 0) {
return err;
};
if ((err = SDL_RenderDrawPoint(renderer, x+1, y)) != 0) {
return err;
}
if ((err = SDL_RenderDrawPoint(renderer, x-1, y)) != 0) {
return err;
}
if ((err = SDL_RenderDrawPoint(renderer, x, y+1)) != 0) {
return err;
}
if ((err = SDL_RenderDrawPoint(renderer, x, y-1)) != 0) {
return err;
}
}
return 0;
}