# Sample cutoff / choke groups

**URL:** <https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383>\
**Category:** Support, Help & Resources\
**Created:** [May 12, 2019, 1:58am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383 "2019-05-12T01:58:24Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![sdsd19](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sdsd19](https://in-thread.sonic-pi.net/u/sdsd19)\
**Post date:** [May 12, 2019, 1:58am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/1 "2019-05-12T01:58:24Z")

</div>

Hello everyone! I’m working on a sample slicing program that allows me to use my midi keyboard to add slices to a sample at points I choose using my keyboard’s control dials, and play back the slices using my keyboard’s pads. Everything works so far, but I need some kind of way to have a sample that starts playing cut off previously playing samples. Is there something similar to choke groups in Sonic Pi? If not, is there a way to stop all currently playing sounds/samples?

---

<div class="post-metadata">

**Author:** ![samaaron](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/samaaron/32/7_2.png) [@samaaron](https://in-thread.sonic-pi.net/u/samaaron)\
**Post date:** [May 12, 2019, 7:09am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/2 "2019-05-12T07:09:09Z")

</div>

Could you explain a little more about choke groups? What kind of code would you like to be able to write and what should it do?

---

<div class="post-metadata">

**Author:** ![PiEaterAndPlayer](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@PiEaterAndPlayer](https://in-thread.sonic-pi.net/u/PiEaterAndPlayer)\
**Post date:** [May 12, 2019, 8:26am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/3 "2019-05-12T08:26:53Z")

</div>

For choking hi-hats, I use this method:

```ruby
use_bpm 130

define :sample_choke_group do |group,samp,args={}|
  player = get group
  
  if player != nil
    control player, amp_slide: 0.01, amp: 0
  end
  
  use_sample_defaults args
  player = sample samp
  set group, player
end

live_loop :drums do
  
  at [0, 1.5, 2] do
    sample :bd_haus
  end
  
  at [1, 3] do
    sample :sn_dolf, pan: 0.5
  end
  
  7.times do
    sample_choke_group( :hat_group,
                        :drum_cymbal_closed,
                        pan: -0.5 )
    sleep 0.5
  end
  
  sample_choke_group( :hat_group,
                      :drum_cymbal_hard,
                      pan: -0.5, amp: 0.5 )
  sleep 0.5
end

```

It doesn’t actually stop the sample playing, just sets its volume to zero. This isn’t a problem for me running code on a Mac where the sample will finish quite soon, but may be a problem for longer samples if a large number of silent sample players end up working at once on a less powerful computer such as a Raspberry Pi.

EDIT: I tried the following. It still works, and might be stopping the choked synth player. Not sure if it’s saving computer power:

```ruby
use_bpm 130

define :sample_choke_group do |group,samp,args={}|
  player = get group
  print player
  
  if player != nil
    control player, amp_slide: 0.01, amp: 0
    
    at 0.01 do
      control player, on: false
    end 
  end
  
  
  
  use_sample_defaults args
  player = sample samp
  set group, player
end

live_loop :drums do
  
  at [0, 1.5, 2] do
    sample :bd_haus
  end
  
  at [1, 3] do
    sample :sn_dolf, pan: 0.5
  end
  
  7.times do
    sample_choke_group( :hat_group,
                        :drum_cymbal_closed,
                        pan: -0.5 )
    sleep 0.5
  end
  
  sample_choke_group( :hat_group,
                      :drum_cymbal_hard,
                      pan: -0.5, amp: 0.5 )
  sleep 0.5
end

```

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [May 12, 2019, 2:16pm UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/4 "2019-05-12T14:16:43Z")

</div>

can you use kill to do this?  
eg

```ruby
k=sample :ambi_glass_hum
sleep 1
kill k #cuts it short after 1 beat

```

---

<div class="post-metadata">

**Author:** ![PiEaterAndPlayer](https://avatars.discourse-cdn.com/v4/letter/p/ce7236/32.png) [@PiEaterAndPlayer](https://in-thread.sonic-pi.net/u/PiEaterAndPlayer)\
**Post date:** [May 12, 2019, 2:24pm UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/5 "2019-05-12T14:24:13Z")

</div>

Oh, I hadn’t used kill. I suppose I shouldn’t answer questions just yet 🙂

New version:

```ruby
use_bpm 127

define :sample_choke_group do |group,samp,args={}|
  player = get group
  
  if player != nil  
    kill player
  end
  
  use_sample_defaults args
  player = sample samp
  set group, player
  return player
end

vars = (ring false, false, false, true)

live_loop :drums do
  tick
  
  at [0, 1.5, 2] do
    sample :bd_haus
  end
  
  at [1, 3] do
    sample :sn_dolf, pan: 0.5
  end
  
  if vars.look
    at 3.5 do
      
      sample :sn_dolf, pan: 0.5
    end
  end
  
  7.times do
    sample_choke_group( :hat_group,
                        :drum_cymbal_closed,
                        pan: -0.5 )
    sleep 0.5
  end
  
  sample_choke_group( :hat_group,
                      :drum_cymbal_hard,
                      pan: -0.5, amp: 0.5 )
  sleep 0.5
end

```

Simply killing the player seems to work fine. No clicks. I don’t know if it will click on other sounds.

---

<div class="post-metadata">

**Author:** ![robin.newman](https://avatars.discourse-cdn.com/v4/letter/r/76d3ee/32.png) [@robin.newman](https://in-thread.sonic-pi.net/u/robin.newman)\
**Post date:** [May 12, 2019, 2:33pm UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/6 "2019-05-12T14:33:31Z")

</div>

provided you have a reference to the sound it should work with play as well eg

```ruby
k = play :c4, sustain: 50, release: 1
sleep 1
kill k

```

However I think this will give a bit of a click, so you could try:

```ruby
k=play :c4,sustain: 50,release: 1
sleep 1
control k,amp: 0,amp_slide: 0.1
sleep 0.1
kill k

```

---

<div class="post-metadata">

**Author:** ![sdsd19](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sdsd19](https://in-thread.sonic-pi.net/u/sdsd19)\
**Post date:** [May 13, 2019, 3:19am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/7 "2019-05-13T03:19:53Z")

</div>

Choke groups are groups of samples that cut each other off if one is played while another is already playing. I would like to write code that allows the sample slices in my program to cut each other off in this way, so that I don’t get multiple slices playing on top of each other if I want to switch to another slice before the one I’m currently playing finishes.

---

<div class="post-metadata">

**Author:** ![sdsd19](https://avatars.discourse-cdn.com/v4/letter/s/3da27b/32.png) [@sdsd19](https://in-thread.sonic-pi.net/u/sdsd19)\
**Post date:** [May 13, 2019, 3:21am UTC](https://in-thread.sonic-pi.net/t/sample-cutoff-choke-groups/2383/8 "2019-05-13T03:21:54Z")

</div>

Thank you! This is exactly what I was looking for.
