Module: SonicPiAkaiApcMini::Helpers

Defined in:
lib/sonic-pi-akai-apc-mini/helpers.rb

Defined Under Namespace

Classes: RangeError

Class Method Summary collapse

Class Method Details

.key(row, col) ⇒ Object



37
38
39
# File 'lib/sonic-pi-akai-apc-mini/helpers.rb', line 37

def key(row, col)
  (row * Controller.model.grid_columns) + col + Controller.model.grid_offset
end

.key_range(row, col, size) ⇒ Object

Given a starting button (row, col) and a size, return the range of corresponding MIDI notes for those buttons. If ‘size` is bigger than the amount of remaining buttons before the end of the row, the range finishes at the end of the row. If the currently configured model has fewer rows/columns, it raises a RangeError error.

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/sonic-pi-akai-apc-mini/helpers.rb', line 27

def key_range(row, col, size)
  raise RangeError, 'out of range' if row >= Controller.model.grid_rows || col >= Controller.model.grid_columns

  first = key(row, col)
  last = first + size - 1
  end_of_row = key(row, Controller.model.grid_columns - 1)

  (first..[last, end_of_row].min)
end

.normalize(value, target) ⇒ Object

Normalizes a MIDI velocity value (between 0 and 127) to the corresponding value in ‘target`, which can be a range, a ring/array, or the special value :pan (shortcut for (-1..1)).



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/sonic-pi-akai-apc-mini/helpers.rb', line 8

def normalize(value, target)
  value /= 127.0
  target = (-1..1) if target == :pan
  case target
  when Range
    target.begin + (value * (target.end - target.begin))
  when Array, SonicPi::Core::RingVector
    index = ((target.size - 1) * value).round
    target[index]
  end
end