-
Notifications
You must be signed in to change notification settings - Fork 1
/
VoiceManager.cpp
49 lines (43 loc) · 1.25 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
#include "VoiceManager.h"
Voice* VoiceManager::findFreeVoice() {
Voice* freeVoice = NULL;
for (int i = 0; i < NumberOfVoices; i++) {
if (!voices[i].isActive) {
freeVoice = &(voices[i]);
break;
}
}
return freeVoice;
}
void VoiceManager::onNoteOn(int noteNumber, int velocity) {
Voice* voice = findFreeVoice();
if (!voice) {
return;
}
voice->reset();
voice->setNoteNumber(noteNumber);
voice->mVelocity = velocity;
voice->isActive = true;
voice->mVolumeEnvelope.enterStage(EnvelopeGenerator::ENVELOPE_STAGE_ATTACK);
voice->mFilterEnvelope.enterStage(EnvelopeGenerator::ENVELOPE_STAGE_ATTACK);
}
void VoiceManager::onNoteOff(int noteNumber, int velocity) {
// Find the voice(s) with the given noteNumber:
for (int i = 0; i < NumberOfVoices; i++) {
Voice& voice = voices[i];
if (voice.isActive && voice.mNoteNumber == noteNumber) {
voice.mVolumeEnvelope.enterStage(EnvelopeGenerator::ENVELOPE_STAGE_RELEASE);
voice.mFilterEnvelope.enterStage(EnvelopeGenerator::ENVELOPE_STAGE_RELEASE);
}
}
}
double VoiceManager::nextSample() {
double output = 0.0;
double lfoValue = mLFO.nextSample();
for (int i = 0; i < NumberOfVoices; i++) {
Voice& voice = voices[i];
voice.setLFOValue(lfoValue);
output += voice.nextSample();
}
return output;
}