Class: Shmidi::Socket

Inherits:
Object
  • Object
show all
Includes:
Base
Defined in:
lib/shmidi/socket.rb

Constant Summary collapse

@@controllers =
{}

Constants included from Base

Base::CTYPE

Instance Attribute Summary collapse

Attributes included from Base

#_attachments, #_deleted, #_id, #_rev, #version

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Base

#[], #[]=, #clone, #dump, #etag, included, #inspect, #reset, #to_hash, #to_s

Constructor Details

#initialize(name, in_id, out_id) ⇒ Socket

Returns a new instance of Socket.



13
14
15
16
17
18
# File 'lib/shmidi/socket.rb', line 13

def initialize(name, in_id, out_id)
  @name = name
  @in = in_id
  @out = out_id
  init
end

Instance Attribute Details

#inObject (readonly)

Returns the value of attribute in.



11
12
13
# File 'lib/shmidi/socket.rb', line 11

def in
  @in
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/shmidi/socket.rb', line 11

def name
  @name
end

#outObject (readonly)

Returns the value of attribute out.



11
12
13
# File 'lib/shmidi/socket.rb', line 11

def out
  @out
end

Class Method Details

.[](name) ⇒ Object



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

def self.[](name)
  @@controllers[name]
end


71
72
73
74
75
76
# File 'lib/shmidi/socket.rb', line 71

def self.print_device_list
  $stdout.puts('Inputs:')
  UniMIDI::Input.list
  $stdout.puts('Outputs:')
  UniMIDI::Output.list
end

Instance Method Details

#initObject



20
21
22
23
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
# File 'lib/shmidi/socket.rb', line 20

def init
  @__in_dev   = UniMIDI::Input.all.find { |d| d.name == @in  }
  @__out_dev  = UniMIDI::Output.all.find { |d| d.name == @out }

  @@controllers[@name] = self
  @__queue = Queue.new
  @__on_event = []
  # @__sync_threads = Hash.new { |hash, key| hash[key] = Clock.new(key, self) }
  @__listener = Thread.new do
    loop do
      break unless @__in_dev
      begin
        @__in_dev.gets.each do |event|
          event[:source] = @name
          event = Event.new(event)
          Shmidi.TRACE_INTERNAL("> #{@name}\t#{event}")
          @__queue.push(event)
          @__on_event.each do |rule|
            next if (channel  = rule[:channel]) && channel  != event.channel
            next if (message  = rule[:message]) && message  != event.message
            next if (note     = rule[:note])    && note     != event.note
            next if (value    = rule[:value])   && value    != event.value

            rule[:block].call(event) rescue Shmidi.ON_EXCEPTION
          end
        end
      rescue
        Shmidi.ON_EXCEPTION
      end
    end
  end
end

#on_event(channel = nil, message = nil, note = nil, value = nil, &block) ⇒ Object



53
54
55
56
57
58
59
60
# File 'lib/shmidi/socket.rb', line 53

def on_event(channel = nil, message = nil, note = nil, value = nil, &block)
  @__on_event << {
    :block => block,
    :channel => channel,
    :message => message,
    :note => note
  }
end

#push(events) ⇒ Object



62
63
64
65
66
67
68
69
# File 'lib/shmidi/socket.rb', line 62

def push(events)
  events = Array(events).reduce([]) do |array, event|
    Shmidi.TRACE_EXTERNAL("< #{@name}\t#{event}")
    array << event.data
    array
  end
  @__out_dev.puts(*events)
end