-
Notifications
You must be signed in to change notification settings - Fork 1
/
VoiceManager.cpp
56 lines (49 loc) · 1.45 KB
/
VoiceManager.cpp
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
module megasynth.voice_manager;
import megasynth.voice;
Voice *VoiceManager::findFreeVoice() {
for (size_t i = 0; i < numberOfVoices; i++) {
if (!voices[i].isVoiceActive()) {
return &(voices[i]);
}
}
return nullptr;
}
void VoiceManager::onNoteOn(int noteNumber, int velocity) {
auto *voice = findFreeVoice();
if (!voice) return;
voice->reset();
voice->setNoteNumber(noteNumber);
voice->setVelocity(velocity);
voice->setActive(true);
voice->getVolumeEnv().enterStage(EnvelopeGenerator::Stage::ATTACK);
voice->getFilterEnv().enterStage(EnvelopeGenerator::Stage::ATTACK);
}
void VoiceManager::onNoteOff(int noteNumber, int velocity) {
for (size_t i = 0; i < numberOfVoices; i++) {
auto &voice = voices[i];
if (voice.isVoiceActive()
&& voice.getNoteNumber() == noteNumber) {
voice.getVolumeEnv().enterStage(EnvelopeGenerator::Stage::RELEASE);
voice.getFilterEnv().enterStage(EnvelopeGenerator::Stage::RELEASE);
}
}
}
double VoiceManager::nextSample() {
double output = 0.0;
double lfoValue = lfo.nextSample();
for (size_t i = 0; i < numberOfVoices; i++) {
auto &voice = voices[i];
voice.setLFOValue(lfoValue);
output += voice.nextSample();
}
return output;
}
void VoiceManager::setSampleRate(double rate) {
EnvelopeGenerator::setSampleRate(rate);
this->lfo.setSampleRate(rate);
for (size_t i = 0; i < numberOfVoices; i++) {
auto &voice = voices[i];
voice.getOsc1().setSampleRate(rate);
voice.getOsc2().setSampleRate(rate);
}
}