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

.core_devicesObject



68
69
70
71
72
73
# File 'lib/audio_stream/audio_output_device.rb', line 68

def self.core_devices
  CoreAudio.devices
    .select {|dev|
      0<dev.output_stream.channels
    }
end

.default_device(soundinfo:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/audio_stream/audio_output_device.rb', line 43

def self.default_device(soundinfo:)
  dev = CoreAudio.default_output_device
  is_supported_sample_rate = dev.available_sample_rate.any? {|min,max|
    min<=soundinfo.samplerate && soundinfo.samplerate<=max
  }
  if !is_supported_sample_rate
    raise Error, "Unsupported sample rate: samplerate=#{soundinfo.samplerate}, device=#{dev.name}, available_sample_rate=#{dev.available_sample_rate}"
  end

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

.devices(soundinfo:) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/audio_stream/audio_output_device.rb', line 56

def self.devices(soundinfo:)
  self.core_devices
    .select {|dev|
      dev.available_sample_rate.any? {|min,max|
        min<=soundinfo.samplerate && soundinfo.samplerate<=max
      }
    }
    .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