Class: ALSA::PCM::Stream
- Inherits:
-
Object
- Object
- ALSA::PCM::Stream
- Defined in:
- lib/alsa/pcm/stream.rb
Instance Attribute Summary collapse
-
#buffer_time_size ⇒ Object
Returns the value of attribute buffer_time_size.
-
#handle ⇒ Object
Returns the value of attribute handle.
Class Method Summary collapse
Instance Method Summary collapse
- #available_frame_count ⇒ Object
- #buffer_frame_count ⇒ Object
- #change_hardware_parameters ⇒ Object
- #change_software_parameters ⇒ Object
- #check_handle! ⇒ Object
- #close ⇒ Object
- #hardware_parameters ⇒ Object (also: #hw_params)
- #hardware_parameters=(attributes = {}) ⇒ Object
- #open(device = "default", hardware_attributes = {}, &block) ⇒ Object
- #opened? ⇒ Boolean
- #software_parameters ⇒ Object (also: #sw_params)
Instance Attribute Details
#buffer_time_size ⇒ Object
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 |
#handle ⇒ Object
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_count ⇒ Object
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_count ⇒ Object
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_parameters ⇒ Object
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_parameters ⇒ Object
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 |
#close ⇒ Object
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_parameters ⇒ Object 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) = {} if Hash === device = device device = ([:device] or "default") end self.buffer_time_size = [:buffer_time_size] if [: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
107 108 109 |
# File 'lib/alsa/pcm/stream.rb', line 107 def opened? not self.handle.nil? end |
#software_parameters ⇒ Object 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 |