Class: ALSA::PCM::Stream

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

Direct Known Subclasses

Capture, Playback

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#buffer_time_sizeObject

Returns the value of attribute buffer_time_size.



5
6
7
# File 'lib/alsa/pcm/stream.rb', line 5

def buffer_time_size
  @buffer_time_size
end

#handleObject

Returns the value of attribute handle.



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

def handle
  @handle
end

Class Method Details

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



7
8
9
# File 'lib/alsa/pcm/stream.rb', line 7

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

Instance Method Details

#available_frame_countObject



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/alsa/pcm/stream.rb', line 123

def available_frame_count
  check_handle!

  ALSA::try_to "wait the interface is ready" do
    ALSA::PCM::Native::wait(self.handle, buffer_time_size)
  end
  available_frame_count = ALSA::try_to "read available space" do
    ALSA::PCM::Native::avail_update(self.handle)
  end

  [available_frame_count, buffer_frame_count].min
end

#buffer_frame_countObject



50
51
52
# File 'lib/alsa/pcm/stream.rb', line 50

def buffer_frame_count
  @buffer_frame_count ||= hw_params.sample_rate * buffer_time_size / 1000
end

#change_hardware_parametersObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/alsa/pcm/stream.rb', line 54

def change_hardware_parameters
  hw_params = ALSA::PCM::HwParameters.new(self).default_for_device

  begin
    yield hw_params
    hw_params.update_attributes :buffer_time => hw_params.buffer_time_max

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

#change_software_parametersObject



89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/alsa/pcm/stream.rb', line 89

def change_software_parameters
  sw_params = software_parameters

  begin
    yield sw_params

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

#check_handle!Object



112
113
114
# File 'lib/alsa/pcm/stream.rb', line 112

def check_handle!
  raise "Stream isn't opened" unless opened?
end

#closeObject



116
117
118
119
120
121
# File 'lib/alsa/pcm/stream.rb', line 116

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

#hardware_parametersObject Also known as: hw_params



69
70
71
# File 'lib/alsa/pcm/stream.rb', line 69

def hardware_parameters
  ALSA::PCM::HwParameters.new(self).current_for_device
end

#hardware_parameters=(attributes = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/alsa/pcm/stream.rb', line 74

def hardware_parameters=(attributes= {})
  attributes = { 
    :access => :rw_interleaved, 
    :channels => 2, 
    :sample_format => :s16_le, 
    :sample_rate => 44100,
    # :buffer_time => buffer_time_size * 1000,
    # :period_time => buffer_time_size * 1000 / 4
  }.update(attributes)

  change_hardware_parameters do |hw_params|
    hw_params.update_attributes(attributes)
  end
end

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



11
12
13
14
15
16
17
18
19
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
# File 'lib/alsa/pcm/stream.rb', line 11

def open(device = "default", hardware_attributes = {}, &block)
  options = {}

  if Hash === device
    options = device
    device = (options[:device] or "default")
  end

  self.buffer_time_size = options[:buffer_time_size] if options[:buffer_time_size]

  handle_pointer = FFI::MemoryPointer.new :pointer
  ALSA::try_to "open audio device #{device}" do
    ALSA::PCM::Native::open handle_pointer, device, native_constant, ALSA::PCM::Native::BLOCK
  end
  self.handle = handle_pointer.read_pointer

  self.hardware_parameters = hardware_attributes

  change_software_parameters do |sw_params|
    sw_params.available_minimum = buffer_frame_count / 4
  end

  ALSA::PCM::Native.prepare(handle)

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

#opened?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/alsa/pcm/stream.rb', line 108

def opened?
  not self.handle.nil?
end

#software_parametersObject Also known as: sw_params



103
104
105
# File 'lib/alsa/pcm/stream.rb', line 103

def software_parameters
  ALSA::PCM::SwParameters.new(self).current_for_device
end