Class: Pocketsphinx::SpeechRecognizer

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

Returns a new instance of SpeechRecognizer.


13
14
15
# File 'lib/pocketsphinx/speech_recognizer.rb', line 13

def initialize(configuration = nil)
  @configuration = configuration
end

Instance Attribute Details

#configurationObject


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

def configuration
  @configuration ||= Configuration.default
end

#decoderObject


21
22
23
# File 'lib/pocketsphinx/speech_recognizer.rb', line 21

def decoder
  @decoder ||= Decoder.new(configuration)
end

#recordableObject


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

#algorithmSymbol

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

Returns:

  • (Symbol)

    :continuous or :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

Returns:

  • (Boolean)

62
63
64
65
# File 'lib/pocketsphinx/speech_recognizer.rb', line 62

def in_speech?
  # Use Pocketsphinx's implementation by default
  decoder.in_speech?
end

#pauseObject


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

Parameters:

  • max_samples (Fixnum) (defaults to: 2048)

    Number of samples to process at a time


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

Returns:

  • (Boolean)

67
68
69
# File 'lib/pocketsphinx/speech_recognizer.rb', line 67

def recognizing?
  @recognizing == true
end

#reconfigure(configuration = nil) ⇒ Object

Reinitialize the decoder with updated configuration.

See Decoder#reconfigure

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.


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

#startObject


79
80
81
82
83
# File 'lib/pocketsphinx/speech_recognizer.rb', line 79

def start
  recordable.start_recording
  decoder.start_utterance
  @recognizing = true
end

#stopObject


85
86
87
88
89
# File 'lib/pocketsphinx/speech_recognizer.rb', line 85

def stop
  decoder.end_utterance
  recordable.stop_recording
  @recognizing = false
end