|
| 1 | +import json |
| 2 | +from speechbrain.pretrained import EncoderClassifier, MelSpectrogramEncoder |
| 3 | +import torchaudio |
| 4 | +import pickle |
| 5 | +import logging |
| 6 | +import os |
| 7 | +from tqdm import tqdm |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +def compute_speaker_embeddings( |
| 13 | + input_filepaths, |
| 14 | + output_file_paths, |
| 15 | + data_folder, |
| 16 | + spk_emb_encoder_path, |
| 17 | + spk_emb_sr, |
| 18 | + mel_spec_params, |
| 19 | + device, |
| 20 | +): |
| 21 | + """This function processes a JSON file to compute the speaker embeddings |
| 22 | +
|
| 23 | + Arguments |
| 24 | + --------- |
| 25 | + input_filepaths : list |
| 26 | + A list of paths to the JSON files to be processed |
| 27 | + output_file_paths : list |
| 28 | + A list of paths to the output pickle files corresponding to the input JSON files |
| 29 | + data_folder : str |
| 30 | + Path to the folder where LibriTTS data is stored |
| 31 | + spk_emb_encoder_path : str |
| 32 | + Path for the speaker encoder |
| 33 | + spk_emb_sr : int |
| 34 | + Sample rate used by the speaker embedding encoder |
| 35 | + mel_spec_params: dict |
| 36 | + Information about mel-spectrogram computation |
| 37 | + device : str |
| 38 | + Device for to be used for computation |
| 39 | + """ |
| 40 | + |
| 41 | + # Checks if this phase is already done (if so, skips it) |
| 42 | + if skip(output_file_paths): |
| 43 | + logger.info("Preparation completed in previous run, skipping.") |
| 44 | + return |
| 45 | + |
| 46 | + # Initializes the speaker encoder |
| 47 | + spk_emb_encoder = None |
| 48 | + if mel_spec_params["custom_mel_spec_encoder"]: |
| 49 | + # To use the custom mel-spectrogram based encoder - for compatibility with future speaker consistency loss work |
| 50 | + spk_emb_encoder = MelSpectrogramEncoder.from_hparams( |
| 51 | + source=spk_emb_encoder_path, run_opts={"device": device} |
| 52 | + ) |
| 53 | + else: |
| 54 | + # To use the speaker encoders available with SpeechBrain |
| 55 | + spk_emb_encoder = EncoderClassifier.from_hparams( |
| 56 | + source=spk_emb_encoder_path, run_opts={"device": device} |
| 57 | + ) |
| 58 | + |
| 59 | + # Processes data manifests files to create corresponding speaker embedding files |
| 60 | + for i in range(len(input_filepaths)): |
| 61 | + logger.info(f"Creating {output_file_paths[i]}.") |
| 62 | + |
| 63 | + speaker_embeddings = dict() # Holds speaker embeddings |
| 64 | + |
| 65 | + json_file = open(input_filepaths[i]) |
| 66 | + json_data = json.load(json_file) |
| 67 | + |
| 68 | + # Processes all utterances in the data manifest file |
| 69 | + for utt_id, utt_data in tqdm(json_data.items()): |
| 70 | + utt_wav_path = utt_data["wav"] |
| 71 | + utt_wav_path = utt_wav_path.replace("{data_root}", data_folder) |
| 72 | + |
| 73 | + # Loads and resamples waveforms if required |
| 74 | + signal, sig_sr = torchaudio.load(utt_wav_path) |
| 75 | + if sig_sr != spk_emb_sr: |
| 76 | + signal = torchaudio.functional.resample( |
| 77 | + signal, sig_sr, spk_emb_sr |
| 78 | + ) |
| 79 | + signal = signal.to(device) |
| 80 | + |
| 81 | + # Computes the speaker embedding |
| 82 | + if mel_spec_params["custom_mel_spec_encoder"]: |
| 83 | + spk_emb = spk_emb_encoder.encode_waveform(signal) |
| 84 | + else: |
| 85 | + spk_emb = spk_emb_encoder.encode_batch(signal) |
| 86 | + |
| 87 | + spk_emb = spk_emb.squeeze() |
| 88 | + spk_emb = spk_emb.detach() |
| 89 | + |
| 90 | + speaker_embeddings[utt_id] = spk_emb.cpu() |
| 91 | + |
| 92 | + # Stores the speaker embeddings at the destination |
| 93 | + with open(output_file_paths[i], "wb") as output_file: |
| 94 | + pickle.dump( |
| 95 | + speaker_embeddings, |
| 96 | + output_file, |
| 97 | + protocol=pickle.HIGHEST_PROTOCOL, |
| 98 | + ) |
| 99 | + |
| 100 | + logger.info(f"Created {output_file_paths[i]}.") |
| 101 | + |
| 102 | + |
| 103 | +def skip(filepaths): |
| 104 | + """ |
| 105 | + Detects if the data preparation has been already done. |
| 106 | + If the preparation has been done, we can skip it. |
| 107 | + Returns |
| 108 | + ------- |
| 109 | + bool |
| 110 | + if True, the preparation phase can be skipped. |
| 111 | + if False, it must be done. |
| 112 | + """ |
| 113 | + for filepath in filepaths: |
| 114 | + if not os.path.isfile(filepath): |
| 115 | + return False |
| 116 | + return True |
0 commit comments