mmplayer 0.0.3 → 0.0.4

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: 1fed9ad028c5f486dafc21121e361ebba0891d1d
4
- data.tar.gz: 5305dd407a4542789cb8ebda7b5f57962e66cbf3
3
+ metadata.gz: 22ba4c7203b98702ef6771a78b984d725e563b53
4
+ data.tar.gz: 677cc0a18786f01cbb6de3d832e5d86f41628818
5
5
  SHA512:
6
- metadata.gz: 2413f416a0e7b22a7985cf90fe536da6e4758265e8e5dfe77435c2877bf92c67b52edea0c954e3fefe948e8f71410a84dba2f1b59f8dfd5151eae0949f440f5b
7
- data.tar.gz: 879d06f34a5770b915fa141bf53bcab43c367d3e6c310c3c444d092d6669c928600a75dee183d38df47e34a722a1ead26eee7ac0390702c09c01ac504b349896
6
+ metadata.gz: 4348a42a44f4d0a9b0a9619bc06fef9d8464cb166d725a1a81dd9f3bbe999265e04e90891b2e3da2cc39ef324b4e3bd290b12639c33e370e8e3179332c0bcbea
7
+ data.tar.gz: 4f87d5c157d78f51bceaf11cf1a210534261245f0178904daa391bc9c9800f947efe2e5ec669ed9105536444192473dc449e512df244e78a6951b972cdbe2f28
@@ -22,19 +22,19 @@ module MMPlayer
22
22
  alias_method :system, :on_system
23
23
 
24
24
  # Assign a callback for a given MIDI note
25
- # @param [Fixnum, String] note A MIDI note eg 64 "F4"
25
+ # @param [Fixnum, String] note A MIDI note eg 64 "F4" or nil for all
26
26
  # @param [Proc] callback The callback to execute when a matching message is received
27
27
  # @return [Hash]
28
- def on_note(note, &callback)
28
+ def on_note(note = nil, &callback)
29
29
  @midi.add_note_callback(note, &callback)
30
30
  end
31
31
  alias_method :note, :on_note
32
32
 
33
33
  # Assign a callback for the given MIDI control change
34
- # @param [Fixnum] index The MIDI control change index to assign the callback for
34
+ # @param [Fixnum] index The MIDI control change index to assign the callback for or nil for all
35
35
  # @param [Proc] callback The callback to execute when a matching message is received
36
36
  # @return [Hash]
37
- def on_cc(index, &callback)
37
+ def on_cc(index = nil, &callback)
38
38
  @midi.add_cc_callback(index, &callback)
39
39
  end
40
40
  alias_method :cc, :on_cc
@@ -13,9 +13,9 @@ module MMPlayer
13
13
  }
14
14
  end
15
15
 
16
- # Add a callback for a given MIDI system message type
16
+ # Add a callback for a given MIDI message type
17
17
  # @param [Symbol] type The MIDI message type (eg :note, :cc)
18
- # @param [String, Symbol] key
18
+ # @param [Fixnum, String] key The ID of the message eg note number/cc index
19
19
  # @param [Proc] callback The callback to execute when the given MIDI command is received
20
20
  # @return [Hash]
21
21
  def add_callback(type, key, &callback)
@@ -23,6 +23,16 @@ module MMPlayer
23
23
  @callback[type]
24
24
  end
25
25
 
26
+ # Add a callback for a given MIDI note
27
+ # @param [Symbol] type The MIDI message type (eg :note, :cc)
28
+ # @param [Fixnum, String] note
29
+ # @param [Proc] callback The callback to execute when the given MIDI command is received
30
+ # @return [Hash]
31
+ def add_note_callback(note, &callback)
32
+ note = MIDIMessage::Constant.value(:note, note) if note.kind_of?(String)
33
+ add_callback(:note, note, &callback)
34
+ end
35
+
26
36
  # Process a message for the given channel
27
37
  # @param [Fixnum, nil] channel
28
38
  # @param [MIDIMessage] message
@@ -39,7 +49,8 @@ module MMPlayer
39
49
  # @param [MIDIMessage] message
40
50
  # @return [Boolean, nil]
41
51
  def note_message(message)
42
- unless (callback = @callback[:note][message.note] || @callback[:note][message.name]).nil?
52
+ callback = callback = @callback[:note][message.note] || @callback[:note][nil]
53
+ unless callback.nil?
43
54
  callback.call(message.velocity)
44
55
  true
45
56
  end
@@ -49,7 +60,8 @@ module MMPlayer
49
60
  # @param [MIDIMessage] message
50
61
  # @return [Boolean, nil]
51
62
  def cc_message(message)
52
- unless (callback = @callback[:cc][message.index] || @callback[:cc][message.name]).nil?
63
+ callback = @callback[:cc][message.index] || @callback[:cc][nil]
64
+ unless callback.nil?
53
65
  callback.call(message.value)
54
66
  true
55
67
  end
data/lib/mmplayer/midi.rb CHANGED
@@ -23,15 +23,15 @@ module MMPlayer
23
23
  end
24
24
 
25
25
  # Add a callback for a given MIDI note
26
- # @param [Fixnum, String] note The MIDI note to add a callback for eg 64 "E4"
26
+ # @param [Fixnum, String, nil] note The MIDI note to add a callback for eg 64 "E4"
27
27
  # @param [Proc] callback The callback to execute when the given MIDI note is received
28
28
  # @return [Hash]
29
29
  def add_note_callback(note, &callback)
30
- @message_handler.add_callback(:note, note, &callback)
30
+ @message_handler.add_note_callback(note, &callback)
31
31
  end
32
32
 
33
33
  # Add a callback for a given MIDI control change
34
- # @param [Fixnum] index The MIDI control change index to add a callback for eg 10
34
+ # @param [Fixnum, nil] index The MIDI control change index to add a callback for eg 10
35
35
  # @param [Proc] callback The callback to execute when the given MIDI control change is received
36
36
  # @return [Hash]
37
37
  def add_cc_callback(index, &callback)
data/lib/mmplayer.rb CHANGED
@@ -24,7 +24,7 @@ require "mmplayer/player"
24
24
 
25
25
  module MMPlayer
26
26
 
27
- VERSION = "0.0.3"
27
+ VERSION = "0.0.4"
28
28
 
29
29
  # Shortcut to Context constructor
30
30
  def self.new(*args, &block)
@@ -27,16 +27,54 @@ class MMPlayer::MessageHandlerTest < Minitest::Test
27
27
  @callback.unstub(:call)
28
28
  end
29
29
 
30
- should "call callback" do
31
- assert @handler.send(:note_message, @message)
30
+ context "has catch-all callback" do
31
+
32
+ setup do
33
+ @var2 = nil
34
+ @catchall = proc { |vel| @var2 = vel }
35
+ @handler.add_callback(:note, nil, &@catchall)
36
+ @catchall.expects(:call).never
37
+ end
38
+
39
+ should "call specific callback" do
40
+ assert @handler.send(:note_message, @message)
41
+ end
42
+
43
+ end
44
+
45
+ context "no catch-all callback" do
46
+
47
+ should "call specific callback" do
48
+ assert @handler.send(:note_message, @message)
49
+ end
50
+
32
51
  end
33
52
 
34
53
  end
35
54
 
36
55
  context "callback doesn't exist" do
37
56
 
38
- should "do nothing" do
39
- refute @handler.send(:note_message, @message)
57
+ context "has catch-all callback" do
58
+
59
+ setup do
60
+ @var = nil
61
+ @callback = proc { |vel| @var = vel }
62
+ @handler.add_callback(:note, nil, &@callback)
63
+ @callback.expects(:call).once
64
+ end
65
+
66
+ should "call callback" do
67
+ assert @handler.send(:note_message, @message)
68
+ end
69
+
70
+ end
71
+
72
+ context "no catch-all callback" do
73
+
74
+ should "do nothing" do
75
+ refute @handler.send(:note_message, @message)
76
+ end
77
+
40
78
  end
41
79
 
42
80
  end
@@ -70,8 +108,27 @@ class MMPlayer::MessageHandlerTest < Minitest::Test
70
108
 
71
109
  context "callback doesn't exist" do
72
110
 
73
- should "do nothing" do
74
- refute @handler.send(:cc_message, @message)
111
+ context "has catch-all callback" do
112
+
113
+ setup do
114
+ @var = nil
115
+ @callback = proc { |vel| @var = vel }
116
+ @handler.add_callback(:cc, nil, &@callback)
117
+ @callback.expects(:call).once
118
+ end
119
+
120
+ should "call callback" do
121
+ assert @handler.send(:cc_message, @message)
122
+ end
123
+
124
+ end
125
+
126
+ context "no catch-all callback" do
127
+
128
+ should "do nothing" do
129
+ refute @handler.send(:cc_message, @message)
130
+ end
131
+
75
132
  end
76
133
 
77
134
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mmplayer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ari Russo