Class: AudioStream::AudioBus

Inherits:
Object
  • Object
show all
Includes:
AudioObservable, AudioObserver
Defined in:
lib/audio_stream/audio_bus.rb

Direct Known Subclasses

AudioOutput

Instance Method Summary collapse

Methods included from AudioObservable

#fx, #mono, #notify_complete, #notify_next, #send_to, #stereo, #subscribe, #subscribe_on_next

Constructor Details

#initializeAudioBus

Returns a new instance of AudioBus.



6
7
8
9
10
# File 'lib/audio_stream/audio_bus.rb', line 6

def initialize
  @mutex = Mutex.new
  @callers = Set[]
  @notifications = {}
end

Instance Method Details

#add(observable, gain:, pan:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 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_completeObject



65
66
67
# File 'lib/audio_stream/audio_bus.rb', line 65

def on_complete
  notify_complete
end

#on_next(input) ⇒ Object



61
62
63
# File 'lib/audio_stream/audio_bus.rb', line 61

def on_next(input)
  notify_next(input)
end

#update(notification) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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