@@ -393,7 +393,7 @@ def align_sequence(
393393 or None to not save them.
394394
395395 Returns:
396- Iterable[soundswallower.Segment ]: Word (or other unit) alignments.
396+ Iterable[soundswallower.Seg ]: Word (or other unit) alignments.
397397
398398 Raises:
399399 RuntimeError: If alignment fails (TODO: figure out why).
@@ -432,38 +432,41 @@ def align_sequence(
432432 ps .process_raw (audio_segment .raw_data , no_search = False , full_utt = True )
433433 ps .end_utt ()
434434
435- return ps .seg ()
435+ return ps .seg
436436
437437
438438def process_segmentation (
439- segmentation : Iterable [soundswallower .Segment ],
439+ segmentation : Iterable [soundswallower .Seg ],
440440 curr_removed_segments : List [dict ],
441441 noisewords : Set [str ],
442442 frame_size : float ,
443443 debug_aligner : Optional [bool ] = False ,
444444) -> List [Dict [str , Any ]]:
445445 """Correct output alignments based on do-not-align segments."""
446- aligned_words = []
446+ aligned_words : List [ Dict [ str , Any ]] = []
447447 for word_seg in segmentation :
448- if word_seg .word in noisewords :
448+ if word_seg .text in noisewords :
449449 continue
450- start = word_seg .start_frame * frame_size
451- end = (word_seg .end_frame + 1 ) * frame_size
452- # change to ms
453- start_ms = int (start * 1000 )
454- end_ms = int (end * 1000 )
450+ start = word_seg .start
451+ end = word_seg .start + word_seg .duration
452+ # round to milliseconds to avoid imprecisions
453+ start_ms = round (start * 1000 )
454+ end_ms = round (end * 1000 )
455+ # possibly adjust for removed sections
455456 if curr_removed_segments :
456457 start_ms += calculate_adjustment (start_ms , curr_removed_segments )
457458 end_ms += calculate_adjustment (end_ms , curr_removed_segments )
458459 start_ms , end_ms = correct_adjustments (
459460 start_ms , end_ms , curr_removed_segments
460461 )
461- # change back to seconds to write to smil
462- start = start_ms / 1000
463- end = end_ms / 1000
464- aligned_words .append ({"id" : word_seg .word , "start" : start , "end" : end })
462+ # change back to seconds
463+ start = start_ms / 1000
464+ end = end_ms / 1000
465+ if aligned_words :
466+ assert start >= aligned_words [- 1 ]["end" ]
467+ aligned_words .append ({"id" : word_seg .text , "start" : start , "end" : end })
465468 if debug_aligner :
466- LOGGER .info ("Segment: %s (%.3f : %.3f)" , word_seg .word , start , end )
469+ LOGGER .info ("Segment: %s (%.3f : %.3f)" , word_seg .text , start , end )
467470 return aligned_words
468471
469472
@@ -634,19 +637,14 @@ def align_audio(
634637 audio_length_in_ms ,
635638 removed_segments ,
636639 )
637- try :
638- # Process raw segmentation, adjusting alignments for DNA
639- aligned_words = process_segmentation (
640- segmentation = segmentation ,
641- curr_removed_segments = curr_removed_segments ,
642- noisewords = noisewords ,
643- frame_size = frame_size ,
644- debug_aligner = debug_aligner ,
645- )
646- except RuntimeError :
647- # We need this here because soundswallower<=0.2.2 will
648- # raise an error rather than returning an empty list
649- aligned_words = []
640+ # Process raw segmentation, adjusting alignments for DNA
641+ aligned_words = process_segmentation (
642+ segmentation = segmentation ,
643+ curr_removed_segments = curr_removed_segments ,
644+ noisewords = noisewords ,
645+ frame_size = frame_size ,
646+ debug_aligner = debug_aligner ,
647+ )
650648
651649 if len (aligned_words ) != len (word_sequence .words ):
652650 LOGGER .warning (f"Align mode { align_modes [j ]} failed for sequence { i } ." )
@@ -990,8 +988,8 @@ def get_word_texts_and_sentences(
990988 "end": end time
991989 """
992990 sentences = []
993- sent_words : List [dict ] = []
994- all_words = []
991+ sent_words : List [Dict [ str , Any ] ] = []
992+ all_words : List [ Dict [ str , Any ]] = []
995993 prev_sent_el = None
996994 for word in words :
997995 # The sentence is considered the set of words under the same <s> element.
@@ -1009,6 +1007,8 @@ def get_word_texts_and_sentences(
10091007 "start" : word ["start" ],
10101008 "end" : word ["end" ],
10111009 }
1010+ if all_words :
1011+ assert word_with_text ["start" ] >= all_words [- 1 ]["end" ]
10121012 sent_words .append (word_with_text )
10131013 all_words .append (word_with_text )
10141014 if sent_words :
0 commit comments