Class: MTK::Events::Parameter

Inherits:
Event
  • Object
show all
Defined in:
lib/mtk/events/parameter.rb

Overview

A non-note event such as pitch bend, pressure (aftertouch), or control change (CC)

Instance Attribute Summary

Attributes inherited from Event

#channel, #duration, #number, #type, #value

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Event

#==, #duration_in_pulses, from_h, #initialize, #instantaneous?, #length, #midi_value=, #rest?, #to_h

Constructor Details

This class inherits a constructor from MTK::Events::Event

Class Method Details

.from_midi(status, data1, data2) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/mtk/events/parameter.rb', line 8

def self.from_midi(status, data1, data2)
  if status.is_a? Array
    type,channel = *status
  else
    type,channel = status & 0xF0, status & 0x0F
  end
  type, number, value = *(
    case type
      when 0xA0,:poly_pressure    then [:pressure, data1, data2]
      when 0xB0,:control_change   then [:control, data1, data2]
      when 0xC0,:program_change   then [:program, data1]
      when 0xD0,:channel_pressure then [:pressure, nil, data1] # no number means all notes on channel
      when 0xE0,:pitch_bend       then [:bend, nil, (data1 + (data2 << 7))]
      else [:unknown, data1, data2]
    end
  )
  if type == :bend
    if value == 16383
      value = 1.0 # special case since the math doesn't quite work out to convert to -1..1 for all values
    else
      value = (value / 8192.0) - 1.0
    end
  elsif value.is_a? Numeric
    value /= 127.0
  end
  new type, :number => number, :value => value, :channel => channel
end

Instance Method Details

#inspectObject



48
49
50
# File 'lib/mtk/events/parameter.rb', line 48

def inspect
  "Parameter(#@type" + (@number ? "[#@number], " : ', ') + "#@value)"
end

#midi_valueObject



36
37
38
39
40
41
42
# File 'lib/mtk/events/parameter.rb', line 36

def midi_value
  if @type == :bend
    (16383*(@value+1)/2).round
  else
    super
  end
end

#to_sObject



44
45
46
# File 'lib/mtk/events/parameter.rb', line 44

def to_s
  "Parameter(#@type" + (@number ? "[#@number], " : ', ') + "#{sprintf '%.2f', @value || Float::NAN})"
end