|
"""Script to create Nemo compatible data manifests for jeli-asr""" |
|
|
|
|
|
import glob |
|
import os |
|
import csv |
|
import random |
|
import json |
|
import shutil |
|
import sys |
|
from pydub import AudioSegment |
|
|
|
|
|
def key_sort_paths(path: str) -> int: |
|
"""Serve as key function to sort the wav files paths |
|
|
|
Args: |
|
path (str): An individual path |
|
|
|
Returns: |
|
int: The number of the split (between 1 and 6) |
|
""" |
|
return int(path[-5]) |
|
|
|
|
|
def read_audios(glob_paths: list[str]) -> AudioSegment: |
|
"""Read the six 10 mns audio as AudioSegments and returns the combined 1 hr audio |
|
|
|
Args: |
|
glob_paths (list[str]): list of the paths of the 6 .wav files |
|
|
|
Returns: |
|
AudioSegment: The combined audio |
|
""" |
|
audios = [] |
|
for wav_file in sorted(glob_paths, key=key_sort_paths): |
|
audios.append(AudioSegment.from_file(file=wav_file, format="wav")) |
|
final_audio = sum(audios[1:], start=audios[0]) |
|
return final_audio |
|
|
|
|
|
def read_tsv(tsv_file_path: str) -> list[list[int | str]]: |
|
"""Read a .tsv file and return the utterances in it |
|
|
|
Args: |
|
tsv_file_path (str): The path to the tsv file |
|
|
|
Returns: |
|
list[list[int | str]]: The returned utterances with the timestamps coverted to int |
|
""" |
|
with open(tsv_file_path,"r", encoding='utf-8') as recording_transcript: |
|
tsv_file_rows = csv.reader(recording_transcript, delimiter="\t") |
|
utterances = [[int(start), int(end), bam, french] for start, end, bam, french in tsv_file_rows] |
|
return utterances |
|
|
|
|
|
def create_var_length_samples(utterances: list[list[int | str]], min_duration: int = 1000, |
|
max_duration: int = 120000) -> list[list[list[int | str]]]: |
|
"""Create variable length combination of utterances to make samples which duration vary between 1s and 2mns |
|
|
|
Args: |
|
utterances (list[list[int | str]]): The read tsv file containing the transcriptions of the audio |
|
min_duration (int, optional): min duration of a sample in milliseconds. Defaults to 1000. |
|
max_duration (int, optional): max duration of a sample in milliseconds. Defaults to 120000. |
|
|
|
Returns: |
|
list[list[list[int | str]]]: The list of created samples |
|
""" |
|
samples = [] |
|
current_slice = [] |
|
current_duration = 0 |
|
|
|
i = 0 |
|
while i < len(utterances): |
|
utterance_start, utterance_end = utterances[i][:2] |
|
utterance_duration = utterance_end - utterance_start |
|
|
|
|
|
if current_duration + utterance_duration <= max_duration: |
|
current_slice.append(utterances[i]) |
|
current_duration += utterance_duration |
|
i += 1 |
|
else: |
|
|
|
samples.append(current_slice) |
|
current_slice = [] |
|
current_duration = 0 |
|
|
|
|
|
if current_duration >= min_duration: |
|
if random.choice([True, False, False]) or len(current_slice) >= random.randint(1, 20): |
|
samples.append(current_slice) |
|
current_slice = [] |
|
current_duration = 0 |
|
|
|
|
|
if current_slice: |
|
samples.append(current_slice) |
|
|
|
return samples |
|
|
|
|
|
def slice_and_save_audios(samples: list[list[list[int | str]]], griot_id: str, |
|
data_dir: str, audio_dir_path: str) -> list[list[float | str]]: |
|
"""Slice and save the audio samples created for a specific 1hr recording |
|
|
|
Args: |
|
samples (list[list[list[int | str]]]): The samples created with function "create_var_length_samples" |
|
griot_id (str): The ID of the griot in the recording (eg: griots_r17) |
|
data_dir (str): The directory containing all the data. |
|
audio_dir_path (str): The diretory the save the sliced audios in. |
|
|
|
Returns: |
|
list[list[int | str]]: A list version of manifests (eg: [[audiofile_path, duration, bambara, translation], ...]) |
|
""" |
|
wav_files_paths = glob.glob(f'{data_dir}/{griot_id}/*.wav') |
|
griot_recording = read_audios(glob_paths=wav_files_paths) |
|
|
|
list_manifests = [] |
|
|
|
for sample in samples: |
|
start = sample[0][0] |
|
end = sample[-1][1] |
|
duration = (end - start) / 1000 |
|
|
|
more_than_100s = " ###" if duration >= 100 else "" |
|
|
|
|
|
transcriptions, translations = [utt[2] for utt in sample], [utt[3] for utt in sample] |
|
transcription = " ".join(transcriptions) |
|
translation = " ".join(translations) |
|
|
|
|
|
audio_file_path = f"{audio_dir_path}/{griot_id}-{start}-{end}.wav" |
|
griot_recording[start:end].export(out_f=audio_file_path, format="wav") |
|
print(f"Sample {griot_id}-{start}-{end} saved in {audio_file_path}{more_than_100s}") |
|
|
|
|
|
list_manifests.append([audio_file_path, duration, transcription, translation]) |
|
return list_manifests |
|
|
|
|
|
def shuffle_and_split(dataset: list[list[float | str]], |
|
test: int | float = 0.15) -> tuple[list[list[float | str]]]: |
|
"""Shuffle and split the whole dataset |
|
|
|
Args: |
|
dataset (list[list[int | str]]): The combined list of all list manifest returned by "slice_and_save_audios" |
|
test (int | float, optional): The number of sample to include that make the test set or and percentage of the whole dataset to use as the test set. Defaults to 0.15. |
|
|
|
Returns: |
|
tuple[list[list[list[int | str]]]]: The train and test sets samples returned separately |
|
""" |
|
random.shuffle(dataset) |
|
if isinstance(test, float): |
|
test = int(test * len(dataset)) |
|
test_set_samples = dataset[0:test] |
|
train_set_samples = dataset[test:] |
|
return train_set_samples, test_set_samples |
|
|
|
|
|
def create_manifest(dataset_split: list[list[float | str]], split_name: str, |
|
dir_path: str) -> None: |
|
"""Create manifest files |
|
|
|
Args: |
|
dataset_split (list[list[float | str]]): Split of the dataset to create manifest for |
|
split_name (str): Name of the split |
|
dir_path (str): The directory to save the new data manifest in |
|
""" |
|
|
|
os.makedirs(f'{dir_path}/manifests', exist_ok=True) |
|
os.makedirs(f'{dir_path}/french-manifests', exist_ok=True) |
|
os.makedirs(f'{dir_path}/audios/{split_name}', exist_ok=True) |
|
|
|
|
|
manifest_path = f'{dir_path}/manifests/{split_name}_manifest.json' |
|
french_manifest_path = f'{dir_path}/french-manifests/{split_name}_french_manifest.json' |
|
audio_dir_path = f'{dir_path}/audios/{split_name}' |
|
|
|
with open(manifest_path, 'w', encoding="utf-8") as manifest_file, open(french_manifest_path, 'w', encoding="utf-8") as french_file: |
|
for sample in dataset_split: |
|
|
|
new_audio_path = f'{audio_dir_path}/{sample[0].split("/")[-1]}' |
|
shutil.move(src=sample[0], dst=new_audio_path) |
|
|
|
|
|
manifest_line = { |
|
"audio_filepath": os.path.relpath(new_audio_path), |
|
"duration": sample[1], |
|
"text": sample[2] |
|
} |
|
|
|
french_manifest_line = { |
|
"audio_filepath": os.path.relpath(new_audio_path), |
|
"duration": sample[1], |
|
"text": sample[3] |
|
} |
|
|
|
|
|
manifest_file.write(json.dumps(manifest_line) + '\n') |
|
french_file.write(json.dumps(french_manifest_line) + '\n') |
|
print(f"{split_name} manifests files have been created successfully!\nCorresponding audios files have been moved to {audio_dir_path}") |
|
|
|
if __name__ == "__main__": |
|
data_path = sys.argv[1] |
|
manifest_dir = sys.argv[2] |
|
tsv_dir = f'{data_path}/aligned-transcriptions' |
|
|
|
|
|
tsv_paths = glob.glob(f'{tsv_dir}/*.tsv') |
|
|
|
final_list_manifest = [] |
|
for tsv_file in tsv_paths: |
|
id_griot = tsv_file.split("/")[-1][:-4] |
|
griot_utterances = read_tsv(tsv_file_path=tsv_file) |
|
|
|
griot_samples = create_var_length_samples(utterances=griot_utterances) |
|
list_manifest = slice_and_save_audios(samples=griot_samples, griot_id=id_griot, |
|
data_dir=data_path, audio_dir_path=f'{manifest_dir}/audios') |
|
final_list_manifest.append(list_manifest) |
|
|
|
final_list_manifest = sum(final_list_manifest, start=[]) |
|
|
|
train_set, test_set = shuffle_and_split(dataset=final_list_manifest, test=0.15) |
|
print(f'len(train_set) == {len(train_set)} and len(test_set) == {len(test_set)}') |
|
|
|
create_manifest(dataset_split=train_set, split_name="train", dir_path=manifest_dir) |
|
create_manifest(dataset_split=test_set, split_name="test", dir_path=manifest_dir) |
|
|