Class: Pocketsphinx::Microphone
- Inherits:
-
Object
- Object
- Pocketsphinx::Microphone
- Includes:
- API::CallHelpers
- Defined in:
- lib/pocketsphinx/microphone.rb
Overview
Provides non-blocking live audio recording using libsphinxad
Implements Recordable interface (#start_recording, #stop_recording and #read_audio)
Instance Attribute Summary collapse
- #ps_api ⇒ Object
-
#ps_audio_device ⇒ Object
readonly
Returns the value of attribute ps_audio_device.
-
#sample_rate ⇒ Object
readonly
Returns the value of attribute sample_rate.
Class Method Summary collapse
Instance Method Summary collapse
- #close_device ⇒ Object
-
#initialize(sample_rate = 16000, default_device = nil, ps_api = nil) ⇒ Microphone
constructor
Opens an audio device for recording.
-
#read_audio(buffer, max_samples = 2048) ⇒ Fixnum
Read next block of audio samples while recording; read upto max samples into buf.
-
#read_audio_delay(max_samples = 2048) ⇒ Object
A Recordable may specify an audio reading delay.
- #record ⇒ Object
- #start_recording ⇒ Object
- #stop_recording ⇒ Object
Methods included from API::CallHelpers
Constructor Details
#initialize(sample_rate = 16000, default_device = nil, ps_api = nil) ⇒ Microphone
Opens an audio device for recording
The device is opened in non-blocking mode and placed in idle state.
19 20 21 22 23 24 25 26 |
# File 'lib/pocketsphinx/microphone.rb', line 19 def initialize(sample_rate = 16000, default_device = nil, ps_api = nil) @sample_rate = sample_rate @ps_api = ps_api @ps_audio_device = self.ps_api.ad_open_dev(default_device, sample_rate) # Ensure that audio device is closed when object is garbage collected ObjectSpace.define_finalizer(self, self.class.finalize(ps_api, @ps_audio_device)) end |
Instance Attribute Details
#ps_api ⇒ Object
71 72 73 |
# File 'lib/pocketsphinx/microphone.rb', line 71 def ps_api @ps_api || API::SphinxAD end |
#ps_audio_device ⇒ Object (readonly)
Returns the value of attribute ps_audio_device.
8 9 10 |
# File 'lib/pocketsphinx/microphone.rb', line 8 def ps_audio_device @ps_audio_device end |
#sample_rate ⇒ Object (readonly)
Returns the value of attribute sample_rate.
10 11 12 |
# File 'lib/pocketsphinx/microphone.rb', line 10 def sample_rate @sample_rate end |
Class Method Details
.finalize(ps_api, ps_audio_device) ⇒ Object
28 29 30 |
# File 'lib/pocketsphinx/microphone.rb', line 28 def self.finalize(ps_api, ps_audio_device) proc { ps_api.ad_close(ps_audio_device) } end |
Instance Method Details
#close_device ⇒ Object
67 68 69 |
# File 'lib/pocketsphinx/microphone.rb', line 67 def close_device api_call :ad_close, @ps_audio_device end |
#read_audio(buffer, max_samples = 2048) ⇒ Fixnum
Read next block of audio samples while recording; read upto max samples into buf.
52 53 54 55 |
# File 'lib/pocketsphinx/microphone.rb', line 52 def read_audio(buffer, max_samples = 2048) samples = ps_api.ad_read(@ps_audio_device, buffer, max_samples) samples if samples >= 0 end |
#read_audio_delay(max_samples = 2048) ⇒ Object
A Recordable may specify an audio reading delay
In the case of the Microphone, because we are doing non-blocking reads, we specify a delay which should fill half of the max buffer size
63 64 65 |
# File 'lib/pocketsphinx/microphone.rb', line 63 def read_audio_delay(max_samples = 2048) max_samples.to_f / (2 * sample_rate) end |
#record ⇒ Object
32 33 34 35 36 |
# File 'lib/pocketsphinx/microphone.rb', line 32 def record start_recording yield stop_recording end |
#start_recording ⇒ Object
38 39 40 |
# File 'lib/pocketsphinx/microphone.rb', line 38 def start_recording api_call :ad_start_rec, @ps_audio_device end |
#stop_recording ⇒ Object
42 43 44 |
# File 'lib/pocketsphinx/microphone.rb', line 42 def stop_recording api_call :ad_stop_rec, @ps_audio_device end |