Sync between live loops

Hello everybody.

I’m searching a way to play a song with a particular structure, that is a song divided in different sections.
My idea was to create a live loop that decides which other live loops should play.

In this example there are:

  • the “main” live loop
  • one loop that plays: e, ,e, ,e, ,…
  • one loop that plays: ,a, ,a, , a,…
enable_first_loop = false
enable_second_loop = false
use_bpm 120
live_loop :main_structure do
  1.times do
    enable_first_loop = true
    enable_second_loop = false
    sleep 1.0
  end
  1.times do
    enable_first_loop = false
    enable_second_loop = true
    sleep 1.0
  end
end



live_loop :first do
  riff = (ring :e3, :r)
  if enable_first_loop
    play riff.tick
  end
  sleep 0.5
end


live_loop :second do
  riff = (ring :r,:a3)
  if enable_second_loop
    play riff.tick
  end
  sleep 0.5
end

The problem is that the live loops are not in sync.
The expected song should be:
e, a, e, a, e, a …

But sometimes it happens someting like:
e+a, , e+a, e, a, …

Do you have any idea?

Many thanks,
Alberto

Hello @Alb85,
For syncing threads/loops together, the function you are after is sync.
See the following sections in the tutorial for details:
Sonic Pi - Tutorial (examples show usage with in_thread and loop, but the same applies to live_loops).
Details also in the app’s Lang help section in the reference page for the sync function.

For more explanations and examples of how and when to use sync in various ways, this topic is also a good start:

You might also want to look into get and set for communicating data between threads in such a way that changing values in one particular place always makes the other threads see those values in the way you expect, rather than changing in unexpected ways:
See:
https://sonic-pi.net/tutorial.html#section-10-1
(and again, more details in the Lang help section in the app under the pages for set and get).

Let us know if you have further questions :slight_smile:

hi @Alb85

this post may answer your question. It’s about create a whole tune. note that you won’t use live_loop

Fell free to ask questions if needed.

1 Like