12345678910111213141516171819202122232425262728293031323334353637 |
- import pathlib
- import typing
- import numpy
- 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: typing.Union[pathlib.Path, str]
- ) -> typing.Tuple[int, numpy.ndarray]:
- # https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html
- rate, data = scipy.io.wavfile.read(path)
- data_first_channel = data[:, 0]
- for channel_index in range(1, data.shape[1]):
- assert (data_first_channel == data[:, channel_index]).all()
- return rate, data_first_channel
|