"""An audio track carries its bit depth and sample rate from the manifest or the service, and shows them.""" from __future__ import annotations import pytest from rich.text import Text from tests.core.test_music_renderer import flac_release, run_release from tests.lang_selection.test_kind_only_selection import FakeService from unshackle.core.manifests.dash import DASH from unshackle.core.manifests.hls import HLS from unshackle.core.manifests.ism import ISM from unshackle.core.music.renderer import MusicRenderer from unshackle.core.tracks import Audio, Tracks, Video def dash_audio(set_attrs: str = "", rep_attrs: str = "") -> Audio: text = f""" """ (audio,) = DASH.from_text(text, "https://cdn.example/x.mpd").to_tracks(language="en").audio return audio @pytest.mark.parametrize( ("set_attrs", "rep_attrs", "rate"), [ ("", ' audioSamplingRate="96000"', 96000), (' audioSamplingRate="44100"', "", 44100), (' audioSamplingRate="44100"', ' audioSamplingRate="192000"', 192000), ("", ' audioSamplingRate="24000 48000"', 48000), ("", "", None), ("", ' audioSamplingRate="high"', None), ("", ' audioSamplingRate="²"', None), ], ) def test_dash_reads_the_sample_rate(set_attrs: str, rep_attrs: str, rate: int | None) -> None: audio = dash_audio(set_attrs, rep_attrs) assert (audio.sample_rate, audio.bit_depth) == (rate, None) def test_ism_reads_the_sample_rate() -> None: text = """ """ (audio,) = ISM.from_text(text, "https://cdn.example/x.ism/manifest").to_tracks().audio assert (audio.sample_rate, audio.bit_depth) == (44100, None) @pytest.mark.parametrize( ("attrs", "expected"), [(",BIT-DEPTH=24,SAMPLE-RATE=96000", (24, 96000)), ("", (None, None)), (",SAMPLE-RATE=x", (None, None))], ) def test_hls_reads_bit_depth_and_sample_rate_when_the_playlist_states_them( attrs: str, expected: tuple[int | None, int | None] ) -> None: text = ( "#EXTM3U\n" f'#EXT-X-MEDIA:TYPE=AUDIO,GROUP-ID="a",NAME="x",LANGUAGE="en",CHANNELS="2",URI="a.m3u8"{attrs}\n' '#EXT-X-STREAM-INF:BANDWIDTH=1000000,CODECS="avc1.640028,alac",AUDIO="a"\n' "v.m3u8\n" ) (audio,) = HLS.from_text(text, "https://cdn.example/master.m3u8").to_tracks(language="en").audio assert (audio.bit_depth, audio.sample_rate) == expected def listing(audio: Audio, music: bool) -> str: """The audio line of the track listing as plain text.""" (node,) = Tracks([audio]).tree(music=music)[0].children[0].children return Text.from_markup(str(node.label)).plain @pytest.mark.parametrize( ("bit_depth", "sample_rate", "label"), [(24, 96000, "24-bit / 96 kHz"), (16, 44100, "16-bit / 44.1 kHz"), (None, 48000, "48 kHz"), (24, None, "24-bit")], ) def test_a_music_listing_shows_bit_depth_and_sample_rate( bit_depth: int | None, sample_rate: int | None, label: str ) -> None: audio = Audio( url="https://cdn.example/a.flac", language="en", bitrate=900_000, bit_depth=bit_depth, sample_rate=sample_rate ) assert f"| 900 kb/s | {label} | English" in listing(audio, music=True) def test_a_music_listing_shows_neither_when_unset() -> None: assert "bit" not in listing(Audio(url="https://cdn.example/a.flac", language="en"), music=True) @pytest.mark.parametrize("list_", [True, False], ids=["list", "download"]) @pytest.mark.parametrize("song", [True, False], ids=["song", "episode"]) def test_dl_shows_the_label_only_for_a_song( monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, song: bool, list_: bool ) -> None: album, tracks = flac_release() titles = album if song else FakeService({1: {}}).get_titles() if not song: tracks.add(Video(id_="v", url="https://x/v", language="en", codec=Video.Codec.AVC, range_=Video.Range.SDR)) monkeypatch.setattr("unshackle.commands.dl.mux_title", lambda *a, **k: ([], [])) out = run_release(monkeypatch, caplog, titles, tracks, list_=list_, to_end=True) # type: ignore[arg-type] assert "3075 kb/s" in out assert ("3075 kb/s | 24-bit / 96 kHz" in out) is song def test_a_video_listing_shows_neither() -> None: audio = Audio(url="https://cdn.example/a.mp4", language="en", bit_depth=24, sample_rate=48000) text = listing(audio, music=False) assert "English" in text assert "kHz" not in text and "24-bit" not in text @pytest.mark.parametrize("value", ["24", 24.0]) def test_a_bit_depth_that_is_not_an_int_is_refused(value: object) -> None: with pytest.raises(TypeError): Audio(url="https://cdn.example/a.flac", language="en", bit_depth=value) # type: ignore[arg-type] @pytest.mark.parametrize( ("label", "bit_depth", "sample_rate", "hires"), [ ("FLAC", 24, 48000, True), ("FLAC", 16, 96000, True), ("FLAC 24-bit/96kHz", 16, 44100, False), ("FLAC 24-bit/96kHz", None, None, True), ("FLAC 24-bit/44.1kHz", None, 44100, True), ("16-bit / 96 kHz", 16, None, True), ], ) def test_hires_is_decided_by_the_fields_before_the_label( label: str, bit_depth: int | None, sample_rate: int | None, hires: bool ) -> None: assert MusicRenderer.is_hires_quality(label, bit_depth, sample_rate) is hires def test_a_song_with_fields_and_no_quality_label_shows_them_with_the_badge() -> None: from tests.core.test_music_renderer import song line = MusicRenderer().option_from_song(song(1, "A", None, data={"bit_depth": 24, "sample_rate": 96000})) assert line.plain.startswith("24-bit / 96 kHz") assert "Hi-Res" in line.plain def test_the_header_reads_hires_from_the_fields() -> None: assert MusicRenderer.quality_summary("FLAC", bit_depth=24, sample_rate=48000) == "Hi-Res Lossless" assert MusicRenderer.quality_summary("FLAC", bit_depth=16, sample_rate=44100) == "Lossless" assert MusicRenderer.quality_summary("", bit_depth=24, sample_rate=96000) == "24-bit / 96 kHz" def test_tracks_that_differ_only_in_bit_depth_are_both_kept() -> None: a, b = (Audio(id_=str(n), url="https://cdn.example/a.flac", language="en", bit_depth=n) for n in (16, 24)) assert Tracks.signature(a) != Tracks.signature(b)