Little fading library

Lately i’v been having this problem that everytime I start live coding I come up with an idea for doing something differently and end up writing supportive code rather than actually composing music … :sweat_smile:

Anyway, here’s a piece of future Ziffers code I hope is helpful for others as well:

$easing = { # Derived from: https://github.com/danro/jquery-easing/blob/master/jquery.easing.js
            linear: -> (t, b, c, d) { c * t / d + b },
            in_quad: -> (t, b, c, d) { c * (t/=d)*t + b },
            out_quad: -> (t, b, c, d) { -c * (t/=d)*(t-2) + b },
            quad: -> (t, b, c, d) { ((t/=d/2) < 1) ? c/2*t*t + b : -c/2 * ((t-=1)*(t-2) - 1) + b },
            in_cubic: -> (t, b, c, d) { c * (t/=d)*t*t + b },
            out_cubic: -> (t, b, c, d) { c * ((t=t/d-1)*t*t + 1) + b },
            cubic: -> (t, b, c, d) { ((t/=d/2) < 1) ? c/2*t*t*t + b : c/2*((t-=2)*t*t + 2) + b },
            in_quart: -> (t, b, c, d) { c * (t/=d)*t*t*t + b },
            out_quart: -> (t, b, c, d) { -c * ((t=t/d-1)*t*t*t - 1) + b },
            quart: -> (t, b, c, d) { ((t/=d/2) < 1) ? c/2*t*t*t*t + b : -c/2 * ((t-=2)*t*t*t - 2) + b },
            in_quint: -> (t, b, c, d) { c * (t/=d)*t*t*t*t + b},
            out_quint: -> (t, b, c, d) { c * ((t=t/d-1)*t*t*t*t + 1) + b },
            quint: -> (t, b, c, d) { ((t/=d/2) < 1) ? c/2*t*t*t*t*t + b : c/2*((t-=2)*t*t*t*t + 2) + b },
            in_sine: -> (t, b, c, d) { -c * Math.cos(t/d * (Math::PI/2)) + c + b },
            out_sine: -> (t, b, c, d) { c * Math.sin(t/d * (Math::PI/2)) + b},
            sine: -> (t, b, c, d) { -c/2 * (Math.cos(Math::PI*t/d) - 1) + b },
            in_expo: -> (t, b, c, d) { (t==0) ? b : c * (2 ** (10 * (t/d - 1))) + b},
            out_expo: -> (t, b, c, d) { (t==d) ? b+c : c * (-2**(-10 * t/d) + 1) + b },
            expo: -> (t, b, c, d) { t == 0 ? b : (t == d ? b + c : (((t /= d/2) < 1) ? (c/2) * 2**(10 * (t-1)) + b : ((c/2) * (-2**(-10 * t-=1) + 2) + b))) },
            in_circ: -> (t, b, c, d) { -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b },
            out_circ: -> (t, b, c, d) { c * Math.sqrt(1 - (t=t/d-1)*t) + b },
            circ: -> (t, b, c, d) { ((t/=d/2) < 1) ? -c/2 * (Math.sqrt(1 - t*t) - 1) + b : c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b }
            }

# Creates array for fading (or easing)
# Example: zfade :circ, 0, 1, 10
# For visual aid see: https://easings.net/
def zfade(func,start,finish,duration)
  (0..duration).map { |t| $easing[func].call(t.to_f,start.to_f,(finish-start).to_f, duration.to_f) }
end

k = $easing.keys

# List example from all available easing functions 
k.each do |k|
  print k.to_s+": "+(zfade k, -1, 1, 20).to_s
end
1 Like

Here’s also short example how to actually use it:

fadeInBeat = zfade :cubic,0,1,30

live_loop :beat do
  sample :bd_808, amp: fadeInBeat.tick if (spread(1,4)*7+spread(4,4)).tick(:beat)
  sleep 0.125
end

fadeInChords = zfade :quart,0.1,1,10
panSine = (zfade :sine, -1,1,4).ring.mirror
vibratoCirc = zfade :circ, 0.1,2,4

live_loop :chords do
  use_synth_defaults amp: fadeInChords.tick, vibrato_rate: 20, vibrato_depth: 0.5, vibrato_delay: vibratoCirc.look
  use_synth :blade
  play (chord :a, :minor), pan: panSine.tick(:pan)
  sleep 1
  play (chord :e, :minor), pan: panSine.tick(:pan)
  sleep 1
  play (chord :d, :minor), pan: panSine.tick(:pan)
  sleep 1
  play (chord :c, :major), pan: panSine.tick(:pan)
  sleep 1
  play (chord :eb, :major), pan: panSine.tick(:pan)
  sleep 1
  play (chord :eb, :major), pan: panSine.tick(:pan)
  sleep 1
  play (chord :e, :major), pan: panSine.tick(:pan)
  sleep 1
  play (chord :e, :major), pan: panSine.tick(:pan)
  sleep 1
end

I dont know if you can actually tell the difference between this and simple range but it was fun idea to try out :slight_smile:

1 Like

Or you could create funny sirens and stuff:

expo = (zfade :expo,0,10,30).ring.mirror
sine = (zfade :cubic,0,30,100).ring.mirror
quint = (zfade :quint,0,20,40).ring.mirror

live_loop :beat do
  play 60+expo.tick
  play 30+sine.look
  play 70+quint.look, amp: 0.1
  sleep 0.05
end
1 Like