class AudioPlayback::Device::Output

An output device

Attributes

id[R]
name[R]
resource[R]

Public Class Methods

all() click to toggle source

All output devices @return [Array<Output>]

# File lib/audio-playback/device/output.rb, line 11
def self.all
  Device.outputs
end
by_id(id) click to toggle source

Select an output device by ID @param [Integer] id @return [Output]

# File lib/audio-playback/device/output.rb, line 47
def self.by_id(id)
  Device.by_id(id)
end
by_name(name) click to toggle source

Select an output device by name @param [String] name @return [Output]

# File lib/audio-playback/device/output.rb, line 54
def self.by_name(name)
  Device.by_name(name)
end
gets() click to toggle source

Streamlined console prompt that asks the user (via standard in) to select a device When their input is received, the device is selected and enabled @return [Output]

# File lib/audio-playback/device/output.rb, line 28
def self.gets
  device = nil
  puts ""
  puts "Select an audio output..."
  while device.nil?
    list
    print "> "
    selection = $stdin.gets.chomp
    if selection != ""
      selection = Integer(selection) rescue nil
      device = all.find { |d| d.id == selection } unless selection.nil?
    end
  end
  device
end
list() click to toggle source

Prints ids and names of each device to standard out @return [Array<String>]

# File lib/audio-playback/device/output.rb, line 17
def self.list
  all.map do |device|
    name = "#{device.id}. #{device.name}"
    $>.puts(name)
    name
  end
end
new(id, options = {}) click to toggle source

@param [Integer] id @param [Hash] options @option options [Float] :latency Device latency in seconds

# File lib/audio-playback/device/output.rb, line 61
def initialize(id, options = {})
  # Init audio output resource
  AudioPlayback.ensure_initialized
  populate(id, options)
end

Public Instance Methods

latency() click to toggle source

Device latency in seconds @return [Float]

# File lib/audio-playback/device/output.rb, line 69
def latency
  @resource[:suggestedLatency]
end
num_channels() click to toggle source

Number of channels the device supports @return [Integer]

# File lib/audio-playback/device/output.rb, line 75
def num_channels
  @resource[:channelCount]
end

Private Instance Methods

info() click to toggle source

The underlying resource info struct for this output @return [FFI::PortAudio::API::PaDeviceInfo]

# File lib/audio-playback/device/output.rb, line 89
def info
  @info ||= Device.device_info(id)
end
populate(id, options = {}) click to toggle source

Populate the output @param [Integer] id @param [Hash] options @option options [Float] :latency @return [FFI::PortAudio::API::PaStreamParameters]

# File lib/audio-playback/device/output.rb, line 98
def populate(id, options = {})
  @resource = FFI::PortAudio::API::PaStreamParameters.new
  @resource[:device] = id
  @name = info[:name]
  @resource[:suggestedLatency] = options[:latency] || info[:defaultHighOutputLatency]
  @resource[:hostApiSpecificStreamInfo] = nil
  @resource[:channelCount] = info[:maxOutputChannels]
  @resource[:sampleFormat] = FFI::PortAudio::API::Float32
  @resource
end