Module: AudioPlayback

Defined in:
lib/audio-playback.rb,
lib/audio-playback/file.rb,
lib/audio-playback/sound.rb,
lib/audio-playback/device.rb,
lib/audio-playback/playback.rb,
lib/audio-playback/position.rb,
lib/audio-playback/commandline.rb,
lib/audio-playback/device/output.rb,
lib/audio-playback/device/stream.rb,
lib/audio-playback/playback/frame.rb,
lib/audio-playback/playback/mixer.rb,
lib/audio-playback/playback/frame_set.rb,
lib/audio-playback/playback/stream_data.rb

Overview

Play audio files

Defined Under Namespace

Modules: Commandline, Device, Playback Classes: File, Position, Sound

Constant Summary collapse

VERSION =
"0.0.8"

Class Method Summary collapse

Class Method Details

.ensure_initializedBoolean

Ensure that the audio system is initialized

Returns:

  • (Boolean)


58
59
60
# File 'lib/audio-playback.rb', line 58

def self.ensure_initialized
  @initialized ||= FFI::PortAudio::API.Pa_Initialize
end

.list_devicesArray<String>

List the available audio output devices

Returns:

  • (Array<String>)


52
53
54
# File 'lib/audio-playback.rb', line 52

def self.list_devices
  Device::Output.list
end

.play(file_paths, options = {}) ⇒ Object

Convenience method to play an audio file

Parameters:

  • file_paths (Array<::File>, Array<String>, ::File, String)
  • options (Hash) (defaults to: {})

Options Hash (options):

  • :buffer_size (Integer)

    Buffer size in bytes. Defaults to 4096

  • :channels (Array<Integer>, Integer) — default: or: :channel

    Output audio to the given channel(s). Eg ‘:channels => [0,1]` will direct the audio to channels 0 and 1. Defaults to use all available channels

  • :duration (Numeric)

    Play for given time in seconds

  • :end_position (Numeric)

    Stop at given time position in seconds (will use :duration if both are included)

  • :is_looping (Boolean) — default: or :loop

    Whether to loop audio

  • :latency (Float)

    Latency in seconds. Defaults to use the default latency for the selected output device

  • :logger (IO)

    Logger object

  • :seek (Numeric)

    Start at given time position in seconds

  • :output_device (Integer, String) — default: or: :output

    Output device id or name



42
43
44
45
46
47
48
# File 'lib/audio-playback.rb', line 42

def self.play(file_paths, options = {})
  sounds = Array(file_paths).map { |path| Sound.load(path, options) }
  requested_device = options[:output_device] || options[:output]
  output = Device::Output.by_name(requested_device) || Device::Output.by_id(requested_device) || Device.default_output
  options[:is_looping] ||= options[:loop]
  Playback.play(sounds, output, options)
end