Ahmed007/hamsa-tiny-v0.4
09
1from pathlib import Path 2import soundfile as sf3import xml.etree.ElementTree as ET4 5split = "train" # or "dev"6 7# set the following path to where you8# extracted the mgb2 archive9archive_path = Path("data/train")10 11wav_dir = archive_path / "wav"12segments_file = archive_path / "xml" / "utf8"13# output directories14output_wav_dir = archive_path / "dataset" / split /"wav"15output_txt_dir = archive_path / "dataset" / split /"txt"16 17# create directories for output datasets18output_wav_dir.mkdir(parents=True, exist_ok=True)19output_txt_dir.mkdir(parents=True, exist_ok=True)20 21# for all xml segments files under utf8 directory from archive22for s_file in segments_file.glob("*.xml"):23 tree = ET.parse(str(s_file))24 root = tree.getroot()25 head = root[0]26 segments = root[1][0]27 28 # get the name of the wav file form the recording tag29 for child in head:30 if child.tag == "recording":31 print(child.attrib)32 file_name = child.attrib.get("filename")33 34 # get the start and end times from the segment under segments tag35 # and join the text from each segment to construct the transcript36 for segment in segments:37 start_time = int(float(segment.attrib.get("starttime")) *16_000)38 end_time = int(float(segment.attrib.get("endtime")) * 16_000)39 40 text = " ".join([x.text for x in segment])41 42 43 # now store the meta data and the correctly sampled wav file in the correct44 # output directories45 wav_path = wav_dir / f"{file_name}.wav"46 sound, _ = sf.read(wav_path, start=start_time, stop=end_time)47 sf.write(output_wav_dir / f"{file_name}_seg{start_time}_{end_time}.wav", sound, 16_000)48 open(output_txt_dir / f"{file_name}_seg{start_time}_{end_time}.txt", "w").write(text)