Question about sync

I am stuck on the tutorial regarding sync. For the codes below, I was expacting that the note e4 and the snare would sound the same time on every beat but instead, the snare hit comes in one beat later and only on the even beats afterwards. I can’t get my head wrapped around it. I thought the whole purpose of sync is to make sure the snare loop (contains sync command) starts the same time as the testNote loop. I am clearly missing something. Could some one enlighten me? Thank you,

live_loop :testNote do
  play :e4, release: 0.5
  sleep 1
end

live_loop :snare do
  sync :testNote
  sample  :drum_snare_hard
  sleep 1
end

Wondered the same.
This code plays both at same time

use_bpm 120

live_loop :met do
  sleep 1
end

live_loop :testNote, sync: :met do
  play :e4, release: 0.5
  sleep 1
end

live_loop :snare, sync: :met  do
  #sync :testNote
  sample  :drum_snare_hard
  sleep 1
end

Thanks. your code is exactly what I was expecting would happen to my code. I still don’t understand why my code doesn’t behave this way…
I guess I just need to read on the tutorial maybe it’ll become clear later… :slightly_smiling_face:

1 Like

Hello @quekou :slightly_smiling_face:

The following might be more helpful: (particularly the other threads I point to there)

Happy to elaborate more if needed after you’ve had a read :+1:

Thanks Ethan. Just when I thought I understood what’s happening with cue and sync, the code below confused me.

use_bpm 100

8.times do
  cue :a
  notes = scale(:a3, :aeolian).shuffle
  
  synth :dpulse, note: notes.tick
  
  sleep 0.5
end

live_loop:met do
  sync :a
  sample :bd_klub
  sleep 1
end

when I hit alt+r the first time, it won’t play the live_loop met. It just plays the synth for 8 beats. But when I hit alt+r again, both blocks will play simultaneously. If now I hit alt+s to stop it and then play again, then just the synth sound again… would you mind explaining to me what’s happening…thanks.

Here, you have some code outside a live_loop, and some other code inside a live_loop.

Code in a Sonic Pi (Ruby) file is interpreted from top to bottom - in this case, this means the code not in the live_loop is interpreted first (this being the 8.times loop) and 8 times, a cue is sent, a note is played, and the script waits for a little time.

Once that has all finished, the live_loop is interpreted and immediately created by Sonic Pi. By this time, all the cues are in the past.

This means that the sync continues waiting for a cue that will never come.

Sam explains this fairly well I think in the issue I raised a few years back:

The reason it works after you re-run is because by that time, Sonic Pi has already interpreted and created the live_loop, (which it did in the first run) so it is already listening by the time the 8.times loop executes, and is able to respond.

Hope that helps! there are a few diagrams in some of the comments on the topics I link to above that might help to show it visually as well :slight_smile: (Live loops Sync questions :-) - #18 by Martin for example)

1 Like