Skip to content

Commit

Permalink
new selectors (with open), coremidi api changes, version
Browse files Browse the repository at this point in the history
  • Loading branch information
arirusso committed Sep 3, 2011
1 parent c39cc3f commit 00d1e91
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 37 deletions.
5 changes: 3 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dir = File.dirname(File.expand_path(__FILE__))
$LOAD_PATH.unshift dir
$:.unshift File.join( File.dirname( __FILE__ ))
$:.unshift File.join( File.dirname( __FILE__ ), 'lib')


require 'rake'
require 'rake/testtask'
Expand Down
2 changes: 1 addition & 1 deletion examples/output.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# UniMIDI::Device.all.to_s will list your midi devices
# or amidi -l from the command line

UniMIDI::Output.first.open do |output|
UniMIDI::Output.first do |output|

(0..((octaves-1)*12)).step(12) do |oct|

Expand Down
2 changes: 1 addition & 1 deletion lib/unimidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

module UniMIDI

VERSION = "0.2.2"
VERSION = "0.2.3"

end

Expand Down
2 changes: 2 additions & 0 deletions lib/unimidi/adapter/alsa-rawmidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module AlsaRawMIDIAdapter

class Input < CongruousApiInput
defer_to AlsaRawMIDI::Input
device_class AlsaRawMIDI::Device
end

class Output < CongruousApiOutput
defer_to AlsaRawMIDI::Output
device_class AlsaRawMIDI::Device
end

class Device < CongruousApiDevice
Expand Down
15 changes: 13 additions & 2 deletions lib/unimidi/adapter/ffi-coremidi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,28 @@ module UniMIDI
module CoreMIDIAdapter

class Input < CongruousApiInput
defer_to CoreMIDI::Input
defer_to CoreMIDI::Source
device_class CoreMIDI::Endpoint
end

class Output < CongruousApiOutput
defer_to CoreMIDI::Output
defer_to CoreMIDI::Destination
device_class CoreMIDI::Endpoint
end

class Device < CongruousApiDevice
defer_to CoreMIDI::Endpoint
input_class Input
output_class Output

def self.populate
klass = @deference[self].respond_to?(:all_by_type) ? @deference[self] : @device_class
@devices = {
:input => klass.all_by_type[:source].map { |d| @input_class.new(d) },
:output => klass.all_by_type[:destination].map { |d| @output_class.new(d) }
}
end

end

end
Expand Down
2 changes: 2 additions & 0 deletions lib/unimidi/adapter/midi-jruby.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module MIDIJRubyAdapter

class Input < CongruousApiInput
defer_to MIDIJRuby::Input
device_class MIDIJRuby::Device
end

class Output < CongruousApiOutput
defer_to MIDIJRuby::Output
device_class MIDIJRuby::Device
end

class Device < CongruousApiDevice
Expand Down
2 changes: 2 additions & 0 deletions lib/unimidi/adapter/midi-winmm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ module MIDIWinMMAdapter

class Input < CongruousApiInput
defer_to MIDIWinMM::Input
device_class MIDIWinMM::Device
end

class Output < CongruousApiOutput
defer_to MIDIWinMM::Output
device_class MIDIWinMM::Device
end

class Device < CongruousApiDevice
Expand Down
57 changes: 38 additions & 19 deletions lib/unimidi/congruous_api_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def initialize(device_obj)
@name = @device.name
@type = @device.type
end

def enabled?
@device.enabled
end

# enable the device for use, can be passed a block to which the device will be passed back
def open(*a, &block)
Expand Down Expand Up @@ -47,13 +51,13 @@ def self.included(base)
module ClassMethods

# returns the first device for this class
def first(*a, &block)
use(@deference[self].first(*a), &block)
def first(&block)
use_device(all.first, &block)
end

# returns the last device for this class
def last(*a, &block)
use(@deference[self].last(*a), &block)
def last(&block)
use_device(all.last, &block)
end

# returns all devices in an array
Expand All @@ -62,25 +66,29 @@ def all
end

# returns the device at <em>index</em>
def [](index, &block)
d = all[index]
d.open(&block) unless block.nil?
d
def use(index, &block)
use_device(all[index], &block)
end

def [](index)
all[index]
end

# returns all devices as a hash as such
# { :input => [input devices], :output => [output devices] }
def all_by_type
{
:input => @deference[self].all_by_type[:input].map { |d| @input_class.new(d) },
:output => @deference[self].all_by_type[:output].map { |d| @output_class.new(d) }
}
ensure_initialized
@devices
end

def defer_to(klass)
@deference ||= {}
@deference[self] = klass
end

def device_class(klass)
@device_class = klass
end

def input_class(klass)
@input_class = klass
Expand All @@ -90,13 +98,24 @@ def output_class(klass)
@output_class = klass
end

def populate
klass = @deference[self].respond_to?(:all_by_type) ? @deference[self] : @device_class
@devices = {
:input => klass.all_by_type[:input].map { |d| @input_class.new(d) },
:output => klass.all_by_type[:output].map { |d| @output_class.new(d) }
}
end
alias_method :refresh, :populate

private

def use(dev, &block)
raise 'Device not found' if dev.nil?
d = new(dev)
d.open(&block) unless block.nil?
d
def ensure_initialized
populate if @devices.nil?
end

def use_device(device, &block)
device.open(&block) unless block.nil?
device
end

end
Expand Down Expand Up @@ -184,7 +203,7 @@ def gets_buffer_data(*a)

# returns all inputs
def self.all
@deference[self].all.map { |d| new(d) }
UniMIDI::Device.all_by_type[:input]
end

end
Expand Down Expand Up @@ -232,7 +251,7 @@ def puts_bytes(*a)

# returns all outputs
def self.all
@deference[self].all.map { |d| new(d) }
UniMIDI::Device.all_by_type[:output]
end

end
Expand Down
21 changes: 12 additions & 9 deletions test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,19 @@ def self.select_devices
end
end

def platform_test(adapter, mod)
def platform_test(adapter, mod, device_class = nil, input_class = nil, output_class = nil)
device_class ||= mod::Device
input_class ||= mod::Input
output_class ||= mod::Output
assert_equal(adapter, UniMIDI::Platform.instance.interface)
assert_not_same(mod::Input, UniMIDI::Input)
assert_not_same(mod::Output, UniMIDI::Output)
assert_not_same(mod::Device, UniMIDI::Device)
assert_equal(mod::Input.first.name, UniMIDI::Input.first.name)
assert_equal(mod::Input.first.id, UniMIDI::Input.first.id)
assert_not_same(mod::Output.first, UniMIDI::Output.first)
assert_equal(mod::Output.first.name, UniMIDI::Output.first.name)
assert_equal(mod::Output.first.id, UniMIDI::Output.first.id)
assert_not_same(input_class, UniMIDI::Input)
assert_not_same(output_class, UniMIDI::Output)
assert_not_same(device_class, UniMIDI::Device)
assert_equal(input_class.first.name, UniMIDI::Input.first.name)
assert_equal(input_class.first.id, UniMIDI::Input.first.id)
assert_not_same(output_class.first, UniMIDI::Output.first)
assert_equal(output_class.first.name, UniMIDI::Output.first.name)
assert_equal(output_class.first.id, UniMIDI::Output.first.id)
end

def bytestrs_to_ints(arr)
Expand Down
2 changes: 2 additions & 0 deletions test/test_input_buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ def test_input_buffer

$test_device[:output].open do |output|
$test_device[:input].open do |input|

input.buffer.clear

messages.each do |msg|

Expand Down
8 changes: 6 additions & 2 deletions test/test_io.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ def test_full_io
pointer = 0
$test_device[:output].open do |output|
$test_device[:input].open do |input|


input.buffer.clear

messages.each do |msg|

$>.puts "sending: " + msg.inspect
Expand Down Expand Up @@ -87,7 +89,9 @@ def test_full_io_objects
pointer = 0
$test_device[:output].open do |output|
$test_device[:input].open do |input|


#input.buffer.clear

messages.each do |msg|

$>.puts "sending: " + msg.inspect
Expand Down
2 changes: 1 addition & 1 deletion test/test_platform.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_linux

def test_osx
if RUBY_PLATFORM.include?("darwin")
platform_test(CoreMIDIAdapter, CoreMIDI)
platform_test(CoreMIDIAdapter, CoreMIDI, CoreMIDI::Endpoint, CoreMIDI::Source, CoreMIDI::Destination)
end
end

Expand Down
56 changes: 56 additions & 0 deletions test/test_selectors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env ruby

require 'helper'

class SelectorTest < Test::Unit::TestCase

include UniMIDI
include TestHelper

def test_first
i = Input.first
assert_equal(Input.all.first, i)
end

def test_last
i = Input.last
assert_equal(Input.all.last, i)
end

def test_first_with_block
sleep(1)
i = Input.first do |i|
assert_equal(true, i.enabled?)
end
assert_equal(Input.all.first, i)
end

def test_last_with_block
sleep(1)
i = Input.last do |i|
assert_equal(true, i.enabled?)
end
assert_equal(Input.all.last, i)
end

def test_brackets
i = Input[0]
assert_equal(Input.first, i)
assert_equal(Input.all.first, i)
end

def test_use_with_block
sleep(1)
i = Input.use(0) do |i|
assert_equal(true, i.enabled?)
end
assert_equal(Input.first, i)
assert_equal(Input.all.first, i)
end

def test_all
i = Input.all
assert_equal(Device.all_by_type[:input], Input.all)
end

end

0 comments on commit 00d1e91

Please sign in to comment.