diff options
Diffstat (limited to 'helpers')
-rw-r--r-- | helpers/music_file.py | 17 |
1 files changed, 8 insertions, 9 deletions
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: | |||
8 | self.channel_id = channel_id | 8 | self.channel_id = channel_id |
9 | self.name = name or filename | 9 | self.name = name or filename |
10 | self.raw_data = None | 10 | self.raw_data = None |
11 | self.sound = None | ||
12 | 11 | ||
13 | self.loaded = False | 12 | self.loaded = False |
14 | self.flag_paused = False | 13 | self.flag_paused = False |
@@ -17,8 +16,9 @@ class MusicFile: | |||
17 | def load_sound(self, lock): | 16 | def load_sound(self, lock): |
18 | lock.acquire() | 17 | lock.acquire() |
19 | print("Loading « {} »".format(self.name)) | 18 | print("Loading « {} »".format(self.name)) |
20 | self.raw_data = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100).raw_data | 19 | audio_segment = pydub.AudioSegment.from_file(self.filename).set_frame_rate(44100) |
21 | self.sound = pygame.mixer.Sound(self.raw_data) | 20 | self.sound_duration = audio_segment.duration_seconds |
21 | self.raw_data = audio_segment.raw_data | ||
22 | print("Loaded « {} »".format(self.name)) | 22 | print("Loaded « {} »".format(self.name)) |
23 | self.loaded = True | 23 | self.loaded = True |
24 | lock.release() | 24 | lock.release() |
@@ -35,15 +35,14 @@ class MusicFile: | |||
35 | self.set_volume(volume) | 35 | self.set_volume(volume) |
36 | 36 | ||
37 | if start_at > 0: | 37 | if start_at > 0: |
38 | song_duration = self.sound.get_length() | ||
39 | raw_data_length = len(self.raw_data) | 38 | raw_data_length = len(self.raw_data) |
40 | start_offset = int((raw_data_length / song_duration) * start_at) | 39 | start_offset = int((raw_data_length / self.sound_duration) * start_at) |
41 | start_offset = start_offset - (start_offset % 2) | 40 | start_offset = start_offset - (start_offset % 2) |
42 | self.sound = pygame.mixer.Sound(self.raw_data[start_offset:]) | 41 | sound = pygame.mixer.Sound(self.raw_data[start_offset:]) |
43 | else: | 42 | else: |
44 | self.sound = pygame.mixer.Sound(self.raw_data) | 43 | sound = pygame.mixer.Sound(self.raw_data) |
45 | 44 | ||
46 | self.channel().play(self.sound, fade_ms = fade_in * 1000) | 45 | self.channel().play(sound, fade_ms = fade_in * 1000) |
47 | self.flag_paused = False | 46 | self.flag_paused = False |
48 | 47 | ||
49 | def pause(self): | 48 | def pause(self): |
@@ -65,7 +64,7 @@ class MusicFile: | |||
65 | value = 0 | 64 | value = 0 |
66 | if value > 100: | 65 | if value > 100: |
67 | value = 100 | 66 | value = 100 |
68 | self.sound.set_volume(value / 100) | 67 | self.channel().set_volume(value / 100) |
69 | 68 | ||
70 | def wait_end(self): | 69 | def wait_end(self): |
71 | pass | 70 | pass |