File: ljspeech_test.py

package info (click to toggle)
pytorch-audio 0.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,512 kB
  • sloc: python: 15,606; cpp: 1,352; sh: 257; makefile: 21
file content (74 lines) | stat: -rw-r--r-- 2,650 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import csv
import os

from torchaudio.datasets import ljspeech

from torchaudio_unittest.common_utils import (
    TempDirMixin,
    TorchaudioTestCase,
    get_whitenoise,
    normalize_wav,
    save_wav,
)


class TestLJSpeech(TempDirMixin, TorchaudioTestCase):
    backend = "default"

    root_dir = None
    data = []
    transcripts = [
        "Test transcript 1",
        "Test transcript 2",
        "Test transcript 3",
        "In 1465 Sweynheim and Pannartz began printing in the monastery of Subiaco near Rome,"
    ]

    normalized_transcripts = [
        "Test transcript one",
        "Test transcript two",
        "Test transcript three",
        "In fourteen sixty-five Sweynheim and Pannartz began printing in the monastery of Subiaco near Rome,"
    ]

    @classmethod
    def setUpClass(cls):
        cls.root_dir = cls.get_base_temp_dir()
        base_dir = os.path.join(cls.root_dir, "LJSpeech-1.1")
        archive_dir = os.path.join(base_dir, "wavs")
        os.makedirs(archive_dir, exist_ok=True)
        metadata_path = os.path.join(base_dir, "metadata.csv")
        sample_rate = 22050

        with open(metadata_path, mode="w", newline='') as metadata_file:
            metadata_writer = csv.writer(
                metadata_file, delimiter="|", quoting=csv.QUOTE_NONE
            )
            for i, (transcript, normalized_transcript) in enumerate(
                zip(cls.transcripts, cls.normalized_transcripts)
            ):
                fileid = f'LJ001-{i:04d}'
                metadata_writer.writerow([fileid, transcript, normalized_transcript])
                filename = fileid + ".wav"
                path = os.path.join(archive_dir, filename)
                data = get_whitenoise(
                    sample_rate=sample_rate, duration=1, n_channels=1, dtype="int16", seed=i
                )
                save_wav(path, data, sample_rate)
                cls.data.append(normalize_wav(data))

    def test_ljspeech(self):
        dataset = ljspeech.LJSPEECH(self.root_dir)
        n_ite = 0
        for i, (waveform, sample_rate, transcript, normalized_transcript) in enumerate(
            dataset
        ):
            expected_transcript = self.transcripts[i]
            expected_normalized_transcript = self.normalized_transcripts[i]
            expected_data = self.data[i]
            self.assertEqual(expected_data, waveform, atol=5e-5, rtol=1e-8)
            assert sample_rate == sample_rate
            assert transcript == expected_transcript
            assert normalized_transcript == expected_normalized_transcript
            n_ite += 1
        assert n_ite == len(self.data)