From b58b8220c1f3f20e97ca806cf8db0e334b920f4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Isma=C3=ABl=20Bouya?= Date: Tue, 21 Jun 2016 14:10:07 +0200 Subject: Reduce memory --- helpers/music_file.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'helpers') diff --git a/helpers/music_file.py b/helpers/music_file.py index cdc9836..1078459 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -8,7 +8,6 @@ class MusicFile: self.channel_id = channel_id self.name = name or filename self.raw_data = None - self.sound = None self.loaded = False self.flag_paused = False @@ -17,8 +16,9 @@ class MusicFile: def load_sound(self, lock): lock.acquire() print("Loading « {} »".format(self.name)) - self.raw_data = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).raw_data - self.sound = pygame.mixer.Sound(self.raw_data) + 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() @@ -35,15 +35,14 @@ class MusicFile: 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().play(self.sound, fade_ms = fade_in * 1000) + self.channel().play(sound, fade_ms = fade_in * 1000) self.flag_paused = False def pause(self): @@ -65,7 +64,7 @@ class MusicFile: value = 0 if value > 100: value = 100 - self.sound.set_volume(value / 100) + self.channel().set_volume(value / 100) def wait_end(self): pass -- cgit v1.2.3