Playing External Samples in time with each other

Hi all,

Previously whilst tinkering with Sonic Pi I managed to get external samples all synched up with each other.

So, I had two drum loop samples of differing BPMs playing at the same speed.

Anyone know how I did it, or how I could do it again?

Do you want the samples to start at the same time, and then restart together when the longer one finishes, or do you want them to be altered to take the same time to complete?

In the former case, play each one in a separate live_loop and sync the shorter one to the start of the longer loop. In the second case use beat_stretch option to alter the rate so that it occupies a known number of beats. eg
method 1 shorter loop synced to start of longer loop

  live_loop :short do
    sync :long #waits for loop_breakbeat to restart
    sample :loop_amen
    t=sample_duration :loop_amen
    puts "duration loop_amen is #{t}"
    #no need for sleep because of sync
  end
  
  live_loop :long,delay: 0.001 do #:delay makes sure short loop is primed before :long starts first time
    sample :loop_breakbeat,amp: 2
    t=sample_duration :loop_breakbeat
    puts "duration loop_breakbeat is #{t}"
    sleep t
  end

method 2 both loops adjusted to last 2 beats using beat_stretch option

live_loop :a do
  sample :loop_amen ,beat_stretch: 2
  puts "loop_amen"
  sleep 2
end
live_loop :b do
  puts "loop_breakbeat"
  sample :loop_breakbeat,beat_stretch: 2
  sleep 2
end

Hi @chrisleach78,

thanks for the question - I hope @robin.newman was able to put you on the right track. I’d like to add to his answer and suggest you never use sample_duration to choose the sleeping time t unless you’re absolutely sure it’s what you want. Typically you’ll always be better either stretching the sample with the beat_stretch: opt or to stretch time with use_sample_bpm. This will allow other temporal elements which work with the current BPM to sound rhythmically harmonious. If you just change the sleep time and not the BPM, then there will be a timing mismatch between things which are set to work at the current BPM (e.g. the :slicer FX) and the rate of the live_loop (set by the sleep time).

Also, I’ve moved your question to the more appropriate section “Support, Help & Resources” from the section “Educators” which is mainly for educators to share ideas, resources and experiences with one another.

Please do consider supporting further development over on Patreon (just to the tune of a couple of cups of coffee a year) to allow Sonic Pi to continue to be developed and released for free and to gain full access to these forums: https://patreon.com/samaaron