Skip to content

Commit

Permalink
Merge pull request #5 from schollz/voyage
Browse files Browse the repository at this point in the history
v1.3
  • Loading branch information
schollz authored Jun 29, 2021
2 parents b632a91 + 130f005 commit f87bed8
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 40 deletions.
66 changes: 38 additions & 28 deletions lib/Engine_MxSamples.sc
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,28 @@ Engine_MxSamples : CroneEngine {
// MxSamples specific
var sampleBuffMxSamples;
var sampleBuffMxSamplesDelay;
var samplerPlayerMxSamples;
var mxsamplesVoices=30;
var mxsamplesMaxVoices=40;
var mxsamplesVoiceAlloc;
// MxSamples ^

*new { arg context, doneCallback;
^super.new(context, doneCallback);
}

alloc {
mxsamplesVoiceAlloc=Dictionary.new(mxsamplesMaxVoices);

context.server.sync;

sampleBuffMxSamples = Array.fill(80, { arg i;
Buffer.new(context.server);
});
sampleBuffMxSamplesDelay = Array.fill(mxsamplesVoices, { arg i;
sampleBuffMxSamplesDelay = Array.fill(mxsamplesMaxVoices, { arg i;
Buffer.alloc(context.server,48000,2);
});

(0..(mxsamplesVoices-1)).do({arg i;
SynthDef("player"++i,{
arg bufnum,bufnumDelay, amp, t_trig=0,envgate=1,
SynthDef("mxPlayer",{
arg bufnum,bufnumDelay, amp, t_trig=0,envgate=1,name=1,
attack=0.015,decay=1,release=2,sustain=0.9,
sampleStart=0,sampleEnd=1,rate=1,pan=0,
lpf=20000,hpf=10,
Expand Down Expand Up @@ -64,22 +66,12 @@ Engine_MxSamples : CroneEngine {
));
// delay w/ 30 voices = 1.5% (one core) per voice
// w/o delay w/ 30 voices = 1.1% (one core) per voice
// SendTrig.kr(Impulse.kr(1),name,1);
DetectSilence.ar(snd,doneAction:2);
// just in case, release after 1 minute
FreeSelf.kr(TDelay.kr(DC.kr(1),60));
Out.ar(0,snd)
}).add;
});

samplerPlayerMxSamples = Array.fill(mxsamplesVoices,{arg i;
Synth("player"++i, [\bufnumDelay,sampleBuffMxSamplesDelay[i]],target:context.xg);
});

this.addCommand("mxsamplesvoicenum","i", { arg msg;
if (msg[1]<mxsamplesVoices,{
(msg[1]..(mxsamplesVoices-1)).do({arg i;
samplerPlayerMxSamples[i].free;
});
},{});
});

}).add;

this.addCommand("mxsamplesrelease","", { arg msg;
(0..79).do({arg i; sampleBuffMxSamples[i].free});
Expand All @@ -91,8 +83,16 @@ Engine_MxSamples : CroneEngine {
});

this.addCommand("mxsampleson","iiffffffffffffff", { arg msg;
// lua is sending 1-index
samplerPlayerMxSamples[msg[1]-1].set(
var name=msg[1];
if (mxsamplesVoiceAlloc.at(name)!=nil,{
if (mxsamplesVoiceAlloc.at(name).isRunning==true,{
("stealing "++name).postln;
mxsamplesVoiceAlloc.at(name).free;
});
});
mxsamplesVoiceAlloc.put(name,
Synth("mxPlayer",[
\bufnumDelay,sampleBuffMxSamplesDelay[msg[1]-1],
\t_trig,1,
\envgate,1,
\bufnum,msg[2],
Expand All @@ -109,21 +109,31 @@ Engine_MxSamples : CroneEngine {
\delayBeats,msg[13],
\delayFeedback,msg[14],
\delaySend,msg[15],
\sampleStart,msg[16],
\sampleStart,msg[16] ],target:context.server).onFree({
("freed "++name).postln;
NetAddr("127.0.0.1", 10111).sendMsg("voice",name,0);
});
);
NodeWatcher.register(mxsamplesVoiceAlloc.at(name));
});

this.addCommand("mxsamplesoff","i", { arg msg;
// lua is sending 1-index
samplerPlayerMxSamples[msg[1]-1].set(
\envgate,0,
);
var name=msg[1];
if (mxsamplesVoiceAlloc.at(name)!=nil,{
if (mxsamplesVoiceAlloc.at(name).isRunning==true,{
mxsamplesVoiceAlloc.at(name).set(
\envgate,0,
);
});
});
});

}

free {
(0..79).do({arg i; sampleBuffMxSamples[i].free});
(0..(mxsamplesVoices-1)).do({arg i; samplerPlayerMxSamples[i].free});
(mxsamplesMaxVoices).do({arg i; sampleBuffMxSamplesDelay[i].free;});
mxsamplesVoiceAlloc.keysValuesDo({ arg key, value; value.free; });
}
}
17 changes: 13 additions & 4 deletions lib/mx.samples.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ local Formatters=require 'formatters'

local MxSamples={}

local MaxVoices=30
local MaxVoices=40
local delay_rates_names={"whole-note","half-note","quarter note","eighth note","sixteenth note","thirtysecond"}
local delay_rates={4,2,1,1/2,1/4,1/8,1/16}

Expand Down Expand Up @@ -74,6 +74,7 @@ function MxSamples:new(args)
l.buffers_used={} -- map buffer number to data
l.buffer=0
l.voice={} -- list of voices and how hold they are
l.voice_last=1
for i=1,MaxVoices do -- initiate with 40 voices
l.voice[i]={age=current_time(),active={name="",midi=0}}
end
Expand Down Expand Up @@ -183,6 +184,17 @@ function MxSamples:new(args)
controlspec=controlspec.new(0,100,'lin',0,0,'%',1/100)}
params:add_option("mxsamples_scale_velocity","scale with velocity",{"off","on"})

osc.event=function(path,args,from)
if path=="voice" then
local voice_num=args[1]
local onoff=args[2]
if onoff==0 and voice_num~=nil then
l.voice[voice_num].age=current_time()
l.voice[voice_num].active={name="",midi=0}
end
end
end

return l
end

Expand Down Expand Up @@ -422,8 +434,6 @@ function MxSamples:off(d)
if self.debug then
print("mxsamples: turning off "..d.name..":"..d.midi)
end
self.voice[i].age=current_time()
self.voice[i].active={name="",midi=0}
engine.mxsamplesoff(i)

-- add a release sound effect if its not a release
Expand All @@ -435,7 +445,6 @@ function MxSamples:off(d)
if voice_i > 0 then
clock.run(function()
clock.sleep(0.5)
self.voice[voice_i].active={name="",midi=0}
engine.mxsamplesoff(voice_i)
end)
end
Expand Down
32 changes: 24 additions & 8 deletions mx.samples.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
-- mx.samples v1.2.0
-- mx.samples v1.3.0
-- download and play samples
--
-- llllllll.co/t/mxsamples
Expand Down Expand Up @@ -49,15 +49,28 @@ 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


print("available instruments: ")
tab.print(skeys:list_instruments())
clock.run(redraw_clock)
clock.run(redraw_clock)
end


Expand Down Expand Up @@ -143,9 +156,9 @@ function update_uilist()
end
table.insert(items,s)
end
local index = 1
if uilist ~= nil then
index = uilist.index
local index=1
if uilist~=nil then
index=uilist.index
end
uilist=UI.ScrollingList.new(0,0,index,items)
end
Expand All @@ -170,6 +183,9 @@ function key(k,z)
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
Expand Down Expand Up @@ -207,14 +223,14 @@ function download(id)
cmd="unzip "..download_file.." -d ".._path.audio.."mx.samples/"..id.."/"
print(cmd)
os.execute(cmd)
cmd = "rm "..download_file
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/30) -- refresh
clock.sleep(1/10) -- refresh
redraw()
end
end
Expand Down

0 comments on commit f87bed8

Please sign in to comment.