Class: ALSA::PCM::Capture

Inherits:
Object
  • Object
show all
Defined in:
lib/alsa.rb

Defined Under Namespace

Classes: HwParameters

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#handleObject

Returns the value of attribute handle.



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

def handle
  @handle
end

Class Method Details

.open(device, hardware_attributes = {}, &block) ⇒ Object



41
42
43
# File 'lib/alsa.rb', line 41

def self.open(device, hardware_attributes = {}, &block)
  Capture.new.open(device, hardware_attributes, &block)
end

Instance Method Details

#change_hardware_parametersObject



63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/alsa.rb', line 63

def change_hardware_parameters
  hw_params = HwParameters.new(self).default_for_device

  begin
    yield hw_params

    ALSA::try_to "set hw parameters" do
      ALSA::PCM::Native::hw_params self.handle, hw_params.handle
    end
  ensure
    hw_params.free
  end
end

#closeObject



106
107
108
109
110
# File 'lib/alsa.rb', line 106

def close
  ALSA::try_to "close audio device" do
    ALSA::PCM::Native::close self.handle
  end
end

#hardware_parametersObject



77
78
79
# File 'lib/alsa.rb', line 77

def hardware_parameters
  HwParameters.new(self).current_for_device
end

#hardware_parameters=(attributes = {}) ⇒ Object



81
82
83
84
85
86
# File 'lib/alsa.rb', line 81

def hardware_parameters=(attributes= {})
  attributes = {:access => :rw_interleaved}.update(attributes)
  change_hardware_parameters do |hw_params|
    hw_params.update_attributes(attributes)
  end
end

#open(device, hardware_attributes = {}, &block) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/alsa.rb', line 45

def open(device, hardware_attributes = {}, &block)
  capture_handle = FFI::MemoryPointer.new :pointer
  ALSA::try_to "open audio device #{device}" do
    ALSA::PCM::Native::open capture_handle, device, ALSA::PCM::Native::STREAM_CAPTURE, ALSA::PCM::Native::BLOCK
  end
  self.handle = capture_handle.read_pointer

  self.hardware_parameters=hardware_attributes

  if block_given?
    begin
      yield self 
    ensure
      self.close
    end
  end
end

#readObject



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/alsa.rb', line 88

def read
  frame_count = hardware_parameters.sample_rate
  buffer = FFI::MemoryPointer.new(hardware_parameters.buffer_size_for(frame_count))

  continue = true
  while continue
    read_count = ALSA::try_to "read from audio interface" do
      ALSA::PCM::Native::readi(self.handle, buffer, frame_count)
    end

    raise "can't read expected frame count (#{read_count}/#{frame_count})" unless read_count == frame_count
    
    continue = yield buffer, read_count
  end

  buffer.free
end