-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSostenuto.cpp
250 lines (188 loc) · 7.26 KB
/
Sostenuto.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#include "Sostenuto.h"
#include "IPlug_include_in_plug_src.h"
#include "IControls.h"
#include "IPlugPaths.h"
Sostenuto::Sostenuto(const InstanceInfo& info) : Plugin(info, MakeConfig(kNumParams, kNumPresets)) {
GetParam(kGain)->InitDouble("Gain", 0., 0., 100.0, 0.01, "%");
#if IPLUG_EDITOR // http://bit.ly/2S64BDd
mMakeGraphicsFunc = [&]() {
return MakeGraphics(*this, PLUG_WIDTH, PLUG_HEIGHT, PLUG_FPS, GetScaleForScreen(PLUG_WIDTH, PLUG_HEIGHT));
};
mLayoutFunc = [&](IGraphics* pGraphics) {
pGraphics->AttachCornerResizer(EUIResizerMode::Scale, false);
pGraphics->AttachPanelBackground(COLOR_GRAY);
pGraphics->LoadFont("Roboto-Regular", ROBOTO_FN);
const IVStyle style {
true, // Show label
true, // Show value
{
DEFAULT_BGCOLOR, // Background
DEFAULT_FGCOLOR, // Foreground
DEFAULT_PRCOLOR, // Pressed
COLOR_BLACK, // Frame
DEFAULT_HLCOLOR, // Highlight
DEFAULT_SHCOLOR, // Shadow
COLOR_BLACK, // Extra 1
DEFAULT_X2COLOR, // Extra 2
DEFAULT_X3COLOR // Extra 3
}, // Colors
IText(20.f, EAlign::Center) // Label text
};
const IRECT b = pGraphics->GetBounds();
IRECT wideCell;
pGraphics->AttachControl(new ITextControl(b.GetFromTop(50),
"Channel: - \t Note Number: - \t Velocity: - \t Control Change: -",
IText(15), COLOR_LIGHT_GRAY), midiView, "misccontrols");
auto setMidiValue = [&](IControl* pCaller) {
pedalValue = (int) pCaller->As<IVNumberBoxControl>()->GetRealValue();
};
pGraphics->AttachControl(new ITextControl(b.GetFromTop(175), "Pedal Value", IText(20)));
pGraphics->AttachControl(new IVNumberBoxControl(b.GetPadded(-100, -100, -100, -150), kNoParameter, setMidiValue,
"", style, 64., -1., 256.));
pGraphics->AttachControl(new IVKeyboardControl(IRECT(0,0,600,300).GetPadded(0, -200, 0, 0), 21, 108), keyboardGraphicView);
GetUI()->GetControlWithTag(keyboardGraphicView)->As<IVKeyboardControl>()->SetWantsMidi(false);
};
#endif
}
#if IPLUG_DSP
void Sostenuto::ProcessBlock(sample** inputs, sample** outputs, int nFrames) {
const int nChansOut = NOutChansConnected();
for (auto s = 0; s < nFrames; s++) {
for (auto c = 0; c < nChansOut; c++) {
outputs[c][s] = outputs[c][s] * 1;
}
}
}
void Sostenuto::printMidiValues(int status, int channel, int controlChange, int noteNumber, int noteVelocity) {
switch(status) {
case 8:
std::cout << "Note Off: {" << std::endl;
case 9:
std::cout << "Note On: {" << std::endl;
case 11:
std::cout << "Control Change: {" << std::endl;
default:
assert("error");
}
std::cout << "\t Status: " << status << std::endl;
std::cout << "\t Channel: " << channel << std::endl;
std::cout << "\t Control Change: " << controlChange << std::endl;
std::cout << "\t Note Number: " << noteNumber << std::endl;
std::cout << "\t Note Velocity: " << noteVelocity << std::endl;
std::cout << "}" << std::endl;
std::cout << std::endl;
}
void Sostenuto::removeNoteFromVector(int note) {
int h = 0;
std::lock_guard<std::mutex> lock(activeNoteMutex);
for (int i=0; i!=activeNotes.size(); i++) {
if (activeNotes[i] == note) {
h = i;
break;
}
}
activeNotes.erase(activeNotes.begin()+h);
}
void Sostenuto::addToActiveNotes(int note) {
if (!activeNotes.empty()) {
if (note > activeNotes.front()) activeNotes.push_back(note);
else activeNotes.insert(activeNotes.begin(), note);
} else {
activeNotes.push_back(note);
}
}
void Sostenuto::fillSostenutoNotes() {
for (int i : activeNotes) {
sostenutoNotes.push_back(i);
}
}
bool Sostenuto::vectorContains(std::vector<int> vec, int note) {
for (int i : vec) {
if (i == note) {
return true;
}
}
return false;
}
void Sostenuto::ProcessMidiMsg(const IMidiMsg& msg) {
TRACE;
bool ui = true;
int status = msg.StatusMsg();
IMidiMsg noteMsg;
int channel = msg.Channel();
int controlChange = msg.ControlChangeIdx();
int noteNumber = msg.NoteNumber();
int noteVelocity = msg.Velocity();
IVKeyboardControl *keyboardGraphic;
if (GetUI() != nil) {
WDL_String str;
str.SetFormatted(256, "Channel: %d \t Note Number: %d \t Velocity: %d \t Control Change: %d",
channel, noteNumber, noteVelocity, controlChange);
GetUI()->GetControlWithTag(midiView)->As<ITextControl>()->SetStr(str.Get());
keyboardGraphic = GetUI()->GetControlWithTag(keyboardGraphicView)->As<IVKeyboardControl>();
} else {
if (noUIKeyboard == nil) {
noUIKeyboard = new IVKeyboardControl(IRECT());
noUIKeyboard->SetWantsMidi(false);
}
}
switch (status) {
case 9:
goto handleNoteOn;
case 8:
goto handleNoteOff;
case 11:
goto handleControlChange;
default:
return;
}
handleNoteOn:
{
if (noteVelocity == 0) {
status = 8;
goto handleNoteOff;
}
addToActiveNotes(noteNumber);
if (sostenuto && vectorContains(sostenutoNotes, noteNumber)) {
noteMsg.MakeNoteOffMsg(noteNumber, 0);
SendMidiMsg(noteMsg);
noteMsg.MakeNoteOnMsg(noteNumber, noteVelocity, 0);
SendMidiMsg(noteMsg);
return;
}
if (ui) keyboardGraphic->SetNoteFromMidi(noteNumber, true);
noteMsg.MakeNoteOnMsg(noteNumber, noteVelocity, 0);
SendMidiMsg(noteMsg);
return;
}
handleNoteOff:
{
if (vectorContains(activeNotes, noteNumber)) removeNoteFromVector(noteNumber);
if (sostenuto && vectorContains(sostenutoNotes, noteNumber)) return;
if (ui) keyboardGraphic->SetNoteFromMidi(noteNumber, false);
noteMsg.MakeNoteOffMsg(noteNumber, 0);
SendMidiMsg(noteMsg);
return;
}
handleControlChange:
{
if (controlChange == pedalValue) {
sostenuto = !sostenuto;
if (sostenuto) {
fillSostenutoNotes();
} else {
for (int i : sostenutoNotes) {
if (!vectorContains(activeNotes, i)) {
if (ui) keyboardGraphic->SetNoteFromMidi(i, false);
noteMsg.MakeNoteOffMsg(i, 0);
SendMidiMsg(noteMsg);
}
}
sostenutoNotes.clear();
}
}
return;
}
}
#endif