Class: Pocketsphinx::SpeechRecognizer
- Inherits:
-
Object
- Object
- Pocketsphinx::SpeechRecognizer
show all
- Defined in:
- lib/pocketsphinx/speech_recognizer.rb
Overview
Reads audio data from a recordable interface and decodes it into utterances
Essentially orchestrates interaction between Recordable and Decoder, and detects new utterances.
Constant Summary
collapse
- ALGORITHMS =
[:after_speech, :continuous]
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(configuration = nil) ⇒ SpeechRecognizer
13
14
15
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 13
def initialize(configuration = nil)
@configuration = configuration
end
|
Instance Attribute Details
#configuration ⇒ Object
25
26
27
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 25
def configuration
@configuration ||= Configuration.default
end
|
#decoder ⇒ Object
21
22
23
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 21
def decoder
@decoder ||= Decoder.new(configuration)
end
|
#recordable ⇒ Object
17
18
19
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 17
def recordable
@recordable or raise "A SpeechRecognizer must have a recordable interface"
end
|
Instance Method Details
#algorithm ⇒ Symbol
Determine which algorithm to use for co-ordinating speech recognition
:continuous yields as soon as any hypothesis is available :after_speech yields hypothesis on speech -> silence transition if one exists Default is :after_speech
97
98
99
100
101
102
103
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 97
def algorithm
if configuration.respond_to?(:recognition_algorithm)
configuration.recognition_algorithm
else
ALGORITHMS.first
end
end
|
#in_speech? ⇒ Boolean
62
63
64
65
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 62
def in_speech?
decoder.in_speech?
end
|
#pause ⇒ Object
71
72
73
74
75
76
77
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 71
def pause
recognizing?.tap do |was_recognizing|
stop if was_recognizing
yield
start if was_recognizing
end
end
|
#recognize(max_samples = 2048, &b) ⇒ Object
Recognize speech and yield hypotheses in infinite loop
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 46
def recognize(max_samples = 2048, &b)
unless ALGORITHMS.include?(algorithm)
raise NotImplementedError, "Unknown speech recognition algorithm: #{algorithm}"
end
start unless recognizing?
FFI::MemoryPointer.new(:int16, max_samples) do |buffer|
loop do
send("recognize_#{algorithm}", max_samples, buffer, &b) or break
end
end
ensure
stop
end
|
#recognizing? ⇒ Boolean
67
68
69
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 67
def recognizing?
@recognizing == true
end
|
Reinitialize the decoder with updated configuration.
See Decoder#reconfigure
35
36
37
38
39
40
41
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 35
def reconfigure(configuration = nil)
self.configuration = configuration if configuration
pause do
decoder.reconfigure(configuration)
end
end
|
#start ⇒ Object
79
80
81
82
83
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 79
def start
recordable.start_recording
decoder.start_utterance
@recognizing = true
end
|
#stop ⇒ Object
85
86
87
88
89
|
# File 'lib/pocketsphinx/speech_recognizer.rb', line 85
def stop
decoder.end_utterance
recordable.stop_recording
@recognizing = false
end
|