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 +4 -4
- data/README.md +3 -2
- data/example/args.rb +23 -0
- data/example/say.rb +1 -1
- data/lib/core_ext/hash.rb +4 -1
- data/lib/midiops/handler.rb +50 -32
- data/lib/midiops/observer.rb +7 -3
- data/lib/midiops/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 085a82aad8264f698430e464c9ebd7097e3248af
|
4
|
+
data.tar.gz: b4442ec22eeafc92902d3c4b893e5668b5be9b49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
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,
|
40
|
+
observer.on_key 0, 'D5' do |vel|
|
40
41
|
puts "Ch.0.C5 pressed with velocity #{vel}!"
|
41
42
|
end
|
42
43
|
|
data/example/args.rb
ADDED
@@ -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
|
data/example/say.rb
CHANGED
data/lib/core_ext/hash.rb
CHANGED
data/lib/midiops/handler.rb
CHANGED
@@ -4,55 +4,73 @@ require 'eventmachine'
|
|
4
4
|
module MIDIOps
|
5
5
|
class Handler
|
6
6
|
module Status
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
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
|
-
|
13
|
-
|
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
|
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
|
26
|
-
if @handlers.
|
27
|
-
Status::
|
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
|
-
|
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
|
-
|
38
|
-
prc = @handlers.get_by_keys(*@events)[:ARG]
|
39
|
-
EM.defer(Proc.new { prc.call(event) })
|
52
|
+
@events << event
|
40
53
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
@events
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
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
|
data/lib/midiops/observer.rb
CHANGED
@@ -6,8 +6,8 @@ require 'unimidi'
|
|
6
6
|
|
7
7
|
module MIDIOps
|
8
8
|
class Observer
|
9
|
-
def initialize
|
10
|
-
@handler = Handler.new
|
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,
|
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
|
data/lib/midiops/version.rb
CHANGED
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.
|
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
|
+
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
|