-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathdecoder_spec.rb
More file actions
270 lines (213 loc) · 7.15 KB
/
decoder_spec.rb
File metadata and controls
270 lines (213 loc) · 7.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
require 'spec_helper'
describe Pocketsphinx::Decoder do
subject { Pocketsphinx::Decoder.new(configuration, ps_decoder) }
let(:ps_api) { subject.ps_api }
let(:ps_decoder) { double }
let(:configuration) { Pocketsphinx::Configuration.default }
before do
subject.ps_api = double
allow(ps_api).to receive(:ps_init).and_return(ps_decoder)
end
describe 'initialization' do
it 'initializes the underlying Pocketsphinx decoder when one is not provided' do
expect(Pocketsphinx::API::Pocketsphinx)
.to receive(:ps_init)
.with(configuration.ps_config)
.and_return(ps_decoder)
Pocketsphinx::Decoder.new(configuration)
end
end
describe '#reconfigure' do
it 'calls libpocketsphinx and the configuration post initialize hook' do
expect(ps_api)
.to receive(:ps_reinit)
.with(subject.ps_decoder, configuration.ps_config)
.and_return(0)
configuration.define_singleton_method(:post_init_decoder) { |decoder| }
expect(configuration)
.to receive(:post_init_decoder)
.with(subject)
subject.reconfigure
end
it 'sets a new configuration if one is passed' do
new_config = Struct.new(:ps_config).new(:ps_config)
expect(ps_api)
.to receive(:ps_reinit)
.with(subject.ps_decoder, new_config.ps_config)
.and_return(0)
subject.reconfigure(new_config)
expect(subject.configuration).to be(new_config)
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_reinit)
.with(subject.ps_decoder, configuration.ps_config)
.and_return(-1)
expect { subject.reconfigure }
.to raise_exception "Decoder#reconfigure failed with error code -1"
end
end
describe '#process_raw' do
it 'calls libpocketsphinx' do
FFI::MemoryPointer.new(:int16, 2048) do |buffer|
expect(ps_api)
.to receive(:ps_process_raw)
.with(subject.ps_decoder, buffer, 2048, 0, 0)
.and_return(0)
subject.process_raw(buffer, 2048, false, false)
end
end
it 'raises an exception on error' do
FFI::MemoryPointer.new(:int16, 2048) do |buffer|
expect(ps_api)
.to receive(:ps_process_raw)
.with(subject.ps_decoder, buffer, 2048, 0, 0)
.and_return(-1)
expect { subject.process_raw(buffer, 2048, false, false) }
.to raise_exception "Decoder#process_raw failed with error code -1"
end
end
end
describe '#start_utterance' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_start_utt)
.with(subject.ps_decoder)
.and_return(0)
subject.start_utterance
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_start_utt)
.with(subject.ps_decoder)
.and_return(-1)
expect { subject.start_utterance }
.to raise_exception "Decoder#start_utterance failed with error code -1"
end
end
describe '#end_utterance' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_end_utt)
.with(subject.ps_decoder)
.and_return(0)
subject.end_utterance
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_end_utt)
.with(subject.ps_decoder)
.and_return(-1)
expect { subject.end_utterance }
.to raise_exception "Decoder#end_utterance failed with error code -1"
end
end
describe '#in_speech' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_get_in_speech)
.with(subject.ps_decoder)
.and_return(0)
expect(subject.in_speech?).to eq(false)
end
end
describe '#hypothesis' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_get_logmath)
expect(ps_api)
.to receive(:ps_get_hyp) do |ps_decoder, mp_path_score|
expect(ps_decoder).to eq(subject.ps_decoder)
expect(mp_path_score).to be_a(FFI::MemoryPointer)
mp_path_score.put_int32(0, 20)
"Hypothesis"
end
expect(ps_api)
.to receive(:logmath_exp) do |logmath, mp_path_score_int|
expect(mp_path_score_int).to be_a(Integer)
1
end
hypothesis = subject.hypothesis
expect(hypothesis).to eq("Hypothesis")
expect(hypothesis.path_score).to eq(20)
expect(hypothesis.posterior_prob).to eq(1)
end
end
describe '#words' do
let(:iterator) { FFI::MemoryPointer.from_string("") }
it 'calls libpocketsphinx' do
expect(ps_api).to receive(:ps_seg_iter).ordered.and_return(iterator)
expect(ps_api).to receive(:ps_seg_frames).ordered do |seg_iter, start_frame, end_frame|
start_frame.put_int16(0, 10)
end_frame.put_int16(0, 20)
end
expect(ps_api).to receive(:ps_seg_word).ordered.and_return("one")
expect(ps_api).to receive(:ps_seg_next).ordered.and_return(iterator)
expect(ps_api).to receive(:ps_seg_frames).ordered do |seg_iter, start_frame, end_frame|
start_frame.put_int16(0, 30)
end_frame.put_int16(0, 40)
end
expect(ps_api).to receive(:ps_seg_word).ordered.and_return("two")
expect(ps_api).to receive(:ps_seg_next).ordered.and_return(FFI::Pointer::NULL)
words = subject.words
expect(words[0]).to eq(Pocketsphinx::Decoder::Word.new("one", 10, 20))
expect(words[1]).to eq(Pocketsphinx::Decoder::Word.new("two", 30, 40))
end
end
describe '#set_jsgf_string' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_set_jsgf_string)
.with(subject.ps_decoder, 'default', 'JSGF')
.and_return(0)
subject.set_jsgf_string('JSGF')
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_set_jsgf_string)
.and_return(-1)
expect { subject.set_jsgf_string('JSGF') }
.to raise_exception "Decoder#set_jsgf_string failed with error code -1"
end
end
describe '#set_search' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_set_search)
.with(subject.ps_decoder, 'search')
.and_return(0)
subject.set_search('search')
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_set_search)
.and_return(-1)
expect { subject.set_search('search') }
.to raise_exception "Decoder#set_search failed with error code -1"
end
end
describe '#unset_search' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_unset_search)
.with(subject.ps_decoder, 'search')
.and_return(0)
subject.unset_search('search')
end
it 'raises an exception on error' do
expect(ps_api)
.to receive(:ps_unset_search)
.and_return(-1)
expect { subject.unset_search('search') }
.to raise_exception "Decoder#unset_search failed with error code -1"
end
end
describe '#get_search' do
it 'calls libpocketsphinx' do
expect(ps_api)
.to receive(:ps_get_search)
.and_return(:search)
expect(subject.get_search).to eq(:search)
end
end
end