from pathlib import Path from typing import Any import pytest from src.atmos.fix_dolby import LFE_LOWPASS_HZ, fix_ds_frequency, fix_stream_pack_refs _FIXTURES_DIR = Path(__file__).parent.parent.parent / "fixtures" _SAMPLE_MASTER_AXML = _FIXTURES_DIR / "sample-master-76665836-axml.xml" _DOLBY_RENDERER_WAV = _FIXTURES_DIR / "dolby_renderer_silent_2s.wav" # All 15 PCM audioStreamFormats in the sample master carry both an # audioChannelFormat and an audioPackFormat reference (the Dolby double-ref # that BS.2076 disallows). _SAMPLE_MASTER_STREAM_FORMAT_IDS = ( "AS_00011001", "AS_00011002", "AS_00011003", "AS_00011004", "AS_00011005", "AS_00011006", "AS_00011007", "AS_00011008", "AS_00011009", "AS_0001100a", "AS_00031001", "AS_00031002", "AS_00031003", "AS_00031004", "AS_00031005", ) @pytest.fixture def sample_master_adm() -> Any: # the src.atmos.fix_dolby import above already triggered the src/__init__.py # path shim, so the vendored ear package resolves here from ear.fileio.adm.adm import ADM from ear.fileio.adm.common_definitions import load_common_definitions from ear.fileio.adm.xml import load_axml_string adm = ADM() load_common_definitions(adm) load_axml_string(adm, _SAMPLE_MASTER_AXML.read_bytes()) return adm def test_fix_stream_pack_refs_drops_pack_refs_on_double_referenced_pcm_streams(sample_master_adm: Any) -> None: fixed_ids = fix_stream_pack_refs(sample_master_adm) assert fixed_ids == _SAMPLE_MASTER_STREAM_FORMAT_IDS fixed_stream_formats = [ stream_format for stream_format in sample_master_adm.audioStreamFormats if stream_format.id in fixed_ids ] assert all(stream_format.audioPackFormat is None for stream_format in fixed_stream_formats) assert all(stream_format.audioChannelFormat is not None for stream_format in fixed_stream_formats) def test_fix_stream_pack_refs_is_idempotent(sample_master_adm: Any) -> None: fix_stream_pack_refs(sample_master_adm) assert fix_stream_pack_refs(sample_master_adm) == () def test_fix_ds_frequency_sets_lowpass_on_lfe_channel(sample_master_adm: Any) -> None: fixed_ids = fix_ds_frequency(sample_master_adm) assert fixed_ids == ("AC_00011004",) lfe_channel_format = next( channel_format for channel_format in sample_master_adm.audioChannelFormats if channel_format.id == "AC_00011004" ) assert lfe_channel_format.frequency.lowPass == LFE_LOWPASS_HZ def test_fix_ds_frequency_is_idempotent(sample_master_adm: Any) -> None: fix_ds_frequency(sample_master_adm) assert fix_ds_frequency(sample_master_adm) == () def test_unfixed_dolby_master_fails_adm_validation() -> None: # The pre-fixes are load-bearing: a real Dolby ADM Profile master fails # BS.2127 validation until fix_stream_pack_refs / fix_ds_frequency run. from ear.fileio.adm.exceptions import AdmError from ear.fileio.bw64 import Bw64Reader from ear.fileio.utils import Bw64AdmReader with _DOLBY_RENDERER_WAV.open("rb") as file_handle: adm = Bw64AdmReader(Bw64Reader(file_handle)).adm with pytest.raises(AdmError): adm.validate()