-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfunc.lua
More file actions
424 lines (349 loc) · 10.9 KB
/
func.lua
File metadata and controls
424 lines (349 loc) · 10.9 KB
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
415
416
417
418
419
420
421
require"util"
require"midi"
require"prefs"
-- global options box
OPTIONS = {
low = 1,
high = 6,
length = 2,
release = .2,
tags = {[0]=false, false, false, false, false, false, false, false},
notes = {[0]=true, false, false, false, true, false, false, false, true, false, false, false},
name = "Recorded Hardware",
hardware_name = "",
background = false,
post_record_normalize_and_trim = false,
mapping = 2,
layers = 1,
rrobin = 1,
between_time = 100
}
TAGS = {[0]="Bass", "Drum", "FX", "Keys", "Lead", "Pad", "Strings", "Vocal"}
NOTES = {[0]="C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"}
-- note/tag button colors
C_PRESSED = {100, 200, 100}
C_NOT_PRESSED = {20, 20, 20}
-- state
STATE = {
midi_device = nil, -- current midi device name
dev = nil, -- current midi device object
midi_device_index = 1,-- current midi device selected in options list
recording = false, -- are we actively recording
notes = nil, -- list of notes to send to the midi device
notei = nil, -- current index in note list
layers = nil, -- number of velocity layers for each note
layeri = nil, -- current layer
rrobin = nil, -- number of round robin layers for each note
rrobini = nil, -- current round robin layer
total = nil, -- total amount of notes that will be sampled
inst = nil, -- instrument to which samples will be saved
inst_index = nil -- instrument index
}
-- reset state to be ready to record
function reset_state()
STATE.notei = nil
STATE.notes = nil
STATE.recording = false
STATE.layers = nil
STATE.layeri = nil
STATE.rrobin = nil
STATE.rrobini = nil
STATE.total = nil
STATE.inst = nil
STATE.inst_index = nil
reset_midi_device_state()
TOCALL = nil -- from util.lua
KILL = false -- from util.lua
-- erase any timers
if renoise.tool():has_timer(start_note) then
renoise.tool():remove_timer(start_note)
end
if renoise.tool():has_timer(stop_note) then
renoise.tool():remove_timer(stop_note)
end
end
function reset_midi_device_state()
local index = 1
local device_name = renoise.Midi.available_output_devices()[index]
STATE.dev = device_name
STATE.midi_device = nil
STATE.midi_device_index = index
end
renoise.Midi.devices_changed_observable(reset_midi_device_state())
-- toggles the on/off state of a button
function toggle_button(button, ttype)
print("toggling ", tostring(ttype), tostring(button))
-- set data to
OPTIONS[ttype][button] = not OPTIONS[ttype][button]
-- set visual
vb.views[tostring(ttype).."_button_"..tostring(button)].color =
OPTIONS[ttype][button] and C_PRESSED or C_NOT_PRESSED
end
-- generate list of midi notes to send to the controller
function gen_notes()
-- midi 0xc = c0
-- renoise 0 = c0
local ret = {}
local step = 4
for n=12*OPTIONS.low,12*(OPTIONS.high + 1)-1 do
if OPTIONS.notes[n%12] then
table.insert(ret, n)
end
end
return ret
end
-- check for invalid options
function check()
if OPTIONS.low > OPTIONS.high then
renoise.app():show_prompt("Oops!", "Low octave must be <= high octave.", {"OK"})
return false
end
local foundnote = false
for i=0,11 do
if OPTIONS.notes[i] == true then
foundnote = true
break
end
end
if not foundnote then
renoise.app():show_prompt("Oops!", "You must select at least one note.", {"OK"})
return false
end
return true
end
-- start the recording process
function go()
print("Starting...")
reset_state()
if not check() then
stop()
return false
end
STATE.notes = gen_notes()
STATE.notei = 1
STATE.dev = get_midi_dev()
STATE.layers = math.floor(OPTIONS.layers)
STATE.layeri = 1
STATE.rrobin = math.floor(OPTIONS.rrobin)
STATE.rrobini = 1
STATE.total = table.count(STATE.notes) * STATE.layers * STATE.rrobin
STATE.inst = renoise.song().selected_instrument
STATE.inst_index = renoise.song().selected_instrument_index
print("Going to create "..tostring(STATE.total).." samples.")
-- get inst
local inst = STATE.inst
-- clear samples
while table.count(inst.samples) > 0 do
inst:delete_sample_at(1)
end
-- insert blank samples
for i=1,STATE.total do
inst:insert_sample_at(i)
end
-- start on first sample
renoise.song().selected_sample_index = 1
-- go!
prep_note()
end
-- returns dict mapping note -> [low, high] mapping boundaries
function get_mapping_dict()
-- set up mappings
local mapping_dict = {}
for i=1,table.count(STATE.notes) do
local low
local high
if OPTIONS.mapping == 1 then -- down
if i == 1 then
low = 0
else
low = STATE.notes[i-1] + 1
end
if i == table.count(STATE.notes) then
high = 119
else
high = STATE.notes[i]
end
elseif OPTIONS.mapping == 2 then -- middle
local function get_dists(note)
local up = 1
local down = 1
-- up
local i = (note + 1)%12
while not OPTIONS.notes[i] do
i = (i + 1)%12
up = up + 1
end
-- down
i = (note - 1)%12
while not OPTIONS.notes[i] do
i = (i - 1)%12
down = down + 1
end
return {up = math.floor(up/2), down = math.floor((down-1)/2)}
end
local diffs = get_dists(STATE.notes[i])
if i == 1 then
diffs.down = STATE.notes[i]
elseif i == table.count(STATE.notes) then
diffs.up = 119 - STATE.notes[i]
end
low = STATE.notes[i] - diffs.down
high = STATE.notes[i] + diffs.up
elseif OPTIONS.mapping == 3 then -- up
if i == 1 then
low = 0
else
low = STATE.notes[i]
end
if i == table.count(STATE.notes) then
high = 119
else
high = STATE.notes[i+1] - 1
end
end
mapping_dict[STATE.notes[i]]={low, high}
end
return mapping_dict
end
function do_mapping(mapping_dict)
local inst = STATE.inst
for i=1,table.count(STATE.notes) do
for l=1,STATE.layers do
for r=1,STATE.rrobin do
if mapping_dict[STATE.notes[i]] then
local idx = get_sample_index(i, l, r)
local mapping = inst.sample_mappings[1][idx]
mapping.base_note = STATE.notes[i]
mapping.note_range = mapping_dict[STATE.notes[i]]
local lunit = 128/STATE.layers
local lrange = {math.floor((l-1)*lunit), math.floor((l)*lunit - 1)}
mapping.velocity_range = lrange
end
end
end
end
end
-- apply finishing touches
function finish()
update_status("Finishing...")
local inst = STATE.inst
local lunit = 128/STATE.layers
-- name instrument
update_instrument_name()
-- name samples
for i=1,table.count(STATE.notes) do
for l=1,STATE.layers do
for r=1,STATE.rrobin do
local vel = math.floor((l)*lunit - 1)
local idx = get_sample_index(i, l, r)
inst.samples[idx].name = note_to_name(STATE.notes[i]).."_"..string.format("%X", vel).."_"..string.format("%X", r)
end
end
end
-- do mappings
do_mapping(get_mapping_dict())
-- set mapping to round robin if specified
if STATE.rrobin > 1 then
inst.sample_mapping_overlap_mode = renoise.Instrument.OVERLAP_MODE_CYCLED
end
-- close recording window
renoise.app().window.sample_record_dialog_is_visible = false
-- close midi
STATE.dev:close()
-- normalize samples if enabled
if OPTIONS.post_record_normalize_and_trim then
normalize_and_trim()
end
end
function normalize_and_trim_coroutine()
-- call processing coroutines serially
update_status("Normalizing samples...")
normalize_coroutine()
update_status("Trimming samples...")
trim_coroutine()
update_status("All samples normalized and trimmed.")
end
-- normalize and trim the samples
function normalize_and_trim()
prep_processing(normalize_and_trim_coroutine)
SAMPLE_PROCESSING_PROCESS:start()
end
-- kill switch
function stop()
KILL = true
if STATE.recording then
renoise.app().window.sample_record_dialog_is_visible = true
renoise.song().transport:start_stop_sample_recording()
STATE.recording = false
STATE.dev:send({NOTE_OFF, STATE.notes[STATE.notei] + 0xC, 0x40}) -- release current note
STATE.dev:send({ALL_NOTE_OFF_1 , ALL_NOTE_OFF_2, 0x00}) -- send all notes off
end
renoise.app().window.sample_record_dialog_is_visible = false
if STATE.dev and STATE.dev.is_open then
STATE.dev:close()
end
update_status("Stopped")
end
-- open the recording settings window
function configure()
renoise.app().window.sample_record_dialog_is_visible = true
end
function get_sample_index(notei, layeri, rrobini)
return (notei-1)*STATE.layers*STATE.rrobin + STATE.rrobin*(layeri-1) + (rrobini-1) + 1
end
-- get ready to play a note
-- recording starts here
function prep_note()
-- check that "new instrument on each take" is not selected by comparing the
-- currently selected instrument index to the one we had when we started.
if renoise.song().selected_instrument_index ~= STATE.inst_index then
renoise.app():show_prompt("Oops!", "A different instrument became selected. The same instrument must remain selected throughout the process. Make sure the \"Create new instrument on each take\" option is NOT checked in the Renoise recording settings dialog.", {"OK"})
stop()
renoise.song().selected_instrument_index = STATE.inst_index
renoise.app().window.sample_record_dialog_is_visible = true
return
end
local idx = get_sample_index(STATE.notei, STATE.layeri, STATE.rrobini)
print("Prepping note "..tostring(idx).."...")
renoise.song().selected_sample_index = idx
renoise.app().window.sample_record_dialog_is_visible = true
renoise.song().transport:start_stop_sample_recording()
STATE.recording = true
call_in(start_note, 50)
end
-- play the note
function start_note()
print("Starting note...")
STATE.dev:send({NOTE_OFF, STATE.notes[STATE.notei] + 0xC, 0x40}) -- just in case...
local lunit = 128/STATE.layers
local vel = math.floor((STATE.layeri)*lunit - 1)
STATE.dev:send({NOTE_ON, STATE.notes[STATE.notei] + 0xC, vel})
call_in(release_note, OPTIONS.length * 1000)
end
-- release the note
function release_note()
print("Releasing note...")
STATE.dev:send({NOTE_OFF, STATE.notes[STATE.notei] + 0xC, 0x40})
call_in(stop_note, OPTIONS.release * 1000)
end
-- stop recording, prep next note
function stop_note()
print("Stopping note...")
renoise.app().window.sample_record_dialog_is_visible = true
renoise.song().transport:start_stop_sample_recording()
STATE.recording = false
STATE.rrobini = STATE.rrobini + 1
if STATE.rrobini > STATE.rrobin then
STATE.rrobini = 1
STATE.layeri = STATE.layeri + 1
if STATE.layeri > STATE.layers then
STATE.layeri = 1
STATE.notei = STATE.notei + 1
end
end
if STATE.notes[STATE.notei] ~= nil then
call_in(prep_note, OPTIONS.between_time)
else
call_in(finish, OPTIONS.between_time)
end
end