-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvibrato.cpp
More file actions
54 lines (50 loc) · 1.34 KB
/
vibrato.cpp
File metadata and controls
54 lines (50 loc) · 1.34 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
#include "vibrato.h"
#include <math.h>
#include <stdio.h>
Vibrato::Vibrato(Keyboard **input, double period, double depth) {
this->depth = depth;
this->input = input;
this->period = (int)(period * SAMPLE_RATE);
cursor = 0;
phase = 0;
lastSample = 0;
pthread_mutex_init(&mutexsum, NULL);
}
double Vibrato::getSample() {
double virtSampleSize, sample, offset, amountToRead;
double i;
pthread_mutex_lock(&mutexsum);
sample = 0;
virtSampleSize =
1 + ((depth * sin(2 * PI * (((double)phase) / period))) / SAMPLE_RATE);
for (i = 0; i < virtSampleSize;) {
offset = fmod(cursor, 1);
if (offset == 0) {
lastSample = (*input)->getSample();
}
amountToRead =
(virtSampleSize - i > 1 - offset) ? 1 - offset : virtSampleSize - i;
i += amountToRead;
cursor += amountToRead;
sample += (amountToRead / virtSampleSize) * lastSample;
}
phase++;
if (phase >= period && cursor >= period) {
phase -= period;
cursor -= period;
}
pthread_mutex_unlock(&mutexsum);
return sample;
}
void Vibrato::setDepth(double depth) {
pthread_mutex_lock(&mutexsum);
this->depth = depth;
pthread_mutex_unlock(&mutexsum);
}
void Vibrato::setPeriod(double period) {
pthread_mutex_lock(&mutexsum);
// phase = 0;
// cursor = 0;
this->period = (int)(period * SAMPLE_RATE);
pthread_mutex_unlock(&mutexsum);
}