Hi Martin
I have had another look at things and I think the code below does what you want. The fadeControl function can be used to control other opts, but here is set to control the volume (:amp) of the sample in loop :test
The fade is controlled by the function setVol which needs two parameters: the final amp level (normal range 0->1 (although you can go higher) It always starts from the current volume level (held in :vol) so the fade is smooth without any jumps. It runs independently of the live_loop :test duration. (8 beats) and can for example last for 16 beats, or 2 beats. The initial volume the first time the program runs is set to 0 in the defonce :setup code, although this could be set to 1 if you wish. Note for such a change to take place, you would have to quit and restart SP or use the override option for defonce.
To test, just run the program. (The initial fade setup 0->0.5 will happen. When it has finished, just set a new fade eg setVol 1,6 and press run again, and so on.
#function returns step values for the fade
define :fade do |start, finish, len|
b = (line start, finish, steps: len, inclusive: true).stretch(2).drop(1).butlast.ramp
return b #cam be omitted but makes explicit what happens
end
define :fadeControl do |start,finish,duration,pointer,opt|
return if start==finish
l=fade start,finish,11 #11 ensures 20 steps for each up/down
dt=duration/20.0
puts "fadeControl #{start} #{finish} #{duration} #{pointer} #{opt}"
in_thread do
t=vt
tick_reset
l.length.times do
#note that amp: is equivalent to :amp=> This enables use of :amp stored in a variable
# similarly for pan: or phase: or any other similar
# note also how the corresponding _slide is created.
control get(pointer),opt=> l.tick,(opt.to_s+"_slide").to_sym => dt
sleep dt
set :vol,l.look #update current volume at end of each step
end
end
end
#set inital volume value on first run
defonce :setup do
set :vol,0
end
live_loop :test do
lv=sample :loop_amen_full,beat_stretch: 8,amp: get(:vol) #sample volume will be controlled
set :lv,lv
sleep 8
end
#defines setvol so that only final vol and duration need to be specified for each fade
define :setVol do |value,duration|
fadeControl get(:vol),value,duration,:lv,:amp
end
setVol 0.5,4 #This sets and carries out the fade
EDIT I also have a version that will control volume and pan independently at the same time if you want that.