-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStandardOscillator.cpp
More file actions
59 lines (43 loc) · 1.39 KB
/
Copy pathStandardOscillator.cpp
File metadata and controls
59 lines (43 loc) · 1.39 KB
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
/* Dependencies */
#include "Waveform.h"
#include "StandardOscillator.h"
namespace CheapTune {
StandardOscillator::StandardOscillator(Waveform* waveform, Frequency_t frequency) :
_waveform(waveform), _tuningWord(0), _phaseAccumulator(0), _syncFlag(false) {
if (frequency)
setFrequency(frequency);
}
void StandardOscillator::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;
}
void StandardOscillator::setWaveform(Waveform* waveform) {
_waveform = waveform;
}
Sample_t StandardOscillator::getSample() {
/* Get the current indexed sample from the waveform generator */
Sample_t sample = _waveform->getSample(_phaseAccumulator);
/* Go to the next sample index */
uint32_t acc = (uint32_t) _phaseAccumulator + _tuningWord;
_phaseAccumulator = acc & WAVETABLE_INDEX_BITMASK;
/* Set the sync flag */
_syncFlag = !!(acc & 0xFFFF0000);
/* Return the sample */
return sample;
}
void StandardOscillator::reset() {
_waveform->reset();
_tuningWord = 0;
_phaseAccumulator = 0;
_syncFlag = false;
}
bool StandardOscillator::isCycleFinished() const {
return _syncFlag;
}
void StandardOscillator::restartCycle(WavetableIndex_t index) {
_phaseAccumulator = index;
}
Waveform* StandardOscillator::waveform() {
return _waveform;
}
}