Class: Pocketsphinx::Microphone

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from API::CallHelpers

#api_call

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.

Parameters:

  • sample_rate (Fixnum) (defaults to: 16000)

    Samples per second for recording, e.g. 16000 for 16kHz

  • default_device (String) (defaults to: nil)

    The device name

  • ps_api (Object) (defaults to: nil)

    A SphinxAD API implementation to use, API::SphinxAD if not provided


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_apiObject


71
72
73
# File 'lib/pocketsphinx/microphone.rb', line 71

def ps_api
  @ps_api || API::SphinxAD
end

#ps_audio_deviceObject (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_rateObject (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_deviceObject


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.

Parameters:

  • buffer (FFI::Pointer)

    16bit buffer of at least max_samples in size

Returns:

  • (Fixnum)

    Samples actually read (could be 0 since non-blocking); nil if not recording and no more samples remaining to be read from most recent recording.


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

Parameters:

  • max_samples (Fixnum) (defaults to: 2048)

    The maximum samples we tried to read from the audio device


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

#recordObject


32
33
34
35
36
# File 'lib/pocketsphinx/microphone.rb', line 32

def record
  start_recording
  yield
  stop_recording
end

#start_recordingObject


38
39
40
# File 'lib/pocketsphinx/microphone.rb', line 38

def start_recording
  api_call :ad_start_rec, @ps_audio_device
end

#stop_recordingObject


42
43
44
# File 'lib/pocketsphinx/microphone.rb', line 42

def stop_recording
  api_call :ad_stop_rec, @ps_audio_device
end