Browse Source

startup: define function `trim_where`

Fabian Peter Hammerle 3 years ago
parent
commit
3dc22bb705
1 changed files with 23 additions and 0 deletions
  1. 23 0
      profile_default/startup/init.py

+ 23 - 0
profile_default/startup/init.py

@@ -1,6 +1,29 @@
+import typing
+
 import scipy.io.wavfile
 
 
+def trim_where(
+    # https://docs.python.org/3.8/library/collections.abc.html#collections-abstract-base-classes
+    sequence: typing.Sequence,
+    condition: typing.Sequence[bool],
+) -> typing.Sequence:
+    start = 0
+    for item_condition in condition:
+        if item_condition:
+            start += 1
+        else:
+            break
+    stop = len(sequence)
+    assert stop == len(condition)
+    for item_condition in condition[::-1]:
+        if item_condition:
+            stop -= 1
+        else:
+            break
+    return sequence[start:stop]
+
+
 def wavfile_read_mono(path):
     # https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html
     rate, data = scipy.io.wavfile.read(path)