-
Notifications
You must be signed in to change notification settings - Fork 7
/
ripple.cpp
73 lines (57 loc) · 1.6 KB
/
ripple.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
#include "pixl.h"
using namespace pixl;
namespace pixl {
RippleVisualization::RippleVisualization(Input* input, int size, int smoothing, bool freq)
: Visualization(input, size),
smoothing_length_(smoothing),
freq_(freq) {
smoothing_amp_ = new float[smoothing_length_];
smoothing_freq_ = new float[smoothing_length_];
for (int i = 0; i < smoothing_length_; i++) {
smoothing_amp_[i] = 0.0;
smoothing_freq_[i] = 0.0;
}
}
RippleVisualization::~RippleVisualization() {
delete[] smoothing_amp_;
delete[] smoothing_freq_;
}
template <typename Type>
Type smooth(Type* smoothing, int length, Type value) {
Type sum = 0.0;
for (int i = 0; i < length; i++) {
sum += smoothing[i];
}
sum += value;
value = sum / (float)(length + 1);
PushQueue(smoothing, length, value);
return value;
}
void RippleVisualization::update_amp() {
float value = input_->getInput();
value = smooth(smoothing_amp_, smoothing_length_, value);
int hue = 192 + (-value * 192);
int val = 255;
if (value < 0.1) { val = 0; }
CRGB color = CHSV(hue, 255, val);
PushQueue(viz_, size_, color);
}
void RippleVisualization::update_freq() {
float amp = input_->getInput(0);
float freq = input_->getInput(1);
amp = smooth(smoothing_amp_, smoothing_length_, amp);
freq = smooth(smoothing_freq_, smoothing_length_, freq);
int hue = 192 * freq;
int val = 255.0 * amp;
if (val < 10) { val = 0; }
CRGB color = CHSV(hue, 255, val);
PushQueue(viz_, size_, color);
}
void RippleVisualization::update() {
if (freq_) {
update_freq();
} else {
update_amp();
}
}
} // end namespace pixl