midiops 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 7fda112a3552a1c02142d825389c00a76b1881e4
4
- data.tar.gz: 7a3d4acacd618629b0260afd27588fd77a8fe354
3
+ metadata.gz: 085a82aad8264f698430e464c9ebd7097e3248af
4
+ data.tar.gz: b4442ec22eeafc92902d3c4b893e5668b5be9b49
5
5
  SHA512:
6
- metadata.gz: d5e63782941ab259e5362489b0963c75b712e6704d4489ee2222104d21b71523b013b79cc3966ff12f9f31647dd89f77966345d05d2deda3c419ff9172ab91cc
7
- data.tar.gz: 31c1113ce6e4da3bc4d2eaf7ad3bd7639f7ea5177e01e82f853c8741e43b5fa11aa8221a101ef1f256dcc6eb907792a9916e87be2df308af0d7a34400ffa1bcc
6
+ metadata.gz: eaa508783840081753f0f0d067665c140842b7eaef3b191086a0b2e6d9a5fd73752b4d14baa13c40ad46bc4366fe4012d992ec960cf70551e64f86db7114e2da
7
+ data.tar.gz: 437b67cab3ab35c05ec02c8aadcf29709cb7dd4eae45b241281b1b74cc1be7057e9f6e56d45f73983148fd4b1e08cdb46415504707704043334e6a10d64c70f7
data/README.md CHANGED
@@ -32,11 +32,12 @@ require 'midiops'
32
32
 
33
33
  observer = MIDIOps::Observer.new
34
34
 
35
- observer.on_key 0, :C, 5 do
35
+ # args: midi ch, note in string
36
+ observer.on_key 0, 'C5' do
36
37
  puts "Ch.0.C5 pressed!"
37
38
  end
38
39
 
39
- observer.on_key 0, :D, 5 do |vel|
40
+ observer.on_key 0, 'D5' do |vel|
40
41
  puts "Ch.0.C5 pressed with velocity #{vel}!"
41
42
  end
42
43
 
@@ -0,0 +1,23 @@
1
+ require 'midiops'
2
+ require 'unimidi'
3
+
4
+ ob = MIDIOps::Observer.new
5
+
6
+ ob.on [144, 50, 127] do
7
+ puts "PRESS NORMAL!!!!!"
8
+ end
9
+
10
+ ob.on_key 0, 'C3' do |i|
11
+ puts "PRESS!!!!!: #{i}"
12
+ end
13
+
14
+ ob.on_key 0, 'C#3' do
15
+ puts "press c#!!!!"
16
+ end
17
+
18
+ ob.on_pitchbend 0 do |a, b|
19
+ puts "Pitchbend!!!: #{a}, #{b}"
20
+ p a, b
21
+ end
22
+
23
+ ob.listen_first
@@ -1,7 +1,7 @@
1
1
  require 'midiops'
2
2
  require 'unimidi'
3
3
 
4
- ob = MIDIOps::Observer.new 3
4
+ ob = MIDIOps::Observer.new
5
5
 
6
6
  ob.on [144, 48, 127] do
7
7
  `say Hello`
@@ -1,6 +1,9 @@
1
1
  class Hash
2
2
  def get_by_keys *keys
3
- if keys.size == 1
3
+ case keys.size
4
+ when 0
5
+ self
6
+ when 1
4
7
  self[keys.first]
5
8
  else
6
9
  key = keys.shift
@@ -4,55 +4,73 @@ require 'eventmachine'
4
4
  module MIDIOps
5
5
  class Handler
6
6
  module Status
7
- HAS_HANDLER = 0
8
- HANDLER_INDEFINITE = 1
9
- NO_HANDLERS = 2
10
- end
7
+ NO_HANDLERS = 0
8
+
9
+ HAS_HANDLER = 1 # just have handler with current @events
10
+ HAS_HANDLER_WITH_ARG = 2 # have handler w/ current @events but last is :ARG
11
11
 
12
- def initialize worker_number
13
- @handlers = {}
14
- @events = []
15
- @waiting_param = false
12
+ HANDLER_INDEFINITE = 3
13
+ WAITING_ARG = 4 # HANDLER_INDEFINITE but current event is :ARG
16
14
  end
17
15
 
18
- def push_job job
16
+ def initialize
17
+ @handlers = {}
18
+ @events = [] # pool for events not processed
19
+ @handler_args = [] # args to pass to handler method
19
20
  end
20
21
 
21
22
  def add event, handler
22
23
  @handlers.set_by_keys event, handler
23
24
  end
24
25
 
25
- def has_handler?
26
- if @handlers.keys?(*@events).nil?
27
- Status::NO_HANDLERS
26
+ def status
27
+ if @handlers.get_by_keys(*@events).is_a? Proc
28
+ Status::HAS_HANDLER
29
+ elsif @handlers.get_by_keys(*(@events.take(@events.size - 1)), :ARG).is_a? Proc
30
+ Status::HAS_HANDLER_WITH_ARG
31
+ elsif @handlers.get_by_keys(*(@events.take(@events.size - 1)), :ARG).is_a? Hash
32
+ Status::WAITING_ARG
28
33
  elsif @handlers.get_by_keys(*@events).is_a? Hash
29
- @waiting_param = true unless @handlers.get_by_keys(*@events)[:ARG].nil?
30
34
  Status::HANDLER_INDEFINITE
35
+ elsif @handlers.get_by_keys(*@events).nil?
36
+ Status::NO_HANDLERS
31
37
  else
32
- Status::HAS_HANDLER
38
+ raise RuntimeError, "Invalid handler status: #{self.inspect}"
33
39
  end
34
40
  end
35
41
 
42
+ def defer handler, args=[]
43
+ EM.defer(Proc.new { handler.call(*args) }) unless handler.nil?
44
+ end
45
+
46
+ def clear_state
47
+ @events.clear
48
+ @handler_args.clear
49
+ end
50
+
36
51
  def handle event
37
- if @waiting_param
38
- prc = @handlers.get_by_keys(*@events)[:ARG]
39
- EM.defer(Proc.new { prc.call(event) })
52
+ @events << event
40
53
 
41
- @events.clear
42
- @waiting_param = false
43
- else
44
- @events << event
45
-
46
- case has_handler?
47
- when Status::HAS_HANDLER
48
- EM.defer(@handlers.get_by_keys(*@events)) unless @handlers.get_by_keys(*@events).nil?
49
-
50
- @events.clear
51
- when Status::NO_HANDLERS
52
- @events.clear
53
- when Status::HANDLER_INDEFINITE
54
- # do nothing
55
- end
54
+ case status
55
+ when Status::HAS_HANDLER
56
+ defer @handlers.get_by_keys(*@events, event), @handler_args
57
+ clear_state
58
+ when Status::HAS_HANDLER_WITH_ARG
59
+ @handler_args << event
60
+ @events.pop
61
+ @events << :ARG
62
+ defer(
63
+ @handlers.get_by_keys(*@events),
64
+ @handler_args.dup
65
+ )
66
+ clear_state
67
+ when Status::WAITING_ARG
68
+ @handler_args << event
69
+ @events.pop
70
+ @events << :ARG
71
+ when Status::NO_HANDLERS
72
+ clear_state
73
+ when Status::HANDLER_INDEFINITE
56
74
  end
57
75
  end
58
76
  end
@@ -6,8 +6,8 @@ require 'unimidi'
6
6
 
7
7
  module MIDIOps
8
8
  class Observer
9
- def initialize(worker_number = 1)
10
- @handler = Handler.new worker_number
9
+ def initialize
10
+ @handler = Handler.new
11
11
  end
12
12
 
13
13
  def on event, &handler
@@ -23,10 +23,14 @@ module MIDIOps
23
23
  end
24
24
  alias_method :on_key, :on_key_press
25
25
 
26
- def on_key_release ch, key, octave, &handler
26
+ def on_key_release ch, note_string, &handler
27
27
  @handler.add [128+ch, MIDIOps::Note.note_to_code(note_string), :ARG], handler
28
28
  end
29
29
 
30
+ def on_pitchbend ch, &handler
31
+ @handler.add [224+ch, :ARG, :ARG], handler
32
+ end
33
+
30
34
  def listen input
31
35
  EM.run do
32
36
  loop do
@@ -1,3 +1,3 @@
1
1
  module MIDIOps
2
- VERSION = "0.2.1"
2
+ VERSION = "0.3.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midiops
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - polamjag
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-11-16 00:00:00.000000000 Z
11
+ date: 2015-12-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unimidi
@@ -112,6 +112,7 @@ files:
112
112
  - bin/console
113
113
  - bin/setup
114
114
  - circle.yml
115
+ - example/args.rb
115
116
  - example/notify-send.rb
116
117
  - example/say.rb
117
118
  - lib/core_ext/hash.rb