47 lines
1.2 KiB
Python
47 lines
1.2 KiB
Python
#!/usr/bin/python3
|
|
|
|
from pydub import AudioSegment
|
|
from pydub.effects import normalize
|
|
from pygame.mixer import Sound
|
|
from tinytag import TinyTag
|
|
|
|
|
|
class Track:
|
|
"""
|
|
Class containing data for a track like file path, raw data...
|
|
"""
|
|
|
|
def __init__(self, app, path: str, property_string: str=None, cache: bool=False):
|
|
self.app = app
|
|
self.path = path
|
|
self.property_string = property_string
|
|
self.cached = cache
|
|
|
|
self.tags = TinyTag.get(self.path)
|
|
|
|
self.audio = None
|
|
self.sound = None
|
|
self.duration = 0
|
|
|
|
if self.cached:
|
|
self.cache()
|
|
|
|
def cache(self):
|
|
self.audio = AudioSegment.from_mp3(self.path)
|
|
# audio = normalize(audio)
|
|
|
|
wav = self.audio.export(format="wav")
|
|
|
|
self.sound = Sound(wav)
|
|
|
|
self.duration = len(self.audio) # track duration in milliseconds
|
|
|
|
def remaining(self, position: int):
|
|
remaining_audio = self.audio[position:]
|
|
|
|
wav = remaining_audio.export(format="wav")
|
|
|
|
sound = Sound(wav)
|
|
|
|
# return the remaining part of the track's audio and the duration of the remaining part
|
|
return sound, len(remaining_audio)
|