2021-02-24 00:46:59 +01:00
|
|
|
from __future__ import annotations
|
2021-09-12 05:58:24 +02:00
|
|
|
from librespot.audio import SuperAudioFormat
|
|
|
|
from librespot.audio.decoders import AudioQuality
|
|
|
|
from librespot.proto import Metadata_pb2 as Metadata
|
|
|
|
from librespot.structure import AudioQualityPicker
|
2021-02-24 00:46:59 +01:00
|
|
|
import logging
|
2021-04-24 12:17:02 +02:00
|
|
|
import typing
|
2021-02-24 00:46:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class VorbisOnlyAudioQuality(AudioQualityPicker):
|
2021-09-12 05:58:24 +02:00
|
|
|
logger = logging.getLogger("Librespot:Player:VorbisOnlyAudioQuality")
|
2021-02-24 00:46:59 +01:00
|
|
|
preferred: AudioQuality
|
|
|
|
|
|
|
|
def __init__(self, preferred: AudioQuality):
|
|
|
|
self.preferred = preferred
|
|
|
|
|
|
|
|
@staticmethod
|
2021-05-26 11:59:04 +02:00
|
|
|
def get_vorbis_file(files: typing.List[Metadata.AudioFile]):
|
2021-02-24 00:46:59 +01:00
|
|
|
for file in files:
|
2021-09-12 05:58:24 +02:00
|
|
|
if hasattr(file, "format") and SuperAudioFormat.get(file.format) == SuperAudioFormat.VORBIS:
|
2021-02-24 00:46:59 +01:00
|
|
|
return file
|
|
|
|
return None
|
|
|
|
|
2021-05-26 11:59:04 +02:00
|
|
|
def get_file(self, files: typing.List[Metadata.AudioFile]):
|
2021-09-12 05:58:24 +02:00
|
|
|
matches: typing.List[Metadata.AudioFile] = self.preferred.get_matches(files)
|
|
|
|
vorbis: Metadata.AudioFile = VorbisOnlyAudioQuality.get_vorbis_file(matches)
|
2021-02-24 00:46:59 +01:00
|
|
|
if vorbis is None:
|
2021-09-12 05:58:24 +02:00
|
|
|
vorbis: Metadata.AudioFile = VorbisOnlyAudioQuality.get_vorbis_file(files)
|
2021-02-24 00:46:59 +01:00
|
|
|
if vorbis is not None:
|
2021-09-12 05:58:24 +02:00
|
|
|
self.logger.warning("Using {} because preferred {} couldn't be found."
|
|
|
|
.format(vorbis.format, self.preferred))
|
2021-02-24 00:46:59 +01:00
|
|
|
else:
|
2021-09-12 05:58:24 +02:00
|
|
|
self.logger.fatal("Couldn't find any Vorbis file, available: {}")
|
2021-02-24 00:46:59 +01:00
|
|
|
return vorbis
|