init.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import pathlib
  2. import typing
  3. import numpy
  4. import scipy.io.wavfile
  5. def trim_where(
  6. # https://docs.python.org/3.8/library/collections.abc.html#collections-abstract-base-classes
  7. sequence: typing.Sequence,
  8. condition: typing.Sequence[bool],
  9. ) -> typing.Sequence:
  10. start = 0
  11. for item_condition in condition:
  12. if item_condition:
  13. start += 1
  14. else:
  15. break
  16. stop = len(sequence)
  17. assert stop == len(condition)
  18. for item_condition in condition[::-1]:
  19. if item_condition:
  20. stop -= 1
  21. else:
  22. break
  23. return sequence[start:stop]
  24. def wavfile_read_mono(
  25. path: typing.Union[pathlib.Path, str]
  26. ) -> typing.Tuple[int, numpy.ndarray]:
  27. # https://docs.scipy.org/doc/scipy/reference/generated/scipy.io.wavfile.read.html
  28. rate, data = scipy.io.wavfile.read(path)
  29. data_first_channel = data[:, 0]
  30. for channel_index in range(1, data.shape[1]):
  31. assert (data_first_channel == data[:, channel_index]).all()
  32. return rate, data_first_channel