Module: Diamond::MIDI::Input

Included in:
Node
Defined in:
lib/diamond/midi.rb

Overview

Methods dealing with MIDI input

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


9
10
11
12
13
14
15
16
17
18
19
# File 'lib/diamond/midi.rb', line 9

def self.included(base)
  base.send(:extend, Forwardable)
  base.send(:def_delegators, :@midi,
            :input,
            :inputs,
            :omni_on,
            :rx_channel, 
            :receive_channel,
            :rx_channel=,
            :receive_channel=)
end

Instance Method Details

#add(*args) ⇒ Array<MIDIMessage> Also known as: <<

Add MIDI input notes

Parameters:

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

Returns:

  • (Array<MIDIMessage>)

24
25
26
# File 'lib/diamond/midi.rb', line 24

def add(*args)
  @midi.input << args
end

#enable_note_control(arpeggiator) ⇒ Boolean

Initialize adding and removing MIDI notes from the sequence

Parameters:

Returns:

  • (Boolean)

40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/diamond/midi.rb', line 40

def enable_note_control(arpeggiator)  
  @midi.input.receive(:class => MIDIMessage::NoteOn) do |event|
    message = event[:message]
    if @midi.input.channel.nil? || @midi.input.channel == message.channel
      puts "[DEBUG] MIDI: add note from input #{message.name} channel: #{message.channel}" if @debug
      arpeggiator.sequence.add(message)
    end
  end 
  @midi.input.receive(:class => MIDIMessage::NoteOff) do |event| 
    message = event[:message]
    if @midi.input.channel.nil? || @midi.input.channel == message.channel
      puts "[DEBUG] MIDI: remove note from input #{message.name} channel: #{message.channel}" if @debug
      arpeggiator.sequence.remove(message)
    end
  end
  true
end

#enable_parameter_control(arpeggiator, map) ⇒ Boolean

Initialize a user-defined map of control change messages

Parameters:

Returns:

  • (Boolean)

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/diamond/midi.rb', line 62

def enable_parameter_control(arpeggiator, map)
  midi_cc_range = 0..127
  @midi.input.receive(:class => MIDIMessage::ControlChange) do |event|
    message = event[:message]
    if @midi.input.channel.nil? || @midi.input.channel == message.channel
      index = message.index
      mapping = map.find { |mapping| mapping[:index] == index }
      property = mapping[:property]
      parameter_range = apeggiator.parameter.constraints(property)
      value = message.value
      value = Scale.transform(value).from(midi_cc_range).to(parameter_range)
      puts "[DEBUG] MIDI: #{property}= #{value} channel: #{message.channel}" if @debug
      arpeggiator.parameter.send("#{property}=", value)
    end
  end
end

#remove(*args) ⇒ Array<MIDIMessage>

Add note offs to cancel input

Parameters:

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

Returns:

  • (Array<MIDIMessage>)

32
33
34
35
# File 'lib/diamond/midi.rb', line 32

def remove(*args)
  messages = MIDIInstrument::Message.to_note_offs(*args)
  @midi.input.add(messages.compact)
end