Class: MicroMIDI::State

Inherits:
Object
  • Object
show all
Defined in:
lib/micromidi/state.rb

Overview

The DSL state

Constant Summary collapse

DEFAULT =
{
  :channel => 0,
  :octave => 2,
  :velocity => 100
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(inputs, outputs, options = {}) ⇒ State

Returns a new instance of State.

Parameters:

  • inputs (Array<UniMIDI::Input>, UniMIDI::Input)
  • outputs (Array<UniMIDI::Output, IO>, IO, UniMIDI::Output)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :channel (Fixnum)
  • :octave (Fixnum)
  • :velocity (Fixnum)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/micromidi/state.rb', line 34

def initialize(inputs, outputs, options = {})
  @inputs = [inputs].flatten
  @outputs = [outputs].flatten

  @channel = options[:channel] || DEFAULT[:channel]
  @velocity = options[:velocity] || DEFAULT[:velocity]
  @octave = options[:octave] || DEFAULT[:octave]

  @auto_output = true
  @last_command = nil
  @last_note = nil
  @listeners = []
  @thru_listeners = []
  @output_cache = []
  @start_time = Time.now.to_f
  @super_sticky = false
end

Instance Attribute Details

#auto_outputObject

Returns the value of attribute auto_output.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def auto_output
  @auto_output
end

#channelObject

Returns the value of attribute channel.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def channel
  @channel
end

#inputsObject (readonly)

Returns the value of attribute inputs.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def inputs
  @inputs
end

#last_commandObject (readonly)

Returns the value of attribute last_command.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def last_command
  @last_command
end

#last_noteObject

Returns the value of attribute last_note.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def last_note
  @last_note
end

#listenersObject (readonly)

Returns the value of attribute listeners.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def listeners
  @listeners
end

#octaveObject

Returns the value of attribute octave.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def octave
  @octave
end

#output_cacheObject (readonly)

Returns the value of attribute output_cache.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def output_cache
  @output_cache
end

#outputsObject (readonly)

Returns the value of attribute outputs.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def outputs
  @outputs
end

#start_timeObject (readonly)

Returns the value of attribute start_time.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def start_time
  @start_time
end

#super_stickyObject

Returns the value of attribute super_sticky.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def super_sticky
  @super_sticky
end

#sysex_nodeObject

Returns the value of attribute sysex_node.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def sysex_node
  @sysex_node
end

#thru_listenersObject (readonly)

Returns the value of attribute thru_listeners.



20
21
22
# File 'lib/micromidi/state.rb', line 20

def thru_listeners
  @thru_listeners
end

#velocityObject

Returns the value of attribute velocity.



12
13
14
# File 'lib/micromidi/state.rb', line 12

def velocity
  @velocity
end

Instance Method Details

#message_properties(options, *properties) ⇒ Hash

Return message properties with regard to the current state

Parameters:

  • options (Hash)
  • properties (*Symbol)

Returns:

  • (Hash)


108
109
110
111
112
113
114
115
116
117
118
# File 'lib/micromidi/state.rb', line 108

def message_properties(options, *properties)
  result = {}
  properties.each do |property|
    result[property] = options[property]
    if !result[property].nil? && (send(property).nil? || @super_sticky)
      send("#{property.to_s}=", result[property])
    end
    result[property] ||= send(property.to_s)
  end
  result
end

#record(method, args, block, result) ⇒ Object

Record that a command was used

Parameters:

  • method (Symbol, String)
  • args (Array<Object>)
  • block (Proc)
  • result (Object)


57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/micromidi/state.rb', line 57

def record(method, args, block, result)
  timestamp = now
  message = {
    :message => result,
    :timestamp => timestamp
  }
  @output_cache << message
  @last_command = {
    :method => method,
    :args => args,
    :block => block,
    :timestamp => timestamp
  }
end

#toggle_auto_outputBoolean

Toggles auto-output mode. In auto-output mode, any messages that are instantiated are sent to any available MIDI outputs.

Returns:

  • (Boolean)


100
101
102
# File 'lib/micromidi/state.rb', line 100

def toggle_auto_output
  @auto_output = !@auto_output
end

#toggle_super_stickyBoolean

Toggles super_sticky mode, a mode where any explicit values used to create MIDI messages automatically become sticky. Normally the explicit value would only be used for the current message.

For example, while in super sticky mode

“‘ruby note “C4”, :channel => 5 note “C3” “`

will have the same results as

“‘ruby channel 5 note “C4” note “C3” “`

Returns:

  • (Boolean)


93
94
95
# File 'lib/micromidi/state.rb', line 93

def toggle_super_sticky
  @super_sticky = !@super_sticky
end