Commit ·
12384dc
1
Parent(s): 66cce9b
crop samples to be around 1 second
Browse files- crop_audio_files.py +18 -16
- data/audio_cropped.tar.gz +2 -2
- metadata_cropped.csv +0 -0
crop_audio_files.py
CHANGED
|
@@ -27,7 +27,7 @@ def copy_sample(data: Any, sample_rate: int, start: float, end: float):
|
|
| 27 |
# [id, audio data, sample rate, extension, words]
|
| 28 |
TSegment = Tuple[str, Any, int, str, str]
|
| 29 |
|
| 30 |
-
def crop_audio_file(audio_file: str, ctm_file: str, target_duration_secs: float, max_word_duration_secs: float =
|
| 31 |
data, sample_rate = sf.read(audio_file)
|
| 32 |
ctm = read_ctm_file(ctm_file)
|
| 33 |
|
|
@@ -46,27 +46,29 @@ def crop_audio_file(audio_file: str, ctm_file: str, target_duration_secs: float,
|
|
| 46 |
return (segment_id, audio_sample, sample_rate, ext, words)
|
| 47 |
|
| 48 |
for _, _, word_start, word_duration_secs, word in ctm:
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
word_end = word_start + word_duration_secs
|
| 53 |
-
|
| 54 |
-
if cur_end - cur_start > target_duration_secs or word_end - cur_start >= max_segment_duration_secs or word_duration_secs > max_word_duration_secs:
|
| 55 |
-
if cur_end - cur_start >= min_segment_duration_secs:
|
| 56 |
segment = make_segment(cur_start, cur_end, cur_words)
|
| 57 |
segments.append(segment)
|
|
|
|
| 58 |
cur_words = []
|
| 59 |
-
|
| 60 |
-
if word_duration_secs > max_word_duration_secs:
|
| 61 |
-
cur_start = None
|
| 62 |
-
continue
|
| 63 |
-
cur_start = word_start
|
| 64 |
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
cur_end = word_end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
-
if cur_start is not None and cur_end - cur_start >= min_segment_duration_secs:
|
| 70 |
segment = make_segment(cur_start, cur_end, cur_words)
|
| 71 |
segments.append(segment)
|
| 72 |
|
|
@@ -105,7 +107,7 @@ if __name__ == "__main__":
|
|
| 105 |
ctm_files_dir = 'data/ctm/words'
|
| 106 |
output_audio_dir = 'data/audio_cropped'
|
| 107 |
output_metadata_file = 'metadata_cropped.csv'
|
| 108 |
-
target_len_secs =
|
| 109 |
|
| 110 |
n = crop_audio_files(input_audio_dir, ctm_files_dir, output_audio_dir, output_metadata_file, target_len_secs)
|
| 111 |
print(f'{n} crops created')
|
|
|
|
| 27 |
# [id, audio data, sample rate, extension, words]
|
| 28 |
TSegment = Tuple[str, Any, int, str, str]
|
| 29 |
|
| 30 |
+
def crop_audio_file(audio_file: str, ctm_file: str, target_duration_secs: float, max_word_duration_secs: float = 2.0, min_segment_duration_secs: float = 0.5, max_segment_duration_secs: float = 1.5) -> List[TSegment]:
|
| 31 |
data, sample_rate = sf.read(audio_file)
|
| 32 |
ctm = read_ctm_file(ctm_file)
|
| 33 |
|
|
|
|
| 46 |
return (segment_id, audio_sample, sample_rate, ext, words)
|
| 47 |
|
| 48 |
for _, _, word_start, word_duration_secs, word in ctm:
|
| 49 |
+
# exclude pathological alignments
|
| 50 |
+
if word_duration_secs > max_word_duration_secs:
|
| 51 |
+
if cur_start is not None and cur_end - cur_start >= min_segment_duration_secs and cur_end - cur_start <= max_segment_duration_secs:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
segment = make_segment(cur_start, cur_end, cur_words)
|
| 53 |
segments.append(segment)
|
| 54 |
+
cur_start = None
|
| 55 |
cur_words = []
|
| 56 |
+
continue
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
+
if cur_start is None:
|
| 59 |
+
cur_start = word_start
|
| 60 |
|
| 61 |
+
word_end = word_start + word_duration_secs
|
| 62 |
cur_end = word_end
|
| 63 |
+
cur_words.append(word)
|
| 64 |
+
|
| 65 |
+
if cur_end - cur_start >= target_duration_secs - 0.1:
|
| 66 |
+
segment = make_segment(cur_start, cur_end, cur_words)
|
| 67 |
+
segments.append(segment)
|
| 68 |
+
cur_start = None
|
| 69 |
+
cur_words = []
|
| 70 |
|
| 71 |
+
if cur_start is not None and cur_end - cur_start >= min_segment_duration_secs and cur_end - cur_start <= max_segment_duration_secs:
|
| 72 |
segment = make_segment(cur_start, cur_end, cur_words)
|
| 73 |
segments.append(segment)
|
| 74 |
|
|
|
|
| 107 |
ctm_files_dir = 'data/ctm/words'
|
| 108 |
output_audio_dir = 'data/audio_cropped'
|
| 109 |
output_metadata_file = 'metadata_cropped.csv'
|
| 110 |
+
target_len_secs = 1
|
| 111 |
|
| 112 |
n = crop_audio_files(input_audio_dir, ctm_files_dir, output_audio_dir, output_metadata_file, target_len_secs)
|
| 113 |
print(f'{n} crops created')
|
data/audio_cropped.tar.gz
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:73801b326a6059a27269efffc9c981caee8f14e6e36c04d4f20668bc9a4fd2ec
|
| 3 |
+
size 725365601
|
metadata_cropped.csv
CHANGED
|
The diff for this file is too large to render.
See raw diff
|
|
|