class AudioMixer::Sox::SoundServer

Public Class Methods

new(composition) click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 5
def initialize(composition)
  @composition = composition
  @sound_buffers = {}
  @timeouts = Hash.new(0)
end

Public Instance Methods

tick() click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 11
def tick
  cache_sounds
  play_sounds
end

Private Instance Methods

cache_sounds() click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 30
def cache_sounds
  @composition.sounds.each do |sound|
    @sound_buffers[sound["url"]] ||= load_raw_sound(sound["url"])
  end
end
load_raw_sound(url) click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 36
def load_raw_sound(url)
  IO.popen("sox \"#{File.expand_path(url)}\" -p", "rb") do |io|
    io.read
  end
end
play_sound(sound) click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 42
def play_sound(sound)
  Thread.new do
    IO.popen("sox -v #{sound["volume"] || 1.0} -p -d pan #{sound["panning"] || 0.0} > /dev/null 2>&1", "wb") do |io|
      io.write(@sound_buffers[sound["url"]])
    end
  end
end
play_sounds() click to toggle source
# File lib/audio_mixer/sox/sound_server.rb, line 18
def play_sounds
  Time.now.to_f.tap { |time| @delta_time, @last_time = time - (@last_time || time), time }

  (0..@composition.sounds.size-1).each do |index|
    sound = @composition.sounds[index]
    if (@timeouts[index] -= @delta_time) < 0
      @timeouts[index] = sound["repeat"] || 1.0
      play_sound(sound) unless sound["mute"]
    end
  end
end