-
Notifications
You must be signed in to change notification settings - Fork 7
/
VoiceManager.cpp
188 lines (166 loc) · 7.43 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
//
// VoiceManager.cpp
// Synthesis
//
// Created by Devin Mooers on 1/21/14.
//
//
#include "VoiceManager.h"
int VoiceManager::getNumberOfActiveVoices() {
int count = 0;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].isActive) {
count++;
}
}
return count;
}
Voice* VoiceManager::findVoicePlayingSameNote(int noteNumber) {
Voice* sameNoteVoice = NULL;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].mNoteNumber == noteNumber) {
sameNoteVoice = &(voices[i]);
break;
}
}
return sameNoteVoice;
}
Voice* VoiceManager::findFreeVoice() {
Voice* freeVoice = NULL;
for (int i = 0; i < MAX_VOICES; i++) {
if (!voices[i].isActive) {
freeVoice = &(voices[i]);
break;
}
}
return freeVoice;
}
Voice* VoiceManager::findOldestVoice() {
double age = 0.0;
int indexOfOldest = 0;
for (int i = 0; i < MAX_VOICES; i++) {
if (voices[i].mTime > age) {
age = voices[i].mTime;
indexOfOldest = i;
}
}
return &(voices[indexOfOldest]);
}
void VoiceManager::onNoteOn(int noteNumber, int velocity) {
// print number of active voices
//std::cout << "\nActive voices = " << getNumberOfActiveVoices();
// first look for a voice that's playing the same note
// Voice* voice = findVoicePlayingSameNote(noteNumber);
// // if no voice playing same note, look for a free voice
// if (!voice) {
// voice = findFreeVoice();
// }
Voice* voice = findFreeVoice();
// then do voice stealing
if (!voice) {
voice = findOldestVoice();
//printf("stole a voice!");
}
/// velocity scaling (scaling from int (0, 127) to float (0,1) WITH velocity curve)
//float scaledVelocity = 0.14948f * powf(1.055f, 0.3f*velocity)-0.14948f; // velocity scaling to (0, 1) w/velocity curve
float scaledVelocity = velocity / 127.0f;
voice->reset();
voice->setNoteNumber(noteNumber);
//voice->mDamping = ((float)noteNumber/100.0f)*2.5f; /// set this to be a param amount set by a knob (and modified by expression pedal) to control decay time
voice->mDamping = ((float)noteNumber/100.0f)*mOpenCL.mDamping;
voice->lastExcitationTimeAgo = 0.0f;
voice->lastExcitationStrength = scaledVelocity;
voice->lastExcitationDuration = 0.005f; // 5ms
voice->isActive = true;
voice->mVelocity = scaledVelocity;
voice->mEnergyHoriz *= (1-scaledVelocity); // louder hits will "reset" the velocity more - a full loudness hit will totally reset the string back to zero energy
voice->mEnergyVert *= (1-scaledVelocity);
//voice->mEnergyHoriz = scaledVelocity;
//voice->mEnergyVert = scaledVelocity;
//printf("---NOTE ON---\n");
//voice->mEnergy = scaledVelocity; /// actually this should also be a RAMP function so we get a smooth ramping up to the target mEnergy value
voice->mStringDetuneAmount = (1.0f-mOpenCL.mStringDetuneRange) + static_cast <float> (rand()) /( static_cast <float> (RAND_MAX/(2.0f*mOpenCL.mStringDetuneRange)));
voice->randomSeed = rand() % 10000+1000; // set random seed on each note hit for randomizing partial frequencies and amplitudes
}
void VoiceManager::onNoteOff(int noteNumber, int velocity) {
for (int i = 0; i < MAX_VOICES; i++) {
Voice& voice = voices[i];
if (voice.isActive && voice.mNoteNumber == noteNumber) {
//voice.mDamping = 50.0f; /// actually, this should be a RAMP function, which takes target mDamping value and # of samples it'll take to get there (or SPEED), and then the function increases damping gradually (iterates over several samples of the damping array, perhaps? how to handle this?)
}
}
}
void VoiceManager::onSustainChange(double sustain) {
mOpenCL.MIDIParams[0] = (float)sustain;
}
void VoiceManager::onExpressionChange(double expression) {
mOpenCL.MIDIParams[1] = (float)expression;
}
void VoiceManager::onModChange(double mod) {
// mOpenCL.MIDIParams[2] = (float)mod;
mOpenCL.mModCurrent = (float)mod;
}
void VoiceManager::setSampleRate(double sampleRate) {
}
void VoiceManager::updateVoiceData() {
int j = 0;
for (int i = 0; i < MAX_VOICES; i++) {
Voice& voice = voices[i];
if (voice.isActive) {
mOpenCL.voicesData[j*NUM_VOICE_PARAMS] = voice.mTime;
mOpenCL.voicesData[j*NUM_VOICE_PARAMS+1] = voice.mFrequency;
mOpenCL.voicesData[j*NUM_VOICE_PARAMS+2] = voice.mVelocity;
mOpenCL.voicesData[j*NUM_VOICE_PARAMS+3] = voice.mStringDetuneAmount;
mOpenCL.voicesData[j*NUM_VOICE_PARAMS+4] = voice.randomSeed;
j++;
voice.mTime += mOpenCL.mTimeStep * BLOCK_SIZE;
}
}
}
void VoiceManager::setFreeInaudibleVoices() {
for (int i = 0; i < MAX_VOICES; i++) {
Voice& voice = voices[i];
if (voice.isActive) {
if ((voice.mEnergyHoriz + voice.mEnergyVert) <= voice.mBrownianThreshold) {
voice.setFree();
// printf("voice %d set free!\n", i);
}
}
}
}
void VoiceManager::updateVoiceDampingAndEnergy(int currentSampleIndex) {
int j = 0;
for (int i = 0; i < MAX_VOICES; i++) {
Voice& voice = voices[i];
if (voice.isActive) {
float duration = voice.lastExcitationDuration;
float timeAgo = voice.lastExcitationTimeAgo;
float timeStep = mOpenCL.mTimeStep;
if (timeAgo < duration) {
float deltaEnergy = timeStep * static_cast<float>( exp( - ( pow(timeAgo - duration/2.0f, 2) / (duration*duration*0.03f) ) ) ) * voice.lastExcitationStrength * 500.0f; // smooth ramp for energy // gaussian distribution force function that starts increasing immediately after strike and goes back down to zero after exactly duration seconds. the 0.03f is so that the gaussian curve just touches zero at the beginning and end of the transient
voice.mEnergyVert += deltaEnergy * (1.0f-voice.mHorizToVertRatio); // this was just 1.0, with 250.0f final multiplier in above line
voice.mEnergyHoriz += deltaEnergy * voice.mHorizToVertRatio;
//printf("timeAgo = %f, duration = %f, voice[%d] energyVert = %f, energyHoriz = %f\n", timeAgo, duration, i, voice.mEnergyVert, voice.mEnergyHoriz);
}
if (currentSampleIndex == 0) {
//printf("timeAgo = %f, duration = %f, voice[%d] energyVert = %f, energyHoriz = %f\n", timeAgo, duration, i, voice.mEnergyVert, voice.mEnergyHoriz);
}
voice.mEnergyVert -= voice.mEnergyVert * timeStep * voice.mDamping * (1.0f-voice.mHorizToVertRatio);
voice.mEnergyHoriz -= voice.mEnergyHoriz * timeStep * voice.mDamping * (voice.mHorizToVertRatio);
mOpenCL.voicesEnergy[j*BLOCK_SIZE + currentSampleIndex] = voice.mEnergyVert + voice.mEnergyHoriz; // set voice energy with sum of horizontal and vertical modes of string vibration
voice.lastExcitationTimeAgo += mOpenCL.mTimeStep;
j++;
}
}
}
boost::array<double, BLOCK_SIZE * NUM_CHANNELS> VoiceManager::getBlockOfSamples() {
numActiveVoices = getNumberOfActiveVoices();
mOpenCL.NUM_ACTIVE_VOICES = numActiveVoices;
//std::cout << "\nactive voices: " << numActiveVoices;
if (numActiveVoices == 0) {
return zeroes;
} else {
updateVoiceData(); // writes new voice data to mOpenCL
return mOpenCL.getBlockOfSamples();
}
}