Module: AlsaRawMIDI::API::Soundcard

Extended by:
Soundcard
Included in:
Soundcard
Defined in:
lib/alsa-rawmidi/api.rb

Overview

Wrapper for ALSA methods dealing with the soundcard and subdevices

Instance Method Summary collapse

Instance Method Details

#exists?(id) ⇒ Boolean

Does a soundcard exist for the given id?



420
421
422
# File 'lib/alsa-rawmidi/api.rb', line 420

def exists?(id)
  API.snd_card_load(id) == 1
end

#get_device_ids(id) ⇒ Array<Fixnum>



403
404
405
406
407
408
409
# 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) ⇒ Fixnum



426
427
428
429
430
# 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) ⇒ String



413
414
415
# File 'lib/alsa-rawmidi/api.rb', line 413

def get_name(soundcard_id)
  "hw:#{soundcard_id.to_s}"
end

#get_subdevice_count(info) ⇒ Fixnum



347
348
349
350
351
# 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) ⇒ String



357
358
359
360
361
# 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, &block) ⇒ Array<Object>



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
# 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) ⇒ Boolean



367
368
369
370
# 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