From 04417ff947180879ff65e374399659836e1f802d Mon Sep 17 00:00:00 2001 From: Hellow Date: Fri, 14 Apr 2023 12:03:07 +0200 Subject: [PATCH] started with the audio config --- src/music_kraken/utils/config/audio.py | 61 +++++++++++++++++++++++++ src/music_kraken/utils/config/config.py | 11 ++++- 2 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/music_kraken/utils/config/audio.py diff --git a/src/music_kraken/utils/config/audio.py b/src/music_kraken/utils/config/audio.py new file mode 100644 index 0000000..afdfdb7 --- /dev/null +++ b/src/music_kraken/utils/config/audio.py @@ -0,0 +1,61 @@ +import logging +from typing import Tuple + +from .config import SingleAttribute, StringAttribute, IntAttribute, Section, Description, EmptyLine + +# Only the formats with id3 metadata can be used +# https://www.audioranger.com/audio-formats.php +# https://web.archive.org/web/20230322234434/https://www.audioranger.com/audio-formats.php +ID3_2_FILE_FORMATS = ( + "mp3", "mp2", "mp1", # MPEG-1 ID3.2 + "wav", "wave", "rmi", # RIFF (including WAV) ID3.2 + "aiff", "aif", "aifc", # AIFF ID3.2 + "aac", "aacp", # Raw AAC ID3.2 + "tta", # True Audio ID3.2 +) + +ID3_1_FILE_FORMATS = ( + "ape", # Monkey's Audio ID3.1 + "mpc", "mpp", "mp+", # MusePack ID3.1 + "wv", # WavPack ID3.1 + "ofr", "ofs" # OptimFrog ID3.1 +) + + +class AudioFormatAttribute(SingleAttribute): + @property + def object_from_value(self) -> str: + v = self.value.strip().lower() + if v in ID3_2_FILE_FORMATS: + return v + if v in ID3_1_FILE_FORMATS: + logging.debug(f"setting audio format to a format that only supports ID3.1: {v}") + return v + + raise ValueError(f"Invalid Audio Format: {v}") + + + +class AudioSection(Section): + def __init__(self): + self.BITRATE = IntAttribute( + name="bitrate", + description="Streams the audio with this bitrate. Can't get more bitrate than the audio source though.", + value="125" + ) + + self.AUDIO_FORMAT = AudioFormatAttribute(name="audio_format", value="mp3", description=f""" +Music Kraken will stream the audio into this format. +You can use Audio formats which support ID3.2 and ID3.1,\n +but you will have cleaner Metadata using ID3.2.\n +ID3.2: {', '.join(f for f in ID3_2_FILE_FORMATS)} +ID3.1: {', '.join(f for f in ID3_1_FILE_FORMATS)} + """.strip()) + + self.attribute_list = [ + self.BITRATE, + self.AUDIO_FORMAT + ] + + +AUDIO_SECTION = AudioSection() diff --git a/src/music_kraken/utils/config/config.py b/src/music_kraken/utils/config/config.py index 6169d9a..bbf5128 100644 --- a/src/music_kraken/utils/config/config.py +++ b/src/music_kraken/utils/config/config.py @@ -34,6 +34,14 @@ class StringAttribute(SingleAttribute): return self.value +class IntAttribute(SingleAttribute): + @property + def object_from_value(self) -> int: + if not self.value.isdigit(): + raise ValueError(f"The value of {self.name} needs to be an integer, not {self.value}") + + return int(self.value) + class ListAttribute(Attribute): value: List[str] @@ -47,7 +55,8 @@ class Description: description: str def __str__(self): - return f"\n{self.description}" + lines = self.description.split("\n") + return "\n".join(f"{COMMENT_PREFIX}{line}" for line in lines) class EmptyLine(Description):