class AudioPlayback::Playback::Mixer

Mix sound data

Public Class Methods

mix(sounds_data) click to toggle source

Mix multiple sounds at equal amplitude @param [Array<Array<Array<Integer>>>] sounds_data @return [Array<Array<Integer>>]

# File lib/audio-playback/playback/mixer.rb, line 11
def self.mix(sounds_data)
  mixer = new(sounds_data)
  mixer.mix
end
new(sounds_data) click to toggle source

@param [Array<Array<Array<Integer>>>] sounds_data

# File lib/audio-playback/playback/mixer.rb, line 17
def initialize(sounds_data)
  @data = sounds_data
  populate
end

Public Instance Methods

mix() click to toggle source

Mix multiple sounds at equal amplitude @return [Array<Array<Integer>>]

# File lib/audio-playback/playback/mixer.rb, line 24
def mix
  (0..@length-1).to_a.map { |index| mix_frame(index) }
end

Private Instance Methods

frames(index) click to toggle source

Get all of the data frames for the given index For example for index 3, two two channel sounds, frames(3) might give you [[1, 3], [2, 3]] @param [Integer] index @return [Array<Array<Integer>>]

# File lib/audio-playback/playback/mixer.rb, line 42
def frames(index)
  @data.map { |sound_data| sound_data[index] }
end
mix_frame(index) click to toggle source

Mix the frame with the given index whereas frames(3) might give you [[1, 3], [2, 3]] mix_frame(3) might give you [1.5, 3] @param [Integer] index @return [Array<Integer>]

# File lib/audio-playback/playback/mixer.rb, line 51
def mix_frame(index)
  totals = frames(index).compact.transpose.map { |x| x && x.reduce(:+) || 0 }
  totals.map { |frame| frame / @depth }
end
populate() click to toggle source

Populate the mixer metadata @return [Mixer]

# File lib/audio-playback/playback/mixer.rb, line 32
def populate
  @length = @data.map(&:size).max
  @depth = @data.count
  self
end