-
Notifications
You must be signed in to change notification settings - Fork 8
/
skiddermidi.cpp
279 lines (240 loc) · 7.23 KB
/
skiddermidi.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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*------------------------------------------------------------------------
Copyright (C) 2000-2021 Sophia Poirier
This file is part of Skidder.
Skidder is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
Skidder is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Skidder. If not, see <https://www.gnu.org/licenses/>.
To contact the author, use the contact form at https://destroyfx.org/
------------------------------------------------------------------------*/
#include "skidder.h"
#include <algorithm>
#include <cmath>
#include "dfxmath.h"
//-----------------------------------------------------------------------------
void Skidder::resetMidi()
{
mNoteTable.fill(0); // zero out the whole note table
mWaitSamples = 0;
mMidiIn = mMidiOut = false;
mMostRecentVelocity = DfxMidi::kMaxValue;
}
//-----------------------------------------------------------------------------
void Skidder::processMidiNotes()
{
auto noteWasOn = isAnyNoteOn();
for (long i = 0; i < getmidistate().getBlockEventCount(); i++)
{
auto const currentNote = getmidistate().getBlockEvent(i).mByte1;
auto const velocity = getmidistate().getBlockEvent(i).mByte2;
switch (getmidistate().getBlockEvent(i).mStatus)
{
// note-on status was received
case DfxMidi::kStatus_NoteOn:
mNoteTable[currentNote] = mMostRecentVelocity = velocity;
// This is not very accurate.
// If more than one note-on happens this block, only the last one is effective.
// This is good enough for this effect, though.
noteOn(getmidistate().getBlockEvent(i).mOffsetFrames);
noteWasOn = true;
break;
// note-off status was received
case DfxMidi::kStatus_NoteOff:
mNoteTable[currentNote] = 0; // turn off the note
break;
// all notes off
case DfxMidi::kStatus_CC:
if (getmidistate().getBlockEvent(i).mByte1 == DfxMidi::kCC_AllNotesOff)
{
mNoteTable.fill(0); // turn off all notes
noteOff(); // do the notes off Skidder stuff
noteWasOn = false;
}
break;
default:
break;
}
}
// check to see whether we are internally in a state of note being on but all,
// and do the Skidder notes-off stuff if we have no notes remaining on
if (noteWasOn && !isAnyNoteOn())
{
noteOff();
}
}
//-----------------------------------------------------------------------------------------
void Skidder::noteOn(unsigned long offsetFrames)
{
switch (mMidiMode)
{
case kMidiMode_Trigger:
case kMidiMode_Apply:
mWaitSamples = dfx::math::ToSigned(offsetFrames);
mState = SkidState::Valley;
mValleySamples = 0;
mMidiIn = true;
break;
default:
break;
}
}
//-----------------------------------------------------------------------------------------
void Skidder::noteOff()
{
switch (mMidiMode)
{
case kMidiMode_Trigger:
switch (mState)
{
case SkidState::SlopeIn:
mState = SkidState::SlopeOut;
mSlopeSamples = mSlopeDur - mSlopeSamples;
mWaitSamples = mSlopeSamples;
break;
case SkidState::Plateau:
mPlateauSamples = 1;
mWaitSamples = mSlopeDur + 1;
break;
case SkidState::SlopeOut:
mWaitSamples = mSlopeSamples;
break;
case SkidState::Valley:
mWaitSamples = 0;
break;
}
mMidiOut = true;
// in case we're still in a MIDI-in phase; it won't get falsed otherwise
if (mMidiIn)
{
mMidiIn = false;
}
// if we're in a slope-out and there's a raised floor, the slope position needs to be rescaled
else if ((mState == SkidState::SlopeOut) && (mFloor > 0.0f))
{
mSlopeSamples = std::lround(((static_cast<float>(mSlopeSamples) * mSlopeStep * mGainRange) + mFloor) * static_cast<float>(mSlopeDur));
mWaitSamples = mSlopeSamples;
}
// just to prevent anything really stupid from happening
mWaitSamples = std::max(mWaitSamples, 0L);
break;
case kMidiMode_Apply:
switch (mState)
{
case SkidState::SlopeIn:
mWaitSamples = mSlopeSamples;
break;
case SkidState::Plateau:
mWaitSamples = 0;
break;
case SkidState::SlopeOut:
mState = SkidState::SlopeIn;
mSlopeSamples = mSlopeDur - mSlopeSamples;
mWaitSamples = mSlopeSamples;
break;
case SkidState::Valley:
mValleySamples = 1;
mWaitSamples = mSlopeDur + 1;
break;
}
mMidiOut = true;
// in case we're still in a MIDI-in phase; it won't get falsed otherwise
mMidiIn = false;
// just to prevent anything really stupid from happening
mWaitSamples = std::max(mWaitSamples, 0L);
break;
default:
break;
}
}
//-----------------------------------------------------------------------------
bool Skidder::isAnyNoteOn() const
{
return std::any_of(mNoteTable.cbegin(), mNoteTable.cend(), [](auto const& velocity){ return (velocity > 0); });
}
//-----------------------------------------------------------------------------
// this gets called when Skidder automates a parameter from CC messages.
// this is where we can link parameter automation for rangeslider points.
void Skidder::settings_doLearningAssignStuff(long tag, dfx::MidiEventType eventType, long eventChannel, long eventNum,
unsigned long /*offsetFrames*/, long eventNum2, dfx::MidiEventBehaviorFlags eventBehaviourFlags,
long data1, long data2, float fdata1, float fdata2)
{
if (getsettings().getSteal())
{
return;
}
switch (tag)
{
case kPulsewidth:
if (mPulsewidthDoubleAutomate)
{
getsettings().assignParam(kPulsewidthRandMin, eventType, eventChannel, eventNum, eventNum2,
eventBehaviourFlags, data1, data2, fdata1, fdata2);
}
break;
case kPulsewidthRandMin:
if (mPulsewidthDoubleAutomate)
{
getsettings().assignParam(kPulsewidth, eventType, eventChannel, eventNum, eventNum2,
eventBehaviourFlags, data1, data2, fdata1, fdata2);
}
break;
case kFloor:
if (mFloorDoubleAutomate)
{
getsettings().assignParam(kFloorRandMin, eventType, eventChannel, eventNum, eventNum2,
eventBehaviourFlags, data1, data2, fdata1, fdata2);
}
break;
case kFloorRandMin:
if (mFloorDoubleAutomate)
{
getsettings().assignParam(kFloor, eventType, eventChannel, eventNum, eventNum2,
eventBehaviourFlags, data1, data2, fdata1, fdata2);
}
break;
default:
break;
}
}
/*
//-----------------------------------------------------------------------------
void Skidder::settings_unassignParam(long tag)
{
VstChunk::unassignParam(tag);
switch (tag)
{
case kPulsewidth:
if (mPulsewidthDoubleAutomate)
{
VstChunk::unassignParam(kPulsewidthRandMin);
}
break;
case kPulsewidthRandMin:
if (mPulsewidthDoubleAutomate)
{
VstChunk::unassignParam(kPulsewidth);
}
break;
case kFloor:
if (mFloorDoubleAutomate)
{
VstChunk::unassignParam(kFloorRandMin);
}
break;
case kFloorRandMin:
if (mFloorDoubleAutomate)
{
VstChunk::unassignParam(kFloor);
}
break;
default:
break;
}
}
*/