Skip to content

Commit

Permalink
Use byte arrays instead of strings for passing around data. All tests…
Browse files Browse the repository at this point in the history
… now pass for both Ruby 1.8.X and 1.9.X.
  • Loading branch information
Jim Menard committed Jan 5, 2009
1 parent f59dbc9 commit 64069b6
Show file tree
Hide file tree
Showing 10 changed files with 150 additions and 84 deletions.
5 changes: 5 additions & 0 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,11 @@ non-commercial purposes as long as the author is given credit.

=== Recent Changes

==== Changes for 1.2.0:

Use byte arrays instead of strings for passing around data. All tests now pass
for both Ruby 1.8.X and 1.9.X.

==== New code repository

The midilib code is now hosted at Github (http://github.com/jimm/midilib).
Expand Down
2 changes: 0 additions & 2 deletions TODO.rdoc
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
== Bugs

* IOTester#test_read_and_write fails under Ruby 1.9.

* Track#recalc_delta_from_times needs to use a stable sorting algorithm. See
Adam's notes at http://github.com/adamjmurray/cosy/wikis/midilib-notes and
solution at
Expand Down
2 changes: 1 addition & 1 deletion install.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# This script installs midilib into the Ruby site-local library directory.
#
# Author:: Jim Menard (mailto:[email protected])
# Copyright:: Copyright (c) 2003-2008 by Jim Menard
# Copyright:: Copyright (c) 2003-2009 by Jim Menard
# License:: Distributed under the same license as Ruby.
#

Expand Down
44 changes: 16 additions & 28 deletions lib/midilib/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,10 @@ def note_to_s
end

def data_as_bytes
data = ''
data = []
data << (@status + @channel)
data << @note
data << @velocity
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end
end

Expand Down Expand Up @@ -246,11 +245,10 @@ def initialize(channel = 0, controller = 0, value = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << (@status + @channel)
data << @controller
data << @value
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -268,10 +266,9 @@ def initialize(channel = 0, program = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << (@status + @channel)
data << @program
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -288,10 +285,9 @@ def initialize(channel = 0, pressure = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << (@status + @channel)
data << @pressure
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -308,11 +304,10 @@ def initialize(channel = 0, value = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << (@status + @channel)
data << (@value & 0x7f) # lsb
data << ((@value >> 7) & 0x7f) # msb
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -336,12 +331,12 @@ def initialize(data, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << @status
data << Utils.as_var_len(@data.length)
data << @data
data << EOX
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
data.flatten
end

def to_s
Expand All @@ -358,11 +353,10 @@ def initialize(pointer = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << @status
data << ((@pointer >> 8) & 0xff)
data << (@pointer & 0xff)
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -379,10 +373,9 @@ def initialize(song = 0, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << @status
data << @song
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -396,9 +389,8 @@ def initialize(delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << @status
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -413,9 +405,8 @@ def initialize(status, delta_time)
end

def data_as_bytes
data = ''
data = []
data << @status
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand Down Expand Up @@ -490,12 +481,12 @@ def initialize(meta_type, data = nil, delta_time = 0)
end

def data_as_bytes
data = ''
data = []
data << @status
data << @meta_type
data << (@data ? Utils.as_var_len(@data.length) : 0)
data << @data if @data
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
data.flatten
end

def to_s
Expand Down Expand Up @@ -573,14 +564,13 @@ def tempo=(val)
end

def data_as_bytes
data = ''
data = []
data << @status
data << @meta_type
data << 3
data << ((@data >> 16) & 0xff)
data << ((@data >> 8) & 0xff)
data << (@data & 0xff)
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

def to_s
Expand All @@ -599,15 +589,14 @@ def initialize(numer, denom, clocks, qnotes, delta_time = 0)

# Returns the complete event as stored in the sequence
def data_as_bytes
data = ''
data = []
data << @status
data << @meta_type
data << 4
data << @data[0]
data << @data[1]
data << @data[2]
data << @data[3]
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

# Calculates the duration (in ticks) for a full measure
Expand Down Expand Up @@ -650,13 +639,12 @@ def initialize(sharpflat, is_minor, delta_time = 0)

# Returns the complete event as stored in the sequence
def data_as_bytes
data = ''
data = []
data << @status
data << @meta_type
data << 2
data << @data[0]
data << (@data[1] ? 1 : 0)
RUBY_VERSION >= '1.9' ? data.bytes.to_a : data
end

# Returns true if it's a minor key, false if major key
Expand Down
4 changes: 2 additions & 2 deletions lib/midilib/info.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module MIDI

VERSION_MAJOR = 1
VERSION_MINOR = 1
VERSION_TWEAK = 5
VERSION_MINOR = 2
VERSION_TWEAK = 0
Version = "#{VERSION_MAJOR}.#{VERSION_MINOR}.#{VERSION_TWEAK}"
Copyright = 'Copyright (c) 2003-2009 by Jim Menard <[email protected]>'

Expand Down
Loading

0 comments on commit 64069b6

Please sign in to comment.