-
Notifications
You must be signed in to change notification settings - Fork 1
/
patch-strudel.py
211 lines (199 loc) · 6.43 KB
/
patch-strudel.py
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
#!/bin/python3
'''
Patching Strudel...
'''
import os
import os.path
# We don't want to patch any file more than once.
'''
Workaround for a bug in Csound's handling of string pfields; Strudel controls
passed in p6 as a string have to be omitted to preserve other pfields.
'''
csound_mjs_filepath = "strudel/packages/csound/index.mjs"
print(f"Patching '{csound_mjs_filepath}'")
with open(csound_mjs_filepath, "r+") as file:
find_this = '''i ${p1} ${p2} ${p3} ${p4} ${p5} "${p6}"'''
replace_with = '''i ${p1} ${p2} ${p3} ${p4} ${p5}'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
find_this = ''' p1 = `"{instrument}"`;'''
replace_with = ''' p1 = `"${instrument}"`;'''
patched_text = patched_text.replace(find_this, replace_with)
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)
'''
Fixes Strudel so that undici does not fail on a URL containing '//' in a
pathspec.
'''
webaudio_sampler_mjs_filepath = "strudel/packages/superdough/sampler.mjs";
print(f"Patching '{webaudio_sampler_mjs_filepath}'")
with open(webaudio_sampler_mjs_filepath, "r+") as file:
find_this = '''`https://raw.githubusercontent.com/${path}/strudel.json`;'''
replace_with = '''`https://raw.githubusercontent.com/${path}/strudel.json`.replace("//strudel.json", "/strudel.json");'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)
'''
Fixes Strudel so that HTML pages are not cached by Workbox, which was screwing
up navigation on the Web site, and to build sourcemaps.
'''
astro_config_mjs_filepath = "strudel/website/astro.config.mjs";
print(f"Patching '{astro_config_mjs_filepath}'")
with open(astro_config_mjs_filepath, "r+") as file:
find_this = ''' workbox: {
globPatterns: ['**/*.{js,css,html'''
replace_with = ''' // MKG patch...
workbox: {
globPatterns: ['**/*.{js,css'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
find_this = ''' vite: {
ssr: {
// Example: Force a broken package to skip SSR processing, if needed
// external: ['fraction.js'], // https://github.com/infusion/Fraction.js/issues/51
},
},'''
replace_with = '''
// MKG patch...
vite: {
ssr: {
// Example: Force a broken package to skip SSR processing, if needed
// external: ['fraction.js'], // https://github.com/infusion/Fraction.js/issues/51
},
build: {
sourcemap: true,
},
},
// MKG patch.
'''
patched_text = patched_text.replace(find_this, replace_with)
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)
'''
Fixes Strudel so that _all_ triggers go to the default output. This helps
with creating stateful patterns.
'''
# pattern_mjs_filepath = "strudel/packages/core/pattern.mjs";
# print(f"Patching '{pattern_mjs_filepath}'")
# with open(pattern_mjs_filepath, "r+") as file:
# find_this = '''if (!dominant && hap.context.onTrigger) {'''
# replace_with = '''if (hap.context.onTrigger) {'''
# text = file.read()
# patched_text = text.replace(find_this, replace_with)
# print(patched_text)
# file.seek(0)
# file.truncate()
# file.write(patched_text)
'''
Fixes the Strudel piano roll to display haps from stateful Patterns as
an alternative to regular patterns (which still work).
'''
pattern_mjs_filepath = "strudel/packages/draw/pianoroll.mjs";
print(f"Patching '{pattern_mjs_filepath}'")
with open(pattern_mjs_filepath, "r+") as file:
find_this = ''' (haps, time) => {
pianoroll({
'''
replace_with = ''' (haps, time) => {
// BEGIN MKG PATCH
// Usually haps is much much larger than haps_from_outputs.
/*
if (globalThis.haps_from_outputs) {
} else {
globalThis.haps_from_outputs = [];
}
if (globalThis.haps_from_outputs.length > 0) {
haps.push(...globalThis.haps_from_outputs);
haps = globalThis.haps_from_outputs.filter(inFrame);
globalThis.haps_from_outputs = haps;
}
*/
// END MKG PATCH
pianoroll({
'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)
'''
Fixes Strudel to store the single Cyclist in globalThis,
and to null the Haps buffer for pianoroll in the start function.
'''
cyclist_mjs_filepath = "strudel/packages/core/cyclist.mjs";
print(f"Patching '{cyclist_mjs_filepath}'")
with open(cyclist_mjs_filepath, "r+") as file:
find_this = ''' interval, // duration of each cycle
0.1,
0.1,
setInterval,
clearInterval,
);
}
now() {'''
replace_with = ''' interval, // duration of each cycle
0.1,
0.1,
setInterval,
clearInterval,
);
// MKG patch...
let that = this;
globalThis.__cyclist__ = that;
// MKG patch.
}
now() {'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
find_this = ''' this.clock.start();
this.setStarted(true);
}'''
replace_with = '''// MKG patch...
this.clock.start();
this.setStarted(true);
globalThis.haps_from_outputs = null;
}
// MKG patch.
'''
patched_text = patched_text.replace(find_this, replace_with);
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)
'''
Fixes Strudel to send Haps to `pianoroll` directly from the default output.
'''
webaudio_mjs_filepath = "strudel/packages/webaudio/webaudio.mjs";
print(f"Patching '{webaudio_mjs_filepath}'")
with open(webaudio_mjs_filepath, "r+") as file:
find_this = '''export const webaudioOutputTrigger = (t, hap, ct, cps) => superdough(hap2value(hap), t - ct, hap.duration / cps, cps);
export const webaudioOutput = (hap, deadline, hapDuration) => superdough(hap2value(hap), deadline, hapDuration);'''
replace_with = '''// MKG patch...
export const webaudioOutputTrigger = (t, hap, ct, cps) => {
if (globalThis.haps_from_outputs) {
globalThis.haps_from_outputs.push(hap);
}
superdough(hap2value(hap), t - ct, hap.duration / cps, cps);
};
export const webaudioOutput = (hap, deadline, hapDuration) => {
if (globalThis.haps_from_outputs) {
globalThis.haps_from_outputs.push(hap);
}
superdough(hap2value(hap), deadline, hapDuration);
};
// MKG patch.
'''
text = file.read()
patched_text = text.replace(find_this, replace_with)
print(patched_text)
file.seek(0)
file.truncate()
file.write(patched_text)