-
Notifications
You must be signed in to change notification settings - Fork 1
/
pitfalls.lua
414 lines (364 loc) · 10.5 KB
/
pitfalls.lua
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
-- "item: There are pitfalls in
-- using equal division
-- within which to calculate
-- 2-interval-patterns which
-- the unwary traveler will
-- most certainly fall into."
-- -- Erv Wilson [0]
-- \/ \/ \/
--
-- Define & play your own
-- microtonal scales.
--
-- Set interval structure as a
-- sequence of large (L), small (s),
-- and medium (M) steps.
--
-- e.g.
-- LLsLLLs L: 2 s: 1 fixed: C
-- - is C Major diatonic scale
--
-- LLsLLLs L: 3 s: 2
-- - is a 19 EDO, 7 note scale
--
-- LLsLLL L: 3 s: 1
-- - is a 16 EDO, 6 note scale
--
-- Or browse named scales. [1]
--
-- Play scale keyboard on grid.
--
-- E1 change PolyPerc cutoff filter
-- randomize MollyThePolly synth params
-- E2 change value
-- E3 select step or parameter
--
-- K1
-- K2 toggle arpeggiator
-- K3 toggle display mode
--
-- [0] http://anaphoria.com/
-- wilsonintroMOS.html
-- [1] http://www.huygens-fokker.org/
-- docs/modename.html
-- .................................................................
--
-- pitfalls 0.4.2 ostinato odington release
-- copyright 02022 robmckinnon
-- GNU GPL v3.0
-- .................................................................
--
include("pitfalls/lib/includes")
pf.debug(false)
local run = false
local coroutine_id = nil
local scale = nil
local intervals = nil
local pitches = nil
local pitches_on = {}
local edit = 1
local previous_edit = 1
local change = {}
local display = display_circle
function init()
-- see lib/parameters.lua for param setup
local sequence = params:get("pitfalls_sequence")
if string.find(sequence, "M") then
scale = Scale:new(params:get("pitfalls_L"), params:get("pitfalls_s"), sequence, params:get("pitfalls_M"))
else
scale = Scale:new(params:get("pitfalls_L"), params:get("pitfalls_s"), sequence)
end
scale:set_mode(params:get("pitfalls_mode"))
scale:set_tonic(params:get("pitfalls_tonic"))
scale:update_edo()
params:set("pitfalls_tonic", scale.tonic, true)
params:set_action("pitfalls_cutoff", function(x) patch.cutoff(x) end)
params:set_action("pitfalls_engine", function(x)
local name = parameters.engine()
patch.load_engine(name)
end)
params:set_action("pitfalls_patch", function(x)
local name = parameters.patch()
patch.load_patch(name)
end)
params:set_action("pitfalls_midi_no_start", function(x) update_pitches(false) end)
params:set_action("pitfalls_tuning", function(x) update_pitches(false) end)
params:set_action("pitfalls_arpeggiate", function(x) update_arpeggiate() end)
params:set_action("pitfalls_grid_lib", function(x)
print("writing grid_lib default params file", parameters.grid_lib())
params:write()
end)
params:set_action("pitfalls_in_virtual_port", function(port)
midi_in.init(port, pitch_on, pitch_off)
params:write()
end)
params:set_action("pitfalls_out_virtual_port", function(port)
midi_out.init(port)
params:write()
end)
patch.load_engine(parameters.engine())
patch.load_patch(parameters.patch())
g.init(pitch_on, pitch_off)
-- midi_out.init()
midi_in.init(params:get("pitfalls_in_virtual_port"), pitch_on, pitch_off)
midi_out.init(params:get("pitfalls_out_virtual_port"))
update_pitches(true)
-- -- mixin the MusicUtil scale names
-- pf.pop_named_sequences(named_scales.lookup)
display.drawintervals(scale, intervals)
arpeggiate.init(pitches_off, pitch_on_position)
if params:get("pitfalls_arpeggiate") ~= 0 then
start_arppegiate()
end
end
function stop_arppegiate()
run = false
if coroutine_id ~= nil then
clock.cancel(coroutine_id)
coroutine_id = nil
end
end
function start_arppegiate()
run = true
coroutine_id = clock.run(step)
end
function step()
while run do
clock.sync(1/params:get("pitfalls_tempo_div"))
do_step()
end
end
function do_step()
arpeggiate[parameters.arpeggiate()](scale, intervals)
redraw()
end
function redraw()
display.redraw(params:get("pitfalls_tuning"), edit, params:get("pitfalls_octave"), arpeggiate.position(), scale, intervals, params:get("pitfalls_midi_no_start"), display == display_patch)
end
function key(n,z)
if n == 3 and z == 1 then
-- increment_mode()
if display == display_strings then
display = display_patch
previous_edit = edit - scale.length
edit = display_orig.tempo_division_input() + scale.length
elseif display == display_patch then
edit = previous_edit + scale.length
display = display_circle
elseif display == display_circle then
display = display_orig
else
display = display_strings
end
redraw()
elseif n == 2 and z == 1 then
parameters.inc_arpeggiate()
redraw()
end
end
function enc(n,d)
if n == 1 then
params:delta("pitfalls_cutoff", d)
elseif n == 2 then
change.edit_position(d)
elseif n ==3 then
change_value(d)
end
end
function toggle_arppegiate()
if run then
stop_arppegiate()
else
start_arppegiate()
end
end
function update_pitches(update_intervals)
if update_intervals then
intervals = ScaleIntervals:new(scale)
arpeggiate.reset_chord_positions()
end
pitches = Pitches:new(scale, intervals, params:get("pitfalls_tuning"), params:get("pitfalls_midi_no_start"))
patch.note_off_all()
display_orig.all_degrees_off()
g.update_grid(scale, intervals, pitches)
midi_in.update_pitches(scale, intervals, pitches)
params:write()
end
function change_value(d)
local input_key = display.edit_position(edit, scale)
if change[input_key] ~= nil then
change[input_key](d)
elseif pcall(params.get, params, input_key) then
params:delta(input_key, d)
end
redraw()
end
function change.edit_position(d)
local input_index = edit - scale.length
local is_display_patch = display == display_patch
local is_scale_name_input = input_index == display_orig.scale_name_input()
local is_tempo_div_input = input_index == display_orig.tempo_division_input()
if is_display_patch and is_scale_name_input and d == 1 then
edit = display_orig.tempo_division_input() + scale.length
elseif is_display_patch and input_index == display_orig.tempo_division_input() and d == -1 then
edit = display_orig.scale_name_input() + scale.length
else
local clamp_input = is_display_patch and patch.is_mx_samples() and display_orig.patch_input()
or is_display_patch and display_orig.engine_input()
or display_orig.o_input()
edit = util.clamp(edit + d, 1, scale.length + clamp_input)
end
input_index = edit - scale.length
if (input_index == display_orig.m_input() and scale:has_medium() == false) then
edit = edit + d
end
redraw()
end
function change.step(d)
if scale:change_step(d, edit) then
params:set("pitfalls_sequence", scale:sequence(), true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
end
function change.tonic(d)
if scale:change_tonic(d) then
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(false)
end
end
function change.mode(d)
if scale:change_mode(d) then
params:set("pitfalls_mode", scale.mode, true)
update_pitches(true)
end
end
function change.small(d)
if scale:change_small(d) then
params:set("pitfalls_s", scale.small, true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
end
function change.medium(d)
if scale:change_medium(d) then
params:set("pitfalls_M", scale.medium, true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
end
function change.large(d)
if scale:change_large(d) then
params:set("pitfalls_L", scale.large, true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
end
function change.scale_name(d)
local scales = reverse_name.no_names[scale.length]
scale_no_index = util.clamp(scale_no_index + d, 1, #scales)
local name = scales[scale_no_index]
local data = reverse_name.no_lookup[scale.length][name]
if data.m == nil then
scale = Scale:new(data.l, data.s, data.seq)
else
scale = Scale:new(data.l, data.s, data.seq, data.m)
end
scale:update_edo()
params:set("pitfalls_sequence", scale:sequence(), true)
params:set("pitfalls_L", scale.large, true)
params:set("pitfalls_M", scale.medium, true)
params:set("pitfalls_s", scale.small, true)
params:set("pitfalls_mode", scale.mode, true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
function change.scale_size(d)
if scale:change_length(d) then
if d == 1 then
edit = util.clamp(edit + 1, 1 + display_orig.n_input(), scale.max_steps + display_orig.n_input())
end
if d == -1 then
edit = util.clamp(edit - 1, scale.min_steps + display_orig.n_input(), edit)
end
params:set("pitfalls_sequence", scale:sequence(), true)
params:set("pitfalls_mode", scale.mode, true)
params:set("pitfalls_tonic", scale.tonic, true)
update_pitches(true)
end
end
function change.tempo_div(d)
if params:get("pitfalls_tempo_div") == 1 then
if d == -1 then
d = -0.5
end
elseif params:get("pitfalls_tempo_div") == 0.5 then
if d == -1 then
d = -0.25
elseif d == 1 then
d = 0.5
end
elseif params:get("pitfalls_tempo_div") == 0.25 then
if d == 1 then
d = 0.25
end
end
params:set("pitfalls_tempo_div",
util.clamp(
params:get("pitfalls_tempo_div") + d,
0.25,
64
)
)
params:write()
end
function update_arpeggiate(x)
-- print(parameters.arpeggiate())
if parameters.arpeggiate() == "off" then
stop_arppegiate()
pitches_off()
patch.note_off_all()
patch.note_kill_all()
elseif parameters.arpeggiate() == "scale_up" then
-- arpeggiate.set_position(scale.length)
start_arppegiate()
elseif parameters.arpeggiate() == "scale_down" then
-- arpeggiate.set_position(1)
patch.note_off_all()
else
arpeggiate.reset()
patch.note_off_all()
end
params:write()
end
function pitch_off(f, deg)
display_orig.degree_off(deg)
g.led_off(f)
patch.pitch_off(f)
midi_out.note_off_pitch_bend(f)
-- midi_out.all_notes_off()
redraw()
end
function pitches_off()
for f,i in pairs(pitches_on) do
if i ~= nil then
pitches_on[f] = nil
pitch_off(f, i)
midi_out.note_off_pitch_bend(f)
end
end
display_orig.all_degrees_off()
redraw()
end
function pitch_on(f, vel, deg)
display_orig.degree_on(deg)
g.led_on(f)
patch.pitch_on(f, vel)
midi_out.note_on_pitch_bend(f)
redraw()
end
function pitch_on_position(i)
local f = pitches:octdegfreq(params:get("pitfalls_octave"), i)
pitches_on[f] = i
pitch_on(f, 120, i)
end