Transform to swing?

Does anyone have a method that converts a beat from straight to swing, and vice versa?
Say, feed it an array of eighths, for hats:
[0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5]
and it transforms into this:
[0.6666, 0.3333, 0.6666, 0.333, 0.6666, 0.3333, 0.6666, 0.333]
I could roll my own, but I don’t want to reinvent the wheel if I don’t have to.
Thanks!

Have you seen/tried the with_swing function? I don’t think it will transform a list of timings, but instead it modifies the timing of code as it plays.

Yeah, I dinked around with it yesterday, but I can’t get it to do what I want. The numbers are obscure to me, and the rests seem fixed, so I can’t grok how to wield it to just get a swing beat.
Can anyone offer some clarity?

live_loop :with_swing do
  tick
  time_warp (ring 0,0.125).look do
    #your code
  end
  sleep 0.5
end

Thanks for the example code.
Why 0.125? What’s the logic behind the numbers?
I wanted something a little more robust, that would handle uneven time intervals and swing them logically. Here’s what I came up with:


define  :swing do |straightbeats|
  swingbeats = []
  leftover = 0 
  straightbeats.each do |straightbeat|
    straightbeat -= leftover  
    swingbeat = (straightbeat * 6.0).round(0, half: :up) / 6.0  
    swingbeats << swingbeat
    leftover = swingbeat - straightbeat  
  end #each straightbeat
  swingbeats #return value
end #define swing

puts swing [0.5, 0.25, 0.25, 0.5, 0.25, 0.25]

#returns [0.5, 0.3333333333333333, 0.16666666666666666, 0.5, 0.3333333333333333, 0.16666666666666666]

Seems to work well for me, but I’d love feedback.

Here’s my attempt at using with_swing:

live_loop :test do
  with_swing pulse: 2, offset: 1, shift: 1.0/6 do
    sample :drum_cymbal_closed
  end
  sleep 0.5
end

The times of the sample are: 0, 0.666, 1, 1.666, 2, 2.666, …

The pulse: 2 is to make it repeat every 2 beats
The offset: 1 is to offset the delay by 1 beat (otherwise it delays the first beat instead of the second)
And the shift: 1.0/6 is to delay every second beat by 1/6 so 0.5 gets shifted to 0.6666