vctk.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import os
  2. from typing import Tuple
  3. import torchaudio
  4. from torch import Tensor
  5. from torch.hub import download_url_to_file
  6. from torch.utils.data import Dataset
  7. from torchaudio.datasets.utils import extract_archive
  8. URL = "https://datashare.is.ed.ac.uk/bitstream/handle/10283/3443/VCTK-Corpus-0.92.zip"
  9. _CHECKSUMS = {
  10. "https://datashare.is.ed.ac.uk/bitstream/handle/10283/3443/VCTK-Corpus-0.92.zip": "f96258be9fdc2cbff6559541aae7ea4f59df3fcaf5cf963aae5ca647357e359c" # noqa: E501
  11. }
  12. SampleType = Tuple[Tensor, int, str, str, str]
  13. class VCTK_092(Dataset):
  14. """Create *VCTK 0.92* [:footcite:`yamagishi2019vctk`] Dataset
  15. Args:
  16. root (str): Root directory where the dataset's top level directory is found.
  17. mic_id (str, optional): Microphone ID. Either ``"mic1"`` or ``"mic2"``. (default: ``"mic2"``)
  18. download (bool, optional):
  19. Whether to download the dataset if it is not found at root path. (default: ``False``).
  20. url (str, optional): The URL to download the dataset from.
  21. (default: ``"https://datashare.is.ed.ac.uk/bitstream/handle/10283/3443/VCTK-Corpus-0.92.zip"``)
  22. audio_ext (str, optional): Custom audio extension if dataset is converted to non-default audio format.
  23. Note:
  24. * All the speeches from speaker ``p315`` will be skipped due to the lack of the corresponding text files.
  25. * All the speeches from ``p280`` will be skipped for ``mic_id="mic2"`` due to the lack of the audio files.
  26. * Some of the speeches from speaker ``p362`` will be skipped due to the lack of the audio files.
  27. * See Also: https://datashare.is.ed.ac.uk/handle/10283/3443
  28. """
  29. def __init__(
  30. self,
  31. root: str,
  32. mic_id: str = "mic2",
  33. download: bool = False,
  34. url: str = URL,
  35. audio_ext=".flac",
  36. ):
  37. if mic_id not in ["mic1", "mic2"]:
  38. raise RuntimeError(f'`mic_id` has to be either "mic1" or "mic2". Found: {mic_id}')
  39. archive = os.path.join(root, "VCTK-Corpus-0.92.zip")
  40. self._path = os.path.join(root, "VCTK-Corpus-0.92")
  41. self._txt_dir = os.path.join(self._path, "txt")
  42. self._audio_dir = os.path.join(self._path, "wav48_silence_trimmed")
  43. self._mic_id = mic_id
  44. self._audio_ext = audio_ext
  45. if download:
  46. if not os.path.isdir(self._path):
  47. if not os.path.isfile(archive):
  48. checksum = _CHECKSUMS.get(url, None)
  49. download_url_to_file(url, archive, hash_prefix=checksum)
  50. extract_archive(archive, self._path)
  51. if not os.path.isdir(self._path):
  52. raise RuntimeError("Dataset not found. Please use `download=True` to download it.")
  53. # Extracting speaker IDs from the folder structure
  54. self._speaker_ids = sorted(os.listdir(self._txt_dir))
  55. self._sample_ids = []
  56. """
  57. Due to some insufficient data complexity in the 0.92 version of this dataset,
  58. we start traversing the audio folder structure in accordance with the text folder.
  59. As some of the audio files are missing of either ``mic_1`` or ``mic_2`` but the
  60. text is present for the same, we first check for the existence of the audio file
  61. before adding it to the ``sample_ids`` list.
  62. Once the ``audio_ids`` are loaded into memory we can quickly access the list for
  63. different parameters required by the user.
  64. """
  65. for speaker_id in self._speaker_ids:
  66. if speaker_id == "p280" and mic_id == "mic2":
  67. continue
  68. utterance_dir = os.path.join(self._txt_dir, speaker_id)
  69. for utterance_file in sorted(f for f in os.listdir(utterance_dir) if f.endswith(".txt")):
  70. utterance_id = os.path.splitext(utterance_file)[0]
  71. audio_path_mic = os.path.join(
  72. self._audio_dir,
  73. speaker_id,
  74. f"{utterance_id}_{mic_id}{self._audio_ext}",
  75. )
  76. if speaker_id == "p362" and not os.path.isfile(audio_path_mic):
  77. continue
  78. self._sample_ids.append(utterance_id.split("_"))
  79. def _load_text(self, file_path) -> str:
  80. with open(file_path) as file_path:
  81. return file_path.readlines()[0]
  82. def _load_audio(self, file_path) -> Tuple[Tensor, int]:
  83. return torchaudio.load(file_path)
  84. def _load_sample(self, speaker_id: str, utterance_id: str, mic_id: str) -> SampleType:
  85. transcript_path = os.path.join(self._txt_dir, speaker_id, f"{speaker_id}_{utterance_id}.txt")
  86. audio_path = os.path.join(
  87. self._audio_dir,
  88. speaker_id,
  89. f"{speaker_id}_{utterance_id}_{mic_id}{self._audio_ext}",
  90. )
  91. # Reading text
  92. transcript = self._load_text(transcript_path)
  93. # Reading FLAC
  94. waveform, sample_rate = self._load_audio(audio_path)
  95. return (waveform, sample_rate, transcript, speaker_id, utterance_id)
  96. def __getitem__(self, n: int) -> SampleType:
  97. """Load the n-th sample from the dataset.
  98. Args:
  99. n (int): The index of the sample to be loaded
  100. Returns:
  101. (Tensor, int, str, str, str):
  102. ``(waveform, sample_rate, transcript, speaker_id, utterance_id)``
  103. """
  104. speaker_id, utterance_id = self._sample_ids[n]
  105. return self._load_sample(speaker_id, utterance_id, self._mic_id)
  106. def __len__(self) -> int:
  107. return len(self._sample_ids)