init.py 945 B

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