Class: Pocketsphinx::Decoder

Inherits:
Struct
  • Object
show all
Includes:
API::CallHelpers
Defined in:
lib/pocketsphinx/decoder.rb

Defined Under Namespace

Classes: Hypothesis

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API::CallHelpers

#api_call

Instance Attribute Details

#configurationObject

Returns the value of attribute configuration

Returns:

  • (Object)

    the current value of configuration



2
3
4
# File 'lib/pocketsphinx/decoder.rb', line 2

def configuration
  @configuration
end

#ps_apiObject



142
143
144
# File 'lib/pocketsphinx/decoder.rb', line 142

def ps_api
  @ps_api || API::Pocketsphinx
end

Instance Method Details

#decode(audio_path_or_file, max_samples = 2048) ⇒ Object

Decode a raw audio stream as a single utterance, opening a file if path given

See #decode_raw

Parameters:

  • audio_path_or_file (IO)

    The raw audio stream or file path to decode as a single utterance

  • max_samples (Fixnum) (defaults to: 2048)

    The maximum samples to process from the stream on each iteration



36
37
38
39
40
41
42
43
# File 'lib/pocketsphinx/decoder.rb', line 36

def decode(audio_path_or_file, max_samples = 2048)
  case audio_path_or_file
  when String
    File.open(audio_path_or_file, 'rb') { |f| decode_raw(f, max_samples) }
  else
    decode_raw(audio_path_or_file, max_samples)
  end
end

#decode_raw(audio_file, max_samples = 2048) ⇒ Object

Decode a raw audio stream as a single utterance.

No headers are recognized in this files. The configuration parameters samprate and input_endian are used to determine the sampling rate and endianness of the stream, respectively. Audio is always assumed to be 16-bit signed PCM.

Parameters:

  • audio_file (IO)

    The raw audio stream to decode as a single utterance

  • max_samples (Fixnum) (defaults to: 2048)

    The maximum samples to process from the stream on each iteration



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/pocketsphinx/decoder.rb', line 53

def decode_raw(audio_file, max_samples = 2048)
  start_utterance

  FFI::MemoryPointer.new(:int16, max_samples) do |buffer|
    while data = audio_file.read(max_samples * 2)
      buffer.write_string(data)
      process_raw(buffer, data.length / 2)
    end
  end

  end_utterance
end

#end_utteranceObject

End utterance processing



88
89
90
# File 'lib/pocketsphinx/decoder.rb', line 88

def end_utterance
  api_call :ps_end_utt, ps_decoder
end

#get_searchObject

Returns name of curent search in decoder



122
123
124
# File 'lib/pocketsphinx/decoder.rb', line 122

def get_search
  ps_api.ps_get_search(ps_decoder)
end

#hypothesisHypothesis

Get hypothesis string (with #path_score and #utterance_id).

Returns:

  • (Hypothesis)

    Hypothesis (behaves like a string)



100
101
102
103
104
105
106
107
108
109
# File 'lib/pocketsphinx/decoder.rb', line 100

def hypothesis
  mp_path_score = FFI::MemoryPointer.new(:int32, 1)

  hypothesis = ps_api.ps_get_hyp(ps_decoder, mp_path_score)

  hypothesis.nil? ? nil : Hypothesis.new(
    hypothesis,
    mp_path_score.get_int32(0)
  )
end

#in_speech?Boolean

Checks if the last feed audio buffer contained speech

Returns:

  • (Boolean)


93
94
95
# File 'lib/pocketsphinx/decoder.rb', line 93

def in_speech?
  ps_api.ps_get_in_speech(ps_decoder) != 0
end

#process_raw(buffer, size, no_search = false, full_utt = false) ⇒ Object

Decode raw audio data.

Parameters:

  • no_search (Boolean) (defaults to: false)

    If non-zero, perform feature extraction but don’t do any recognition yet. This may be necessary if your processor has trouble doing recognition in real-time.

  • full_utt (Boolean) (defaults to: false)

    If non-zero, this block of data is a full utterance worth of data. This may allow the recognizer to produce more accurate results.

Returns:

  • Number of frames of data searched



74
75
76
# File 'lib/pocketsphinx/decoder.rb', line 74

def process_raw(buffer, size, no_search = false, full_utt = false)
  api_call :ps_process_raw, ps_decoder, buffer, size, no_search ? 1 : 0, full_utt ? 1 : 0
end

#ps_decoderObject



146
147
148
149
# File 'lib/pocketsphinx/decoder.rb', line 146

def ps_decoder
  init_decoder if @ps_decoder.nil?
  @ps_decoder
end

#reconfigure(configuration = nil) ⇒ Object

Reinitialize the decoder with updated configuration.

This function allows you to switch the acoustic model, dictionary, or other configuration without creating an entirely new decoding object.

Parameters:

  • configuration (Configuration) (defaults to: nil)

    An optional new configuration to use. If this is nil, the previous configuration will be reloaded, with any changes applied.



25
26
27
28
# File 'lib/pocketsphinx/decoder.rb', line 25

def reconfigure(configuration = nil)
  self.configuration = configuration if configuration
  reinit_decoder
end

#set_jsgf_string(jsgf_string, name = 'default') ⇒ Object

Adds new search using JSGF model.

Convenience method to parse JSGF model from string and create a search.

Parameters:

  • jsgf_string (String)

    The JSGF grammar

  • name (String) (defaults to: 'default')

    The search name



117
118
119
# File 'lib/pocketsphinx/decoder.rb', line 117

def set_jsgf_string(jsgf_string, name = 'default')
  api_call :ps_set_jsgf_string, ps_decoder, name, jsgf_string
end

#set_search(name = 'default') ⇒ Object

Actives search with the provided name.

Activates search with the provided name. The search must be added before using either ps_set_fsg(), ps_set_lm() or ps_set_kws().



130
131
132
# File 'lib/pocketsphinx/decoder.rb', line 130

def set_search(name = 'default')
  api_call :ps_set_search, ps_decoder, name
end

#start_utteranceObject

Start utterance processing.

This function should be called before any utterance data is passed to the decoder. It marks the start of a new utterance and reinitializes internal data structures.



83
84
85
# File 'lib/pocketsphinx/decoder.rb', line 83

def start_utterance
  api_call :ps_start_utt, ps_decoder
end

#unset_search(name = 'default') ⇒ Object

Unsets the search and releases related resources.

Unsets the search previously added with using either ps_set_fsg(), ps_set_lm() or ps_set_kws().



138
139
140
# File 'lib/pocketsphinx/decoder.rb', line 138

def unset_search(name = 'default')
  api_call :ps_unset_search, ps_decoder, name
end