Module: MIDIInstrument::Message

Extended by:
Message
Included in:
Message
Defined in:
lib/midi-instrument/message.rb

Overview

Helper for converting MIDI messages

Instance Method Summary collapse

Instance Method Details

#bytes?(object) ⇒ Boolean

Does this object look like a MIDI byte?

Parameters:

  • object (Object)

Returns:

  • (Boolean)


70
71
72
# File 'lib/midi-instrument/message.rb', line 70

def bytes?(object)
  object.kind_of?(Fixnum) && object >= 0x00 && object <= 0xFF
end

#to_bytes(*args) ⇒ Array<Fixnum>

Convert the input to bytes

Parameters:

  • args (Array<Fixnum>, Array<MIDIMessage>, Array<String>, MIDIMessage, *MIDIMessage, String)

Returns:

  • (Array<Fixnum>)


23
24
25
26
27
28
29
30
# File 'lib/midi-instrument/message.rb', line 23

def to_bytes(*args)
  messages = to_messages(*args)
  if !messages.nil?
    messages.map(&:to_bytes).flatten
  elsif args.all? { |arg| bytes?(arg)}
    args
  end
end

#to_messages(*args) ⇒ Array<MIDIMessage>

Convert the input to MIDI messages

Parameters:

  • args (Array<MIDIMessage>, Array<String>, MIDIMessage, *MIDIMessage, String)

Returns:

  • (Array<MIDIMessage>)


11
12
13
14
15
16
17
18
# File 'lib/midi-instrument/message.rb', line 11

def to_messages(*args)
  data = [args.dup].flatten
  if data.all? { |item| note?(item) }
    Message.to_note_ons(*data) # string note names
  elsif data.all? { |item| message?(item) }
    data # messages
  end
end

#to_note_offs(*args) ⇒ Array<MIDIMessage::NoteOn, nil>

Convert the input to MIDI note off messages

Parameters:

  • args (*MIDIMessage::NoteOff, *MIDIMessage::NoteOn, *String)
  • options (Hash)

Returns:

  • (Array<MIDIMessage::NoteOn, nil>)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/midi-instrument/message.rb', line 38

def to_note_offs(*args)
  notes = [args.dup].flatten
  options = notes.last.kind_of?(Hash) ? notes.pop : {}
  notes.map do |note|
    case note
    when String then string_to_note_off(note, options) if note?(note)
    when MIDIMessage::NoteOff then note
    when MIDIMessage::NoteOn then note.to_note_off
    end
  end
end

#to_note_ons(*args) ⇒ Array<MIDIMessage::NoteOn, nil>

Convert the input to MIDI note on messages

Parameters:

  • args (*MIDIMessage::NoteOn, *String)
  • options (Hash)

Returns:

  • (Array<MIDIMessage::NoteOn, nil>)


56
57
58
59
60
61
62
63
64
65
# File 'lib/midi-instrument/message.rb', line 56

def to_note_ons(*args)
  notes = [args.dup].flatten
  options = notes.last.kind_of?(Hash) ? notes.pop : {}
  notes.map do |note|
    case note
    when String then string_to_note_on(note, options) if note?(note)
    when MIDIMessage::NoteOn then note
    end
  end
end