Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

trimmer.py 3.8 KB

You have to be logged in to leave a comment. Sign In
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
  1. import scipy.io.wavfile
  2. def split_multiple_recordings(audio, min_silence_duration=0.25, noise_threshold=150, sample_rate_hz=8e3):
  3. """ Accepts a numpy array of audio data and splits it at the points of silence into multiple arrays of data.
  4. :param audio: numpy array of audio data
  5. :param min_silence_duration: the required period of silence to split the recording
  6. :param sample_rate_hz: the sample rate of the audio
  7. :return: a list of split numpy arrays
  8. """
  9. # A list of tuples (start, stop)
  10. min_silence_frame = sample_rate_hz * min_silence_duration
  11. silence_zones = []
  12. zone_start = None
  13. zone_end = None
  14. for idx, point in enumerate(audio):
  15. if abs(point) < noise_threshold and zone_start is None:
  16. zone_start = idx
  17. if abs(point) > noise_threshold and zone_start is not None:
  18. zone_end = idx
  19. # If we are in a silent zone and we come to the end point
  20. if zone_start is not None and zone_end and abs(point) > noise_threshold:
  21. if (zone_end - zone_start) > min_silence_frame:
  22. silence_zones.append((zone_start, zone_end))
  23. zone_start = None
  24. zone_end = None
  25. # Split the recording by the zones
  26. split_recordings = []
  27. for idx, zone in enumerate(silence_zones):
  28. if idx == 0:
  29. start = 0
  30. else:
  31. start = silence_zones[idx - 1][1]
  32. end = zone[0]
  33. split_recordings.append(audio[start:end])
  34. return split_recordings
  35. def trim_silence(audio, noise_threshold=150):
  36. """ Removes the silence at the beginning and end of the passed audio data
  37. :param audio: numpy array of audio
  38. :param noise_threshold: the maximum amount of noise that is considered silence
  39. :return: a trimmed numpy array
  40. """
  41. start = None
  42. end = None
  43. for idx, point in enumerate(audio):
  44. if abs(point) > noise_threshold:
  45. start = idx
  46. break
  47. # Reverse the array for trimming the end
  48. for idx, point in enumerate(audio[::-1]):
  49. if abs(point) > noise_threshold:
  50. end = len(audio) - idx
  51. break
  52. return audio[start:end]
  53. def trim_silence_file(file_path, noise_threshold=150):
  54. """Accepts a file path, trims the audio and overwrites the original file with the trimmed version.
  55. :param file_path: file to trim
  56. :param noise_threshold: the maximum amount of noise that is considered silence
  57. :return: None
  58. """
  59. rate, audio = scipy.io.wavfile.read(file_path)
  60. trimmed_audio = trim_silence(audio, noise_threshold=noise_threshold)
  61. scipy.io.wavfile.write(file_path, rate, trimmed_audio)
  62. def split_multiple_recordings_file(file_path, min_silence_duration=0.25, noise_threshold=150):
  63. """Accepts a file_path of a `wav` file, splits it by it's silent periods and creates new files for each split.
  64. This is useful when contributing recordings, as it allwos one to record multiple pronunciations in one file and then
  65. split them programmaticly.
  66. :param file_path: wav file path to split
  67. :param min_silence_duration: the required period of silence to split the recording
  68. :param noise_threshold: the maximum amount of noise that is considered silence
  69. :return:
  70. """
  71. rate, audio = scipy.io.wavfile.read(file_path)
  72. split_recordings = split_multiple_recordings(audio, min_silence_duration=min_silence_duration,
  73. noise_threshold=noise_threshold, sample_rate_hz=rate)
  74. if file_path.count('.') != 1:
  75. raise Exception('File_path must contain exactly one period, usually in extension. IE: /home/test.wav')
  76. for idx, recording in enumerate(split_recordings):
  77. new_file_path = file_path.split('.')[0] + '_' + str(idx) + ".wav"
  78. scipy.io.wavfile.write(new_file_path, rate, recording)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...