midiops 0.1.1 → 0.2.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: a04bb045250e64a5cdd25dbdabad651413c01862
4
- data.tar.gz: ab08124e7aaa484815c30e58ed5b98249494d680
3
+ metadata.gz: c863cc841a88dc2ba892df7a86cd3c8d7eef9f6f
4
+ data.tar.gz: bec221cd5ffb9874547dabb2418f81565141df4b
5
5
  SHA512:
6
- metadata.gz: 99ddb03ea77b0b26722fdb03371c13b940e3f8711aeee9b7b721778fb9fe74877db00bd00e6b02efabfa5a0fbe29da31130e4ab64ba5a6789dd09f6de9918ae4
7
- data.tar.gz: 3c75d2e29a474e7eb60bfecb72077e9cffadcaa4d3e081ab128498aada75e3a79183cd35814e5afb984f9004b1f085a905254092325292a2e76da35dcd353514
6
+ metadata.gz: da6ff6e7f9533fc5c1540b47c4a8d507e02e114975836cb8b640329556a48a6fb0116d3d9285651cb55dd6a03537026a9f67957a327f850392b78c78ff228325
7
+ data.tar.gz: d8062bdbbd76083eb7a4ee9e7a12f8c067a16e4893b36c13b166dbd75407f17e0750cfc2adcf00df373e9019e97a519991b8ac318f64e82c442e28822bf623b7
data/README.md CHANGED
@@ -1,8 +1,9 @@
1
1
  # MIDIOps
2
2
 
3
3
  [![Gem Version](https://badge.fury.io/rb/midiops.svg)](https://rubygems.org/gems/midiops)
4
+ [![Circle CI](https://circleci.com/gh/polamjag/midiops.svg?style=svg)](https://circleci.com/gh/polamjag/midiops)
4
5
 
5
- MIDI Operator. Heavily inspired by [this blog entry](http://hitode909.hatenablog.com/entry/2015/07/28/134304) and [hitode909/sketch-midi](https://github.com/hitode909/sketch-midi).
6
+ MIDI Operations. Heavily inspired by [this blog entry](http://hitode909.hatenablog.com/entry/2015/07/28/134304) and [hitode909/sketch-midi](https://github.com/hitode909/sketch-midi).
6
7
 
7
8
  ## Installation
8
9
 
@@ -27,28 +28,22 @@ Or install it yourself as:
27
28
  This is a super simple example:
28
29
 
29
30
  ```ruby
30
- require 'unimidi'
31
+ require 'midiops'
31
32
 
32
33
  observer = MIDIOps::Observer.new
33
34
 
34
- observer.on [144, 72, 127] do
35
+ observer.on_key 0, :C, 5 do
35
36
  puts "Ch.0.C5 pressed!"
36
37
  end
37
38
 
39
+ observer.on_key 0, :D, 5 do |vel|
40
+ puts "Ch.0.C5 pressed with velocity #{vel}!"
41
+ end
42
+
38
43
  observer.listen_first
39
44
  ```
40
45
 
41
- Execute and press C5 key on your midi device.
42
-
43
- Optionally, you can set number of worker thread for massive parallelism like this:
44
-
45
- ```ruby
46
- require 'unimidi'
47
-
48
- observer = MIDIOps::Observer.new 3 # three workers
49
-
50
- # ...
51
- ```
46
+ Connect some MIDI keyboard and execute the script, then press key C5 or D5 on your midi device. Hooray!
52
47
 
53
48
  See `/examples` for more materials.
54
49
 
data/circle.yml ADDED
@@ -0,0 +1,3 @@
1
+ dependencies:
2
+ pre:
3
+ - gem install bundler --pre
@@ -1,4 +1,5 @@
1
1
  require 'core_ext/hash'
2
+ require 'eventmachine'
2
3
 
3
4
  module MIDIOps
4
5
  class Handler
@@ -11,41 +12,47 @@ module MIDIOps
11
12
  def initialize worker_number
12
13
  @handlers = {}
13
14
  @events = []
14
- @q = Queue.new
15
- @worker = Array.new worker_number do |i|
16
- Thread.start do
17
- while res = @q.pop
18
- res.call
19
- end
20
- end
21
- end
15
+ @waiting_param = false
16
+ end
17
+
18
+ def push_job job
22
19
  end
23
20
 
24
21
  def add event, handler
25
22
  @handlers.set_by_keys event, handler
26
23
  end
27
24
 
28
- def has_handler? events
29
- if @handlers.keys?(*events)
30
- unless @handlers.get_by_keys(*events).is_a? Hash
31
- Status::HAS_HANDLER
32
- else
33
- Status::HANDLER_INDEFINITE
34
- end
35
- else
25
+ def has_handler?
26
+ if @handlers.keys?(*@events).nil?
36
27
  Status::NO_HANDLERS
28
+ elsif @handlers.get_by_keys(*@events).is_a? Hash
29
+ @waiting_param = true unless @handlers.get_by_keys(*@events)[:ARG].nil?
30
+ Status::HANDLER_INDEFINITE
31
+ else
32
+ Status::HAS_HANDLER
37
33
  end
38
34
  end
39
35
 
40
36
  def handle event
41
- @events << event
37
+ if @waiting_param
38
+ prc = @handlers.get_by_keys(*@events)[:ARG]
39
+ EM.defer(Proc.new { prc.call(event) })
42
40
 
43
- case has_handler?(@events)
44
- when Status::HAS_HANDLER
45
- @q.push @handlers.get_by_keys(*@events)
46
- @events.clear
47
- when Status::NO_HANDLERS
48
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
49
56
  end
50
57
  end
51
58
  end
@@ -0,0 +1,40 @@
1
+ module MIDIOps
2
+ module Note
3
+ KEY_OFFSETS = {
4
+ C: 0,
5
+
6
+ Csharp: 1,
7
+ Dflat: 1,
8
+
9
+ D: 2,
10
+
11
+ Dsharp: 3,
12
+ Eflat: 3,
13
+
14
+ E: 4,
15
+
16
+ F: 5,
17
+
18
+ Fsharp: 6,
19
+ Gflat: 6,
20
+
21
+ G: 7,
22
+
23
+ Gsharp: 8,
24
+ Aflat: 8,
25
+
26
+ A: 9,
27
+
28
+ Asharp: 10,
29
+ Bflat: 10,
30
+
31
+ B: 11
32
+ }
33
+
34
+ class << self
35
+ def key_to_code key, octave
36
+ KEY_OFFSETS[key] + 12 + (12 * octave)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -1,4 +1,7 @@
1
1
  require 'midiops/handler'
2
+ require 'midiops/note'
3
+ require 'eventmachine'
4
+
2
5
  require 'unimidi'
3
6
 
4
7
  module MIDIOps
@@ -11,11 +14,26 @@ module MIDIOps
11
14
  @handler.add event, handler
12
15
  end
13
16
 
17
+ def on_cc ch, number, &handler
18
+ @handler.add [176+ch, number, :ARG], handler
19
+ end
20
+
21
+ def on_key_press ch, key, octave, &handler
22
+ @handler.add [144+ch, MIDIOps::Note.key_to_code(key, octave), :ARG], handler
23
+ end
24
+ alias_method :on_key, :on_key_press
25
+
26
+ def on_key_release ch, key, octave, &handler
27
+ @handler.add [128+ch, MIDIOps::Note.key_to_code(key, octave), :ARG], handler
28
+ end
29
+
14
30
  def listen input
15
- loop do
16
- input.gets.each do |events|
17
- events[:data].each do |ev|
18
- @handler.handle ev
31
+ EM.run do
32
+ loop do
33
+ input.gets.each do |events|
34
+ events[:data].each do |ev|
35
+ @handler.handle ev
36
+ end
19
37
  end
20
38
  end
21
39
  end
@@ -1,3 +1,3 @@
1
- module Midiops
2
- VERSION = "0.1.1"
1
+ module MIDIOps
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/midiops.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  require "midiops/version"
2
2
  require "midiops/observer"
3
3
 
4
- module Midiops
4
+ module MIDIOps
5
5
  # Your code goes here...
6
6
  end
data/midiops.gemspec CHANGED
@@ -5,7 +5,7 @@ require 'midiops/version'
5
5
 
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = "midiops"
8
- spec.version = Midiops::VERSION
8
+ spec.version = MIDIOps::VERSION
9
9
  spec.authors = ["polamjag"]
10
10
  spec.email = ["s@polamjag.info"]
11
11
 
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
  spec.require_paths = ["lib"]
21
21
 
22
22
  spec.add_dependency "unimidi"
23
+ spec.add_dependency "eventmachine"
23
24
 
24
25
  spec.add_development_dependency "bundler", "~> 1.10"
25
26
  spec.add_development_dependency "rake", "~> 10.0"
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.1.1
4
+ version: 0.2.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-09-02 00:00:00.000000000 Z
11
+ date: 2015-11-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: unimidi
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: eventmachine
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  - !ruby/object:Gem::Dependency
28
42
  name: bundler
29
43
  requirement: !ruby/object:Gem::Requirement
@@ -97,11 +111,13 @@ files:
97
111
  - Rakefile
98
112
  - bin/console
99
113
  - bin/setup
114
+ - circle.yml
100
115
  - example/notify-send.rb
101
116
  - example/say.rb
102
117
  - lib/core_ext/hash.rb
103
118
  - lib/midiops.rb
104
119
  - lib/midiops/handler.rb
120
+ - lib/midiops/note.rb
105
121
  - lib/midiops/observer.rb
106
122
  - lib/midiops/version.rb
107
123
  - midiops.gemspec