bjelkenhed commited on
Commit
b452ae2
1 Parent(s): 677853f

silence timestamps

Browse files
Files changed (1) hide show
  1. babelbox_voice.py +29 -1
babelbox_voice.py CHANGED
@@ -9,6 +9,10 @@ from pathlib import Path
9
  from tqdm import tqdm
10
  import torchaudio
11
  from io import BytesIO
 
 
 
 
12
  logger = datasets.logging.get_logger(__name__)
13
 
14
  _CITATION = """\
@@ -149,6 +153,28 @@ class BabelboxVoice(datasets.GeneratorBasedBuilder):
149
 
150
  sampling_rate = 16000
151
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
152
  def filter_sentence(sentence):
153
  if "... tyst under denna inspelning ..." in sentence: return False
154
  return True
@@ -172,10 +198,12 @@ class BabelboxVoice(datasets.GeneratorBasedBuilder):
172
  audio_len = len(audio_array) / sampling_rate
173
  audio_len = round(audio_len / 2, 2) * 2
174
 
 
 
175
  text = {
176
  "text": metadata_item['sentence'],
177
  "offsets": [
178
- {"text": metadata_item['sentence'], "timestamp": (0.0, audio_len) }
179
  ]
180
  }
181
 
 
9
  from tqdm import tqdm
10
  import torchaudio
11
  from io import BytesIO
12
+ import pydub
13
+ from pydub import AudioSegment
14
+ from pydub.silence import detect_leading_silence
15
+ import numpy as np
16
  logger = datasets.logging.get_logger(__name__)
17
 
18
  _CITATION = """\
 
153
 
154
  sampling_rate = 16000
155
 
156
+ def get_audiosegment(array):
157
+ byte_array = np.int16(array * 2 ** 15).tobytes()
158
+ audio_segment = pydub.AudioSegment(byte_array, frame_rate=16000, sample_width=2, channels=1)
159
+ return audio_segment
160
+
161
+ def get_leading_and_trailing_silence(audio_array):
162
+ audio_segment = get_audiosegment(audio_array)
163
+
164
+ leading = detect_leading_silence(audio_segment)
165
+ trailing = detect_leading_silence(audio_segment.reverse())
166
+ return leading, trailing
167
+
168
+ def get_timestamp(audio_array):
169
+ leading_silence, trailing_silence = get_leading_and_trailing_silence(audio_array)
170
+ start_len = leading_silence / 1000
171
+ start_time = round(start_len / 2, 2) * 2
172
+
173
+ end_len = (len(audio_array) / sampling_rate) - (trailing_silence / 1000)
174
+ end_time = round(end_len / 2, 2) * 2
175
+ return (start_time, end_time)
176
+
177
+
178
  def filter_sentence(sentence):
179
  if "... tyst under denna inspelning ..." in sentence: return False
180
  return True
 
198
  audio_len = len(audio_array) / sampling_rate
199
  audio_len = round(audio_len / 2, 2) * 2
200
 
201
+ print(get_leading_and_trailing_silence(audio_array))
202
+
203
  text = {
204
  "text": metadata_item['sentence'],
205
  "offsets": [
206
+ {"text": metadata_item['sentence'], "timestamp": get_timestamp(audio_array) }
207
  ]
208
  }
209