Code keeps looping on top of each other every time i hit run


#1

So I hit ‘run’. it plays a certain loop i.e:

loop do
play 70
sleep 1
end

but if i create a variation such as

loop do
play 60, release: 20
sleep 1
end

repress ‘run’, it adds the loop onto the old loop and now it has created a rhythm. how do i fix this? i just want it to play what i have asked for this one time, and not add more and more layers “building” a song.

I have also attempted to hit ‘stop’ but issue persists.


#2

I’m not sure it’s the right answer, but each time you just create a loop, you don’t give it a name, so Sonic Pi is creating a new instance of the loop each time you run the code. To avoid this, you must name your loop using the following syntax :

live_loop :myloop do
  play 55, release: 1
  sleep 1
end

You could even synchronize several live_loops together using different methods. Here is a simple one :

live_loop :myloop do
  play 55, release: 1
  sleep 1
end

live_loop :myotherloop, sync: :myloop do
  play 50, release: 1
 sleep 1
end

Note that each loop should have his own name, so you must find a different name for each one.

These topics are covered by the in-built tutorial, so be sure to read everything in there, because you will learn even fancier ways to create looping structures.

Have fun!