class AudioStream::AudioObservableFxBus

Public Class Methods

new(effector) click to toggle source
# File lib/audio_stream/audio_observable_fx_bus.rb, line 6
def initialize(effector)
  @effector = effector
  @mutex = Mutex.new
  @observable_keys = {}
  @notifications = {}
end

Public Instance Methods

connect_observable(key, observable) click to toggle source
# File lib/audio_stream/audio_observable_fx_bus.rb, line 13
def connect_observable(key, observable)
  @mutex.synchronize {
    if !@effector.audio_input_keys.include?(key)
      raise Error.new("audio input key is not registed: %s => %s", @effector.class.name, key)
    end

    # delete
    @observable_keys.select {|obs, key1|
      key1==key
    }.each {|obs, key1|
      obs.delete_observer(self)
      @observable_keys.delete(obs)
    }

    # add
    @observable_keys[observable] = key
    observable.add_observer(self)
  }
end
on_complete() click to toggle source
# File lib/audio_stream/audio_observable_fx_bus.rb, line 72
def on_complete
  notify_complete
end
on_next(inputs) click to toggle source
# File lib/audio_stream/audio_observable_fx_bus.rb, line 67
def on_next(inputs)
  output = @effector.process(inputs)
  notify_next(output)
end
update(notification) click to toggle source
# File lib/audio_stream/audio_observable_fx_bus.rb, line 33
def update(notification)
  do_notify = false
  is_completed = false
  next_inputs = nil

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

    if @notifications.length==@observable_keys.length
      next_inputs = {}
      @notifications.each {|caller_obj, notification|
        key = @observable_keys[notification.caller_obj]
        case notification.stat
        when AudioNotification::STAT_NEXT
          next_inputs[key] = notification.input
        when AudioNotification::STAT_COMPLETE
          is_completed = true
        end
      }

      do_notify = true
      @notifications.clear
    end
  }

  if do_notify
    if !is_completed
      on_next(next_inputs)
    else
      on_complete
    end
  end
end