Get and translate values from rotary knobs


#1

Hi,

I am currently working on a Sonic Pi Sample Slicer applicaton using my Arturia Beatstep as Midi control. For that purpose I have some code for getting values from the rotary knobs:

  1. simple translation of values (e. g. volume i. e. 0-127 to let’s say 0-4)

  2. a rotary function which has a null range at 63 and triggers the lowpass filter if turned to the left and the highpass when turned to the (I was able to generalise 1 + 2 into functions, which are reusable and easy to use) … and

  3. some code which turns a range of midi input into one step of a scale (e. g. 0-21 returns 64 aso.):

live_loop :getset_rotaries do
  use_real_time
  m = sync "/midi/arturia_beatstep_midi_1/1/1/control_change"
  
  # 2 Beat_stretch
  if m[0] == 103
    val = m[1]
    steps = (ramp 64, 32, 16, 8, 4, 2)
    d = 127 / steps.size.to_f
    
    if val < d
      v = steps[0]
    elsif val < (d * 2)
      v = steps[1]
    elsif val < (d * 3)
      v = steps[2]
    elsif val < (d * 4)
      v = steps[3]
    elsif val < (d * 5)
      v = steps[4]
    elsif val <= (d * 6)
      v = steps[5]
    end
    
    spark (range 0, v)
    set :bs_beat_stretch, v
    control get(:bread), beat_stretch: get(:bs_beat_stretch)
  end
end

This does work quite well but is not very generic and only a solution for one single knob. So I was wondering how to turn this into a reusable function taking as arguments the midi value and an array of steps. I tried turning the rather clumsy if condition part into a loop but I it does not work this way. My problem is, that I would have to contruct the if-part dynamically according to the number of steps …

Any ideas?