-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfm.cpp
More file actions
36 lines (32 loc) · 1.08 KB
/
fm.cpp
File metadata and controls
36 lines (32 loc) · 1.08 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
#include "fm.h"
#include <stdio.h>
FM::FM(WaveTable *waveTable, waveformType waveform, double freq, double *depth)
: Oscillator(waveTable, waveform, freq) {
this->depth = depth;
carrierPhase = 0;
envelopeModifier = 0;
}
double FM::getSample() {
double in, out, freq, currentDepth;
pthread_mutex_lock(&setFreqMutex);
currentDepth = *depth + envelopeModifier + (getLfoPosition() * 10000);
currentDepth = currentDepth < 0 ? 0 : currentDepth;
freq = SAMPLE_RATE / period;
in = ((waveTable)->*(waveform))(phase, SAMPLE_RATE / period);
out = sin(2 * PI * (carrierPhase / period));
carrierPhase =
fmod((carrierPhase + 1 + (((currentDepth)*in) / freq)), period);
advance();
pthread_mutex_unlock(&setFreqMutex);
return out;
}
void FM::modulate(double amount) { envelopeModifier = amount * 10000; }
void FM::setFreq(double freq) {
double newPeriod;
newPeriod = SAMPLE_RATE / freq;
pthread_mutex_lock(&setFreqMutex);
phase = phase * (newPeriod / period);
carrierPhase = carrierPhase * (newPeriod / period);
period = newPeriod;
pthread_mutex_unlock(&setFreqMutex);
}