aboutsummaryrefslogblamecommitdiff
path: root/helpers/music_file.py
blob: 39b0566ccd5c68ae925af87d93c45cb630f2dc78 (plain) (tree)


































                                                                            
import threading
import pydub
import pygame

class MusicFile:
    def __init__(self, filename, lock):
        self.filename = filename
        self.channel = None
        self.raw_data = None
        self.sound = None

        self.loaded = False
        threading.Thread(target = self.load_sound, args = [lock]).start()

    def load_sound(self, lock):
        lock.acquire()
        print("Loading {}".format(self.filename))
        self.raw_data = pydub.AudioSegment.from_file(self.filename).raw_data
        self.sound = pygame.mixer.Sound(self.raw_data)
        print("Loaded {}".format(self.filename))
        self.loaded = True
        lock.release()

    def play(self):
        self.channel = self.sound.play()

    def pause(self):
        if self.channel is not None:
            self.channel.pause()

    def stop(self):
        self.channel = None
        self.sound.stop()