-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPhaseGenerator.cpp
More file actions
41 lines (30 loc) · 989 Bytes
/
PhaseGenerator.cpp
File metadata and controls
41 lines (30 loc) · 989 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
39
40
41
#include <cmath>
#include "PhaseGenerator.h"
namespace tinyalg::waveu {
PhaseGenerator::PhaseGenerator(uint32_t sampleRate)
: sampleRate_(sampleRate) {}
void PhaseGenerator::setFrequency(float frequency) {
frequency_ = frequency;
phaseIncrement_ = (uint32_t)((double)frequency_ / (double)sampleRate_
* ((double)(1ULL << N_BITS)));
}
float PhaseGenerator::getFrequency() {
return frequency_;
}
void PhaseGenerator::updatePhase() {
phase_ += phaseIncrement_;
}
uint32_t PhaseGenerator::getPhase() const {
return phase_;
}
void PhaseGenerator::reset() {
phase_ = 0;
}
void PhaseGenerator::reset(double elapsedTime) {
const uint64_t phaseScale = 1ULL << N_BITS;
// Calculate phase in cycles and wrap to [0, 1) range
double phaseCycles = std::fmod(frequency_ * elapsedTime, 1.0);
// Scale the phase to fixed-point representation
phase_ = (uint32_t)(phaseCycles * phaseScale);
}
} // namespace tinyalg::waveu