-
Notifications
You must be signed in to change notification settings - Fork 0
/
gaussian_humanization
170 lines (125 loc) · 4.56 KB
/
gaussian_humanization
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
desc:Gaussian Humanization
slider1:0.33<0,1.78,0.01>timing standard deviation (ms)
slider2:1<0,30,1>velocity standard deviation
slider3:0<-50,50>timing shift (ms) [read-only]
slider4:0<-127,127,1>velocity shift [read-only]
@init
buffer = 0;
blockLength = 128;
memset(0, 0, blockLength);
bufferCounter = 0;
NOTE_ON = 9;
NOTE_OFF = 8;
AFTERTCH = 10;
previousBeatPosition = -1;
previousOffset = -1;
positionShift = 0;
noteNumberIndex = 1;
while ( noteNumberIndex < 128 ) (
lastNoteOffIndexArray[noteNumberIndex] = -1;
noteNumberIndex += 1;
);
@block
while (
midirecv(offset, msg1, msg23) ? (
// we're in a new block now
// we no longer have access to the last note off
(offset == 0) ? (
noteNumberIndex = 1;
while ( noteNumberIndex < 128 ) (
lastNoteOffIndexArray[noteNumberIndex] = -1;
noteNumberIndex += 1;
);
);
minimumPosition = 0;
maximumPosition = samplesblock - 1;
noteNumber = msg23 & 127;
messageType = (msg1 / 16) | 0;
velocity = (msg23 / 256) | 0;
(messageType == NOTE_ON && velocity > 0) ? (
// generate normal deviates
while (
v1 = 2.0 * rand(1) - 1.0;
v2 = 2.0 * rand(1) - 1.0;
s = v1 * v1 + v2 * v2;
s >= 1.0 || s == 0;
);
multiplier = sqrt(-2.0 * log(s) / s);
v1 *= multiplier;
v2 *= multiplier;
(beat_position == previousBeatPosition) ? (
offset = previousOffset;
) : (
// when offset is 0 we can't adjust the previous note off, so we can't normalize this note
// we already can't move it negative because of minimumPosition
// we also can't move it positive because it will create a gap
(offset == 0) ? (
positionShift = 0;
slider3 = 0;
) : (
oldOffsetForDebugger = offset;
// adjust position
unboundedTimeShiftInMilliseconds = v1*slider1;
unboundedPositionShift = unboundedTimeShiftInMilliseconds * srate / 1000;
unboundedNewPosition = offset + unboundedPositionShift;
newPosition = unboundedNewPosition;
(newPosition < minimumPosition) ? (
newPosition = minimumPosition;
);
(newPosition > maximumPosition) ? (
newPosition = maximumPosition;
);
positionShift = newPosition - offset;
offset = newPosition;
timeShiftInMilliseconds = positionShift * 1000 / srate;
slider3 = timeShiftInMilliseconds;
);
sliderchange(2^2);
previousOffset = offset;
);
// adjust the position of the note's last note off, to avoid overlapping notes or gaps
(lastNoteOffIndexArray[noteNumber] > -1) ? (
buffer[lastNoteOffIndexArray[noteNumber]] += positionShift;
);
// adjust velocity
unboundedVelocityShift = floor(v2*slider2 + 0.5);
unboundedAdjustedVelocity = velocity + unboundedVelocityShift;
adjustedVelocity = unboundedAdjustedVelocity;
(adjustedVelocity < 1) ? (
adjustedVelocity = 1;
);
(adjustedVelocity > 127) ? (
adjustedVelocity = 127;
);
velocityShift = adjustedVelocity - velocity;
adjustedVelocity |= 0;
msg23 = noteNumber + adjustedVelocity * 256;
slider4 = velocityShift;
sliderchange(2^3);
previousBeatPosition = beat_position;
);
(messageType == NOTE_OFF || (messageType == NOTE_ON && velocity < 1)) ? (
lastNoteOffIndexArray[noteNumber] = blockLength+bufferCounter;
);
buffer[blockLength+bufferCounter] = offset;
buffer[blockLength+bufferCounter+1] = msg1;
buffer[blockLength+bufferCounter+2] = msg23;
bufferCounter += 3;
);
);
// Find everything in the buffer that is due
// to be played back in this block.
i = 0;
while (
(i < bufferCounter) ? (
offset = buffer[blockLength + i];
(offset < samplesblock) ? (
midisend(offset, buffer[blockLength+i+1], buffer[blockLength+i+2]);
memcpy(blockLength+i, blockLength+i+3, bufferCounter-i);
bufferCounter -= 3;
) : (
buffer[blockLength+i] -= samplesblock;
i += 3;
);
);
);