Skip to content

Instantly share code, notes, and snippets.

@DevloperHS
Created January 7, 2022 13:11
Show Gist options
  • Select an option

  • Save DevloperHS/bae7f6cd32af52cc981a88261afd34f0 to your computer and use it in GitHub Desktop.

Select an option

Save DevloperHS/bae7f6cd32af52cc981a88261afd34f0 to your computer and use it in GitHub Desktop.
def process_files(csv_file, data_root, num_workers):
""" Read *.csv file description, convert mp3 to wav, process text.
Save results to data_root.
Args:
csv_file: str, path to *.csv file with data description, usually start from 'cv-'
data_root: str, path to dir to save results; wav/ dir will be created
"""
wav_dir = os.path.join(data_root, 'wav/')
os.makedirs(wav_dir, exist_ok=True)
audio_clips_path = os.path.dirname(csv_file) + '/clips/'
def process(x):
file_path, text = x
file_name = os.path.splitext(os.path.basename(file_path))[0]
text = text.lower().strip()
audio_path = os.path.join(audio_clips_path, file_path)
output_wav_path = os.path.join(wav_dir, file_name + '.wav')
tfm = Transformer()
tfm.rate(samplerate=args.sample_rate)
tfm.build(
input_filepath=audio_path,
output_filepath=output_wav_path
)
duration = sox.file_info.duration(output_wav_path)
return output_wav_path, duration, text
logging.info('Converting mp3 to wav for {}.'.format(csv_file))
with open(csv_file) as csvfile:
reader = csv.DictReader(csvfile, delimiter='\t')
next(reader, None) # skip the headers
data = [(row['path'], row['sentence']) for row in reader]
with ThreadPool(num_workers) as pool:
data = list(tqdm(pool.imap(process, data), total=len(data)))
return data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment