Starting a ring from the begining

Hello everyone,

I’m a total newbie to live coding, but I have some programming background so I was able to get up and running with Sonic Pi without too much difficulty. Tremendous thanks to Sam Aaron for creating such an amazing piece of software :clap:

Now, onto the question: I have a live loop with a ring in it, and I can turn it on and off by commenting it out. However, every time I hit run, the ring starts from the point where it was last stopped. I would like it to start from the beginning so I was looking through the methods for something like “reset”. Does not seem to exist… :confused:

Here is the code:

use_bpm 120
use_synth:pretty_bell

notes = (ring :C3, :D3, :E3, :F3, :G3, :A3, :B3, :C4)
live_loop :ex do
  play notes.tick
  sleep 0.5
end

live_loop :bar do
  sample :bd_pure, amp: 2.5
  sleep 1
end

For completeness, I am commenting out the “play notes.tick” line. There must be an easy solution to this - in a performance you would want your melody to start from the beginning when you “unmute” it (not that the C major scale counts a melody but you get the idea). Sorry if this is a common question, I searched the forum but could not find an answer.

Thanks

Hello @ugokcen, and welcome! :slightly_smiling_face:

Great that you’re enjoying Sonic Pi!

In regards to your question: the function you are after (you can find details of it in the ‘Lang’ section of the help documents) is: tick_reset :slightly_smiling_face:

Thank you for the quick reply!

But stupid me cannot work this out. tick_reset does not work as a method next to the play command and if I put it on a separate line inside the live loop then all I get is the first note repeated over and over.

Maybe this is not a matter of specifying some simple option? Should I be manually keeping track of ticks, storing them in variables, incrementing them, etc.?

Hi Ugo,

Lets look at the logic of this.

If you put it inside the live_loop, then
every time the loop loops around, tick_reset
will reset tick, and of course it will then
only play the first note.

If you put it outside the live_loop, like so,
tick_reset will only be callled once, when you
press run…

use_bpm 120
use_synth:pretty_bell
tick_reset

notes = (ring :C3, :D3, :E3, :F3, :G3, :A3, :B3, :C4)

live_loop :ex do
  play notes.tick
  sleep 0.5
end

live_loop :bar do
  sample :bd_pure, amp: 2.5
  sleep 1
end

Easy peasy. :slight_smile:

Eli…

1 Like

Hey @Eli, actually I think this is not the solution. At least it does not work for me. I think this is due to the fact that tick_reset is not global and has no effect inside the loop. Please correct me if I am wrong.

I see this solution if you don’t want to comment/uncomment two lines (tick_reset inside the loop and play ...) reciprocally (which is not very elegant but for me totally okay in a live coding situation):

use_synth :pretty_bell

notes = (ring :C3, :D3, :E3, :F3, :G3, :A3, :B3, :C4)

live_loop :ex do
  halt = false # set to true to stop the loop and reset tick once
  if halt
    stop
    tick_reset
  end
  play notes.tick
  sleep 0.5
end

live_loop :bar do
  sample :bd_pure, amp: 2.5
  sleep 1
end

I think the tick_reset after the stop is doing nothing because the stop exits the thread immediately. Things just happen to work because when the live_loop is restarted next time, the tick starts from the beginning again - you could remove the tick_reset and it behaves exactly the same.
So you could in fact just have a stop at the top of the live_loop, and comment it in/out, with the same behaviour:

live_loop :ex do
  #stop # uncomment to pause
  play notes.tick
  sleep 0.5
end

However, because stop completely stops the live_loop, the next time it starts it won’t be in sync with the other live_loop unless you sync it explicitly, something like:

use_synth :pretty_bell

notes = (ring :C3, :D3, :E3, :F3, :G3, :A3, :B3, :C4)

live_loop :bar, delay: 0.01 do
  sample :bd_pure, amp: 2.5
  sleep 1
end

live_loop :ex, sync: :bar do
  #stop # uncomment to pause
  play notes.tick
  sleep 0.5
end

(I added a small delay on the :bar live_loop so that the :ex loop has time to start and sync to it the first time, otherwise :ex would start one loop later).

1 Like

I think you’re probably right Martin… that will teach me for
thinking I know it all… :slight_smile:

However, I replaced the tick_reset with tick_reset_all, in my
example, and on the next run, the ring had reset to the first
note, the :C3 … so perhaps thats the answer.

If not I’m happy to let the big fellah’s such as Robin or Sam
explain.

Thats the great thing about this place… someone, somewhere
amongst us usually can find an answer that works.

Eli…

1 Like

I stand corrected also :joy:
@eli, @martin, @emlyn, thanks guys for the ideas! I forgot about stop :wink:
Emlyn’s is probably the most succinct here so far :man_shrugging:

2 Likes

Haha, I am always amazed of how many thinks can go wrong with my/some code and how I am able to interpret some sense into something which actually does not make sense at all :wink:

Thanks @emlyn for the clarification.

1 Like

Thanks to everyone who took the time to answer, I learned quite a bit!

Indeed, “stop” does the job, which is interesting because in all the live coding videos I’ve watched I’ve never seen it being used. It is either the play or the sample command that gets commented in/out to introduce new elements. I wonder why that is…

This brings me to “sync”. When I first started testing multiple live loops I noticed that they were automatically synced, a bit like Ableton where stop and play do not immediately come into effect but wait until the next bar. Then I read about the sync command in the tutorial and it was not clear to me why that was needed until this example with the “stop” came up. Are there any other instances where sync is useful?

Hi @ugokcen,

as far as I have seen Sam uses comments often (which will let the live_loop running and thus not neccessarily relies on syncing). I myself mostly use ‘stop’. This is also one of the use cases where synchronisation will be needed. See some examples here.

Thanks @Martin, those links are really useful. I’ve got a lot to explore :grinning:.

1 Like