/
std_library_voices.cmajor
225 lines (195 loc) · 8.03 KB
/
std_library_voices.cmajor
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
//
// ,ad888ba, 88
// d8"' "8b
// d8 88,dba,,adba, ,aPP8A.A8 88 The Cmajor Standard Library
// Y8, 88 88 88 88 88 88
// Y8a. .a8P 88 88 88 88, ,88 88 (C)2024 Cmajor Software Ltd
// '"Y888Y"' 88 88 88 '"8bbP"Y8 88 https://cmajor.dev
// ,88
// 888P"
//
// The Cmajor standard library may be used under the terms of the ISC license:
//
// Permission to use, copy, modify, and/or distribute this software for any purpose with or
// without fee is hereby granted, provided that the above copyright notice and this permission
// notice appear in all copies. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
// WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
// CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
// WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
// CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
/// std.voices
/**
This module demonstrates a simple voice-allocation algorithm.
The way it's structured is that you pass an incoming stream of note events to
the voice allocator processor, and it redirects them to an array of destination
processors which each handle a single voice.
The voice allocators use the channel property of the incoming events to group
them together, and take care of ensuring that each target voice only receives
the events from one channel at a time.
*/
namespace std::voices
{
/** A basic least-recently-used voice allocator.
If there's an inactive voice, then a new note will be assigned to it,
otherwise it will steal the least-recently-used active voice. For more
fancy note-stealing algorithms, you can use this as an example and add
your own logic!
*/
processor VoiceAllocator (int numVoices,
int MPEMasterChannel = 0)
{
//==============================================================================
input event (std::notes::NoteOn,
std::notes::NoteOff,
std::notes::PitchBend,
std::notes::Slide,
std::notes::Pressure,
std::notes::Control) eventIn;
output event (std::notes::NoteOn,
std::notes::NoteOff,
std::notes::PitchBend,
std::notes::Slide,
std::notes::Pressure,
std::notes::Control) voiceEventOut[numVoices];
//==============================================================================
event eventIn (std::notes::NoteOn noteOn)
{
let oldest = findOldestIndex();
// send a note-off when stealing a voice
if (voiceState[oldest].isActive)
voiceEventOut[oldest] <- std::notes::NoteOff (voiceState[oldest].channel,
voiceState[oldest].pitch,
0.0f);
voiceState[oldest].start (noteOn.channel, noteOn.pitch);
voiceEventOut[oldest] <- noteOn;
}
event eventIn (std::notes::NoteOff noteOff)
{
for (wrap<numVoices> i)
{
if (voiceState[i].channel == noteOff.channel
&& voiceState[i].pitch == noteOff.pitch)
{
if (isSustainActive (noteOff.channel))
{
voiceState[i].isReleasing = true;
}
else
{
voiceEventOut[i] <- noteOff;
voiceState[i].free();
}
}
}
}
event eventIn (std::notes::PitchBend bend)
{
for (wrap<numVoices> i)
if (voiceState[i].channel == bend.channel)
voiceEventOut[i] <- bend;
}
event eventIn (std::notes::Pressure pressure)
{
for (wrap<numVoices> i)
if (voiceState[i].channel == pressure.channel)
voiceEventOut[i] <- pressure;
}
event eventIn (std::notes::Slide slide)
{
for (wrap<numVoices> i)
if (voiceState[i].channel == slide.channel)
voiceEventOut[i] <- slide;
}
event eventIn (std::notes::Control control)
{
if (control.control == 64) // 64 = sustain
{
bool isMPEMasterChannel = control.channel == MPEMasterChannel;
bool sustainActive = control.value >= 0.5f;
setChannelSustain (control.channel, sustainActive);
if (isMPEMasterChannel)
mpeMasterSustainActive = sustainActive;
if (! sustainActive)
{
for (wrap<numVoices> i)
{
if ((isMPEMasterChannel || voiceState[i].channel == control.channel)
&& voiceState[i].isActive
&& voiceState[i].isReleasing)
{
voiceEventOut[i] <- std::notes::NoteOff (voiceState[i].channel,
voiceState[i].pitch,
0.0f);
voiceState[i].free();
}
}
}
}
else
{
for (wrap<numVoices> i)
if (voiceState[i].channel == control.channel)
voiceEventOut[i] <- control;
}
}
//==============================================================================
struct VoiceState
{
bool isActive, isReleasing;
int32 channel, age;
float32 pitch;
void start (int32 channel, float32 pitch)
{
this.isActive = true;
this.isReleasing = false;
this.channel = channel;
this.pitch = pitch;
this.age = ++nextActiveTime;
}
void free()
{
this.isActive = false;
this.age = ++nextInactiveTime;
}
}
static_assert (numVoices > 0);
VoiceState[numVoices] voiceState;
int32 nextActiveTime = 0x70000000, nextInactiveTime = 1;
bool mpeMasterSustainActive;
int64 perChannelSustainActive; // one per bit (assumes that channel IDs are <64, and could be made more general)
bool isSustainActive (int32 channel)
{
return mpeMasterSustainActive || (perChannelSustainActive & (1L << channel)) != 0;
}
void setChannelSustain (int32 channel, bool active)
{
if (active)
perChannelSustainActive |= (1L << channel);
else
perChannelSustainActive &= ~(1L << channel);
}
wrap<numVoices> findOldestIndex()
{
if const (numVoices == 1)
{
return 0;
}
else
{
wrap<numVoices> index;
var oldest = voiceState[0].age;
for (wrap<numVoices> i = 1)
{
let age = voiceState[i].age;
if (age < oldest)
{
index = i;
oldest = age;
}
}
return index;
}
}
}
}