-
Notifications
You must be signed in to change notification settings - Fork 7
/
mx.samples.lua
316 lines (296 loc) · 8.78 KB
/
mx.samples.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
-- mx.samples v1.4.3
-- download and play samples
--
-- llllllll.co/t/mxsamples
--
-- 1. plug in a midi controller
-- 2. select a sample.
-- 3. profit.
local UI=require "ui"
mxsamples=include("mx.samples/lib/mx.samples")
engine.name="MxSamples"
skeys=nil
uilist=nil
downloading=false
download_available=0
instrument_current=1
available_instruments={
{name="12string piano",size=40*1.5},
{name="alto sax choir",size=17*1.5},
{name="angelic dobro",size=35},
{name="asat classic clean",size=28*1.5},
{name="ash harmonium",size=307*1.5},
{name="bedroom clarinet short",size=7*1.5},
{name="bedroom clarinet sustained",size=50*1.5},
{name="blackhole guitar",size=27*1.5},
{name="box violin",size=8*1.5},
{name="cello",size=22*1.5},
{name="cello pad",size=4*1.5},
{name="claus piano",size=616},
{name="claus piano wpedal",size=614},
{name="cow pad",size=61},
{name="dictaphone",size=18},
{name="discord choir",size=18},
{name="doom drone",size=47},
{name="drums acoustic",size=31*1.5},
{name="drums snapping",size=1.5},
{name="drums violin",size=5},
{name="dyn evo choir slow",size=288*1.5},
{name="dyn evo choir fast",size=172*1.5},
{name="ebow guitar",size=137*1.5},
{name="epiphone guitar",size=5.9*1.5},
{name="epiano dx7",size=15},
{name="epiano r3",size=350},
{name="fender rhodes",size=102},
{name="fender strato vib",size=15*5.2},
{name="gentle vibes",size=206},
{name="glockenspiel",size=12*1.5},
{name="ghost piano",size=40*1.5},
{name="hannas melodica",size=40*1.5},
{name="hohner guitaret",size=137*1.5},
{name="harmonium",size=535},
{name="india ashberry dry",size=15},
{name="kawai felt",size=62.8*2},
{name="lamp",size=4},
{name="kalimba",size=217*1.5},
{name="marimba red",size=3.8*1.5},
{name="marimba white",size=3.8*1.5},
{name="music box",size=20},
{name="november piano",size=5*1.5},
{name="piano soft",size=53.3*1.5},
{name="phils banjo",size=3.8*1.5},
{name="raindrop c40",size=7},
{name="reverse piano",size=7},
{name="sheltone organ",size=31*1.5},
{name="steel string",size=33*1.5},
{name="steinway model b",size=128*1.5},
{name="strat 62",size=26*1.5},
{name="string spurs",size=221},
{name="string spurs swells",size=31},
{name="sufjanwho",size=1.5*7.3},
{name="sweep bassoon",size=50},
{name="sweep celli",size=50},
{name="sweep clarinet",size=50},
{name="sweep euphonium",size=50},
{name="sweep flute",size=50},
{name="sweep horns",size=50},
{name="sweep oboe",size=50},
{name="sweep trombone",size=50},
{name="sweep trumpet",size=50},
{name="sweep violins",size=50},
{name="tatak piano",size=127*1.5},
{name="toypiano barbie",size=55*1.5},
{name="toypiano base",size=10*1.5},
{name="toypiano blue",size=29*1.5},
{name="toypiano decreipt",size=11*1.5},
{name="toypiano madeline",size=8*1.5},
{name="toypiano tatak",size=62*1.5},
{name="telecaster",size=7.1*1.5},
{name="trembling radiator",size=16*1.5},
{name="trembling radiator ebow",size=16*1.5},
{name="uilleann pipes",size=117},
{name="wind chimes",size=5},
}
function init()
cmd="mkdir -p ".._path.audio.."mx.samples/"
print(cmd)
os.execute(cmd)
skeys=mxsamples:new()
update_uilist()
setup_midi()
local f=io.open(_path.data.."mx.samples/last","rb")
if f~=nil then
local content=f:read("*all")
f:close()
local last_instrument_current=tonumber(content)
if last_instrument_current~=nil then
instrument_current=last_instrument_current
uilist.index=instrument_current
update_uilist()
end
end
-- set default delay
params:set("mxsamples_delay_rate",4)
params:set("mxsamples_delay_times",4)
print("available instruments: ")
tab.print(skeys:list_instruments())
clock.run(redraw_clock)
end
function setup_midi()
-- get list of devices
local mididevice={}
local mididevice_list={"none"}
midi_channels={"all"}
for i=1,16 do
table.insert(midi_channels,i)
end
for _,dev in pairs(midi.devices) do
if dev.port~=nil then
local name=string.lower(dev.name)
table.insert(mididevice_list,name)
print("adding "..name.." to port "..dev.port)
mididevice[name]={
name=name,
port=dev.port,
midi=midi.connect(dev.port),
active=false,
}
mididevice[name].midi.event=function(data)
if mididevice[name].active==false then
do return end
end
local d=midi.to_msg(data)
--if d.type~="clock" then
-- tab.print(d)
--end
if d.ch~=midi_channels[params:get("midichannel")] and params:get("midichannel")>1 then
do return end
end
if d.type=="note_on" then
skeys:on({name=available_instruments[instrument_current].id,midi=data[2],velocity=data[3]})
elseif d.type=="note_off" then
skeys:off({name=available_instruments[instrument_current].id,midi=data[2]})
elseif d.cc==64 then -- sustain pedal
local val=d.val
if val>126 then
val=1
else
val=0
end
if params:get("mxsamples_pedal_mode")==1 then
engine.mxsamples_sustain(val)
else
engine.mxsamples_sustenuto(val)
end
end
end
end
end
tab.print(mididevice_list)
params:add{type="option",id="midi",name="midi in",options=mididevice_list,default=1}
params:set_action("midi",function(v)
if v==1 then
do return end
end
for name,_ in pairs(mididevice) do
mididevice[name].active=false
end
mididevice[mididevice_list[v]].active=true
end)
params:add{type="option",id="midichannel",name="midi ch",options=midi_channels,default=1}
if #mididevice_list>1 then
params:set("midi",2)
end
end
function update_uilist()
-- check if downloaded
items={}
for i,a in ipairs(available_instruments) do
available_instruments[i].id=string.gsub(a.name," ","_")
local files_for=os.capture("ls /home/we/dust/audio/mx.samples/"..available_instruments[i].id.."/*.wav")
local downloaded=false
if string.find(files_for,".wav") then
downloaded=true
end
local s=a.name
available_instruments[i].downloaded=downloaded
available_instruments[i].active=(downloaded and i==instrument_current)
if not downloaded then
s=s.." - get?"
end
if available_instruments[i].active then
s='> '..s..' <'
else
s=' '..s
end
table.insert(items,s)
end
local index=1
if uilist~=nil then
index=uilist.index
end
uilist=UI.ScrollingList.new(0,0,index,items)
end
function os.capture(cmd,raw)
local f=assert(io.popen(cmd,'r'))
local s=assert(f:read('*a'))
f:close()
if raw then return s end
s=string.gsub(s,'^%s+','')
s=string.gsub(s,'%s+$','')
s=string.gsub(s,'[\n\r]+',' ')
return s
end
function enc(k,d)
uilist:set_index_delta(d,true)
end
function key(k,z)
if z==1 then
local i=uilist.index
if available_instruments[i].downloaded then
instrument_current=i
f=io.open(_path.data.."mx.samples/last","w")
f:write(instrument_current)
f:close()
update_uilist()
elseif download_available>0 then
if k==2 then
download_available=0
elseif k==3 then
-- download!
downloading=true
redraw()
clock.run(function()
download(available_instruments[download_available].id)
instrument_current=download_available
update_uilist()
skeys:add_folder(_path.audio.."mx.samples/"..available_instruments[download_available].id.."/")
download_available=0
downloading=false
redraw()
end)
end
else
download_available=i
end
end
end
function download(id)
local url="https://github.com/schollz/mx.samples/releases/download/samples/"..id..".zip"
local download_file=_path.audio.."mx.samples/"..id.."/download.zip"
cmd="mkdir -p ".._path.audio.."mx.samples/"..id
print(cmd)
os.execute(cmd)
cmd="curl -L -o "..download_file.." "..url
print(cmd)
os.execute(cmd)
cmd="unzip "..download_file.." -d ".._path.audio.."mx.samples/"..id.."/"
print(cmd)
os.execute(cmd)
cmd="rm "..download_file
print(cmd)
os.execute(cmd)
end
function redraw_clock() -- our grid redraw clock
while true do -- while it's running...
clock.sleep(1/10) -- refresh
redraw()
end
end
function redraw()
screen.clear()
if downloading then
msg=UI.Message.new({"downloading",available_instruments[download_available].name,"please wait..."})
msg:redraw()
elseif download_available>0 then
local s=available_instruments[download_available].name..' ('..available_instruments[download_available].size..' MB)'
msg=UI.Message.new({"are you sure you","want to download",s.."?","k2 = no, k3 = yes"})
msg:redraw()
else
uilist:redraw()
end
screen.update()
end
function rerun()
norns.script.load(norns.state.script)
end