-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathSFZSynth.cpp
More file actions
187 lines (169 loc) · 4.97 KB
/
SFZSynth.cpp
File metadata and controls
187 lines (169 loc) · 4.97 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
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
/*************************************************************************************
* Original code copyright (C) 2012 Steve Folta
* Converted to Juce module (C) 2016 Leo Olivers
* Forked from https://github.com/stevefolta/SFZero
* For license info please see the LICENSE file distributed with this source code
*************************************************************************************/
#include "SFZSynth.h"
#include "SFZSound.h"
#include "SFZVoice.h"
sfzero::Synth::Synth() : Synthesiser() {
threadCleaner = new SFZCleaner("Cleaner");
threadCleaner->startThread(5);
for(int i=0; i<16; i++)
midiVolume_[i] = 127;
}
sfzero::Synth::~Synth(){
delete threadCleaner;
}
void sfzero::Synth::noteOn(int midiChannel, int midiNoteNumber, float velocity)
{
int i;
const juce::ScopedLock locker(lock);
int midiVelocity = static_cast<int>(velocity * 127);
// First, stop any currently-playing sounds in the group.
//*** Currently, this only pays attention to the first matching region.
int group = 0;
sfzero::Sound *sound = dynamic_cast<sfzero::Sound *>(getSound(0));
if (sound)
{
sfzero::Region *region = sound->getRegionFor(midiNoteNumber, midiVelocity);
if (region)
{
group = region->group;
}
}
if (group != 0)
{
for (i = voices.size(); --i >= 0;)
{
sfzero::Voice *voice = dynamic_cast<sfzero::Voice *>(voices.getUnchecked(i));
if (voice == nullptr)
{
continue;
}
if (voice->getOffBy() == group)
{
voice->stopNoteForGroup();
}
}
}
// Are any notes playing? (Needed for first/legato trigger handling.)
// Also stop any voices still playing this note.
bool anyNotesPlaying = false;
for (i = voices.size(); --i >= 0;)
{
sfzero::Voice *voice = dynamic_cast<sfzero::Voice *>(voices.getUnchecked(i));
if (voice == nullptr)
{
continue;
}
if (voice->isPlayingChannel(midiChannel))
{
if (voice->isPlayingNoteDown())
{
if (voice->getCurrentlyPlayingNote() == midiNoteNumber)
{
if (!voice->isPlayingOneShot())
{
voice->stopNoteQuick();
}
}
else
{
anyNotesPlaying = true;
}
}
}
}
// Play *all* matching regions.
sfzero::Region::Trigger trigger = (anyNotesPlaying ? sfzero::Region::legato : sfzero::Region::first);
if (sound)
{
int numRegions = sound->getNumRegions();
for (i = 0; i < numRegions; ++i)
{
sfzero::Region *region = sound->regionAt(i);
if (region->matches(midiNoteNumber, midiVelocity, trigger))
{
sfzero::Voice *voice =
dynamic_cast<sfzero::Voice *>(findFreeVoice(sound, midiNoteNumber, midiChannel, isNoteStealingEnabled()));
if (voice)
{
voice->setRegion(region);
voice->setMidiVolume(midiVolume_[midiChannel-1]);
startVoice(voice, sound, midiChannel, midiNoteNumber, velocity);
}
}
}
}
noteVelocities_[midiNoteNumber] = midiVelocity;
}
void sfzero::Synth::noteOff(int midiChannel, int midiNoteNumber, float velocity, bool allowTailOff)
{
const juce::ScopedLock locker(lock);
Synthesiser::noteOff(midiChannel, midiNoteNumber, velocity, allowTailOff);
// Start release region.
sfzero::Sound *sound = dynamic_cast<sfzero::Sound *>(getSound(0));
if (sound)
{
sfzero::Region *region = sound->getRegionFor(midiNoteNumber, noteVelocities_[midiNoteNumber], sfzero::Region::release);
if (region)
{
sfzero::Voice *voice = dynamic_cast<sfzero::Voice *>(findFreeVoice(sound, midiNoteNumber, midiChannel, false));
if (voice)
{
// Synthesiser is too locked-down (ivars are private rt protected), so
// we have to use a "setRegion()" mechanism.
voice->setRegion(region);
startVoice(voice, sound, midiChannel, midiNoteNumber, noteVelocities_[midiNoteNumber] / 127.0f);
}
}
}
}
void sfzero::Synth::handleController (int midiChannel, int controllerNumber, int controllerValue)
{
switch(controllerNumber){
case 7:
midiVolume_[midiChannel-1] = controllerValue;
break;
}
Synthesiser::handleController (midiChannel, controllerNumber, controllerValue);
}
int sfzero::Synth::numVoicesUsed()
{
int numUsed = 0;
for (int i = voices.size(); --i >= 0;)
{
if (voices.getUnchecked(i)->getCurrentlyPlayingNote() >= 0)
{
numUsed += 1;
}
}
return numUsed;
}
juce::String sfzero::Synth::voiceInfoString()
{
enum
{
maxShownVoices = 20,
};
juce::StringArray lines;
int numUsed = 0, numShown = 0;
for (int i = voices.size(); --i >= 0;)
{
sfzero::Voice *voice = dynamic_cast<sfzero::Voice *>(voices.getUnchecked(i));
if (voice->getCurrentlyPlayingNote() < 0)
{
continue;
}
numUsed += 1;
if (numShown >= maxShownVoices)
{
continue;
}
lines.add(voice->infoString());
}
lines.insert(0, "voices used: " + juce::String(numUsed));
return lines.joinIntoString("\n");
}