Simple function for transposing samples on beat

Okay, I’ve been trying to wrap my brain around the sample command – in particular, how to get samples to stay in sync while transposing pitch. It took a while for me to figure out how pitch_stretch and rpitch interacted – when you rpitch up, it speeds up the sample, so you need to compensate with the pitch_stretch.
So I wrapped this into a handy function:


mysample = "D:\\Loops\\Afroplug - Soul and Jazz Guitar Loops\\looperman-l-6258600-0353860-spilled-coffee.wav"


define :transposesample do |thissample, pitch_stretch, rpitch, \
    time_dis=0.01, \
    window_size=0.1, \
    pitch_dis=0.01|
  ratio = midi_to_hz(60 + rpitch) / midi_to_hz(60)
  puts "ratio: " + ratio. to_s
  sample thissample \
    , pitch_stretch: pitch_stretch * ratio \
    , rpitch: rpitch \
    , time_dis: time_dis \
    , window_soze: window_size \
    , pitch_dis: pitch_dis
end


[90, 120, 150].each do |thisbpm|
  [0, -5, 3, 7].each do |thispitch|
    
    use_bpm thisbpm
    transposesample mysample, 16, thispitch
    sleep 16
  end
  sleep 2
end


The sample is freely available on looperman. Here’s the link:

I hope y’all find this useful. Any feedback most welcome!

1 Like