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

#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



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

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

Instance Method Details

#change_hardware_parametersObject



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/alsa/pcm/stream.rb', line 28

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

#check_handle!Object



64
65
66
# File 'lib/alsa/pcm/stream.rb', line 64

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

#closeObject



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

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



42
43
44
# File 'lib/alsa/pcm/stream.rb', line 42

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

#hardware_parameters=(attributes = {}) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/alsa/pcm/stream.rb', line 47

def hardware_parameters=(attributes= {})
  attributes = { 
    :access => :rw_interleaved, 
    :channels => 2, 
    :sample_format => :s16_le, 
    :sample_rate => 44100 
  }.update(attributes)

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

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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/alsa/pcm/stream.rb', line 10

def open(device = "default", hardware_attributes = {}, &block)
  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

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

#opened?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/alsa/pcm/stream.rb', line 60

def opened?
  not self.handle.nil?
end