# How to improve this code, so Sonic Pi dont crash?

**URL:** https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219
**Category:** Support, Help & Resources
**Created:** [December 13, 2021, 9:11pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219 "2021-12-13T21:11:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Relaxnow](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/relaxnow/32/2172_2.png) [@Relaxnow](https://in-thread.sonic-pi.net/u/Relaxnow)
#### Post date: [December 13, 2021, 9:11pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/1 "2021-12-13T21:11:05Z")

</div>

How do I improve this code, so Sonic Pi dont crash during tabla samples? v3.3.1  
[YoutTube of Crash](https://youtu.be/nEMVqTLApM4?t=237)

```ruby
#drone 211213 15:33

with_fx :gverb do
  use_synth :fm
  # play :a5, release: 16, attack: 4, amp: 0.2
  
  # play :ds4, release: 16, attack: 4, amp: 0.2
  #play :c2+01, release: 16, attack: 4, amp: 0.2
  # play :c1, release: 32, attack: 4, amp: 0.2
end

live_loop :ambiance do
  use_synth :dark_ambience
  # play :c3, release: 24, attack: 4, cutoff: 80
  sleep 16
end

live_loop :noise do
  use_synth :gnoise
  # play :c5, release: 24, attack: 4, cutoff: 40, amp: 2
  sleep 16
end

#sample

live_loop :fast do
  with_fx :reverb do
    # sample :tabla_dhec, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,5,1,1).choose+2, pan: ring(-1,1).choose, pitch: ring(0,24,12).choose
    # sample :tabla_ghe6, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,1,1).choose+0, pan: ring(-1,1).choose, pitch: ring(0,-12,12).choose
    # sample :tabla_tas2, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,1,1).choose+3, pan: ring(-1,1).choose, pitch: ring(0,-12,12).choose
    # sample :tabla_tun2, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,5,1).choose+4, pan: ring(-1,1).choose, pitch: ring(0,-24,-12).choose
    sleep ring(0.125).choose
  end
end

live_loop :drums do
  with_fx :reverb do
    with_fx :bitcrusher do
      sleep ring(0.5,0.25,0.75).choose
      # sample :bd_haus, cutoff: ring(80,90,110,70).choose, amp: 2
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)
#### Post date: [December 14, 2021, 3:01am UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/2 "2021-12-14T03:01:24Z")

</div>

Hello @Relaxnow,

I’ve taken the liberty of formatting your code nicely. (FYI, you can do this by wrapping code with a line before and after that both have three backticks ``` on them).

Regarding the crashing, details of the issue and several solutions can be found in the in-app tutorial in chapter 6.2, ‘FX in practice’ (also online at [Sonic Pi - Tutorial](https://sonic-pi.net/tutorial.html#section-6-2)). Have a read and try one of those - let us know if you have any further questions 🙂

---

<div class="post-metadata">

### Author: ![Relaxnow](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/relaxnow/32/2172_2.png) [@Relaxnow](https://in-thread.sonic-pi.net/u/Relaxnow)
#### Post date: [December 14, 2021, 2:26pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/3 "2021-12-14T14:26:47Z")

</div>

> [@ethancrawford](#):
>
> ```ruby
> 
> ```

Ty Ethan  
With\_fx outside live\_loop’s seems to have fixed the issue. I can run all code without crashing.  
Kind regards  
Relaxnow

```ruby
#drone 211214 15:23 (learned to place with_fx outside live_loop, so Sonic Pi dont crash)

use_sched_ahead_time 1

with_fx :gverb do
  use_synth :saw
  play :a5, release: 16, attack: 4, amp: 0.2
  play :ds4, release: 16, attack: 4, amp: 0.2
  play :c2, release: 16, attack: 4, amp: 0.2, cutoff: 70
  play :c1, release: 32, attack: 4, amp: 0.2
end

live_loop :ambiance do
  use_synth :dark_ambience
  play :c3, release: 24, attack: 4, cutoff: 80
  sleep 16
end

live_loop :noise do
  use_synth :gnoise
  play :c5, release: 24, attack: 4, cutoff: 40, amp: 2
  sleep 16
end

#samples
with_fx :reverb do
  live_loop :fast do
    sample :tabla_dhec, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,5,1,1).choose+2, pan: ring(-1,1).choose, pitch: ring(0,24,12).choose
    sample :tabla_ghe6, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,1,1).choose+0, pan: ring(-1,1).choose, pitch: ring(0,-12,12).choose
    sample :tabla_tas2, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,1,1).choose+3, pan: ring(-1,1).choose, pitch: ring(0,-12,12).choose
    sample :tabla_tun2, cutoff: ring(40,50,60,90,110).choose, amp: ring(3,1,5,1).choose+4, pan: ring(-1,1).choose, pitch: ring(0,-24,-12).choose
    sleep ring(0.125).choose
  end
end

with_fx :reverb do
  with_fx :bitcrusher do
    live_loop :drums do
      sleep ring(0.5,0.25,0.75).choose
      sample :bd_haus, cutoff: ring(80,90,110,70).choose, amp: 4
    end
  end
end

```

---

<div class="post-metadata">

### Author: ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)
#### Post date: [December 14, 2021, 2:37pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/4 "2021-12-14T14:37:06Z")

</div>

You’re welcome - sounds good too btw 🙂

---

<div class="post-metadata">

### Author: ![Relaxnow](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/relaxnow/32/2172_2.png) [@Relaxnow](https://in-thread.sonic-pi.net/u/Relaxnow)
#### Post date: [December 14, 2021, 2:51pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/5 "2021-12-14T14:51:54Z")

</div>

Just wondering: Do “with\_fx” run when all the samples are uncommented with # inside?

I mean. Do I need to use / / around on a code section that are not active to be on the safe side?

---

<div class="post-metadata">

### Author: ![ethancrawford](https://avatars.discourse-cdn.com/v4/letter/e/a88e4f/32.png) [@ethancrawford](https://in-thread.sonic-pi.net/u/ethancrawford)
#### Post date: [December 16, 2021, 12:36pm UTC](https://in-thread.sonic-pi.net/t/how-to-improve-this-code-so-sonic-pi-dont-crash/6219/6 "2021-12-16T12:36:04Z")

</div>

As I understand it, yes, fx will run regardless of whether any sounds inside them are commented out or not.  
As to whether this can still become a performance problem, perhaps, but I’d appreciate @samaaron’s confirmation on that 🙂 Just like when you have active sounds within the fx block, I’m sure it depends on the amount of time in each cycle of the enclosing live\_loop. If there are enough sleeps of the right duration in the live\_loop to allow a single fx instance to complete before the next one is triggered for example, then there shouldn’t really be any need to comment anything out I’d say.
