Finding correct bpm of drum loops


I have been playing around with ways to use break beats and drum loops without having to use beat_stretch or variables for sleep values in order to have other sounds or sample line up correctly. I came up with a function that will take any loop and return the correct bpm. You just need to provide the number of beats in the loop.

Here is an example of it in use with the Amen break:

define :bpm do |samp, subDiv| # samp = loop you want to use, subDiv = number of beats in that loop
  t = sample_duration samp #variable to store duration of loop
  bps = t/subDiv # beats per second found by dividing loop duration by the number of beats in the loop
  beatsPerMin = 60/bps #converts beats per second to beats per minute
  return beatsPerMin # returns bpm of loop

use_bpm bpm :loop_amen, 4 #setting bpm to the correct bpm of the loop by using the function

live_loop :amen do
  sample :loop_amen #play sample
  sleep 4 #sleep for the same number of beats in the loop of the sample, most likely 2, 4 or 8

live_loop :bass, sync: :amen do
  play (octs, :d1, 3).choose, release: 0.5
  sleep 0.5 #Any accompanying synths or samples can use evenly divided sleep values and should align perfectly with loop