module AlsaRawMIDI::API::Soundcard

Wrapper for ALSA methods dealing with the soundcard and subdevices

Public Instance Methods

exists?(id) click to toggle source

Does a soundcard exist for the given id? @param [Integer] id @return [Boolean]

# File lib/alsa-rawmidi/api.rb, line 420
def exists?(id)
  API.snd_card_load(id) == 1
end
get_device_ids(id) click to toggle source

@param [Integer] id @return [Array<Integer>]

# File lib/alsa-rawmidi/api.rb, line 403
def get_device_ids(id)
  handle = API::Soundcard.get_handle(id)
  (0..31).to_a.select do |n|
    device_id = FFI::MemoryPointer.new(:int).write_int(n)
    API.snd_ctl_rawmidi_next_device(handle, device_id) >= 0
  end
end
get_handle(soundcard_id) click to toggle source

@param [Integer] soundcard_id @return [Integer]

# File lib/alsa-rawmidi/api.rb, line 426
def get_handle(soundcard_id)
  handle_pointer = FFI::MemoryPointer.new(FFI.type_size(:int))
  API.snd_ctl_open(handle_pointer, get_name(soundcard_id), 0)
  handle_pointer.read_int
end
get_name(soundcard_id) click to toggle source

@param [Integer] soundcard_id @return [String]

# File lib/alsa-rawmidi/api.rb, line 413
def get_name(soundcard_id)
  "hw:#{soundcard_id.to_s}"
end
get_subdevice_count(info) click to toggle source

@param [SndCtlCardInfo] info @return [Integer]

# File lib/alsa-rawmidi/api.rb, line 347
def get_subdevice_count(info)
  subdev_count = API.snd_rawmidi_info_get_subdevices_count(info.pointer)
  subdev_count = 0 if subdev_count > 32
  subdev_count
end
get_subdevice_id(soundcard_id, device_id, subdev_count, id) click to toggle source

@param [Integer, String] device_num @param [Integer] subdev_count @param [Integer] id @return [String]

# File lib/alsa-rawmidi/api.rb, line 357
def get_subdevice_id(soundcard_id, device_id, subdev_count, id)
  ext = (subdev_count > 1) ? ",#{id}" : ''
  name = API::Soundcard.get_name(soundcard_id)
  "#{name},#{device_id.to_s}#{ext}"
end
get_subdevices(direction, soundcard_id, device_id) { |device_hash| ... } click to toggle source

@param [Integer] soundcard_id @param [Integer] device_id @param [Symbol] direction @param [Proc] block @return [Array<Object>]

# File lib/alsa-rawmidi/api.rb, line 377
def get_subdevices(direction, soundcard_id, device_id, &block)
  handle = API::Soundcard.get_handle(soundcard_id)
  info = API::Device.get_info(device_id, direction)
  i = 0
  subdev_count = 1
  available = []
  while i <= subdev_count
    if API::Soundcard.valid_subdevice?(info, i, handle)
      subdev_count = API::Soundcard.get_subdevice_count(info) if i.zero?
      system_id = API::Soundcard.get_subdevice_id(soundcard_id, device_id, subdev_count, i)
      device_hash = {
        :id => system_id,
        :name => info[:name].to_s,
        :subname => info[:subname].to_s
      }
      available << yield(device_hash)
      i += 1
    else
      break
    end
  end
  available
end
valid_subdevice?(info, id, handle) click to toggle source

@param [SndCtlCardInfo] info @param [Integer] id @param [Integer] handle @return [Boolean]

# File lib/alsa-rawmidi/api.rb, line 367
def valid_subdevice?(info, id, handle)
  API.snd_rawmidi_info_set_subdevice(info.pointer, id)
  API.snd_ctl_rawmidi_info(handle, info.pointer) >= 0
end