import os import re from ctypes import Array from dataclasses import dataclass from typing import List, Tuple from pathlib import Path import xml.etree.ElementTree as ET import csv import datasets import numpy as np try: import ffmpeg FFMPEG_AVAILABLE = True except (ImportError, ModuleNotFoundError): import librosa FFMPEG_AVAILABLE = False _CITATION = """\ @inproceedings{salesky2021mtedx, title={Multilingual TEDx Corpus for Speech Recognition and Translation}, author={Elizabeth Salesky and Matthew Wiesner and Jacob Bremerman and Roldano Cattoni and Matteo Negri and Marco Turchi and Douglas W. Oard and Matt Post}, booktitle={Proceedings of Interspeech}, year={2021}, } """ _DESCRIPTION = """\ French subpart of the multilingual TEDX dataset """ SAMPLING_RATE = 16_000 @dataclass class Utterance: speaker_id: str index: int sentence: str start_timestamp: float end_timestamp: float class TEDXConfig(datasets.BuilderConfig): """BuilderConfig for TEDX.""" def __init__(self, name, **kwargs): """ Args: name: `string`, name of dataset config (=language) **kwargs: keyword arguments forwarded to super. """ super(TEDXConfig, self).__init__( version=datasets.Version("2.14.5", ""), name=name, **kwargs ) self.single_samples = (name == "single_samples") self.max = (name == "max") if not self.single_samples and not self.max: self.max_duration = float(name.split("=")[1][:-1]) else: self.max_duration = np.inf class TEDX(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ TEDXConfig(name="single_samples", description="all samples taken separately, can be very short and imprecise"), TEDXConfig(name="max", description="all samples of a talk are merged together"), TEDXConfig(name="max=30s", description="samples are merged in order to reach a max duration of 30 seconds." "Does not remove single utterances that may exceed " "the maximum duration"), TEDXConfig(name="max=10s", description="samples are merged in order to reach a max duration of 10 seconds" "Does not remove single utterances that may exceed " "the maximum duration"), ] DEFAULT_CONFIG_NAME = "single_samples" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "file": datasets.Value("string"), "audio": datasets.features.Audio(sampling_rate=SAMPLING_RATE), "sentence": datasets.Value("string"), "speaker_id": datasets.Value("string"), "start_timestamp": datasets.Value("float"), "end_timestamp": datasets.Value("float"), "index": datasets.Value("int32"), } ), citation=_CITATION, ) def _split_by_audio_file(self, segments_path: str, sentences_path: str, split_name: str) -> Tuple[List[str], List[List[Utterance]]]: speaker_paths = [] seen_ids = set() segments_by_speaker = [] with open(segments_path, "r") as segments, open(sentences_path) as sentences: segments_reader = csv.DictReader(segments, delimiter=' ', fieldnames=["segment_id", "speaker_id", "start_timestamp", "end_timestamp"]) sentences_list = sentences.readlines() for segment, sentence in zip(segments_reader, sentences_list): if segment["speaker_id"] not in seen_ids: seen_ids.add(segment["speaker_id"]) speaker_paths.append(Path("data") / Path(split_name) / Path("wav") / Path(f"{segment['speaker_id']}.flac")) segments_by_speaker.append([]) segments_by_speaker[-1].append(Utterance(speaker_id=segment["speaker_id"], index=int(segment["segment_id"].split("_")[-1]), sentence=sentence, start_timestamp=float(segment["start_timestamp"]), end_timestamp=float(segment["end_timestamp"]) )) return speaker_paths, segments_by_speaker def _split_generators(self, dl_manager): segments = { "train": dl_manager.download("data/train/txt/segments"), "test": dl_manager.download("data/test/txt/segments"), "valid": dl_manager.download("data/valid/txt/segments") } sentences = { "train": dl_manager.download("data/train/txt/train.fr"), "test": dl_manager.download("data/test/txt/test.fr"), "valid": dl_manager.download("data/valid/txt/valid.fr"), } splitted_dataset = {} segments = dl_manager.download(segments) sentences = dl_manager.download(sentences) for split in segments: audios_path, utterances = self._split_by_audio_file(segments[split], sentences[split], split) audios_path = dl_manager.download(audios_path) splitted_dataset[split] = { "audios_path": audios_path, "utterances": utterances } splits = [ datasets.SplitGenerator( name=datasets.Split.TRAIN, gen_kwargs= splitted_dataset["train"] ), datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs=splitted_dataset["test"] ), datasets.SplitGenerator( name=datasets.Split.VALIDATION, gen_kwargs=splitted_dataset["test"] ), ] return splits @staticmethod def merge_utterances(utterance1: Utterance, utterance2: Utterance) -> Utterance: assert(utterance1.speaker_id == utterance2.speaker_id) assert(utterance2.index > utterance1.index) return Utterance( speaker_id=utterance1.speaker_id, sentence=re.sub(r"\s+", " ", utterance1.sentence + " " + utterance2.sentence), start_timestamp=utterance1.start_timestamp, end_timestamp=utterance2.end_timestamp, index = utterance1.index ) def _merged_utterances_iterator(self, utterances: List[Utterance]): utterances = iter(utterances) if self.config.single_samples: yield from utterances return merged_utterance = next(utterances) start_time = merged_utterance.start_timestamp while True: try: new_utterance = next(utterances) except StopIteration: yield merged_utterance return end_time = new_utterance.end_timestamp if end_time - start_time > self.config.max_duration: yield merged_utterance merged_utterance = new_utterance start_time = merged_utterance.start_timestamp else: merged_utterance = TEDX.merge_utterances(merged_utterance, new_utterance) @staticmethod def load_audio(file: str, sr: int = SAMPLING_RATE): """ Open an audio file and read as mono waveform, resampling as necessary Parameters ---------- file:vThe audio file to read sr: int The sample rate to resample the audio if necessary Returns ------- A NumPy array containing the audio waveform, in float32 dtype. """ #import librosa #with open(file, "rb") as f: # return librosa.load(f, sr=sr) if FFMPEG_AVAILABLE: try: # This launches a subprocess to decode audio while down-mixing and resampling as necessary. # Requires the ffmpeg CLI and `ffmpeg-python` package to be installed. out, _ = ( ffmpeg.input(file) .output('-', format='s16le', acodec='pcm_s16le', ac=1, ar=sr) .run(capture_stdout=True, capture_stderr=True) ) except ffmpeg.Error as e: raise RuntimeError(f"Failed to load audio: {e.stderr.decode()}") from e return np.frombuffer(out, np.int16).flatten().astype(np.float32) / 32768.0 else: with open(file, "rb") as f: return librosa.load(f, sr=sr)[0] @staticmethod def _cut_audio(audio: Array, start_timestamp: float, end_timestamp: float): return audio[int(round(start_timestamp * SAMPLING_RATE)): int(round(end_timestamp * SAMPLING_RATE)) + 1] def _generate_examples(self, audios_path: List[str], utterances: List[List[Utterance]]): """Generate examples from a Multilingual LibriSpeech data dir.""" for audio_path, utterances in zip(audios_path, utterances): audio = self.load_audio(audio_path) for utterance in self._merged_utterances_iterator(utterances): transcript_name = f"{utterance.speaker_id}-{utterance.index}" start_timestamp = float(utterance.start_timestamp) end_timestamp = float(utterance.end_timestamp) yield transcript_name, { "file": transcript_name, "index": utterance.index, "sentence": utterance.sentence, "start_timestamp": start_timestamp, "end_timestamp": end_timestamp, "speaker_id": utterance.speaker_id, "audio": {"path": transcript_name, "array": self._cut_audio(audio, start_timestamp, end_timestamp), "sampling_rate": SAMPLING_RATE}}