-
Notifications
You must be signed in to change notification settings - Fork 0
/
LowFrequencyOscillator.cpp
52 lines (38 loc) · 1.19 KB
/
LowFrequencyOscillator.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
/* Dependencies */
#include "LowFrequencyOscillator.h"
namespace CheapTune {
LowFrequencyOscillator::LowFrequencyOscillator(Waveform* waveform,
Prescaler_t prescaler, Frequency_t frequency) :
StandardOscillator(waveform, frequency), _sample(0), _prescaler(prescaler), _counter(0) {
}
void LowFrequencyOscillator::setPrescaler(Prescaler_t prescaler) {
_prescaler = prescaler;
}
void LowFrequencyOscillator::setFrequency(Frequency_t frequency) {
/* Based on DDS algebra : Fo (output frequency) = (M (tuning word) x Fc (sample frequency)) / pow(2, N)*/
_tuningWord = ((uint32_t) frequency * WAVETABLE_SAMPLES_COUNT) / (SAMPLE_RATE / _prescaler);
}
Sample_t LowFrequencyOscillator::getSample() {
/* If counter reach zero */
if(_counter == 0) {
/* Get a fresh sample */
_sample = StandardOscillator::getSample();
/* Restart counter */
_counter = _prescaler;
} else
/* Decrement counter */
--_counter;
/* Return the sample */
return _sample;
}
void LowFrequencyOscillator::reset() {
_sample = 0;
_prescaler = 1;
_counter = 0;
StandardOscillator::reset();
}
void LowFrequencyOscillator::restartCycle(WavetableIndex_t index) {
_counter = 0;
StandardOscillator::restartCycle(index);
}
}