]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blame - helpers/music_file.py
Move classes to separate file
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
CommitLineData
be27763f
IB
1import threading
2import pydub
3import pygame
4
5class MusicFile:
6 def __init__(self, filename, lock):
7 self.filename = filename
8 self.channel = None
9 self.raw_data = None
10 self.sound = None
11
12 self.loaded = False
13 threading.Thread(target = self.load_sound, args = [lock]).start()
14
15 def load_sound(self, lock):
16 lock.acquire()
17 print("Loading {}".format(self.filename))
18 self.raw_data = pydub.AudioSegment.from_file(self.filename).raw_data
19 self.sound = pygame.mixer.Sound(self.raw_data)
20 print("Loaded {}".format(self.filename))
21 self.loaded = True
22 lock.release()
23
24 def play(self):
25 self.channel = self.sound.play()
26
27 def pause(self):
28 if self.channel is not None:
29 self.channel.pause()
30
31 def stop(self):
32 self.channel = None
33 self.sound.stop()
34
35