Class: ALSA::PCM::Capture::HwParameters

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(device = nil) ⇒ HwParameters

Returns a new instance of HwParameters.



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

def initialize(device = nil)
  hw_params_pointer = FFI::MemoryPointer.new :pointer

  ALSA::PCM::Native::hw_params_malloc hw_params_pointer        
  self.handle = hw_params_pointer.read_pointer

  self.device = device if device
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



114
115
116
# File 'lib/alsa.rb', line 114

def device
  @device
end

#handleObject

Returns the value of attribute handle.



114
115
116
# File 'lib/alsa.rb', line 114

def handle
  @handle
end

Instance Method Details

#access=(access) ⇒ Object



143
144
145
146
147
# File 'lib/alsa.rb', line 143

def access=(access)
  ALSA::try_to "set access type" do
    ALSA::PCM::Native::hw_params_set_access self.device.handle, self.handle, ALSA::PCM::Native::Access.const_get(access.to_s.upcase)
  end
end

#buffer_size_for(frame_count) ⇒ Object



219
220
221
# File 'lib/alsa.rb', line 219

def buffer_size_for(frame_count)
  ALSA::PCM::Native::format_size(self.sample_format, frame_count) * self.channels
end

#channelsObject



208
209
210
211
212
213
214
215
216
217
# File 'lib/alsa.rb', line 208

def channels
  channels = nil
  FFI::MemoryPointer.new(:int) do |channels_pointer|
    ALSA::try_to "get channels" do
      ALSA::PCM::Native::hw_params_get_channels self.handle, channels_pointer
    end
    channels = channels_pointer.read_int
  end
  channels
end

#channels=(channels) ⇒ Object



149
150
151
152
153
# File 'lib/alsa.rb', line 149

def channels=(channels)
  ALSA::try_to "set channel count : #{channels}" do
    ALSA::PCM::Native::hw_params_set_channels self.device.handle, self.handle, channels
  end
end

#current_for_deviceObject



136
137
138
139
140
141
# File 'lib/alsa.rb', line 136

def current_for_device
  ALSA::try_to "retrieve current hardware parameters" do
    ALSA::PCM::Native::hw_params_current device.handle, self.handle
  end
  self
end

#default_for_deviceObject



129
130
131
132
133
134
# File 'lib/alsa.rb', line 129

def default_for_device
  ALSA::try_to "initialize hardware parameter structure" do
    ALSA::PCM::Native::hw_params_any device.handle, self.handle
  end
  self
end

#freeObject



223
224
225
226
227
# File 'lib/alsa.rb', line 223

def free
  ALSA::try_to "unallocate hw_params" do
    ALSA::PCM::Native::hw_params_free self.handle
  end
end

#sample_formatObject



197
198
199
200
201
202
203
204
205
206
# File 'lib/alsa.rb', line 197

def sample_format
  format = nil
  FFI::MemoryPointer.new(:int) do |format_pointer|
    ALSA::try_to "get sample format" do
      ALSA::PCM::Native::hw_params_get_format self.handle, format_pointer
    end
    format = format_pointer.read_int
  end
  format
end

#sample_format=(sample_format) ⇒ Object



191
192
193
194
195
# File 'lib/alsa.rb', line 191

def sample_format=(sample_format)
  ALSA::try_to "set sample format" do
    ALSA::PCM::Native::hw_params_set_format self.device.handle, self.handle, ALSA::PCM::Native::Format.const_get(sample_format.to_s.upcase)
  end
end

#sample_rateObject



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'lib/alsa.rb', line 172

def sample_rate
  rate = nil
  ALSA::try_to "get sample rate" do
    rate_pointer = FFI::MemoryPointer.new(:int)
    dir_pointer = FFI::MemoryPointer.new(:int)
    dir_pointer.write_int(0)

    error_code = ALSA::PCM::Native::hw_params_get_rate self.handle, rate_pointer, dir_pointer

    rate = rate_pointer.read_int

    rate_pointer.free
    dir_pointer.free

    error_code
  end
  rate
end

#sample_rate=(sample_rate) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/alsa.rb', line 155

def sample_rate=(sample_rate)
  ALSA::try_to "set sample rate" do
    rate = FFI::MemoryPointer.new(:int)
    rate.write_int(sample_rate)

    dir = FFI::MemoryPointer.new(:int)
    dir.write_int(0)

    error_code = ALSA::PCM::Native::hw_params_set_rate_near self.device.handle, self.handle, rate, dir

    rate.free
    dir.free

    error_code
  end
end

#update_attributes(attributes) ⇒ Object



125
126
127
# File 'lib/alsa.rb', line 125

def update_attributes(attributes)
  attributes.each_pair { |name, value| send("#{name}=", value) }
end