class AudioPlayback::Playback::FrameSet

Container for playback data

Public Class Methods

new(playback) click to toggle source

@param [Playback::Action] playback

# File lib/audio-playback/playback/frame_set.rb, line 13
def initialize(playback)
  populate(playback)
end

Private Instance Methods

build_channels(data, playback) click to toggle source

(Re-)build the channel structure of the frame set @param [Array<Frame>] data @param [Playback::Action] playback @return [Array<Frame>]

# File lib/audio-playback/playback/frame_set.rb, line 54
def build_channels(data, playback)
  ensure_num_channels(data, playback.num_channels)

  if playback.channels_requested?
    ensure_requested_channels(data, playback)
  else
    ensure_output_channels(data, playback)
  end
  data
end
build_for_sound(playback, sound) click to toggle source

Build the frame set data for the given playback action and sound @param [Playback::Action] playback @param [Sound] sound @return [Array<Array<Float>>]

# File lib/audio-playback/playback/frame_set.rb, line 23
def build_for_sound(playback, sound)
  data = sound.data
  data = ensure_array_frames(data)
  data = to_frame_objects(data)
  data = build_channels(data, playback) unless channels_match?(playback, sound)
  data
end
channels_match?(playback, sound) click to toggle source

Does the channel structure of the playback action match the channel structure of the sound? @param [Playback::Action] playback @return [Boolean]

# File lib/audio-playback/playback/frame_set.rb, line 46
def channels_match?(playback, sound)
  sound.num_channels == playback.num_channels && playback.channels.nil?
end
ensure_array_frames(data) click to toggle source

Ensure that the input data is Array<Array<Float>>. Single channel audio will be provided as

Array<Float> and is converted here so that the frame set data structure can be built in a
uniform way

@param [Array<Float>, Array<Array<Float>>] data @return [Array<Array<Float>>]

# File lib/audio-playback/playback/frame_set.rb, line 107
def ensure_array_frames(data)
  if data.sample.kind_of?(Array)
    data
  else
    data.map { |frame| Array(frame) }
  end
end
ensure_num_channels(data, num_channels, options = {}) click to toggle source

Ensure that the channel structure of the frameset is according to the given number of channels

and to the given particular channels when provided

@param [Array<Frame>] data @param [Integer] num_channels @param [Hash] options @option options [Array<Integer>] :channels @return [Array<Frame>]

# File lib/audio-playback/playback/frame_set.rb, line 90
def ensure_num_channels(data, num_channels, options = {})
  data.each do |frame|
    difference = num_channels - frame.size
    if difference > 0
      frame.fill(difference, :channels => options[:channels], :num_channels => num_channels)
    else
      frame.truncate(num_channels)
    end
  end
  data
end
ensure_output_channels(data, playback) click to toggle source

Build the channel structure of the frameset to that of the playback output device @param [Array<Frame>] data @param [Playback::Action] playback @return [Array<Frame>]

# File lib/audio-playback/playback/frame_set.rb, line 77
def ensure_output_channels(data, playback)
  if playback.num_channels != playback.output.num_channels
    ensure_num_channels(data, playback.output.num_channels)
  end
end
ensure_requested_channels(data, playback) click to toggle source

Build the channel structure of the frame set to what was requested of playback @param [Array<Frame>] data @param [Playback::Action] playback @return [Array<Frame>]

# File lib/audio-playback/playback/frame_set.rb, line 69
def ensure_requested_channels(data, playback)
  ensure_num_channels(data, playback.output.num_channels, :channels => playback.channels)
end
populate(playback) click to toggle source

Populate the Container @param [Playback::Action] playback @return [Array<Array<Float>>]

# File lib/audio-playback/playback/frame_set.rb, line 34
def populate(playback)
  data = playback.sounds.map { |sound| build_for_sound(playback, sound) }
  @data = if data.count > 1
    Mixer.mix(data)
  else
    data[0]
  end
end
to_frame_objects(data) click to toggle source

Convert the raw sound data to Frame objects @param [Array<Array<Float>>] data @return [Array<Frame>]

# File lib/audio-playback/playback/frame_set.rb, line 118
def to_frame_objects(data)
  data.map { |frame| Frame.new(frame) }
end