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
|
|
|
|
|
|
|
|
|
|
|
|
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
|
|
|
self.cached = cache
|
|
|
|
|
2024-12-28 20:41:18 +01:00
|
|
|
# get filename (will be replaced by proper name getter in future)
|
|
|
|
self.title = path.split("/")[-1].split(".")[0]
|
2024-12-24 17:22:30 +01:00
|
|
|
self.audio = None
|
|
|
|
self.sound = None
|
|
|
|
self.duration = 0
|
|
|
|
|
|
|
|
if self.cached:
|
|
|
|
self.cache()
|
2024-12-21 16:07:27 +01:00
|
|
|
|
|
|
|
def cache(self):
|
2024-12-24 17:22:30 +01:00
|
|
|
self.audio = AudioSegment.from_mp3(self.path)
|
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-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)
|