|
1 |
| -import os |
2 |
| -from pathlib import Path |
3 |
| - |
4 | 1 | from torchaudio.datasets import librispeech
|
5 |
| -from torchaudio_unittest.common_utils import ( |
6 |
| - get_whitenoise, |
7 |
| - normalize_wav, |
8 |
| - save_wav, |
9 |
| - TempDirMixin, |
10 |
| - TorchaudioTestCase, |
11 |
| -) |
12 |
| - |
13 |
| -# Used to generate a unique transcript for each dummy audio file |
14 |
| -_NUMBERS = ["ZERO", "ONE", "TWO", "THREE", "FOUR", "FIVE", "SIX", "SEVEN", "EIGHT", "NINE"] |
15 |
| - |
16 |
| - |
17 |
| -def get_mock_dataset(root_dir): |
18 |
| - """ |
19 |
| - root_dir: directory to the mocked dataset |
20 |
| - """ |
21 |
| - mocked_data = [] |
22 |
| - dataset_dir = os.path.join(root_dir, librispeech.FOLDER_IN_ARCHIVE, librispeech.URL) |
23 |
| - os.makedirs(dataset_dir, exist_ok=True) |
24 |
| - sample_rate = 16000 # 16kHz |
25 |
| - seed = 0 |
26 |
| - |
27 |
| - for speaker_id in range(5): |
28 |
| - speaker_path = os.path.join(dataset_dir, str(speaker_id)) |
29 |
| - os.makedirs(speaker_path, exist_ok=True) |
30 |
| - |
31 |
| - for chapter_id in range(3): |
32 |
| - chapter_path = os.path.join(speaker_path, str(chapter_id)) |
33 |
| - os.makedirs(chapter_path, exist_ok=True) |
34 |
| - trans_content = [] |
35 |
| - |
36 |
| - for utterance_id in range(10): |
37 |
| - filename = f"{speaker_id}-{chapter_id}-{utterance_id:04d}.wav" |
38 |
| - path = os.path.join(chapter_path, filename) |
39 |
| - |
40 |
| - transcript = " ".join([_NUMBERS[x] for x in [speaker_id, chapter_id, utterance_id]]) |
41 |
| - trans_content.append(f"{speaker_id}-{chapter_id}-{utterance_id:04d} {transcript}") |
42 |
| - |
43 |
| - data = get_whitenoise(sample_rate=sample_rate, duration=0.01, n_channels=1, dtype="float32", seed=seed) |
44 |
| - save_wav(path, data, sample_rate) |
45 |
| - sample = (normalize_wav(data), sample_rate, transcript, speaker_id, chapter_id, utterance_id) |
46 |
| - mocked_data.append(sample) |
47 |
| - |
48 |
| - seed += 1 |
49 |
| - |
50 |
| - trans_filename = f"{speaker_id}-{chapter_id}.trans.txt" |
51 |
| - trans_path = os.path.join(chapter_path, trans_filename) |
52 |
| - with open(trans_path, "w") as f: |
53 |
| - f.write("\n".join(trans_content)) |
54 |
| - return mocked_data |
55 |
| - |
56 |
| - |
57 |
| -class TestLibriSpeech(TempDirMixin, TorchaudioTestCase): |
58 |
| - backend = "default" |
59 |
| - |
60 |
| - root_dir = None |
61 |
| - samples = [] |
62 |
| - |
63 |
| - @classmethod |
64 |
| - def setUpClass(cls): |
65 |
| - cls.root_dir = cls.get_base_temp_dir() |
66 |
| - cls.samples = get_mock_dataset(cls.root_dir) |
67 |
| - |
68 |
| - @classmethod |
69 |
| - def tearDownClass(cls): |
70 |
| - # In case of test failure |
71 |
| - librispeech.LIBRISPEECH._ext_audio = ".flac" |
72 |
| - |
73 |
| - def _test_librispeech(self, dataset): |
74 |
| - num_samples = 0 |
75 |
| - for i, (data, sample_rate, transcript, speaker_id, chapter_id, utterance_id) in enumerate(dataset): |
76 |
| - self.assertEqual(data, self.samples[i][0], atol=5e-5, rtol=1e-8) |
77 |
| - assert sample_rate == self.samples[i][1] |
78 |
| - assert transcript == self.samples[i][2] |
79 |
| - assert speaker_id == self.samples[i][3] |
80 |
| - assert chapter_id == self.samples[i][4] |
81 |
| - assert utterance_id == self.samples[i][5] |
82 |
| - num_samples += 1 |
83 |
| - |
84 |
| - assert num_samples == len(self.samples) |
85 |
| - librispeech.LIBRISPEECH._ext_audio = ".flac" |
| 2 | +from torchaudio_unittest.common_utils import TorchaudioTestCase |
| 3 | +from torchaudio_unittest.datasets.librispeech_test_impl import LibriSpeechTestMixin |
86 | 4 |
|
87 |
| - def test_librispeech_str(self): |
88 |
| - librispeech.LIBRISPEECH._ext_audio = ".wav" |
89 |
| - dataset = librispeech.LIBRISPEECH(self.root_dir) |
90 |
| - self._test_librispeech(dataset) |
91 | 5 |
|
92 |
| - def test_librispeech_path(self): |
93 |
| - librispeech.LIBRISPEECH._ext_audio = ".wav" |
94 |
| - dataset = librispeech.LIBRISPEECH(Path(self.root_dir)) |
95 |
| - self._test_librispeech(dataset) |
| 6 | +class TestLibriSpeech(LibriSpeechTestMixin, TorchaudioTestCase): |
| 7 | + librispeech_cls = librispeech.LIBRISPEECH |
0 commit comments