2024-12-21 16:07:27 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
|
|
from pydub import AudioSegment
|
|
|
|
from pydub.effects import normalize
|
|
|
|
from pygame.mixer import Sound
|
2024-12-29 13:50:19 +01:00
|
|
|
from tinytag import TinyTag
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
|
2024-12-29 20:10:06 +01:00
|
|
|
SUPPORTED_FORMATS = [
|
|
|
|
"mp3",
|
|
|
|
"wav",
|
|
|
|
"ogg"
|
|
|
|
]
|
|
|
|
|
|
|
|
|
2024-12-21 16:07:27 +01:00
|
|
|
class Track:
|
|
|
|
"""
|
|
|
|
Class containing data for a track like file path, raw data...
|
|
|
|
"""
|
|
|
|
|
2024-12-24 17:22:30 +01:00
|
|
|
def __init__(self, app, path: str, property_string: str=None, cache: bool=False):
|
|
|
|
self.app = app
|
2024-12-21 16:07:27 +01:00
|
|
|
self.path = path
|
2024-12-24 17:22:30 +01:00
|
|
|
self.property_string = property_string
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-29 13:50:19 +01:00
|
|
|
self.tags = TinyTag.get(self.path)
|
|
|
|
|
2024-12-29 14:31:21 +01:00
|
|
|
self.cached = False
|
2024-12-24 17:22:30 +01:00
|
|
|
self.audio = None
|
|
|
|
self.sound = None
|
|
|
|
self.duration = 0
|
|
|
|
|
2024-12-29 14:31:21 +01:00
|
|
|
if cache:
|
2024-12-24 17:22:30 +01:00
|
|
|
self.cache()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def cache(self):
|
2024-12-29 20:10:06 +01:00
|
|
|
self.load_audio()
|
2024-12-21 21:06:10 +01:00
|
|
|
# audio = normalize(audio)
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-24 17:22:30 +01:00
|
|
|
wav = self.audio.export(format="wav")
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-24 17:22:30 +01:00
|
|
|
self.sound = Sound(wav)
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-24 17:22:30 +01:00
|
|
|
self.duration = len(self.audio) # track duration in milliseconds
|
2024-12-21 16:07:27 +01:00
|
|
|
|
2024-12-29 14:31:21 +01:00
|
|
|
self.cached = True
|
|
|
|
|
2024-12-29 20:10:06 +01:00
|
|
|
def load_audio(self):
|
|
|
|
type = self.path.split(".")[-1]
|
|
|
|
|
|
|
|
if type in SUPPORTED_FORMATS:
|
|
|
|
self.audio = AudioSegment.from_file(self.path)
|
|
|
|
|
2024-12-21 19:00:06 +01:00
|
|
|
def remaining(self, position: int):
|
2024-12-21 20:20:06 +01:00
|
|
|
remaining_audio = self.audio[position:]
|
|
|
|
|
|
|
|
wav = remaining_audio.export(format="wav")
|
|
|
|
|
|
|
|
sound = Sound(wav)
|
|
|
|
|
2024-12-24 17:22:30 +01:00
|
|
|
# return the remaining part of the track's audio and the duration of the remaining part
|
2024-12-21 20:20:06 +01:00
|
|
|
return sound, len(remaining_audio)
|