Module: AlsaRawMIDI::API::Soundcard

Defined in:
lib/alsa-rawmidi/api.rb

Overview

Wrapper for ALSA methods dealing with the soundcard and subdevices

Class Method Summary collapse

Class Method Details

.exists?(id) ⇒ Boolean

Does a soundcard exist for the given id?

Parameters:

  • id (Integer)

Returns:

  • (Boolean)


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

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

.get_device_ids(id) ⇒ Array<Integer>

Parameters:

  • id (Integer)

Returns:

  • (Array<Integer>)


397
398
399
400
401
402
403
# File 'lib/alsa-rawmidi/api.rb', line 397

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) ⇒ Integer

Parameters:

  • soundcard_id (Integer)

Returns:

  • (Integer)


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

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

Parameters:

  • soundcard_id (Integer)

Returns:

  • (String)


407
408
409
# File 'lib/alsa-rawmidi/api.rb', line 407

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

.get_subdevice_count(info) ⇒ Integer

Parameters:

Returns:

  • (Integer)


341
342
343
344
345
# File 'lib/alsa-rawmidi/api.rb', line 341

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

Parameters:

  • device_num (Integer, String)
  • subdev_count (Integer)
  • id (Integer)

Returns:

  • (String)


351
352
353
354
355
# File 'lib/alsa-rawmidi/api.rb', line 351

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}#{ext}"
end

.get_subdevices(direction, soundcard_id, device_id) ⇒ Array<Object>

Parameters:

  • soundcard_id (Integer)
  • device_id (Integer)
  • direction (Symbol)
  • block (Proc)

Returns:

  • (Array<Object>)


371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
# File 'lib/alsa-rawmidi/api.rb', line 371

def get_subdevices(direction, soundcard_id, device_id)
  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

Parameters:

Returns:

  • (Boolean)


361
362
363
364
# File 'lib/alsa-rawmidi/api.rb', line 361

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