|
| 1 | +"""Test suite for compliance with the ITU-R BS.1770-4 recommendation""" |
| 2 | +import zipfile |
| 3 | + |
| 4 | +import pytest |
| 5 | + |
| 6 | +import torch |
| 7 | +import torchaudio |
| 8 | +import torchaudio.functional as F |
| 9 | + |
| 10 | + |
| 11 | +# Test files linked in https://www.itu.int/dms_pub/itu-r/opb/rep/R-REP-BS.2217-2-2016-PDF-E.pdf |
| 12 | +@pytest.mark.parametrize( |
| 13 | + "filename,url,expected", |
| 14 | + [ |
| 15 | + ( |
| 16 | + "1770-2_Comp_RelGateTest", |
| 17 | + "http://www.itu.int/dms_pub/itu-r/oth/11/02/R11020000010030ZIPM.zip", |
| 18 | + -10.0, |
| 19 | + ), |
| 20 | + ( |
| 21 | + "1770-2_Comp_AbsGateTest", |
| 22 | + "http://www.itu.int/dms_pub/itu-r/oth/11/02/R11020000010029ZIPM.zip", |
| 23 | + -69.5, |
| 24 | + ), |
| 25 | + ( |
| 26 | + "1770-2_Comp_24LKFS_500Hz_2ch", |
| 27 | + "http://www.itu.int/dms_pub/itu-r/oth/11/02/R11020000010018ZIPM.zip", |
| 28 | + -24.0, |
| 29 | + ), |
| 30 | + ( |
| 31 | + "1770-2 Conf Mono Voice+Music-24LKFS", |
| 32 | + "http://www.itu.int/dms_pub/itu-r/oth/11/02/R11020000010038ZIPM.zip", |
| 33 | + -24.0, |
| 34 | + ), |
| 35 | + ], |
| 36 | +) |
| 37 | +def test_loudness(tmp_path, filename, url, expected): |
| 38 | + zippath = tmp_path / filename |
| 39 | + torch.hub.download_url_to_file(url, zippath, progress=False) |
| 40 | + with zipfile.ZipFile(zippath) as file: |
| 41 | + file.extractall(zippath.parent) |
| 42 | + |
| 43 | + waveform, sample_rate = torchaudio.load(zippath.with_suffix(".wav")) |
| 44 | + loudness = F.loudness(waveform, sample_rate) |
| 45 | + assert pytest.approx(loudness.item(), rel=0.01, abs=0.1) == expected |
0 commit comments