midi-eye 0.3.9 → 0.3.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE +1 -1
- data/README.md +15 -15
- data/lib/midi-eye.rb +5 -3
- data/lib/midi-eye/event.rb +16 -18
- data/lib/midi-eye/listener.rb +10 -11
- data/lib/midi-eye/source.rb +23 -21
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: df1b8c5bbb797b25f58dfe308f5a30b97d3d7897
|
4
|
+
data.tar.gz: 6bdc24368840613721fc897b1e64d3892042c8d7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fba1db63b3c1d8399eb9f6443a976e8be5ef9e75959d8ec693c4206f8b28d0ffc9b7c7146db2755367cbdfcf2d2477e755b2e685fe7d30db85a1c595c2eee6e1
|
7
|
+
data.tar.gz: 8d23fb30ed11046561b6bf3b6833e78a49042e6ae0c4aaf27b362070a3e46ba7faf8cc37427ef52212273e0663dc9e85ba8f728b394f549378582f2b1f7f30bf
|
data/LICENSE
CHANGED
data/README.md
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
# MIDI EYE
|
2
2
|
|
3
|
-
MIDI input event listener for Ruby
|
3
|
+
MIDI input event listener for Ruby
|
4
4
|
|
5
5
|
## Install
|
6
6
|
|
7
7
|
`gem install midi-eye`
|
8
|
-
|
8
|
+
|
9
9
|
or using Bundler, add this to your Gemfile
|
10
10
|
|
11
11
|
`gem "midi-eye"`
|
12
|
-
|
12
|
+
|
13
13
|
## Usage
|
14
14
|
|
15
15
|
```ruby
|
@@ -17,17 +17,17 @@ require 'midi-eye'
|
|
17
17
|
```
|
18
18
|
|
19
19
|
The following is an example that takes any note messages received from a unimidi input, transposes them up one octave and then sends them to an output
|
20
|
-
|
20
|
+
|
21
21
|
First, pick some MIDI IO ports
|
22
22
|
|
23
|
-
```ruby
|
23
|
+
```ruby
|
24
24
|
@input = UniMIDI::Input.gets
|
25
25
|
@output = UniMIDI::Output.gets
|
26
26
|
```
|
27
27
|
|
28
28
|
Then create a listener for the input port
|
29
29
|
|
30
|
-
```ruby
|
30
|
+
```ruby
|
31
31
|
transpose = MIDIEye::Listener.new(@input)
|
32
32
|
```
|
33
33
|
|
@@ -39,23 +39,23 @@ In this example, we specify that the listener listens for note on/off messages,
|
|
39
39
|
|
40
40
|
```ruby
|
41
41
|
transpose.listen_for(:class => [MIDIMessage::NoteOn, MIDIMessage::NoteOff]) do |event|
|
42
|
-
|
42
|
+
|
43
43
|
# raise the note value by an octave
|
44
44
|
event[:message].note += 12
|
45
|
-
|
46
|
-
# send the altered note message to the output you chose earlier
|
45
|
+
|
46
|
+
# send the altered note message to the output you chose earlier
|
47
47
|
@output.puts(event[:message])
|
48
|
-
|
48
|
+
|
49
49
|
end
|
50
50
|
```
|
51
51
|
|
52
52
|
There is also the option of leaving out the parameters altogether and including using conditional if/unless/case/etc statements in the callback.
|
53
|
-
|
53
|
+
|
54
54
|
You can bind as many events to a listener as you wish by repeatedly calling `Listener#listen_for`
|
55
55
|
|
56
56
|
Once all the events are bound, start the listener
|
57
57
|
|
58
|
-
```ruby
|
58
|
+
```ruby
|
59
59
|
transpose.run
|
60
60
|
```
|
61
61
|
|
@@ -63,7 +63,7 @@ A listener can also be run in a background thread by passing in `:background =>
|
|
63
63
|
|
64
64
|
```ruby
|
65
65
|
transpose.run(:background => true)
|
66
|
-
|
66
|
+
|
67
67
|
transpose.join # join the background thread later
|
68
68
|
```
|
69
69
|
|
@@ -71,7 +71,7 @@ transpose.join # join the background thread later
|
|
71
71
|
|
72
72
|
* [examples](http://github.com/arirusso/midi-eye/tree/master/examples)
|
73
73
|
* [rdoc](http://rdoc.info/gems/midi-eye)
|
74
|
-
|
74
|
+
|
75
75
|
## Author
|
76
76
|
|
77
77
|
* [Ari Russo](http://github.com/arirusso) <ari.russo at gmail.com>
|
@@ -80,4 +80,4 @@ transpose.join # join the background thread later
|
|
80
80
|
|
81
81
|
Apache 2.0, See the file LICENSE
|
82
82
|
|
83
|
-
Copyright (c) 2011-
|
83
|
+
Copyright (c) 2011-2017 [Ari Russo](http://arirusso.com)
|
data/lib/midi-eye.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
#
|
2
2
|
# midi-eye
|
3
|
-
#
|
4
3
|
# MIDI input event listener for Ruby
|
5
|
-
#
|
4
|
+
# https://github.com/arirusso/midi-eye
|
5
|
+
#
|
6
|
+
# (c)2011-2017 Ari Russo
|
6
7
|
# Apache 2.0 License
|
7
8
|
#
|
8
9
|
|
9
10
|
# libs
|
11
|
+
require "forwardable"
|
10
12
|
require "midi-message"
|
11
13
|
require "nibbler"
|
12
14
|
require "unimidi"
|
@@ -18,6 +20,6 @@ require "midi-eye/source"
|
|
18
20
|
|
19
21
|
module MIDIEye
|
20
22
|
|
21
|
-
VERSION = "0.3.
|
23
|
+
VERSION = "0.3.10"
|
22
24
|
|
23
25
|
end
|
data/lib/midi-eye/event.rb
CHANGED
@@ -1,11 +1,15 @@
|
|
1
1
|
module MIDIEye
|
2
|
-
|
2
|
+
|
3
3
|
# User defined callbacks for input events
|
4
4
|
class Event
|
5
5
|
|
6
|
+
extend Forwardable
|
7
|
+
|
8
|
+
def_delegators :@event, :count
|
9
|
+
|
6
10
|
def initialize
|
7
11
|
@event = []
|
8
|
-
@queue =
|
12
|
+
@queue = Queue.new
|
9
13
|
end
|
10
14
|
|
11
15
|
# Delete an event by name
|
@@ -29,10 +33,10 @@ module MIDIEye
|
|
29
33
|
def add(options = {}, &callback)
|
30
34
|
name = options[:listener_name]
|
31
35
|
options.delete(:listener_name)
|
32
|
-
event = {
|
33
|
-
:conditions => options,
|
34
|
-
:proc => callback,
|
35
|
-
:listener_name => name
|
36
|
+
event = {
|
37
|
+
:conditions => options,
|
38
|
+
:proc => callback,
|
39
|
+
:listener_name => name
|
36
40
|
}
|
37
41
|
@event << event
|
38
42
|
event
|
@@ -57,21 +61,15 @@ module MIDIEye
|
|
57
61
|
|
58
62
|
# Add an event to the trigger queue
|
59
63
|
# @return [Hash]
|
60
|
-
def enqueue(action, message)
|
61
|
-
event = {
|
62
|
-
:action => action,
|
63
|
-
:message => message
|
64
|
+
def enqueue(action, message)
|
65
|
+
event = {
|
66
|
+
:action => action,
|
67
|
+
:message => message
|
64
68
|
}
|
65
69
|
@queue << event
|
66
70
|
event
|
67
71
|
end
|
68
72
|
|
69
|
-
# The number of events
|
70
|
-
# @return [Fixnum]
|
71
|
-
def count
|
72
|
-
@event.count
|
73
|
-
end
|
74
|
-
|
75
73
|
private
|
76
74
|
|
77
75
|
# Does the given message meet the given conditions?
|
@@ -98,11 +96,11 @@ module MIDIEye
|
|
98
96
|
begin
|
99
97
|
action[:proc].call(event[:message])
|
100
98
|
rescue Exception => exception
|
101
|
-
Thread.main.raise
|
99
|
+
Thread.main.raise(exception)
|
102
100
|
end
|
103
101
|
end
|
104
102
|
end
|
105
|
-
|
103
|
+
|
106
104
|
end
|
107
105
|
|
108
106
|
end
|
data/lib/midi-eye/listener.rb
CHANGED
@@ -2,6 +2,8 @@ module MIDIEye
|
|
2
2
|
|
3
3
|
class Listener
|
4
4
|
|
5
|
+
LISTEN_INTERVAL = 1.0 / 1000
|
6
|
+
|
5
7
|
attr_reader :event
|
6
8
|
attr_accessor :sources
|
7
9
|
|
@@ -25,10 +27,8 @@ module MIDIEye
|
|
25
27
|
# @return [Array<MIDIEye::Source>] The updated list of sources for this listener
|
26
28
|
def add_input(inputs)
|
27
29
|
inputs = [inputs].flatten.compact
|
28
|
-
|
29
|
-
|
30
|
-
end
|
31
|
-
@sources += new_sources.compact
|
30
|
+
input_sources = inputs.reject { |input| uses_input?(input) }
|
31
|
+
@sources += input_sources.map { |input| Source.new(input) }
|
32
32
|
@sources
|
33
33
|
end
|
34
34
|
alias_method :add_inputs, :add_input
|
@@ -100,7 +100,6 @@ module MIDIEye
|
|
100
100
|
self
|
101
101
|
end
|
102
102
|
alias_method :on_message, :listen_for
|
103
|
-
alias_method :listen, :listen_for
|
104
103
|
|
105
104
|
# Poll the input source for new input. This will normally be done by the background thread
|
106
105
|
def poll
|
@@ -109,10 +108,11 @@ module MIDIEye
|
|
109
108
|
objs.each do |batch|
|
110
109
|
messages = [batch[:messages]].flatten.compact
|
111
110
|
messages.each do |message|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
111
|
+
data = {
|
112
|
+
:message => message,
|
113
|
+
:timestamp => batch[:timestamp]
|
114
|
+
}
|
115
|
+
@event.enqueue_all(data)
|
116
116
|
end
|
117
117
|
end
|
118
118
|
end
|
@@ -123,11 +123,10 @@ module MIDIEye
|
|
123
123
|
|
124
124
|
# A loop that runs while the listener is active
|
125
125
|
def listen_loop
|
126
|
-
interval = 1.0/1000
|
127
126
|
loop do
|
128
127
|
poll
|
129
128
|
@event.trigger_enqueued
|
130
|
-
sleep(
|
129
|
+
sleep(LISTEN_INTERVAL)
|
131
130
|
end
|
132
131
|
end
|
133
132
|
|
data/lib/midi-eye/source.rb
CHANGED
@@ -1,44 +1,46 @@
|
|
1
1
|
module MIDIEye
|
2
|
-
|
2
|
+
|
3
3
|
# Retrieves new messages from a unimidi input buffer
|
4
4
|
class Source
|
5
|
-
|
5
|
+
|
6
6
|
attr_reader :device, :pointer
|
7
|
-
|
7
|
+
|
8
|
+
# Whether the given object is a UniMIDI input
|
9
|
+
# @param [Object] input
|
10
|
+
# @return [Boolean]
|
11
|
+
def self.compatible?(input)
|
12
|
+
input.respond_to?(:gets) && input.respond_to?(:buffer)
|
13
|
+
end
|
14
|
+
|
8
15
|
# @param [UniMIDI::Input] input
|
9
|
-
def initialize(input)
|
16
|
+
def initialize(input)
|
10
17
|
@parser = Nibbler.new
|
11
18
|
@pointer = 0
|
12
19
|
@device = input
|
13
20
|
end
|
14
|
-
|
21
|
+
|
15
22
|
# Grabs new messages from the input buffer
|
16
23
|
def poll(&block)
|
17
24
|
messages = @device.buffer.slice(@pointer, @device.buffer.length - @pointer)
|
18
25
|
@pointer = @device.buffer.length
|
19
|
-
messages.each do |raw_message|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
26
|
+
messages.compact.each do |raw_message|
|
27
|
+
parsed_messages = begin
|
28
|
+
@parser.parse(raw_message[:data], :timestamp => raw_message[:timestamp])
|
29
|
+
rescue
|
30
|
+
nil
|
24
31
|
end
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
# Whether the given object is a UniMIDI input
|
29
|
-
# @param [Object] input
|
30
|
-
# @return [Boolean]
|
31
|
-
def self.compatible?(input)
|
32
|
-
input.respond_to?(:gets) && input.respond_to?(:buffer)
|
32
|
+
objects = [parsed_messages].flatten.compact
|
33
|
+
yield(objects)
|
34
|
+
end
|
33
35
|
end
|
34
|
-
|
36
|
+
|
35
37
|
# If this source was created from the given input
|
36
38
|
# @param [UniMIDI::Input] input
|
37
39
|
# @return [Boolean]
|
38
40
|
def uses?(input)
|
39
41
|
@device == input
|
40
42
|
end
|
41
|
-
|
43
|
+
|
42
44
|
end
|
43
|
-
|
45
|
+
|
44
46
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: midi-eye
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.10
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ari Russo
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2017-07-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest
|
@@ -167,7 +167,7 @@ files:
|
|
167
167
|
- test/listener_test.rb
|
168
168
|
homepage: http://github.com/arirusso/midi-eye
|
169
169
|
licenses:
|
170
|
-
- Apache
|
170
|
+
- Apache-2.0
|
171
171
|
metadata: {}
|
172
172
|
post_install_message:
|
173
173
|
rdoc_options: []
|
@@ -185,7 +185,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
185
|
version: 1.3.6
|
186
186
|
requirements: []
|
187
187
|
rubyforge_project: midi-eye
|
188
|
-
rubygems_version: 2.
|
188
|
+
rubygems_version: 2.6.12
|
189
189
|
signing_key:
|
190
190
|
specification_version: 4
|
191
191
|
summary: MIDI event listener for Ruby
|