@@ -20,9 +20,6 @@ type context struct {
2020 params whisper.Params
2121}
2222
23- // Make sure context adheres to the interface
24- var _ Context = (* context )(nil )
25-
2623///////////////////////////////////////////////////////////////////////////////
2724// LIFECYCLE
2825
@@ -241,26 +238,49 @@ func (context *context) Process(
241238 return nil
242239}
243240
244- // Return the next segment of tokens
241+ // NextSegment returns the next segment from the context buffer
245242func (context * context ) NextSegment () (Segment , error ) {
246243 if context .model .ctx == nil {
247244 return Segment {}, ErrInternalAppError
248245 }
249246 if context .n >= context .model .ctx .Whisper_full_n_segments () {
250247 return Segment {}, io .EOF
251248 }
252-
253- // Populate result
254249 result := toSegment (context .model .ctx , context .n )
255-
256- // Increment the cursor
257250 context .n ++
258-
259- // Return success
260251 return result , nil
261252}
262253
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
264284func (context * context ) IsText (t Token ) bool {
265285 switch {
266286 case context .IsBEG (t ):
@@ -280,70 +300,34 @@ func (context *context) IsText(t Token) bool {
280300 }
281301}
282302
283- // Test for "begin" token
284303func (context * context ) IsBEG (t Token ) bool {
285304 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_beg ()
286305}
287306
288- // Test for "start of transcription" token
289307func (context * context ) IsSOT (t Token ) bool {
290308 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_sot ()
291309}
292310
293- // Test for "end of transcription" token
294311func (context * context ) IsEOT (t Token ) bool {
295312 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_eot ()
296313}
297314
298- // Test for "start of prev" token
299315func (context * context ) IsPREV (t Token ) bool {
300316 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_prev ()
301317}
302318
303- // Test for "start of lm" token
304319func (context * context ) IsSOLM (t Token ) bool {
305320 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_solm ()
306321}
307322
308- // Test for "No timestamps" token
309323func (context * context ) IsNOT (t Token ) bool {
310324 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_not ()
311325}
312326
313- // Test for token associated with a specific language
314327func (context * context ) IsLANG (t Token , lang string ) bool {
315328 if id := context .model .ctx .Whisper_lang_id (lang ); id >= 0 {
316329 return whisper .Token (t .Id ) == context .model .ctx .Whisper_token_lang (id )
317330 } else {
318331 return false
319332 }
320333}
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