Class: ALSA::PCM::Capture

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

Instance Attribute Summary

Attributes inherited from Stream

#buffer_time_size, #handle

Instance Method Summary collapse

Methods inherited from Stream

#available_frame_count, #buffer_frame_count, #change_hardware_parameters, #change_software_parameters, #check_handle!, #close, #hardware_parameters, #hardware_parameters=, #open, open, #opened?, #software_parameters

Instance Method Details

#native_constantObject



4
5
6
# File 'lib/alsa/pcm/capture.rb', line 4

def native_constant
  ALSA::PCM::Native::Stream::CAPTURE
end

#readObject



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/alsa/pcm/capture.rb', line 8

def read
  check_handle!

  ALSA.logger.debug { "start read with #{hw_params.inspect}"}

  FFI::MemoryPointer.new(:char, hw_params.buffer_size_for(buffer_frame_count)) do |buffer|
    begin
      read_buffer buffer, buffer_frame_count
    end while yield buffer, buffer_frame_count
  end
end

#read_buffer(buffer, frame_count) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/alsa/pcm/capture.rb', line 55

def read_buffer(buffer, frame_count)
  check_handle!

  read_count = ALSA::try_to "read from audio interface" do
    response = ALSA::PCM::Native::readi(self.handle, buffer, frame_count)
    if ALSA::Native::error_code?(response)
      ALSA.logger.warn { "try to recover '#{ALSA::Native::strerror(response)}' on read"}
      ALSA::PCM::Native::pcm_recover(self.handle, response, 1)
    else
      response
    end
  end

  ALSA.logger.debug { "read frame count: #{read_count}/#{frame_count}"}

  missing_frame_count = frame_count - read_count
  if missing_frame_count > 0
    ALSA.logger.debug { "re-read missing frame count: #{missing_frame_count}"}
    read_buffer_size = hw_params.buffer_size_for(read_count)
    # buffer[read_buffer_size] doesn't return a MemoryPointer
    read_buffer(buffer + read_buffer_size, missing_frame_count)
  end
end

#read_in_background(&block) ⇒ Object



20
21
22
23
24
25
26
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
# File 'lib/alsa/pcm/capture.rb', line 20

def read_in_background(&block)
  check_handle!

  async_handler = FFI::MemoryPointer.new(:pointer)
  buffer = FFI::MemoryPointer.new(:char, hw_params.buffer_size_for(buffer_frame_count))

  started = false

  capture_callback = Proc.new do |async_handler|
    if started
      frame_to_read = buffer_frame_count
      read_buffer buffer, frame_to_read
      yield buffer, frame_to_read
    end
  end

  ALSA::try_to "add pcm handler" do
    ALSA::Native::async_add_pcm_handler(async_handler, handle, capture_callback, nil)
  end

  ALSA::try_to "start capture" do
    ALSA::PCM::Native::start(handle)
  end

  hole_frame_count = ALSA::try_to "read available space" do
    ALSA::PCM::Native::avail_update(self.handle)
  end
  ALSA.logger.debug { "read synchronously #{hole_frame_count} frames"}
  FFI::MemoryPointer.new(:char, hw_params.buffer_size_for(hole_frame_count)) do |hole|
    ALSA::PCM::Native::readi self.handle, hole, hole_frame_count
  end

  started = true
end