Skip to content

Instantly share code, notes, and snippets.

@heerdyes
Last active November 9, 2024 11:47
Show Gist options
  • Save heerdyes/75d9be811c78fad330bf07101f7aac88 to your computer and use it in GitHub Desktop.
Save heerdyes/75d9be811c78fad330bf07101f7aac88 to your computer and use it in GitHub Desktop.
/ccjam.d/sonicpi/buffers
# ----------------- mod looper ----------------- #
kc=nil
t=0.15
vab='0123456789abcdefghijklmnopqrstuvwxyz'
mz=[]
# 10 tracks
10.times do
mz.append({'q'=>'....', 'o'=>'60', 'v'=>'t', 'mute'=>'n', 'sy'=>"mod_fm",
'mw'=>'0', 'mp'=>'1', 'mr'=>'2',
'dv'=>'2', 'dp'=>'2', 'co'=>'p', 'dt'=>'0',
'a'=>'0', 'd'=>'0', 's'=>'0', 'r'=>'1'})
end
tjump=false
ctr=0
# osc keyboard fsm single state for now
# 0 -> [0-9] -> 0 // toggle track
# 0 -> . -> 0 // mute all
# 0 -> / -> 0 // unmute all
# 0 -> SPACE -> 0 // timejump
state=0
define :cn do |c,n|
return c + ('.'*n)
end
define :sn do |s,n|
tmp=''
s.each_char { |c| tmp=tmp+cn(c, n) }
return tmp
end
# osc control interfaces
live_loop :nwseq do
use_real_time
idx, k, v = sync "/osc*/piseq"
mz[idx][k]=v
end
live_loop :nwcue do
use_real_time
args = sync "/osc*/picue"
kc=args[0]
if kc==46 then
puts "muting everything"
10.times do |i|
mz[i]['mute']='y'
end
elsif kc==47 then
puts "unmuting everything"
10.times do |i|
mz[i]['mute']='n'
end
elsif kc==32 then
puts "timejump: 1t"
tjump=true
elsif kc>=48 and kc<=57 then
trk=kc-48
mz[trk]['mute']=if mz[trk]['mute']=='y' then 'n' else 'y' end
puts "toggled track #{trk}"
end
end
# keymap
keytab={
'z'=>0, 's'=>1,
'x'=>2, 'd'=>3,
'c'=>4,
'v'=>5, 'g'=>6,
'b'=>7, 'h'=>8,
'n'=>9, 'j'=>10,
'm'=>11,
'Z'=>12, 'S'=>13,
'X'=>14, 'D'=>15,
'C'=>16,
'V'=>17, 'G'=>18,
'B'=>19, 'H'=>20,
'N'=>21, 'J'=>22,
'M'=>23
}
define :ntdcode do |c,o|
if not keytab.key?(c) then
puts "#{c} is not a key!"
return o
end
return o+keytab[c]
end
define :vdcode do |c,mx|
if not vab[c] then
puts "#{c} not an alphanumeral"
return 0
end
vi=vab.index(c)
return (vi/(10.0+26.0)) * mx
end
define :xoxdcode do |s,lim|
c=s[ctr%s.length]
v=vdcode c, lim
return v
end
# melody stepper
define :melostep do |j|
if mz[j]['mute']=='y' then; return; end
# note parsing
mq=mz[j]['q']
qi=mq[ctr%mq.length]
if qi=='.' then; return; end
oct=mz[j]['o'].to_i
nt=ntdcode qi, oct
# velocity parsing
vy=xoxdcode mz[j]['v'], 1.0
# adsr parsing
atk=xoxdcode mz[j]['a'], 1.0
dcy=xoxdcode mz[j]['d'], 1.0
sus=xoxdcode mz[j]['s'], 1.0
rel=xoxdcode mz[j]['r'], 1.0
# mod_wave parsing
mmw=mz[j]['mw']
mwi=mmw[ctr%mmw.length]
mw=vab.index(mwi)%4
# mod_ (range,phase) parsing
mr=xoxdcode mz[j]['mr'], 12.0
mp=0.005 + (xoxdcode mz[j]['mp'], 4*t)
# divisor/depth parsing
dv=0.1 + (xoxdcode mz[j]['dv'], 10.0)
dp=0.1 + (xoxdcode mz[j]['dp'], 10.0)
# cutoff/detune parsing
co=20 + (xoxdcode mz[j]['co'], 100.0)
dt=0.1 + (xoxdcode mz[j]['dt'], 5.0)
# plug the synth
use_synth mz[j]['sy']
play nt, amp: vy,
attack: atk, decay: dcy, sustain: sus, release: rel,
mod_phase:mp, mod_range:mr, mod_wave:mw,
divisor: dv, depth: dp, cutoff: co, detune: dt
end
define :ctrstep do
ctr=ctr+1
if ctr>=4096 then
ctr=0
end
end
# the main loop
live_loop :mainlooper do
if tjump then
tjump=false
else
melostep 0
melostep 1
melostep 2
melostep 3
melostep 4
melostep 5
melostep 6
melostep 7
melostep 8
melostep 9
end
ctrstep
sleep t
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment