Class: MMPlayer::Context

Inherits:
Object
  • Object
show all
Includes:
Helper::Numbers, Instructions::MIDI, Instructions::Player
Defined in:
lib/mmplayer/context.rb

Overview

DSL context for interfacing an instance of MPlayer with MIDI

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Instructions::Player

#on_end_of_file, #on_progress

Methods included from Instructions::MIDI

#on_cc, #on_note, #on_system, #receive_channel

Methods included from Helper::Numbers

#to_midi_value, #to_percent

Constructor Details

#initialize(midi_input, options = {}) { ... } ⇒ Context

Returns a new instance of Context.

Parameters:

  • midi_input (UniMIDI::Input, Array<UniMIDI::Input>)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :midi_buffer_length (Integer)

    Length of MIDI message buffer in seconds

  • :mplayer_flags (String)

    The command-line flags to invoke MPlayer with

  • :receive_channel (Integer) — default: also: :rx_channel

    A MIDI channel to subscribe to. By default, responds to all

Yields:



18
19
20
21
22
23
24
25
26
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class MMPlayer::Instructions::Player

Instance Attribute Details

#midiObject (readonly)

Returns the value of attribute midi.



10
11
12
# File 'lib/mmplayer/context.rb', line 10

def midi
  @midi
end

#playerObject (readonly)

Returns the value of attribute player.



10
11
12
# File 'lib/mmplayer/context.rb', line 10

def player
  @player
end

Instance Method Details

#start(options = {}) ⇒ Boolean

Start listening for MIDI Note that MPlayer will start when Context#play (aka Instructions::Player#play) is called

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :background (Boolean)

    Whether to run in a background thread

Returns:

  • (Boolean)


33
34
35
36
37
38
# File 'lib/mmplayer/context.rb', line 33

def start(options = {})
  @midi.start
  @playback_thread = playback_loop
  @playback_thread.join unless !!options[:background]
  true
end

#stopBoolean

Stop the player

Returns:

  • (Boolean)


42
43
44
45
46
47
# File 'lib/mmplayer/context.rb', line 42

def stop
  @midi.stop
  @player.quit
  @playback_thread.kill
  true
end