midi-message 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/lib/midi-message.rb CHANGED
@@ -8,7 +8,7 @@ module MIDIMessage
8
8
  module Process
9
9
  end
10
10
 
11
- VERSION = "0.2.2"
11
+ VERSION = "0.2.3"
12
12
 
13
13
  end
14
14
 
@@ -17,25 +17,24 @@ module MIDIMessage
17
17
  EndByte = 0xF7
18
18
 
19
19
  # an array of message parts. multiple byte parts will be represented as an array of bytes
20
- def to_a
20
+ def to_a(options = {})
21
+ omit = options[:omit] || []
21
22
  # this may need to be cached when properties are updated
22
23
  # might be worth benchmarking
23
24
  [
24
25
  self.class::StartByte,
25
- @node.manufacturer_id,
26
- @node.device_id, # (@device_id || @node.device_id) ?? dunno
27
- @node.model_id,
28
- type_byte,
29
- [address].flatten,
30
- [value].flatten,
31
- checksum,
26
+ (@node.to_a(options) unless @node.nil? || omit.include?(:node)),
27
+ (type_byte unless omit.include?(:type)),
28
+ [address].compact.flatten,
29
+ [value].compact.flatten,
30
+ (checksum unless omit.include?(:checksum)),
32
31
  self.class::EndByte
33
- ]
32
+ ].compact
34
33
  end
35
34
 
36
35
  # a flat array of message bytes
37
- def to_numeric_byte_array
38
- to_a.flatten
36
+ def to_numeric_byte_array(options = {})
37
+ to_a(options).flatten
39
38
  end
40
39
  alias_method :to_numeric_bytes, :to_numeric_byte_array
41
40
  alias_method :to_byte_array, :to_numeric_byte_array
@@ -81,7 +80,7 @@ module MIDIMessage
81
80
 
82
81
  include Base
83
82
 
84
- attr_reader :data
83
+ attr_accessor :data
85
84
  alias_method :value, :data
86
85
  #alias_method :value=, :data=
87
86
 
@@ -130,6 +129,33 @@ module MIDIMessage
130
129
  end
131
130
 
132
131
  end
132
+
133
+ # A SysEx message with no implied type
134
+ #
135
+ class Message
136
+
137
+ include Base
138
+
139
+ attr_accessor :data
140
+
141
+ def initialize(data, options = {})
142
+ @data = (data.kind_of?(Array) && data.length.eql?(1)) ? data[0] : data
143
+ initialize_sysex(nil, options)
144
+ end
145
+
146
+ # an array of message parts. multiple byte parts will be represented as an array of bytes
147
+ def to_a(options = {})
148
+ omit = options[:omit] || []
149
+ # this may need to be cached when properties are updated
150
+ # might be worth benchmarking
151
+ [
152
+ self.class::StartByte,
153
+ @data,
154
+ self.class::EndByte
155
+ ].compact
156
+ end
157
+
158
+ end
133
159
 
134
160
  #
135
161
  # The SystemExclusive::Node represents a destination for a message. For example a hardware
@@ -145,6 +171,15 @@ module MIDIMessage
145
171
  @model_id = options[:model_id]
146
172
  @manufacturer_id = manufacturer.kind_of?(Numeric) ? manufacturer : Constant.find("Manufacturer", manufacturer).value
147
173
  end
174
+
175
+ def to_a(options = {})
176
+ omit = options[:omit] || []
177
+ [
178
+ (@manufacturer_id unless omit.include?(:manufacturer) || omit.include?(:manufacturer_id)),
179
+ (@device_id unless omit.include?(:device) || omit.include?(:device_id)),
180
+ (@model_id unless omit.include?(:model) || omit.include?(:model_id))
181
+ ].compact
182
+ end
148
183
 
149
184
  # this message takes a prototype message, copies it, and returns the copy with its node set
150
185
  # to this node
@@ -177,18 +212,24 @@ module MIDIMessage
177
212
  end_status = bytes.pop
178
213
 
179
214
  return nil unless start_status.eql?(0xF0) && end_status.eql?(0xF7)
180
-
215
+
216
+ type_byte = bytes[3]
217
+
218
+ # if the 4th byte isn't status, we will just make this a Message object -- this may need some tweaking
219
+ if type_byte == 0x11
220
+ msg_class = Request
221
+ elsif type_byte == 0x12
222
+ msg_class = Command
223
+ else
224
+ return Message.new(bytes)
225
+ end
226
+
181
227
  fixed_length_message_part = bytes.slice!(0,7)
182
228
 
183
229
  manufacturer_id = fixed_length_message_part[0]
184
230
  device_id = fixed_length_message_part[1]
185
231
  model_id = fixed_length_message_part[2]
186
232
 
187
- msg_class = case fixed_length_message_part[3]
188
- when 0x11 then Request
189
- when 0x12 then Command
190
- end
191
-
192
233
  address = fixed_length_message_part.slice(4,3)
193
234
  checksum = bytes.slice!((bytes.length - 1), 1)
194
235
  value = bytes
@@ -19,8 +19,30 @@ class SystemExclusiveMessageTest < Test::Unit::TestCase
19
19
  assert_equal(0x41, node.manufacturer_id)
20
20
  assert_equal(0x42, node.model_id)
21
21
  assert_equal(0x10, node.device_id)
22
+ assert_equal([0x41, 0x10, 0x42], node.to_a)
22
23
  end
23
-
24
+
25
+ def test_no_node
26
+ msg = SystemExclusive::Command.new([0x40, 0x7F, 0x00], 0x10)
27
+ assert_equal([0xF0, 0x12, 0x40, 0x7F, 0x00, 0x10, 0x31, 0xF7], msg.to_bytes)
28
+ end
29
+
30
+ def test_node_without_model_id
31
+ node = SystemExclusive::Node.new(:Roland, :device_id => 0x10)
32
+ assert_equal(0x41, node.manufacturer_id)
33
+ assert_equal(nil, node.model_id)
34
+ assert_equal(0x10, node.device_id)
35
+ assert_equal([0x41, 0x10], node.to_a)
36
+ msg = SystemExclusive::Command.new([0x40, 0x7F, 0x00], 0x10, :node => node)
37
+ assert_equal(10, msg.to_bytes.size)
38
+ end
39
+
40
+ def test_parse_weird_message
41
+ msg = SystemExclusive.new(0xF0, 0x41, 0x12, 0x40, 0x00, 0x7F, 0x10, 0x41, 0xF7)
42
+ assert_equal([0x41, 0x12, 0x40, 0x00, 0x7F, 0x10, 0x41], msg.data)
43
+ assert_equal([0xF0, 0x41, 0x12, 0x40, 0x00, 0x7F, 0x10, 0x41, 0xF7], msg.to_bytes)
44
+ end
45
+
24
46
  def test_new
25
47
  msg = SystemExclusive.new(0xF0, 0x41, 0x10, 0x42, 0x12, 0x40, 0x00, 0x7F, 0x10, 0x41, 0xF7)
26
48
  assert_equal(0x41, msg.node.manufacturer_id)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: midi-message
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-09-10 00:00:00.000000000Z
12
+ date: 2011-09-12 00:00:00.000000000Z
13
13
  dependencies: []
14
14
  description: MIDI messages, objectified in Ruby
15
15
  email: