-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanalog_drift.h
More file actions
38 lines (34 loc) · 1012 Bytes
/
analog_drift.h
File metadata and controls
38 lines (34 loc) · 1012 Bytes
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
#pragma once
#include <random>
#include <vector>
#include <numeric>
#include <cmath>
class AnalogDrift {
public:
AnalogDrift(int numOctaves = 5)
: numOctaves_(numOctaves), totalValue_(0.0f) {
rng_.seed(std::random_device{}());
dist_ = std::uniform_real_distribution<float>(-1.0f, 1.0f);
values_.resize(numOctaves_, 0.0f);
counters_.resize(numOctaves_, 0);
}
float process() {
totalValue_ = 0.0f;
for (int i = 0; i < numOctaves_; ++i) {
counters_[i]++;
if (counters_[i] >= (1u << i)) {
counters_[i] = 0;
values_[i] = dist_(rng_);
}
totalValue_ += values_[i];
}
return totalValue_ / static_cast<float>(numOctaves_);
}
private:
int numOctaves_;
std::vector<float> values_;
std::vector<unsigned int> counters_;
std::default_random_engine rng_;
std::uniform_real_distribution<float> dist_;
float totalValue_;
};