Wobuzz/wobuzz/player/track.py

63 lines
1.4 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/python3
from pydub import AudioSegment
from pydub.effects import normalize
from pygame.mixer import Sound
from tinytag import TinyTag
SUPPORTED_FORMATS = [
"mp3",
"wav",
"ogg"
]
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
self.path = path
2024-12-24 17:22:30 +01:00
self.property_string = property_string
self.tags = TinyTag.get(self.path)
self.cached = False
2024-12-24 17:22:30 +01:00
self.audio = None
self.sound = None
self.duration = 0
if cache:
2024-12-24 17:22:30 +01:00
self.cache()
def cache(self):
self.load_audio()
2024-12-21 21:06:10 +01:00
# audio = normalize(audio)
2024-12-24 17:22:30 +01:00
wav = self.audio.export(format="wav")
2024-12-24 17:22:30 +01:00
self.sound = Sound(wav)
2024-12-24 17:22:30 +01:00
self.duration = len(self.audio) # track duration in milliseconds
self.cached = True
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)