Trance gate for synths, but how to do it for samples?

I wasn’t able to find a trancegate effect in sonic pi, so I decided to roll my own.
This works with synth notes, but I’d like to come up with a similar tool for samples. Is there any way to control the volume of a sample like I did here with the synth? There wasn’t a corresponding control_sample handle that I could find. How would one go about this?
Thanks!

##| tranceplay
##| A function to play a synth note with a trance gate, driven by a ring of amp lveels.
##| assumes synth defaults are all already set.Overrides sustain to cover whole pattern.

define :tranceplay do |thisnote_or_chord, howlong, howmany, thispattern|
  
  gutter = 0.01 #dead space between chunks of the trance gate
  stepsize = (howlong * 1.0/ howmany) - gutter
  puts howlong
  puts howlong / howmany
  puts howmany
  puts stepsize
  puts gutter
  
  
  mysynth = play thisnote_or_chord, sustain: howlong
  howmany.times do |thistime|
    howloud = thispattern[thistime]
    control mysynth, amp: howloud
    sleep stepsize
    control mysynth, amp: 0
    sleep gutter
  end
end


use_synth :blade

thischord = (chord :c, :sus2)
tranceplay thischord, 8, 64, [0.75, 1, 0.25, 0.75, 1, 0.25, 1, 0].ring



s = sample "my/sample"
control s

should work

Also the slicer effect might do something similar

Thanks, David, I’ll give that a try.
In the meanwhile, I figured out a way to do it using the start: and finish: options. I’ve also updated the tranceplay method to support different note lengths, implementing gutter as a ring of ratios (between 0 and 1) representing how much silence to put between each slice. trancesample works the same way.
Hope people find these useful.
Cheers,
H

##| tranceplay
##| A function to play a synth note with a trance gate, driven by a ring of amp lveels.
##| assumes synth defaults are all already set.Overrides sustain to cover whole pattern.

define :tranceplay do |thisnote_or_chord, howlong, howmany, thispattern, gutter = [0.125].ring|
  
  stepsize = (howlong * 1.0/ howmany)
  ##| puts howlong
  ##| puts howlong / howmany
  ##| puts howmany
  ##| puts stepsize
  ##| puts gutter
  
  
  mysynth = play thisnote_or_chord, sustain: howlong
  howmany.times do |thistime|
    howloud = thispattern[thistime]
    control mysynth, amp: howloud
    sleep stepsize * (1 - gutter[thistime])
    control mysynth, amp: 0
    sleep stepsize * gutter[thistime]
  end
end

define :trancesample do |thissample, howlong, howmany, thispattern, stretchmode=0, \
    gutter=[0.0625].ring, \
    rpitch=0, \
    time_dis=0.01, \
    window_size=0.1, \
    pitch_dis=0.01|
  ##| stretchmode values:
  ##| 0: nostretch -- assumes sample lines up w/current bpm
  ##| 1: beat_stretch -- uses the beat_stretch arg
  ##| 2 pitch_stretch -- uses the pitch_stretch arg
  
  stepsize = (howlong * 1.0/ howmany)
  ##| puts "rpitch: " + rpitch.to_s
  ##| puts "howlong: " + howlong.to_s
  ##| puts "howmany: " + howmany.to_s
  ##| puts "stepsize: " + stepsize.to_s
  ##| puts "gutter: " + gutter.to_s
  ##| puts "elapsed_offset: 0"
  chunk = 1.0 / howmany
  
  
  
  
  elapsed_offset = 0
  howmany.times do |thistime|
    ##| puts "thistime: " + thistime.to_s
    thisgutter = gutter[thistime]
    ##| puts "thisgutter: " + thisgutter.to_s
    pchunk = chunk * (1.0 - thisgutter)
    ##| puts "pchunk " + pchunk.to_s
    ##| puts "pchunk ratio " + (pchunk * howmany).to_s
    thisfinish = elapsed_offset + pchunk
    ##| puts "thisfinish: " + thisfinish.to_s
    case stretchmode
    when 0    #no stretch
      sample thissample, amp: thispattern[thistime], start: elapsed_offset, finish: thisfinish
    when 1
      sample thissample, amp: thispattern[thistime], start: elapsed_offset, finish: thisfinish, beat_stretch: howlong
    when 2
      sample thissample, amp: thispattern[thistime], start: elapsed_offset, finish: thisfinish, \
        pitch_stretch: howlong, rpitch: rpitch, time_dis: time_dis, window_size: window_size, pitch_dis: pitch_dis
    end
    sleep stepsize
    elapsed_offset = elapsed_offset + chunk
    ##| puts "elapsed_offset: " + elapsed_offset.to_s
  end #loop
end #def


use_synth :blade

thischord = (chord :c, :sus2)
tranceplay thischord, 8, 32, [0.75, 1, 0.25, 0.75, 1, 0.25, 1, 0.1].ring, [0.125, 0.75, 0.75, 0.125, 0.75, 0.75, 0.125, 0.5].ring

trancesample :loop_amen_full, 8, 32, [1, 1, 0, 1, 1, 0, 1, 0].ring, 1
trancesample :ambi_drone, 8, 32, [1, 0.6].ring, 2,[0.03125, 0.0625, 0.125, 0.25, 0.5, 0.25, 0.125, 0.0625].ring
trancesample :ambi_drone, 4, 16, [1, 0.6].ring, 2,[0.03125, 0.0625, 0.125, 0.25, 0.5, 0.25, 0.125, 0.0625].ring, 3
trancesample :ambi_drone, 4, 16, [1, 0.6].ring, 2,[0.03125, 0.0625, 0.125, 0.25, 0.5, 0.25, 0.125, 0.0625].ring, -2
trancesample :ambi_drone, 8, 32, [1, 0.6].ring, 2,[0.03125, 0.0625, 0.125, 0.25, 0.5, 0.25, 0.125, 0.0625].ring