# Transform to swing?

**URL:** <https://in-thread.sonic-pi.net/t/transform-to-swing/9340>\
**Category:** Support, Help & Resources\
**Created:** [November 29, 2024, 3:35am UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340 "2024-11-29T03:35:16Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [November 29, 2024, 3:35am UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/1 "2024-11-29T03:35:16Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [November 29, 2024, 10:29am UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/2 "2024-11-29T10:29:49Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [November 29, 2024, 4:44pm UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/3 "2024-11-29T16:44:20Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Davids-Music-Lab](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/davids-music-lab/32/512_2.png) [@Davids-Music-Lab](https://in-thread.sonic-pi.net/u/Davids-Music-Lab)\
**Post date:** [November 29, 2024, 6:49pm UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/4 "2024-11-29T18:49:01Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![HarryLeBlanc](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/harryleblanc/32/2813_2.png) [@HarryLeBlanc](https://in-thread.sonic-pi.net/u/HarryLeBlanc)\
**Post date:** [November 29, 2024, 10:28pm UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/5 "2024-11-29T22:28:41Z")

</div>

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:

```ruby

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.

---

<div class="post-metadata">

**Author:** ![emlyn](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/emlyn/32/1460_2.png) [@emlyn](https://in-thread.sonic-pi.net/u/emlyn)\
**Post date:** [November 30, 2024, 12:36am UTC](https://in-thread.sonic-pi.net/t/transform-to-swing/9340/6 "2024-11-30T00:36:37Z")

</div>

Here’s my attempt at using with\_swing:

```ruby
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
