]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blobdiff - helpers/music_file.py
Reduce memory
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_file.py
index 36a7dd9ee913bc350058ae24de4079c17a4eb749..10784596a38db66cbde3594aab0adcac3b2231fb 100644 (file)
@@ -3,56 +3,71 @@ import pydub
 import pygame
 
 class MusicFile:
-    def __init__(self, filename, lock):
+    def __init__(self, filename, lock, channel_id, name = None):
         self.filename = filename
-        self.channel = None
+        self.channel_id = channel_id
+        self.name = name or filename
         self.raw_data = None
-        self.sound = None
 
         self.loaded = False
-        threading.Thread(target = self.load_sound, args = [lock]).start()
+        self.flag_paused = False
+        threading.Thread(name = "MSMusicLoad", 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))
+        print("Loading « {} »".format(self.name))
+        audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100)
+        self.sound_duration = audio_segment.duration_seconds
+        self.raw_data = audio_segment.raw_data
+        print("Loaded « {} »".format(self.name))
         self.loaded = True
         lock.release()
 
     def is_playing(self):
-        return self.channel is not None and self.channel.get_busy()
+        return self.channel().get_busy()
+
+    def is_paused(self):
+        return self.flag_paused
 
     def play(self, fade_in = 0, volume = 100, start_at = 0):
+        self.channel().set_endevent()
+        self.channel().set_endevent(pygame.USEREVENT)
         self.set_volume(volume)
 
         if start_at > 0:
-            song_duration = self.sound.get_length()
             raw_data_length = len(self.raw_data)
-            start_offset = int((raw_data_length / song_duration) * start_at)
+            start_offset = int((raw_data_length / self.sound_duration) * start_at)
             start_offset = start_offset - (start_offset % 2)
-            self.sound = pygame.mixer.Sound(self.raw_data[start_offset:])
+            sound = pygame.mixer.Sound(self.raw_data[start_offset:])
         else:
-            self.sound = pygame.mixer.Sound(self.raw_data)
+            sound = pygame.mixer.Sound(self.raw_data)
 
-        self.channel = self.sound.play(fade_ms = fade_in * 1000)
+        self.channel().play(sound, fade_ms = fade_in * 1000)
+        self.flag_paused = False
 
     def pause(self):
-        if self.channel is not None:
-            self.channel.pause()
+        self.channel().pause()
+        self.flag_paused = True
+
+    def unpause(self):
+        self.channel().unpause()
+        self.flag_paused = False
 
     def stop(self, fade_out = 0):
-        self.channel = None
         if fade_out > 0:
-            self.sound.fadeout(fade_out * 1000)
+            self.channel().fadeout(fade_out * 1000)
         else:
-            self.sound.stop()
+            self.channel().stop()
 
     def set_volume(self, value):
         if value < 0:
             value = 0
         if value > 100:
             value = 100
-        self.sound.set_volume(value / 100)
+        self.channel().set_volume(value / 100)
+
+    def wait_end(self):
+        pass
 
+    def channel(self):
+        return pygame.mixer.Channel(self.channel_id)