@@ -20,9 +20,6 @@ type context struct {
20
20
params whisper.Params
21
21
}
22
22
23
- // Make sure context adheres to the interface
24
- var _ Context = (* context )(nil )
25
-
26
23
///////////////////////////////////////////////////////////////////////////////
27
24
// LIFECYCLE
28
25
@@ -241,26 +238,49 @@ func (context *context) Process(
241
238
return nil
242
239
}
243
240
244
- // Return the next segment of tokens
241
+ // NextSegment returns the next segment from the context buffer
245
242
func (context * context ) NextSegment () (Segment , error ) {
246
243
if context .model .ctx == nil {
247
244
return Segment {}, ErrInternalAppError
248
245
}
249
246
if context .n >= context .model .ctx .Whisper_full_n_segments () {
250
247
return Segment {}, io .EOF
251
248
}
252
-
253
- // Populate result
254
249
result := toSegment (context .model .ctx , context .n )
255
-
256
- // Increment the cursor
257
250
context .n ++
258
-
259
- // Return success
260
251
return result , nil
261
252
}
262
253
263
- // Test for text tokens
254
+ ///////////////////////////////////////////////////////////////////////////////
255
+ // PRIVATE METHODS
256
+
257
+ func toSegment (ctx * whisper.Context , n int ) Segment {
258
+ return Segment {
259
+ Num : n ,
260
+ Text : strings .TrimSpace (ctx .Whisper_full_get_segment_text (n )),
261
+ Start : time .Duration (ctx .Whisper_full_get_segment_t0 (n )) * time .Millisecond * 10 ,
262
+ End : time .Duration (ctx .Whisper_full_get_segment_t1 (n )) * time .Millisecond * 10 ,
263
+ Tokens : toTokens (ctx , n ),
264
+ }
265
+ }
266
+
267
+ func toTokens (ctx * whisper.Context , n int ) []Token {
268
+ result := make ([]Token , ctx .Whisper_full_n_tokens (n ))
269
+ for i := 0 ; i < len (result ); i ++ {
270
+ data := ctx .Whisper_full_get_token_data (n , i )
271
+
272
+ result [i ] = Token {
273
+ Id : int (ctx .Whisper_full_get_token_id (n , i )),
274
+ Text : ctx .Whisper_full_get_token_text (n , i ),
275
+ P : ctx .Whisper_full_get_token_p (n , i ),
276
+ Start : time .Duration (data .T0 ()) * time .Millisecond * 10 ,
277
+ End : time .Duration (data .T1 ()) * time .Millisecond * 10 ,
278
+ }
279
+ }
280
+ return result
281
+ }
282
+
283
+ // Token helpers
264
284
func (context * context ) IsText (t Token ) bool {
265
285
switch {
266
286
case context .IsBEG (t ):
@@ -280,70 +300,34 @@ func (context *context) IsText(t Token) bool {
280
300
}
281
301
}
282
302
283
- // Test for "begin" token
284
303
func (context * context ) IsBEG (t Token ) bool {
285
304
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_beg ()
286
305
}
287
306
288
- // Test for "start of transcription" token
289
307
func (context * context ) IsSOT (t Token ) bool {
290
308
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_sot ()
291
309
}
292
310
293
- // Test for "end of transcription" token
294
311
func (context * context ) IsEOT (t Token ) bool {
295
312
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_eot ()
296
313
}
297
314
298
- // Test for "start of prev" token
299
315
func (context * context ) IsPREV (t Token ) bool {
300
316
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_prev ()
301
317
}
302
318
303
- // Test for "start of lm" token
304
319
func (context * context ) IsSOLM (t Token ) bool {
305
320
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_solm ()
306
321
}
307
322
308
- // Test for "No timestamps" token
309
323
func (context * context ) IsNOT (t Token ) bool {
310
324
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_not ()
311
325
}
312
326
313
- // Test for token associated with a specific language
314
327
func (context * context ) IsLANG (t Token , lang string ) bool {
315
328
if id := context .model .ctx .Whisper_lang_id (lang ); id >= 0 {
316
329
return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_lang (id )
317
330
} else {
318
331
return false
319
332
}
320
333
}
321
-
322
- ///////////////////////////////////////////////////////////////////////////////
323
- // PRIVATE METHODS
324
-
325
- func toSegment (ctx * whisper.Context , n int ) Segment {
326
- return Segment {
327
- Num : n ,
328
- Text : strings .TrimSpace (ctx .Whisper_full_get_segment_text (n )),
329
- Start : time .Duration (ctx .Whisper_full_get_segment_t0 (n )) * time .Millisecond * 10 ,
330
- End : time .Duration (ctx .Whisper_full_get_segment_t1 (n )) * time .Millisecond * 10 ,
331
- Tokens : toTokens (ctx , n ),
332
- }
333
- }
334
-
335
- func toTokens (ctx * whisper.Context , n int ) []Token {
336
- result := make ([]Token , ctx .Whisper_full_n_tokens (n ))
337
- for i := 0 ; i < len (result ); i ++ {
338
- data := ctx .Whisper_full_get_token_data (n , i )
339
-
340
- result [i ] = Token {
341
- Id : int (ctx .Whisper_full_get_token_id (n , i )),
342
- Text : ctx .Whisper_full_get_token_text (n , i ),
343
- P : ctx .Whisper_full_get_token_p (n , i ),
344
- Start : time .Duration (data .T0 ()) * time .Millisecond * 10 ,
345
- End : time .Duration (data .T1 ()) * time .Millisecond * 10 ,
346
- }
347
- }
348
- return result
349
- }
0 commit comments