-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchorus.cpp
More file actions
76 lines (68 loc) · 2.03 KB
/
chorus.cpp
File metadata and controls
76 lines (68 loc) · 2.03 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "chorus.h"
#include <math.h>
#include <stdio.h>
Chorus::Chorus(SoundProcessor *input, double period, double depth) {
this->depth = depth;
this->input = input;
this->period = (int)(period * SAMPLE_RATE);
voiceOne.period = this->period;
voiceOne.phase = 0;
voiceOne.cursor = 0;
voiceOne.lastSample = 0;
voiceOne.toRead = &(voiceTwo.alreadyRead);
voiceTwo.period = this->period * 1.1;
voiceTwo.phase = 0;
voiceTwo.cursor = 0;
voiceTwo.lastSample = 0;
voiceTwo.toRead = &(voiceOne.alreadyRead);
pthread_mutex_init(&mutexsum, NULL);
}
double Chorus::getSample() {
double sample = 0;
sample += getVoiceSample(voiceOne);
sample += getVoiceSample(voiceTwo);
return sample / 2;
}
double Chorus::getVoiceSample(struct voice &voice) {
double virtSampleSize, sample, offset, amountToRead;
double i;
pthread_mutex_lock(&mutexsum);
sample = 0;
virtSampleSize =
1 + ((depth * sin(2 * PI * (((double)voice.phase) / voice.period))) /
SAMPLE_RATE);
for (i = 0; i < virtSampleSize;) {
offset = fmod(voice.cursor, 1);
if (offset == 0) {
if ((voice.toRead)->empty()) {
voice.lastSample = input->getSample();
voice.alreadyRead.push(voice.lastSample);
} else {
voice.lastSample = (voice.toRead)->front();
(voice.toRead)->pop();
}
}
amountToRead =
(virtSampleSize - i > 1 - offset) ? 1 - offset : virtSampleSize - i;
i += amountToRead;
voice.cursor += amountToRead;
sample += (amountToRead / virtSampleSize) * voice.lastSample;
}
pthread_mutex_unlock(&mutexsum);
voice.phase++;
if (voice.phase >= voice.period && voice.cursor >= voice.period) {
voice.phase -= voice.period;
voice.cursor -= voice.period;
}
return sample;
}
void Chorus::setDepth(double depth) {
pthread_mutex_lock(&mutexsum);
this->depth = depth;
pthread_mutex_unlock(&mutexsum);
}
void Chorus::setPeriod(double period) {
pthread_mutex_lock(&mutexsum);
this->period = (int)(period * SAMPLE_RATE);
pthread_mutex_unlock(&mutexsum);
}