class AudioStream::AudioBus

Public Class Methods

new() click to toggle source
# File lib/audio_stream/audio_bus.rb, line 6
def initialize
  @mutex = Mutex.new
  @callers = Set[]
  @notifications = {}
end

Public Instance Methods

add(observable, gain:, pan:) click to toggle source
# File lib/audio_stream/audio_bus.rb, line 12
def add(observable, gain:, pan:)
  if gain && gain!=0.0
    observable = observable.fx(Fx::AGain.new(level: gain))
  end

  if pan && pan!=0.0
    observable = observable.fx(Fx::Panning.new(pan: pan))
  end

  @mutex.synchronize {
    @callers << observable
    observable.add_observer(self)
  }
end
on_complete() click to toggle source
# File lib/audio_stream/audio_bus.rb, line 65
def on_complete
  notify_complete
end
on_next(input) click to toggle source
# File lib/audio_stream/audio_bus.rb, line 61
def on_next(input)
  notify_next(input)
end
update(notification) click to toggle source
# File lib/audio_stream/audio_bus.rb, line 27
def update(notification)
  do_notify = false
  next_notifications = nil

  @mutex.synchronize {
    @notifications[notification.caller_obj] = notification

    if @callers.length==@notifications.length
      next_notifications = []
      @notifications.each {|caller_obj, notification|
        case notification.stat
        when AudioNotification::STAT_NEXT
          next_notifications << notification
        when AudioNotification::STAT_COMPLETE
          @callers.delete(caller_obj)
        end
      }

      do_notify = true
      @notifications.clear
    end
  }

  if do_notify
    if 0<next_notifications.length
      inputs = next_notifications.map(&:input)
      output = Buffer.merge(inputs)
      on_next(output)
    else
      on_complete
    end
  end
end