vissen-input 0.2.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: bf9e2d7e4ab570645627dab31c509d35c94c1dd0fab403113505fcd807acf4ec
4
+ data.tar.gz: 12845d0b04e56faf957a4780d0a04e671d304197a9d0b2040f7c5665ab1aa0fc
5
+ SHA512:
6
+ metadata.gz: 560ac7d4f5e7670e2ebf5d79fd8e4ce5ada839b61ab8ec30815bf44be5b6e5b5e683aac9f992f1e003bdc2c22947cb2db5a8a97e4209b87636391e88de46d30d
7
+ data.tar.gz: ed3da5a2f4f479dcc9ef4fac688945ebdda4ec2b1a3c7e045ffb991babe330201f53e076c39e5783ead938e1cf1b2309bab84c6b70d59637fe5f5b6c3624330e
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/.rubocop.yml ADDED
@@ -0,0 +1,48 @@
1
+ AllCops:
2
+ Exclude:
3
+ - 'vendor/**/*'
4
+ - 'tmp/**/*'
5
+ TargetRubyVersion: 2.5
6
+
7
+ Style/FrozenStringLiteralComment:
8
+ EnforcedStyle: always
9
+
10
+ Layout/EndOfLine:
11
+ EnforcedStyle: lf
12
+
13
+ Layout/ClassStructure:
14
+ Enabled: true
15
+ Categories:
16
+ module_inclusion:
17
+ - include
18
+ - prepend
19
+ - extend
20
+ ExpectedOrder:
21
+ - module_inclusion
22
+ - constants
23
+ - public_class_methods
24
+ - initializer
25
+ - instance_methods
26
+ - protected_methods
27
+ - private_methods
28
+
29
+ Layout/IndentHeredoc:
30
+ EnforcedStyle: squiggly
31
+
32
+ Lint/AmbiguousBlockAssociation:
33
+ Exclude:
34
+ - 'test/**/*.rb'
35
+
36
+ Lint/InterpolationCheck:
37
+ Exclude:
38
+ - 'test/**/*.rb'
39
+
40
+ Metrics/BlockLength:
41
+ Exclude:
42
+ - 'Rakefile'
43
+ - '**/*.rake'
44
+ - 'test/**/*.rb'
45
+
46
+ Metrics/ModuleLength:
47
+ Exclude:
48
+ - 'test/**/*.rb'
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.0
5
+ before_install: gem install bundler -v 1.16.1
data/CHANGELOG.md ADDED
@@ -0,0 +1,26 @@
1
+ # Changelog
2
+ All notable changes to this project will be documented in this file.
3
+
4
+ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
+ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
+
7
+ ## [Unreleased]
8
+
9
+ ## [0.2.2] - 2018-04-18
10
+ ### Changed
11
+ - Message subscribers can now prevent a message from reaching lower priority subscribers.
12
+
13
+ ## [0.2.1] - 2018-04-11
14
+ ### Added
15
+ - Yard compatible documentation.
16
+ ### Changed
17
+ - Broker#run_once now returns true if a message was handled.
18
+ - PitchBendChange.create now accepts a float value instead of the raw bytes.
19
+
20
+ ### Fixed
21
+ - Bug in Message::Base.create that ment the status was always set to zero.
22
+
23
+ ## [0.2.0] - 2018-04-11
24
+ ### Added
25
+ - Subscription object to handle objects subscribing to incoming messages.
26
+ - Message broker implementation.
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in vissen-input.gemspec
8
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,46 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vissen-input (0.2.2)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.0)
10
+ docile (1.3.0)
11
+ json (2.1.0)
12
+ minitest (5.11.3)
13
+ parallel (1.12.1)
14
+ parser (2.5.1.0)
15
+ ast (~> 2.4.0)
16
+ powerpack (0.1.1)
17
+ rainbow (3.0.0)
18
+ rake (10.5.0)
19
+ rubocop (0.55.0)
20
+ parallel (~> 1.10)
21
+ parser (>= 2.5)
22
+ powerpack (~> 0.1)
23
+ rainbow (>= 2.2.2, < 4.0)
24
+ ruby-progressbar (~> 1.7)
25
+ unicode-display_width (~> 1.0, >= 1.0.1)
26
+ ruby-progressbar (1.9.0)
27
+ simplecov (0.16.1)
28
+ docile (~> 1.1)
29
+ json (>= 1.8, < 3)
30
+ simplecov-html (~> 0.10.0)
31
+ simplecov-html (0.10.2)
32
+ unicode-display_width (1.3.0)
33
+
34
+ PLATFORMS
35
+ ruby
36
+
37
+ DEPENDENCIES
38
+ bundler (~> 1.16)
39
+ minitest (~> 5.0)
40
+ rake (~> 10.0)
41
+ rubocop (~> 0.52)
42
+ simplecov (~> 0.16)
43
+ vissen-input!
44
+
45
+ BUNDLED WITH
46
+ 1.16.1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sebastian Lindberg
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,38 @@
1
+ # Vissen::Input
2
+
3
+ [![Build Status](https://travis-ci.org/midi-visualizer/vissen-input.svg?branch=master)](https://travis-ci.org/midi-visualizer/vissen-input)
4
+ [![Inline docs](http://inch-ci.org/github/midi-visualizer/vissen-input.svg?branch=master)](http://inch-ci.org/github/midi-visualizer/vissen-input)
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'vissen-input'
12
+ ```
13
+
14
+ And then execute:
15
+
16
+ $ bundle
17
+
18
+ Or install it yourself as:
19
+
20
+ $ gem install vissen-input
21
+
22
+ ## Usage
23
+
24
+ TODO: Write usage instructions here
25
+
26
+ ## Development
27
+
28
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
29
+
30
+ 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).
31
+
32
+ ## Contributing
33
+
34
+ Bug reports and pull requests are welcome on GitHub at https://github.com/midi-visualizer/vissen-input.
35
+
36
+ ## License
37
+
38
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rake/testtask'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ end
11
+
12
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'bundler/setup'
5
+ require 'vissen/input'
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require 'irb'
15
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,174 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ # Message broker that consumes a stream of messages and exposes a simple
6
+ # subscription interface.
7
+ #
8
+ # == Usage
9
+ # This example subscribes to note messages on channel 1 and calls the
10
+ # fictitious method play when a matching message is published and processed.
11
+ #
12
+ # broker = Broker.new
13
+ # broker.subscribe Message::Note[1], priority: 1 do |message|
14
+ # play message.note
15
+ # end
16
+ #
17
+ # message = Message::Note.create 42, channel: 1
18
+ # broker.publish message
19
+ # broker.run_once
20
+ #
21
+ # The next example sets up two different priority listeners, one of which
22
+ # blocks the other for some messages.
23
+ #
24
+ # broker = Broker.new
25
+ # broker.subscribe Message::Note[1], priority: 1 do |message|
26
+ # play message.note
27
+ # end
28
+ #
29
+ # broker.subscribe Message::Note[1], priority: 2 do |message, ctrl|
30
+ # ctrl.stop! if message.note % 2 == 0
31
+ # end
32
+ #
33
+ class Broker
34
+ def initialize
35
+ @subscriptions = []
36
+ @message_queue = Queue.new
37
+ end
38
+
39
+ # Register a callback for the broker to run when a message matched by the
40
+ # given matcher is published.
41
+ #
42
+ # By specifying a priority a subscriber added after another can still be
43
+ # handled at an earlier time. The handler can either be specified as an
44
+ # object responding to `#call` or as a block.
45
+ #
46
+ # @param matcher [#match?] the matcher that will be used to filter
47
+ # messeges.
48
+ # @param handler [#call] the handler that will be called when a matching
49
+ # message is published. Mandatory unless a block is given.
50
+ # @param priority [Integer] the priority determines when the subscription
51
+ # will be matched against a published message in relation to other
52
+ # subscriptions.
53
+ # @return [Subscription] the new subscription object.
54
+ def subscribe(matcher, handler = nil, priority: 0, &block)
55
+ if block_given?
56
+ raise ArgumentError if handler
57
+ handler = block
58
+ else
59
+ raise ArgumentError unless handler
60
+ end
61
+
62
+ insert_subscription Subscription.new(matcher, handler, priority)
63
+ end
64
+
65
+ # Removes the given subscription.
66
+ #
67
+ # @param subscription [Subscription] the subscription to cancel.
68
+ # @return [Subscription, nil] the subscription that was cancelled, or nil
69
+ # if the subscription was not found.
70
+ def unsubscribe(subscription)
71
+ @subscriptions.delete subscription
72
+ end
73
+
74
+ # Insert a new message into the message queue. The message is handled at a
75
+ # later time in `#run_once`.
76
+ #
77
+ # @param message [Message] the message(s) to handle.
78
+ def publish(*message)
79
+ message.each { |m| @message_queue.push m }
80
+ end
81
+
82
+ # Takes one message from the message queue and handles it.
83
+ #
84
+ # @return [true, false] true if the message_queue contained a message,
85
+ # otherwise false.
86
+ def run_once
87
+ return false if @message_queue.empty?
88
+ ctrl = PropagationControl.new
89
+ call @message_queue.shift, ctrl
90
+ true
91
+ end
92
+
93
+ # Processes one message. By design, implementing this method allows for
94
+ # multiple brokers being chained.
95
+ #
96
+ # @param message [Message] the message to match against the
97
+ # subscriptions.
98
+ # @return [nil]
99
+ def call(message, ctrl)
100
+ # TODO: Remap the message if needed.
101
+ @subscriptions.each do |subscription|
102
+ break if ctrl.stop?(subscription.priority)
103
+ next unless subscription.match? message
104
+
105
+ subscription.handle message, ctrl
106
+ end
107
+ nil
108
+ end
109
+
110
+ private
111
+
112
+ # Insert of append a new subscription to the list. The subscription will
113
+ # be placed before the first subscription that is found to have a lower
114
+ # priority, or last.
115
+ #
116
+ # @param subscription [Subscription] the subscription to add.
117
+ # @return [Subscription] the subscription that was added.
118
+ def insert_subscription(subscription)
119
+ @subscriptions.each_with_index do |other, index|
120
+ if other.priority < subscription.priority
121
+ @subscriptions.insert index, subscription
122
+ return subscription
123
+ end
124
+ end
125
+
126
+ @subscriptions.push subscription
127
+ subscription
128
+ end
129
+
130
+ # Internal class used by the `Broker` to control the propagation of a
131
+ # message and provide a control surface for subscription handlers,
132
+ # allowing them to stop the propagation at priorites lower than (not equal
133
+ # to) their own.
134
+ #
135
+ # == Usage
136
+ # The following example uses a propagation control object to stop
137
+ # propagation at priority 1. Note that `#stop?` must be called for each
138
+ # message before `#stop!` to ensure that the correct priority is set.
139
+ #
140
+ # ctrl = PropagationControl.new
141
+ # ctrl.stop? 2 # => false
142
+ # ctrl.stop? 1 # => false
143
+ #
144
+ # ctrl.stop!
145
+ #
146
+ # ctrl.stop? 1 # => false
147
+ # ctrl.stop? 0 # => true
148
+ #
149
+ class PropagationControl
150
+ def initialize
151
+ @stop_at_priority = -1
152
+ @current_priority = 0
153
+ end
154
+
155
+ # @param [Integer] the priority to test if
156
+ # @return [true, false] whether to stop or not.
157
+ def stop?(priority)
158
+ return true if priority < @stop_at_priority
159
+
160
+ @current_priority = priority
161
+ false
162
+ end
163
+
164
+ # Sets the priority stopping threshold.
165
+ #
166
+ # @return [nil]
167
+ def stop!
168
+ @stop_at_priority = @current_priority
169
+ nil
170
+ end
171
+ end
172
+ end
173
+ end
174
+ end
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ # The job of an input matcher is to match a raw message with a message
6
+ # class.
7
+ #
8
+ # == Usage
9
+ # In the following example a matcher is setup to match aftertouch messages
10
+ # (on channel 0). Only data arrays of the correct length and content causes
11
+ # `#match?` to return true
12
+ #
13
+ # matcher = Matcher.new(Message::Aftertouch) { |d| d[0] == 0xA0 }
14
+ #
15
+ # matcher.match? [0xB0, 0, 0] # => false
16
+ # matcher.match? [0xA0, 0] # => false
17
+ # matcher.match? [0xA0, 0, 0] # => true
18
+ #
19
+ class Matcher
20
+ attr_reader :klass
21
+
22
+ # @param message_klass [Message] the message class that should be used to
23
+ # parse the data that this matcher matches. The class constant
24
+ # `DATA_LENGTH` will be used to verify that the data has the correct
25
+ # length.
26
+ # @param proc [#call] the block that will be called to match messages.
27
+ def initialize(message_klass, &proc)
28
+ raise TypeError unless message_klass <= Message
29
+
30
+ @klass = message_klass
31
+ @rule = proc
32
+
33
+ freeze
34
+ end
35
+
36
+ # Match either a byte array or a `Message` against the rule stored in the
37
+ # matcher.
38
+ #
39
+ # @param obj [#to_a] the message data to match.
40
+ # @return [true, false] true if the data matches.
41
+ def match?(obj)
42
+ data = obj.to_a
43
+
44
+ return false unless data.length >= @klass::DATA_LENGTH
45
+ @rule.call data
46
+ end
47
+ end
48
+ end
49
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # Polyphonic Key Pressure (Aftertouch). This message is most often sent
8
+ # by pressing down on the key after it "bottoms out".
9
+ class Aftertouch < Base
10
+ STATUS = 0xA0
11
+
12
+ # @return [Integer] the note value.
13
+ def note
14
+ data[1]
15
+ end
16
+
17
+ # @return [Integer] the preassure value.
18
+ def preassure
19
+ data[2]
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,155 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # This is the base message implementaion. This class should never be used
7
+ # directly, but rather be subclassed to create the various messages that
8
+ # the system understands.
9
+ #
10
+ # The Base class keeps track of subclasses and can produce a message
11
+ # factory for all the implementations that it knows about (see
12
+ # `.factory`).
13
+ class Base
14
+ include Message
15
+
16
+ DATA_LENGTH = 3
17
+ STATUS = 0
18
+
19
+ # Checks message data consistency with the class default matcher.
20
+ #
21
+ # @return [true, false] true if the message data matches the class
22
+ # matcher.
23
+ def valid?
24
+ self.class.matcher.match? data
25
+ end
26
+
27
+ class << self
28
+ # Returns a new instance of a Matcher, configured to match this
29
+ # particular Message class. Subclasses of Base can utilize the same
30
+ # functionality by simply redefining STATUS and, if necessary,
31
+ # STATUS_MASK.
32
+ #
33
+ # By supplying the optional named arguments channel and number
34
+ #
35
+ # Raises a RangeError if the channel is given and is outside its valid
36
+ # range of (0..15).
37
+ # Raises a RangeError if number is given and is outside its valid
38
+ # range of (0..127).
39
+ #
40
+ # @param channel [nil, Integer] the channel to match, or nil to match
41
+ # all channels.
42
+ # @param number [nil, Integer] the second byte value to match, or nil
43
+ # to match all values.
44
+ # @return [Matcher] the matcher that fulfills the requirements.
45
+ def matcher(channel: nil, number: nil)
46
+ return klass_matcher unless channel || number
47
+ val, mask = status_value_and_mask channel
48
+
49
+ if number
50
+ raise RangeError unless (0...128).cover? number
51
+ Matcher.new(self) { |d| (d[0] & mask) == val && d[1] == number }
52
+ else
53
+ Matcher.new(self) { |d| (d[0] & mask) == val }
54
+ end
55
+ end
56
+
57
+ # Accessor for the class default matcher.
58
+ #
59
+ # @param message [#to_a] the message or data to match.
60
+ # @return [true, false] see `Matcher#match?`.
61
+ def match?(message)
62
+ matcher.match? message
63
+ end
64
+
65
+ # Creates a new factory with all the subclasses of base added to it as
66
+ # matchers.
67
+ #
68
+ # @return [MessageFactory] a factory configured to build all
69
+ # subclasses of Base.
70
+ def factory
71
+ raise RuntimeError unless defined? @subclasses
72
+ MessageFactory.new @subclasses.map(&:matcher)
73
+ end
74
+
75
+ # Alias to `#matcher` that swaps named arguments for positional ones.
76
+ #
77
+ # @param (see #matcher)
78
+ # @return (see #matcher)
79
+ def [](channel, number = nil)
80
+ matcher channel: channel, number: number
81
+ end
82
+
83
+ # Build a new instance of `Message::Base`, or a subclass, using more
84
+ # intuitive arguments. Subclasses of Base can utilize the same
85
+ # functionality by simply redefining `DATA_LENGTH` to correspond to
86
+ # their message length.
87
+ #
88
+ # Note that status and channel are masked using the default masks, and
89
+ # not the constants that may have been defined by a subclass.
90
+ #
91
+ # @param bytes [Array<Integer>] the message data byte values.
92
+ # Unspecified values default to 0.
93
+ # @param status [Integer] the status to use for the new message.
94
+ # @param channel [Integer] the channel to use for the new message.
95
+ # @param timestamp [Float] the timestamp to use for the new message.
96
+ # @return [Base] a new instance of this class.
97
+ def create(*bytes, status: self::STATUS,
98
+ channel: 0,
99
+ timestamp: Time.now.to_f)
100
+ raise ArgumentError if bytes.length >= self::DATA_LENGTH
101
+
102
+ validate_status status
103
+ validate_channel channel
104
+
105
+ data = Array.new self::DATA_LENGTH, 0
106
+
107
+ # Note: this line line must reference
108
+ # STATUS_MASK and not self::STATUS_MASK
109
+ data[0] = (status & STATUS_MASK) + (channel & CHANNEL_MASK)
110
+
111
+ # Copy the bytes
112
+ bytes.each_with_index { |value, index| data[index + 1] = value }
113
+
114
+ new data, timestamp
115
+ end
116
+
117
+ protected
118
+
119
+ def inherited(subclass)
120
+ (@subclasses ||= []) << subclass
121
+ end
122
+
123
+ def validate_status(status)
124
+ raise RangeError unless (status & ~STATUS_MASK).zero?
125
+ end
126
+
127
+ def validate_channel(channel)
128
+ raise RangeError unless (channel & ~CHANNEL_MASK).zero?
129
+ end
130
+
131
+ def status_value_and_mask(channel = nil)
132
+ if channel
133
+ validate_channel channel
134
+ [(self::STATUS & self::STATUS_MASK) + (channel & CHANNEL_MASK),
135
+ self::STATUS_MASK + CHANNEL_MASK]
136
+ else
137
+ [self::STATUS, self::STATUS_MASK]
138
+ end
139
+ end
140
+
141
+ def klass_matcher(&block)
142
+ return @klass_matcher if defined?(@klass_matcher)
143
+
144
+ unless block_given?
145
+ val, mask = status_value_and_mask
146
+ block = proc { |d| (d[0] & mask) == val }
147
+ end
148
+
149
+ @klass_matcher = Matcher.new(self, &block)
150
+ end
151
+ end
152
+ end
153
+ end
154
+ end
155
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # This the same code as the Control Change (above), but implements Mode
8
+ # control and special message by using reserved controller numbers
9
+ # 120-127. The commands are:
10
+ #
11
+ # - All Sound Off (120)
12
+ # When All Sound Off is received all oscillators will turn off, and
13
+ # their volume envelopes are set to zero as soon as possible.
14
+ # - Reset All Controllers (121)
15
+ # When Reset All Controllers is received, all controller values are
16
+ # reset to their default values.
17
+ # - Local Control (122)
18
+ # When Local Control is Off, all devices on a given channel will
19
+ # respond only to data received over MIDI. Played data, etc. will be
20
+ # ignored. Local Control On restores the functions of the normal
21
+ # controllers.
22
+ # - All Notes Off (123..127)
23
+ # When an All Notes Off is received, all oscillators will turn off.
24
+ #
25
+ class ChannelMode < Base
26
+ DATA_LENGTH = 2
27
+ STATUS = 0xB0
28
+
29
+ # @return [Integer] the control number.
30
+ def number
31
+ data[1]
32
+ end
33
+
34
+ class << self
35
+ protected
36
+
37
+ # The channel mode message is special in that it is only valid when
38
+ # the second byte takes values equal to or greather than 120. We
39
+ # therefore need to override `Base.klass_matcher`.
40
+ #
41
+ # FIXME: other matchers created may not be correct.
42
+ def klass_matcher
43
+ super do |d|
44
+ (d[0] & STATUS_MASK) == STATUS && d[1] >= 120
45
+ end
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # Channel Pressure (After-touch). This message is most often sent by
8
+ # pressing down on the key after it "bottoms out". This message is
9
+ # different from polyphonic after-touch. Use this message to send the
10
+ # single greatest pressure value (of all the current depressed keys)
11
+ class ChannelPressure < Base
12
+ STATUS = 0xD0
13
+ DATA_LENGTH = 2
14
+
15
+ # @return [Integer] the pressure value.
16
+ def pressure
17
+ data[1]
18
+ end
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # This message is sent when a controller value changes. Controllers
8
+ # include devices such as pedals and levers. Controller numbers 120-127
9
+ # are reserved as "Channel Mode Messages".
10
+ class ControlChange < Base
11
+ STATUS = 0xB0
12
+
13
+ # @return [Integer] the control number.
14
+ def number
15
+ data[1]
16
+ end
17
+
18
+ # @return [Integer] the control value.
19
+ def value
20
+ data[2]
21
+ end
22
+
23
+ class << self
24
+ protected
25
+
26
+ # The control change message is special in that it is only valid when
27
+ # the second byte takes values lower than 120. We therefore need to
28
+ # override `Base.klass_matcher`.
29
+ #
30
+ # FIXME: other matchers created may not be correct.
31
+ def klass_matcher
32
+ super do |d|
33
+ (d[0] & STATUS_MASK) == STATUS && d[1] < 120
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # Note On event.
8
+ # This message is sent when a note is depressed (start).
9
+ #
10
+ # Note Off event.
11
+ # This message is sent when a note is released (ended).
12
+ class Note < Base
13
+ STATUS_MASK = 0xE0
14
+ STATUS = 0x80
15
+ NOTE_ON = 0x10
16
+ NOTE_OFF = 0x00
17
+
18
+ # @return [Integer] the note value.
19
+ def note
20
+ data[1]
21
+ end
22
+
23
+ # @return [Integer] the velocity value.
24
+ def velocity
25
+ data[2]
26
+ end
27
+
28
+ # @return [true, false] true if the note was released.
29
+ def off?
30
+ (data[0] & NOTE_ON).zero?
31
+ end
32
+
33
+ # @return [true, false] true if the note was depressed.
34
+ def on?
35
+ !off?
36
+ end
37
+
38
+ class << self
39
+ # @param bytes (see Base.create)
40
+ # @param on [true, false] true if the note should be depressed,
41
+ # otherwise false.
42
+ # @param args (see Base.create)
43
+ # @return [Note]
44
+ def create(*bytes, on: true, **args)
45
+ super(*bytes, status: STATUS + (on ? NOTE_ON : NOTE_OFF), **args)
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # This message is sent to indicate a change in the pitch bender (wheel
8
+ # or lever, typically). The pitch bender is measured by a fourteen bit
9
+ # value. Center (no pitch change) is 2000H. Sensitivity is a function of
10
+ # the receiver, but may be set using RPN 0.
11
+ class PitchBendChange < Base
12
+ STATUS = 0xE0
13
+ CENTER_VALUE = 0x2000
14
+
15
+ # @return [Integer] the integer pitch bend value.
16
+ def raw
17
+ (data[2] << 7) + data[1] - CENTER_VALUE
18
+ end
19
+
20
+ # @return [Float] the pitch bend value normalized to the range (-1..1).
21
+ def value
22
+ raw.to_f / CENTER_VALUE
23
+ end
24
+
25
+ class << self
26
+ # TODO: Check the range on value.
27
+ #
28
+ # @param value [Float] the pitch bend value in the range (-1..1).
29
+ # @param args (see Base.create)
30
+ # @return [PitchBendChange]
31
+ def create(value = 0.0, **args)
32
+ bin_value = (value.to_f * CENTER_VALUE).round + CENTER_VALUE
33
+
34
+ super(bin_value & 0xFF, bin_value >> 7, **args)
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # From the MIDI Association:
7
+ # This message sent when the patch number changes.
8
+ class ProgramChange < Base
9
+ DATA_LENGTH = 2
10
+ STATUS = 0xC0
11
+
12
+ # @return [Integer] the program number.
13
+ def number
14
+ data[1]
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ module Message
6
+ # The unknown message type is not a subclass of Base since it is never
7
+ # meant to be used directly. It is instead expected to be created by the
8
+ # MessageFactory whenever the incomming data does not match any known
9
+ # matcher.
10
+ class Unknown
11
+ include Message
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Vissen
6
+ module Input
7
+ # This module implements the minimal interface for an input message. All
8
+ # message classes must _include_ this module to be compatible with the input
9
+ # matching engine.
10
+ #
11
+ # All Vissen Input Messages must be representable by one to three bytes.
12
+ # This stems from the tight connection with the MIDI protocol. Each message
13
+ # must also store a timestamp from when the input arrived to the system.
14
+ #
15
+ # The individual message implementations are based off the information given
16
+ # on the [midi association website](https://www.midi.org/specifications/item/table-1-summary-of-midi-message).
17
+ #
18
+ # Terminology
19
+ # -----------
20
+ # The first (and mandatory) byte of the data byte array is referred to as
21
+ # the message status. Since this byte also include channel information the
22
+ # term _status_ is, howerver, sometimes also used to name only the upper
23
+ # nibble of the status field.
24
+ module Message
25
+ STATUS_MASK = 0xF0
26
+ CHANNEL_MASK = 0x0F
27
+ DATA_LENGTH = 1
28
+
29
+ # @return [Array<Integer>]
30
+ attr_reader :data
31
+
32
+ # @return [Float]
33
+ attr_reader :timestamp
34
+
35
+ # Allow a message to pass for the raw byte array
36
+ alias to_a data
37
+
38
+ # @param data [Array<Integer>] the raw message data.
39
+ # @param timestamp [Float] the time that the message was received.
40
+ def initialize(data, timestamp)
41
+ raise TypeError unless data.length >= self.class::DATA_LENGTH
42
+
43
+ @data = data.freeze
44
+ @timestamp = timestamp.freeze
45
+ end
46
+
47
+ # The default for messages is to always be valid. Message implementations
48
+ # can override this behaviour.
49
+ #
50
+ # @return [true]
51
+ def valid?
52
+ true
53
+ end
54
+
55
+ # @return [Integer] the message status.
56
+ def status
57
+ @data[0] & self.class::STATUS_MASK
58
+ end
59
+
60
+ # @return [Integer] the message channel.
61
+ def channel
62
+ @data[0] & self.class::CHANNEL_MASK
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,100 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ # The facatory takes raw input messages and builds matching objects around
6
+ # them. It stores a list of input matchers that it knows about.
7
+ #
8
+ # If an array of matchers is passed to the constructor the MessageFactory
9
+ # will freeze itself to create an immutable factory.
10
+ #
11
+ # TODO: Sort the matchers on input frequency for performance?
12
+ #
13
+ # == Usage
14
+ # The following example sets up a factory to build ControlChange messages.
15
+ #
16
+ # matcher = Message::ControlChange.matcher
17
+ # factory = MessageFactory.new [matcher]
18
+ #
19
+ # factory.build [0xC0, 3, 42], 0.0 # => Message::ControlChange
20
+ # factory.build [0xB0, 0, 0], 0.0 # => Message::Unknown
21
+ #
22
+ class MessageFactory
23
+ # @param matchers [nil, Array<Matcher>] the matchers to use when building
24
+ # messages. If provided the factory will be frozen after creation.
25
+ def initialize(matchers = nil)
26
+ @lookup_table = Array.new(16)
27
+ @matchers = []
28
+
29
+ return unless matchers
30
+
31
+ matchers.each { |m| add_matcher m }
32
+ freeze
33
+ end
34
+
35
+ # Prevents any more matchers from being added.
36
+ #
37
+ # @return [self]
38
+ def freeze
39
+ @matchers.freeze
40
+ super
41
+ end
42
+
43
+ # Inserts another matcher to the list of known input data matchers.
44
+ #
45
+ # @param matcher [Matcher] the matcher to add.
46
+ # @return [self]
47
+ def add_matcher(matcher)
48
+ raise TypeError unless matcher.is_a? Matcher
49
+ @matchers << matcher
50
+ self
51
+ end
52
+
53
+ # Creates a new Message object by matching the data against the stored
54
+ # message classes.
55
+ #
56
+ # @param obj [#to_a] the data object to build the new message arround.
57
+ # @param timestamp [Float] the time that the message was first received.
58
+ # @return [Message]
59
+ def build(obj, timestamp)
60
+ data = obj.to_a
61
+ klass =
62
+ if obj.is_a?(Message) && obj.valid?
63
+ return obj if obj.timestamp == timestamp
64
+ obj.class
65
+ else
66
+ matcher = lookup data
67
+ matcher ? matcher.klass : Message::Unknown
68
+ end
69
+
70
+ klass.new data, timestamp
71
+ end
72
+
73
+ private
74
+
75
+ def lookup(data)
76
+ status = data[0] >> 4
77
+ entry = @lookup_table[status]
78
+ matcher = entry&.find { |m| m.match? data }
79
+
80
+ unless matcher
81
+ matcher = @matchers.find { |m| m.match? data }
82
+ add_to_lookup matcher, data if matcher
83
+ end
84
+
85
+ matcher
86
+ end
87
+
88
+ def add_to_lookup(matcher, data)
89
+ status = data[0] >> 4
90
+ entry = @lookup_table[status]
91
+
92
+ if entry
93
+ entry << matcher
94
+ else
95
+ @lookup_table[status] = [matcher]
96
+ end
97
+ end
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,42 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'forwardable'
4
+
5
+ module Vissen
6
+ module Input
7
+ # Whenever a callable object wants to receive messages from a broker it is
8
+ # wrapped inside of a subscription. A subscription is a basic immutable
9
+ # value object with a minimal api that keeps track of three things: a
10
+ # Matcher, a callable handler and a numeric priority.
11
+ class Subscription
12
+ extend Forwardable
13
+
14
+ # @return [Integer] the subscription priority.
15
+ attr_reader :priority
16
+
17
+ # @!method match?(message)
18
+ # This method is forwarded to `Message#match?`.
19
+ #
20
+ # @return [true, false] (see Matcher#match?).
21
+ def_delegator :@matcher, :match?, :match?
22
+
23
+ # @!method handle(message)
24
+ # Calls the registered handler with the given message.
25
+ #
26
+ # @return (see Matcher#match?)
27
+ def_delegator :@handler, :call, :handle
28
+
29
+ # @param matcher [#match?] the matcher to use when filtering messages.
30
+ # @param handler [#call] the target of the subscription.
31
+ # @param priority [Integer] the priority of the subscription relative
32
+ # other subscriptions.
33
+ def initialize(matcher, handler, priority)
34
+ @matcher = matcher
35
+ @handler = handler
36
+ @priority = priority
37
+
38
+ freeze
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Vissen
4
+ module Input
5
+ # The Vissen Input library version number.
6
+ VERSION = '0.2.2'
7
+ end
8
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'vissen/input/version'
4
+
5
+ require 'vissen/input/matcher'
6
+ require 'vissen/input/message_factory'
7
+ require 'vissen/input/subscription'
8
+ require 'vissen/input/broker'
9
+
10
+ require 'vissen/input/message'
11
+ require 'vissen/input/message/base'
12
+ require 'vissen/input/message/aftertouch'
13
+ require 'vissen/input/message/channel_pressure'
14
+ require 'vissen/input/message/channel_mode'
15
+ require 'vissen/input/message/control_change'
16
+ require 'vissen/input/message/note'
17
+ require 'vissen/input/message/pitch_bend_change'
18
+ require 'vissen/input/message/program_change'
19
+ require 'vissen/input/message/unknown'
20
+
21
+ module Vissen
22
+ # Input
23
+ #
24
+ # This module includes all the input messages that can be sent to the vissen
25
+ # engine, as well as some facilities for converting them to and from their
26
+ # binary form.
27
+ module Input
28
+ end
29
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+
5
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
6
+ require 'vissen/input/version'
7
+
8
+ Gem::Specification.new do |spec|
9
+ spec.name = 'vissen-input'
10
+ spec.version = Vissen::Input::VERSION
11
+ spec.authors = ['Sebastian Lindberg']
12
+ spec.email = ['seb.lindberg@gmail.com']
13
+
14
+ spec.summary = 'The input side of the vissen system.'
15
+ spec.description = 'This gem implements the input messages and message ' \
16
+ 'matching engine used in the vissen project.'
17
+ spec.homepage = 'https://github.com/midi-visualizer/vissen-input'
18
+ spec.license = 'MIT'
19
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
20
+ f.match(%r{^(test|spec|features)/})
21
+ end
22
+
23
+ spec.bindir = 'exe'
24
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
25
+ spec.require_paths = ['lib']
26
+
27
+ spec.add_development_dependency 'bundler', '~> 1.16'
28
+ spec.add_development_dependency 'minitest', '~> 5.0'
29
+ spec.add_development_dependency 'rake', '~> 10.0'
30
+ spec.add_development_dependency 'rubocop', '~> 0.52'
31
+ spec.add_development_dependency 'simplecov', '~> 0.16'
32
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vissen-input
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.2
5
+ platform: ruby
6
+ authors:
7
+ - Sebastian Lindberg
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: minitest
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.0'
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: rubocop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.52'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.52'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.16'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.16'
83
+ description: This gem implements the input messages and message matching engine used
84
+ in the vissen project.
85
+ email:
86
+ - seb.lindberg@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rubocop.yml"
93
+ - ".travis.yml"
94
+ - CHANGELOG.md
95
+ - Gemfile
96
+ - Gemfile.lock
97
+ - LICENSE.txt
98
+ - README.md
99
+ - Rakefile
100
+ - bin/console
101
+ - bin/setup
102
+ - lib/vissen/input.rb
103
+ - lib/vissen/input/broker.rb
104
+ - lib/vissen/input/matcher.rb
105
+ - lib/vissen/input/message.rb
106
+ - lib/vissen/input/message/aftertouch.rb
107
+ - lib/vissen/input/message/base.rb
108
+ - lib/vissen/input/message/channel_mode.rb
109
+ - lib/vissen/input/message/channel_pressure.rb
110
+ - lib/vissen/input/message/control_change.rb
111
+ - lib/vissen/input/message/note.rb
112
+ - lib/vissen/input/message/pitch_bend_change.rb
113
+ - lib/vissen/input/message/program_change.rb
114
+ - lib/vissen/input/message/unknown.rb
115
+ - lib/vissen/input/message_factory.rb
116
+ - lib/vissen/input/subscription.rb
117
+ - lib/vissen/input/version.rb
118
+ - vissen-input.gemspec
119
+ homepage: https://github.com/midi-visualizer/vissen-input
120
+ licenses:
121
+ - MIT
122
+ metadata: {}
123
+ post_install_message:
124
+ rdoc_options: []
125
+ require_paths:
126
+ - lib
127
+ required_ruby_version: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ required_rubygems_version: !ruby/object:Gem::Requirement
133
+ requirements:
134
+ - - ">="
135
+ - !ruby/object:Gem::Version
136
+ version: '0'
137
+ requirements: []
138
+ rubyforge_project:
139
+ rubygems_version: 2.7.3
140
+ signing_key:
141
+ specification_version: 4
142
+ summary: The input side of the vissen system.
143
+ test_files: []