class MMPlayer::Context

DSL context for interfacing an instance of MPlayer with MIDI

Attributes

midi[R]
player[R]

Public Class Methods

new(midi_input, options = {}, &block) click to toggle source

@param [UniMIDI::Input, Array<UniMIDI::Input>] midi_input @param [Hash] options @option options [Integer] :midi_buffer_length Length of MIDI message buffer in seconds @option options [String] :mplayer_flags The command-line flags to invoke MPlayer with @option options [Integer] :receive_channel (also: :rx_channel) A MIDI channel to subscribe to. By default, responds to all @yield

# File lib/mmplayer/context.rb, line 18
def initialize(midi_input, options = {}, &block)
  midi_options = {
    :buffer_length => options[:midi_buffer_length],
    :receive_channel => options[:receive_channel] || options[:rx_channel]
  }
  @midi = MIDI.new(midi_input, midi_options)
  @player = Player.new(:flags => options[:mplayer_flags])
  instance_eval(&block) if block_given?
end

Public Instance Methods

start(options = {}) click to toggle source

Start listening for MIDI Note that MPlayer will start when Context#play (aka Instructions::Player#play) is called @param [Hash] options @option options [Boolean] :background Whether to run in a background thread @return [Boolean]

# File lib/mmplayer/context.rb, line 33
def start(options = {})
  @midi.start
  @playback_thread = playback_loop
  @playback_thread.join unless !!options[:background]
  true
end
stop() click to toggle source

Stop the player @return [Boolean]

# File lib/mmplayer/context.rb, line 42
def stop
  @midi.stop
  @player.quit
  @playback_thread.kill
  true
end

Private Instance Methods

playback_loop() click to toggle source

Main playback loop

# File lib/mmplayer/context.rb, line 52
def playback_loop
  ::MMPlayer::Thread.new(:timeout => false) do
    until @player.active?
      sleep(0.1)
    end
    @player.playback_loop
  end
end