-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsamplazzi.lua
163 lines (135 loc) · 3.26 KB
/
samplazzi.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
-- ~ samplazzi ~
-- a port of rodrigo's max patch
-- to norns
-- by: @cfd90
--
-- load four samples
-- and plug in a shnth!
--
-- controls:
-- bars - play sample
-- major - toggle 1/2 rate
-- minor - directions
-- corps - rate slew
--
-- don't forget to (tar)!
local shnth = require "shnth/lib/shnth"
local ui_metro
local s
local rates = {1, 1, 1, 1}
local max_rates = {1, 1, 1, 1}
local dirs = {1, 1, 1, 1}
local slews = {1, 1}
function init()
-- Connect to shnth device.
s = hid.connect()
s.event = shnth.event
-- Setup UI refresh metro.
ui_metro = metro.init()
ui_metro.time = 1/15
ui_metro.event = function()
redraw()
end
ui_metro:start()
-- Reset softcut state.
softcut.buffer_clear()
-- Build out params.
params:add_separator()
for i=1,4 do
params:add_file("sample " .. i, "sample " .. i)
params:set_action("sample " .. i, function(filename)
load_file(i, filename)
end)
end
end
function shnth.bar(n, d)
rates[n] = d
-- Note: we call abs() on rate to keep direction, but pan goes from -1 to 1.
softcut.rate(n, math.abs(d) * max_rates[n] * dirs[n])
softcut.pan(n, d)
end
function shnth.minor(n, z)
if z == 0 then
return
end
if dirs[n] == 1 then
dirs[n] = -1
else
dirs[n] = 1
end
end
function shnth.major(n, z)
if z == 0 then
return
end
if max_rates[n] == 1 then
max_rates[n] = 0.5
else
max_rates[n] = 1
end
end
function shnth.corp(n, d)
local slew = math.abs(d)
if n == 1 then
slews[1] = slew
softcut.rate_slew_time(1, slew * 3)
softcut.rate_slew_time(2, slew * 3)
else
slews[2] = slew
softcut.rate_slew_time(3, slew * 3)
softcut.rate_slew_time(4, slew * 3)
end
end
function redraw()
screen.clear()
-- Draw bar indicators and sample info.
for i=1,4 do
local r = rates[i]
-- Draw bars.
if math.abs(r) >= 0.1 then
screen.level(r > 0 and 15 or 2)
screen.move((i * 32) - 16 + math.abs(r * 15), 26)
screen.circle((i * 32) - 16, 26, math.abs(r * 15))
screen.stroke()
end
-- Draw sample info (max speed, direction).
screen.level(5)
screen.move((i * 32) - 16, 55)
screen.text_center((max_rates[i] == 0.5 and "0.5" or "1.0") .. (dirs[i] == 1 and ">" or "<"))
end
-- Draw slew rects at bottom.
screen.level(15)
screen.move(8, 60)
screen.rect(8, 60, 48 * slews[1], 2)
screen.fill()
screen.move(72, 60)
screen.rect(72, 60, 48 * slews[2], 2)
screen.fill()
screen.update()
end
function load_file(i, filename)
-- Samples get loaded two buffer 1 or 2 and
-- "slot" (offset) 1 or 2 depending on bar index.
local buf = (i == 1 or i == 2) and 1 or 2
local off = (i == 1 or i == 3) and 1 or 30
-- Load into appropriate buffer.
softcut.buffer_read_mono(filename, 0, off, -1, 1, buf)
-- Configure default voice settings.
softcut.enable(i, 1)
softcut.buffer(i, buf)
softcut.level(i, 1)
softcut.loop(i, 1)
softcut.loop_start(i, off)
softcut.loop_end(i, off + file_length(filename))
softcut.position(i, off)
softcut.play(i, 1)
end
function file_length(file)
if util.file_exists(file) == true then
local ch, samples, samplerate = audio.file_info(file)
local duration = samples / samplerate
return duration
else
return 0
end
end