midiops 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1715f8d7b74d5b2622559230ee32a9a5ffbc4839
4
+ data.tar.gz: bfcf7dc79e91b102f277610eee565d658f58940b
5
+ SHA512:
6
+ metadata.gz: 1cd24b5bfe05388934ca1701e12d9e8b2b6b0067de2a8703743f6c07735a359783638bb4e4323527079fca1bca206af6a063ba36df83cb1ba954860b21bd5e49
7
+ data.tar.gz: a999c7969f414e4eb7c2c73aaa875f03d2eddeec4fbed03a3618a3d0bf23ed42feb47aa90891bcb94c4616976b2056b847fcdc93041a06c2d70db21c1e477cdc
data/.gitignore ADDED
@@ -0,0 +1,10 @@
1
+ /vendor/
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in midiops.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 polamjag
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,64 @@
1
+ # MIDIOps
2
+
3
+ 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).
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'midiops'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install midiops
20
+
21
+ ## Usage
22
+
23
+ This is a super simple example:
24
+
25
+ ```ruby
26
+ require 'unimidi'
27
+
28
+ observer = MIDIOps::Observer.new
29
+
30
+ observer.on [144, 72, 127] do
31
+ puts "Ch.0.C5 pressed!"
32
+ end
33
+
34
+ observer.listen_first
35
+ ```
36
+
37
+ Execute and press C5 key on your midi device.
38
+
39
+ Optionally, you can set number of worker thread for massive parallelism like this:
40
+
41
+ ```ruby
42
+ require 'unimidi'
43
+
44
+ observer = MIDIOps::Observer.new 3 # three workers
45
+
46
+ # ...
47
+ ```
48
+
49
+ See `/examples` for more materials.
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/polamjag/midiops. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
60
+
61
+
62
+ ## License
63
+
64
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "midiops"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,10 @@
1
+ require 'midiops'
2
+ require 'unimidi'
3
+
4
+ ob = MIDIOps::Observer.new
5
+
6
+ ob.on [144, 0, 127] do
7
+ `notify-send '#{Time.now}'`
8
+ end
9
+
10
+ ob.listen_first
data/example/say.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'midiops'
2
+ require 'unimidi'
3
+
4
+ ob = MIDIOps::Observer.new 3
5
+
6
+ ob.on [144, 48, 127] do
7
+ `say Hello`
8
+ end
9
+
10
+ ob.listen_device 'Launchpad S'
@@ -0,0 +1,41 @@
1
+ class Hash
2
+ def get_by_keys *keys
3
+ if keys.size == 1
4
+ self[keys.first]
5
+ else
6
+ key = keys.shift
7
+ if self.key? key
8
+ self[key].get_by_keys(*keys)
9
+ else
10
+ nil
11
+ end
12
+ end
13
+ end
14
+
15
+ def set_by_keys keys, value
16
+ key = keys.shift
17
+
18
+ if keys.empty?
19
+ self[key] = value
20
+ else
21
+ self[key] ||= {}
22
+ self[key].set_by_keys keys, value
23
+ end
24
+ end
25
+
26
+ def keys? *keys
27
+ h = self
28
+ keys.each_with_index do |key, i|
29
+ if h.key? key
30
+ unless i == (keys.size-1)
31
+ return false unless h[key].is_a? Hash
32
+ h = h[key]
33
+ else
34
+ return true
35
+ end
36
+ else
37
+ return false
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,52 @@
1
+ require 'core_ext/hash'
2
+
3
+ module MIDIOps
4
+ class Handler
5
+ module Status
6
+ HAS_HANDLER = 0
7
+ HANDLER_INDEFINITE = 1
8
+ NO_HANDLERS = 2
9
+ end
10
+
11
+ def initialize worker_number
12
+ @handlers = {}
13
+ @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
22
+ end
23
+
24
+ def add event, handler
25
+ @handlers.set_by_keys event, handler
26
+ end
27
+
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
36
+ Status::NO_HANDLERS
37
+ end
38
+ end
39
+
40
+ def handle event
41
+ @events << event
42
+
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
+ @events.clear
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,39 @@
1
+ require 'midiops/handler'
2
+ require 'unimidi'
3
+
4
+ module MIDIOps
5
+ class Observer
6
+ def initialize(worker_number = 1)
7
+ @handler = Handler.new worker_number
8
+ end
9
+
10
+ def on event, &handler
11
+ @handler.add event, handler
12
+ end
13
+
14
+ def listen input
15
+ loop do
16
+ input.gets.each do |events|
17
+ events[:data].each do |ev|
18
+ @handler.handle ev
19
+ end
20
+ end
21
+ end
22
+ end
23
+
24
+ def listen_device device_name
25
+ if !(dev = UniMIDI::Input.find_by_name device_name).nil?
26
+ listen dev.open
27
+ elsif !(dev = UniMIDI::Input.find { |input| input.name.include? device_name }).nil?
28
+ listen dev.open
29
+ else
30
+ raise RuntimeError,
31
+ "Specified device \"#{device_name}\" not available; available device(s): #{UniMIDI::Input.map{|i| '"' + i.name + '"' }.join(', ')}"
32
+ end
33
+ end
34
+
35
+ def listen_first
36
+ listen UniMIDI::Input.first.open
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,3 @@
1
+ module Midiops
2
+ VERSION = "0.1.0"
3
+ end
data/lib/midiops.rb ADDED
@@ -0,0 +1,6 @@
1
+ require "midiops/version"
2
+ require "midiops/observer"
3
+
4
+ module Midiops
5
+ # Your code goes here...
6
+ end
data/midiops.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'midiops/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "midiops"
8
+ spec.version = Midiops::VERSION
9
+ spec.authors = ["polamjag"]
10
+ spec.email = ["s@polamjag.info"]
11
+
12
+ spec.summary = %q{midiops}
13
+ spec.homepage = "https://github.com/polamjag/midiops"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "unimidi"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec"
26
+ spec.add_development_dependency "pry"
27
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: midiops
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - polamjag
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-09-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: unimidi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description:
84
+ email:
85
+ - s@polamjag.info
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - CODE_OF_CONDUCT.md
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/console
99
+ - bin/setup
100
+ - example/notify-send.rb
101
+ - example/say.rb
102
+ - lib/core_ext/hash.rb
103
+ - lib/midiops.rb
104
+ - lib/midiops/handler.rb
105
+ - lib/midiops/observer.rb
106
+ - lib/midiops/version.rb
107
+ - midiops.gemspec
108
+ homepage: https://github.com/polamjag/midiops
109
+ licenses:
110
+ - MIT
111
+ metadata: {}
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubyforge_project:
128
+ rubygems_version: 2.4.5.1
129
+ signing_key:
130
+ specification_version: 4
131
+ summary: midiops
132
+ test_files: []
133
+ has_rdoc: