Class: AudioStream::AudioOutputDevice

Inherits:
AudioOutput show all
Defined in:
lib/audio_stream/audio_output_device.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from AudioOutput

device, file

Methods inherited from AudioBus

#add, #on_complete, #update

Methods included from AudioObservable

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

Methods included from AudioObserver

#update

Constructor Details

#initialize(dev, soundinfo:) ⇒ AudioOutputDevice

Returns a new instance of AudioOutputDevice.



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

def initialize(dev, soundinfo:)
  super()
  @dev = dev
  @channels = dev.output_stream.channels
  @buf = dev.output_buffer(soundinfo.window_size)
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev.



3
4
5
# File 'lib/audio_stream/audio_output_device.rb', line 3

def dev
  @dev
end

Class Method Details

.default_device(soundinfo:) ⇒ Object



43
44
45
46
# File 'lib/audio_stream/audio_output_device.rb', line 43

def self.default_device(soundinfo:)
  dev = CoreAudio.default_output_device({nominal_rate: soundinfo.samplerate})
  new(dev, soundinfo: soundinfo)
end

.devices(soundinfo:) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/audio_stream/audio_output_device.rb', line 48

def self.devices(soundinfo:)
  CoreAudio.devices({nominal_rate: soundinfo.samplerate})
    .select{|dev|
      0<dev.output_stream.channels
    }
    .map {|dev|
      new(dev, soundinfo: soundinfo)
    }
end

Instance Method Details

#connectObject



12
13
14
# File 'lib/audio_stream/audio_output_device.rb', line 12

def connect
  @buf.start
end

#disconnectObject



16
17
18
# File 'lib/audio_stream/audio_output_device.rb', line 16

def disconnect
  @buf.stop
end

#on_completedObject



39
40
41
# File 'lib/audio_stream/audio_output_device.rb', line 39

def on_completed
  disconnect
end

#on_error(error) ⇒ Object



34
35
36
37
# File 'lib/audio_stream/audio_output_device.rb', line 34

def on_error(error)
  puts error
  puts error.backtrace.join("\n")
end

#on_next(input) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/audio_stream/audio_output_device.rb', line 20

def on_next(input)
  window_size = input.window_size
  channels = input.channels

  case @channels
  when 1
    input = input.mono
  when 2
    input = input.stereo
  end

  @buf << input.to_sint_na
end