Class: Pocketsphinx::Decoder
- Inherits:
-
Object
- Object
- Pocketsphinx::Decoder
- Defined in:
- lib/pocketsphinx/decoder.rb
Constant Summary collapse
- Error =
Class.new(StandardError)
Instance Attribute Summary collapse
- #ps_api ⇒ Object
-
#ps_decoder ⇒ Object
readonly
Returns the value of attribute ps_decoder.
Instance Method Summary collapse
-
#decode(audio_path_or_file, max_samples = 2048) ⇒ Object
Decode a raw audio stream as a single utterance, opening a file if path given.
-
#decode_raw(audio_file, max_samples = 2048) ⇒ Object
Decode a raw audio stream as a single utterance.
-
#end_utterance ⇒ Object
End utterance processing.
-
#hypothesis ⇒ String
Get hypothesis string and path score.
-
#in_speech? ⇒ Boolean
Checks if the last feed audio buffer contained speech.
-
#initialize(configuration) ⇒ Decoder
constructor
A new instance of Decoder.
-
#process_raw(buffer, size, no_search = false, full_utt = false) ⇒ Object
Decode raw audio data.
-
#start_utterance(name = nil) ⇒ Object
Start utterance processing.
Constructor Details
#initialize(configuration) ⇒ Decoder
Returns a new instance of Decoder.
8 9 10 11 |
# File 'lib/pocketsphinx/decoder.rb', line 8 def initialize(configuration) @configuration = configuration @ps_decoder = ps_api.ps_init(configuration.ps_config) end |
Instance Attribute Details
#ps_api ⇒ Object
96 97 98 |
# File 'lib/pocketsphinx/decoder.rb', line 96 def ps_api @ps_api || API::Pocketsphinx end |
#ps_decoder ⇒ Object (readonly)
Returns the value of attribute ps_decoder.
5 6 7 |
# File 'lib/pocketsphinx/decoder.rb', line 5 def ps_decoder @ps_decoder 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
19 20 21 22 23 24 25 26 |
# File 'lib/pocketsphinx/decoder.rb', line 19 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.
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/pocketsphinx/decoder.rb', line 36 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_utterance ⇒ Object
End utterance processing
77 78 79 80 81 |
# File 'lib/pocketsphinx/decoder.rb', line 77 def end_utterance ps_api.ps_end_utt(@ps_decoder).tap do |result| raise Error, "Decoder#end_utterance failed with error code #{result}" if result < 0 end end |
#hypothesis ⇒ String
Expand to return path score and utterance ID
Get hypothesis string and path score.
92 93 94 |
# File 'lib/pocketsphinx/decoder.rb', line 92 def hypothesis ps_api.ps_get_hyp(@ps_decoder, nil, nil) end |
#in_speech? ⇒ Boolean
Checks if the last feed audio buffer contained speech
84 85 86 |
# File 'lib/pocketsphinx/decoder.rb', line 84 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.
57 58 59 60 61 |
# File 'lib/pocketsphinx/decoder.rb', line 57 def process_raw(buffer, size, no_search = false, full_utt = false) ps_api.ps_process_raw(@ps_decoder, buffer, size, no_search ? 1 : 0, full_utt ? 1 : 0).tap do |result| raise Error, "Decoder#process_raw failed with error code #{result}" if result < 0 end end |
#start_utterance(name = nil) ⇒ Object
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.
70 71 72 73 74 |
# File 'lib/pocketsphinx/decoder.rb', line 70 def start_utterance(name = nil) ps_api.ps_start_utt(@ps_decoder, name).tap do |result| raise Error, "Decoder#start_utterance failed with error code #{result}" if result < 0 end end |