nerpin 0.0.1

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.
@@ -0,0 +1,39 @@
1
+ module Nerpin
2
+ class Controller
3
+ def initialize(destination, nrpn_class)
4
+ @destination = destination
5
+ @nrpn_class = nrpn_class
6
+ end
7
+
8
+ def puts(id, value)
9
+ mutex.synchronize do
10
+ [
11
+ [0x63, (id >> 7) & 0b1111111],
12
+ [0x62, id & 0b1111111],
13
+ [0x06, value[:v0x06]],
14
+ [0x26, value[:v0x26]]
15
+ ].each do |message|
16
+ @destination.puts(0b10110000, message[0], message[1])
17
+ end
18
+ end
19
+ end
20
+
21
+ def nrpns
22
+ @nrpn_class.instance_variable_get('@nrpns')
23
+ end
24
+
25
+ private
26
+
27
+ def mutex
28
+ @mutex ||= Mutex.new
29
+ end
30
+
31
+ def method_missing(name, *args)
32
+ if nrpn = @nrpn_class.find_by_key(name)
33
+ puts(nrpn.id, nrpn.value(args.first))
34
+ else
35
+ super
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+
3
+ module Nerpin
4
+ module Nrpn
5
+ class Micron < Base
6
+ def value(value)
7
+ {
8
+ :v0x06 => (value >= 0) ? value / 128 : 127 - (value.abs / 128),
9
+ :v0x26 => (value >= 0) ? value % 128 : 128 - (value.abs % 128)
10
+ }
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,52 @@
1
+ require 'json'
2
+
3
+ module Nerpin
4
+ module Nrpn
5
+ class Base
6
+ attr_reader :id, :key, :min, :max
7
+
8
+ class << self
9
+ def find_by_key(key)
10
+ nrpns[key]
11
+ end
12
+
13
+ def nrpns
14
+ return @nrpns if @nrpns
15
+
16
+ data = JSON.parse(
17
+ open(
18
+ File.dirname(
19
+ File.expand_path(__FILE__)
20
+ ) + '/../../data/nrpn/%s.json' % self.name.split(/::/).last.downcase
21
+ ).read
22
+ )
23
+
24
+ @nrpns = Hash[
25
+ data.map do |k, v|
26
+ [k.to_sym, new(v['id'], k.to_sym, v['min'], v['max'])]
27
+ end
28
+ ].freeze
29
+ end
30
+ end
31
+
32
+ def initialize(id, key, min, max)
33
+ @id = id
34
+ @key = key
35
+ @min = min
36
+ @max = max
37
+ end
38
+
39
+ def value(value)
40
+ raise 'subclass must override this'
41
+ end
42
+
43
+ def value_at(scale)
44
+ value((((min.abs + max) * scale) - min.abs).to_i)
45
+ end
46
+
47
+ def sample
48
+ value(rand(min.abs + max + (min < 0 ? 2 : 1)) - min.abs - (min < 0 ? 1 : 0))
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,36 @@
1
+ require 'nokogiri'
2
+ require 'open-uri'
3
+
4
+ module Nerpin
5
+ module Util
6
+ # ruby -rnerpin -rjson -e "puts JSON.pretty_generate(Nerpin::Util.parse_spec_for_micron)"
7
+ def self.parse_spec_for_micron
8
+ spec_url = 'http://ion-micron-miniak.wikia.com/wiki/Common_FAQ'
9
+ doc = Nokogiri::HTML(open(spec_url))
10
+ table = doc.xpath('//table').last
11
+
12
+ table.xpath('tr').drop(1).inject({}) do |result, tr|
13
+ id, key, min, max = tr.children.map do |el|
14
+ el.text.strip
15
+ end
16
+
17
+ unless key.empty?
18
+ key = key.downcase.
19
+ gsub(/\-/, 'minus').
20
+ gsub(/[\W]+/, '_').
21
+ split('_').
22
+ join('_').
23
+ to_sym
24
+
25
+ result[key] = {
26
+ :id => id.to_i,
27
+ :min => min.to_i,
28
+ :max => max.to_i
29
+ }
30
+ end
31
+
32
+ result
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,3 @@
1
+ module Nerpin
2
+ VERSION = "0.0.1"
3
+ end
data/lib/nerpin.rb ADDED
@@ -0,0 +1,19 @@
1
+ require "nerpin/nrpn"
2
+ require "nerpin/nrpn/micron"
3
+ require "nerpin/controller"
4
+ require "nerpin/util"
5
+ require "nerpin/version"
6
+
7
+ module Nerpin
8
+ ObjectSpace.each_object(class << Nrpn::Base; self; end) do |m|
9
+ if m != Nrpn::Base
10
+ name = m.name.split(/::/).last.to_sym
11
+
12
+ define_method(name) do |destination|
13
+ Controller.new(destination, m)
14
+ end
15
+
16
+ module_function name
17
+ end
18
+ end
19
+ end
data/nerpin.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/nerpin/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["youpy"]
6
+ gem.email = ["youpy@buycheapviagraonlinenow.com"]
7
+ gem.description = %q{A Ruby library to send manufacturer-specific or instrument-specific MIDI messages}
8
+ gem.summary = %q{A Ruby library to send manufacturer-specific or instrument-specific MIDI messages}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "nerpin"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = Nerpin::VERSION
17
+
18
+ gem.add_development_dependency('rspec', ['~> 2.12'])
19
+ gem.add_development_dependency('rake')
20
+ gem.add_development_dependency('webmock')
21
+ gem.add_development_dependency('nerpin')
22
+
23
+ gem.add_dependency('nokogiri')
24
+ gem.add_dependency('json')
25
+ end