Class: AudioStream::AudioInputDevice

Inherits:
Object
  • Object
show all
Includes:
AudioInput
Defined in:
lib/audio_stream/audio_input_device.rb

Instance Attribute Summary collapse

Attributes included from AudioInput

#connection

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AudioInput

buffer, device, file, #publish, #published?, #sync

Methods included from AudioObservable

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

Constructor Details

#initialize(dev, soundinfo:) ⇒ AudioInputDevice

Returns a new instance of AudioInputDevice.



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

def initialize(dev, soundinfo:)
  @dev = dev
  @soundinfo = soundinfo
end

Instance Attribute Details

#devObject (readonly)

Returns the value of attribute dev.



5
6
7
# File 'lib/audio_stream/audio_input_device.rb', line 5

def dev
  @dev
end

Class Method Details

.core_devicesObject



74
75
76
77
78
79
# File 'lib/audio_stream/audio_input_device.rb', line 74

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

.default_device(soundinfo:) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/audio_stream/audio_input_device.rb', line 49

def self.default_device(soundinfo:)
  dev = CoreAudio.default_input_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_input_device({nominal_rate: soundinfo.samplerate})
  new(dev, soundinfo: soundinfo)
end

.devices(soundinfo:) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
# File 'lib/audio_stream/audio_input_device.rb', line 62

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



16
17
18
19
20
21
22
# File 'lib/audio_stream/audio_input_device.rb', line 16

def connect
  if !connected?
    @inbuf = @dev.input_buffer(@soundinfo.window_size)
    @inbuf.start
  end
  self
end

#connected?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/audio_stream/audio_input_device.rb', line 32

def connected?
  !!@inbuf
end

#disconnectObject



24
25
26
27
28
29
30
# File 'lib/audio_stream/audio_input_device.rb', line 24

def disconnect
  if connected?
    @inbuf.stop
    @inbuf = nil
  end
  super
end

#each(&block) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/audio_stream/audio_input_device.rb', line 36

def each(&block)
  Enumerator.new do |y|
    if !connected?
      raise Error, "Device is not connected. You need to exec #{self.class.name}.connect: #{name}"
    end

    loop {
      na = @inbuf.read(@soundinfo.window_size)
      y << Buffer.from_na(na)
    }
  end.each(&block)
end

#nameObject



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

def name
  @dev.name
end