-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoscillator.cpp
More file actions
44 lines (39 loc) · 1.11 KB
/
oscillator.cpp
File metadata and controls
44 lines (39 loc) · 1.11 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
#include "oscillator.h"
#include <stdio.h>
Oscillator::Oscillator(WaveTable *waveTable, waveformType waveform, double freq)
: waveTable(waveTable) {
this->waveform = waveform;
period = SAMPLE_RATE / freq;
phase = 0;
pthread_mutex_init(&setFreqMutex, NULL);
pthread_mutex_init(&setWaveformMutex, NULL);
}
void Oscillator::setFreq(double freq) {
pthread_mutex_lock(&setFreqMutex);
double newPeriod;
newPeriod = SAMPLE_RATE / freq;
phase = phase * (newPeriod / period);
period = newPeriod;
pthread_mutex_unlock(&setFreqMutex);
}
void Oscillator::advance() {
phase += 1;
if (phase >= period - 0.001) {
phase -= period;
}
}
double Oscillator::getSample() {
pthread_mutex_lock(&setFreqMutex);
double sample;
pthread_mutex_lock(&setWaveformMutex);
sample = (waveTable->*waveform)(phase, SAMPLE_RATE / period);
pthread_mutex_unlock(&setWaveformMutex);
advance();
pthread_mutex_unlock(&setFreqMutex);
return sample;
}
void Oscillator::setWaveform(waveformType waveform) {
pthread_mutex_lock(&setWaveformMutex);
this->waveform = waveform;
pthread_mutex_unlock(&setWaveformMutex);
}