|
@@ -1,3 +1,4 @@
|
|
|
+import itertools
|
|
|
import pathlib
|
|
|
import typing
|
|
|
|
|
@@ -5,6 +6,25 @@ import numpy
|
|
|
import scipy.io.wavfile
|
|
|
|
|
|
|
|
|
+def split_sequence_by_delimiter(
|
|
|
+ sequence: typing.Sequence, delimiter: typing.Any, delimiter_min_length: int = 1
|
|
|
+) -> typing.Iterator[typing.Sequence]:
|
|
|
+ slice_start_index, slice_length = 0, 0
|
|
|
+ for is_delimiter, group in itertools.groupby(
|
|
|
+ sequence, key=lambda item: item == delimiter
|
|
|
+ ):
|
|
|
+ group_length = sum(1 for _ in group)
|
|
|
+ if is_delimiter and group_length >= delimiter_min_length:
|
|
|
+ if slice_length > 0:
|
|
|
+ yield sequence[slice_start_index : slice_start_index + slice_length]
|
|
|
+ slice_start_index += slice_length + group_length
|
|
|
+ slice_length = 0
|
|
|
+ else:
|
|
|
+ slice_length += group_length
|
|
|
+ if slice_length > 0:
|
|
|
+ yield sequence[slice_start_index : slice_start_index + slice_length]
|
|
|
+
|
|
|
+
|
|
|
def trim_where(
|
|
|
|
|
|
sequence: typing.Sequence,
|