Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added ability to open input/output devices with parameter :input_devi…

…ce_id/:output_device_id
  • Loading branch information...
commit 55375aa31f9606f206136fca497789c2315ceb81 1 parent e0c44c2
@thomasjachmann authored
Showing with 22 additions and 9 deletions.
  1. +11 −8 lib/launchpad/device.rb
  2. +11 −1 test/test_device.rb
View
19 lib/launchpad/device.rb
@@ -12,6 +12,8 @@ class Device
# Initializes the launchpad
# {
+ # :input_device_id => ID of the MIDI input device to use, optional, :device_name will be used if omitted
+ # :output_device_id => ID of the MIDI output device to use, optional, :device_name will be used if omitted
# :device_name => Name of the MIDI device to use, optional, defaults to Launchpad
# :input => true/false, whether to use MIDI input for user interaction, optional, defaults to true
# :output => true/false, whether to use MIDI output for data display, optional, defaults to true
@@ -25,8 +27,8 @@ def initialize(opts = nil)
Portmidi.start
- @input = device(Portmidi.input_devices, Portmidi::Input, :name => opts[:device_name]) if opts[:input]
- @output = device(Portmidi.output_devices, Portmidi::Output, :name => opts[:device_name]) if opts[:output]
+ @input = device(Portmidi.input_devices, Portmidi::Input, :id => opts[:input_device_id], :name => opts[:device_name]) if opts[:input]
+ @output = device(Portmidi.output_devices, Portmidi::Output, :id => opts[:output_device_id], :name => opts[:device_name]) if opts[:output]
reset if output_enabled?
end
@@ -183,13 +185,14 @@ def read_pending_actions
def device(devices, device_type, opts)
id = opts[:id]
- device = devices.select {|device| device.name == opts[:name]}.first
- raise NoSuchDeviceError.new("MIDI device #{opts[:name]} doesn't exist") if device.nil?
- begin
- device_type.new(device.device_id)
- rescue RuntimeError => e
- raise DeviceBusyError.new(e)
+ if id.nil?
+ device = devices.select {|device| device.name == opts[:name]}.first
+ id = device.device_id unless device.nil?
end
+ raise NoSuchDeviceError.new("MIDI device #{opts[:id] || opts[:name]} doesn't exist") if id.nil?
+ device_type.new(id)
+ rescue RuntimeError => e
+ raise DeviceBusyError.new(e)
end
def input
View
12 test/test_device.rb
@@ -73,7 +73,7 @@ def stub_input(device, *args)
assert_nil d.instance_variable_get('@output')
end
- should 'initialize the correct input output devices' do
+ should 'initialize the correct input output devices when specified by name' do
Portmidi.stubs(:input_devices).returns(mock_devices(:id => 4, :name => 'Launchpad Name'))
Portmidi.stubs(:output_devices).returns(mock_devices(:id => 5, :name => 'Launchpad Name'))
d = Launchpad::Device.new(:device_name => 'Launchpad Name')
@@ -83,6 +83,16 @@ def stub_input(device, *args)
assert_equal 5, output.device_id
end
+ should 'initialize the correct input output devices when specified by id' do
+ Portmidi.stubs(:input_devices).returns(mock_devices(:id => 4))
+ Portmidi.stubs(:output_devices).returns(mock_devices(:id => 5))
+ d = Launchpad::Device.new(:input_device_id => 4, :output_device_id => 5, :device_name => 'nonexistant')
+ assert_equal Portmidi::Input, (input = d.instance_variable_get('@input')).class
+ assert_equal 4, input.device_id
+ assert_equal Portmidi::Output, (output = d.instance_variable_get('@output')).class
+ assert_equal 5, output.device_id
+ end
+
should 'raise NoSuchDeviceError when requested input device does not exist' do
assert_raise Launchpad::NoSuchDeviceError do
Portmidi.stubs(:input_devices).returns(mock_devices(:name => 'Launchpad Input'))
Please sign in to comment.
Something went wrong with that request. Please try again.