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



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

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
# 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

    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



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

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



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

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

#closeObject



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

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



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

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

#hardware_parameters=(attributes = {}) ⇒ Object



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

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 / 2
  end

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

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

#opened?Boolean

Returns:

  • (Boolean)


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

def opened?
  not self.handle.nil?
end

#software_parametersObject Also known as: sw_params



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

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