Class: Shmidi::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/shmidi/clock.rb,
lib/shmidi/event.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(h) ⇒ Event

Returns a new instance of Event.



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/shmidi/event.rb', line 24

def initialize(h)
  @source     = h[:source]
  @timestamp  = (h[:timestamp] || 0).floor
  @data       = h[:data].clone if h[:data]
  if @data
    @channel    = (@data[0] & 0x0f) + 1
    @message_int= @data[0] >> 4
    @message    = @message_int
    @message    = MESSAGES[@message_int] || @message_int
    @note_int   = @data[1]
    @note       = @note_int
    parse_note_int if [:on, :off].include?(@message)
    @value      = @data[2]
  else # no numeric data array
    @message_int = h[:message_int] || MESSAGES[h[:message]] || h[:message]
    @message = MESSAGES[@message_int]
    @value = h[:value] || 0
    @channel = h[:channel] || 1
    @note = h[:note]
    @note_int = h[:note]
    if h[:note].kind_of?(Numeric) && [:on, :off].include?(@message)
      parse_note_int
    elsif h[:note].kind_of?(String)
      @note = h[:note]
      #@note += h[:note][1] if h[:note][1] == '#'
      h[:note] =~ /^(.#?)(-?\d+)$/
      @note_int = ($2.to_i + 1) * 12 + 'C C#D D#E F F#G G#A A#B '.index($1) / 2
    end
    @data = [0,0,0]
    @data[1] = @note_int
    @data[2] = @value
    @data[0] = (@message_int << 4) + (@channel - 1)
  end
end

Instance Attribute Details

#channelObject (readonly)

Returns the value of attribute channel.



7
8
9
# File 'lib/shmidi/event.rb', line 7

def channel
  @channel
end

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/shmidi/event.rb', line 6

def data
  @data
end

#destinationObject

Returns the value of attribute destination.



4
5
6
# File 'lib/shmidi/clock.rb', line 4

def destination
  @destination
end

#messageObject (readonly)

Returns the value of attribute message.



8
9
10
# File 'lib/shmidi/event.rb', line 8

def message
  @message
end

#message_intObject (readonly)

Returns the value of attribute message_int.



7
8
9
# File 'lib/shmidi/event.rb', line 7

def message_int
  @message_int
end

#noteObject (readonly)

Returns the value of attribute note.



8
9
10
# File 'lib/shmidi/event.rb', line 8

def note
  @note
end

#note_intObject (readonly)

Returns the value of attribute note_int.



7
8
9
# File 'lib/shmidi/event.rb', line 7

def note_int
  @note_int
end

#sourceObject (readonly)

TODO: eat symbols and strings as needed for shmidi piping



5
6
7
# File 'lib/shmidi/event.rb', line 5

def source
  @source
end

#timestampObject (readonly)

Returns the value of attribute timestamp.



6
7
8
# File 'lib/shmidi/event.rb', line 6

def timestamp
  @timestamp
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/shmidi/event.rb', line 7

def value
  @value
end

Class Method Details

.new_cc(channel, cc, value) ⇒ Object



75
76
77
78
79
80
81
# File 'lib/shmidi/event.rb', line 75

def self.new_cc(channel, cc, value)
  Event.new(
    :channel  => channel,
    :message  => :cc,
    :note     => cc,
    :value    => value)
end

.new_off(channel, note, value = 0) ⇒ Object



67
68
69
70
71
72
73
# File 'lib/shmidi/event.rb', line 67

def self.new_off(channel, note, value = 0)
  Event.new(
    :channel  => channel,
    :message  => :off,
    :note     => note,
    :value    => value)
end

.new_on(channel, note, value = 127) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/shmidi/event.rb', line 59

def self.new_on(channel, note, value = 127)
  Event.new(
    :channel  => channel,
    :message  => :on,
    :note     => note,
    :value    => value)
end

Instance Method Details

#octaveObject



19
20
21
22
# File 'lib/shmidi/event.rb', line 19

def octave
  return nil unless @note.kind_of?(String)
  (@note_int / 12) - 1
end

#to_hashObject



87
88
89
90
91
92
93
# File 'lib/shmidi/event.rb', line 87

def to_hash
  hash = {}
  instance_variables.each do |var|
    hash[var[1..-1].to_sym] = instance_variable_get(var)
  end
  hash
end

#to_sObject



83
84
85
# File 'lib/shmidi/event.rb', line 83

def to_s
  "CH:#{@channel}\t#{@message}\t#{@note}\t=#{@value}"
end