Module: AlsaRawMIDI::API::Input

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

Overview

Wrapper for ALSA methods dealing with input

Constant Summary collapse

BUFFER_SIZE =
256

Class Method Summary collapse

Class Method Details

.open(id) ⇒ Integer

Open the output with the given ID

Parameters:

  • id (Integer)

Returns:

  • (Integer)


245
246
247
248
249
# File 'lib/alsa-rawmidi/api.rb', line 245

def open(id)
  API::Device.open(id) do |pointer|
    API.snd_rawmidi_open(pointer, nil, id, API::CONSTANTS[:SND_RAWMIDI_NONBLOCK])
  end
end

.poll(handle) ⇒ String

Get the next bytes from the buffer

Returns:

  • (String)


253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/alsa-rawmidi/api.rb', line 253

def poll(handle)
  buffer = FFI::MemoryPointer.new(:uint8, BUFFER_SIZE)
  if (err = API.snd_rawmidi_read(handle, buffer, BUFFER_SIZE)) < (0) && !err.eql?(-11)
    raise "Can't read MIDI input: #{API.snd_strerror(err)}"
  end

  # Upon success, err is positive and equal to the number of bytes read
  # into the buffer.
  if err.positive?
    bytes = buffer.get_bytes(0, err).unpack1('a*').unpack('H*')
    bytes.first.upcase
  end
end