aboutsummaryrefslogtreecommitdiff
path: root/helpers/music_file.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-18 23:45:19 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-06-18 23:45:19 +0200
commit0e5d59f77586134da457ed7a3c57fc99649be590 (patch)
tree214318f8ed6a75e71ab8c3af5e806849a0407f12 /helpers/music_file.py
parentbe27763f8be0f647cbe17ecee8c782901ce2cede (diff)
downloadMusicSampler-0e5d59f77586134da457ed7a3c57fc99649be590.tar.gz
MusicSampler-0e5d59f77586134da457ed7a3c57fc99649be590.tar.zst
MusicSampler-0e5d59f77586134da457ed7a3c57fc99649be590.zip
Add some actions
Diffstat (limited to 'helpers/music_file.py')
-rw-r--r--helpers/music_file.py33
1 files changed, 28 insertions, 5 deletions
diff --git a/helpers/music_file.py b/helpers/music_file.py
index 39b0566..36a7dd9 100644
--- a/helpers/music_file.py
+++ b/helpers/music_file.py
@@ -21,15 +21,38 @@ class MusicFile:
21 self.loaded = True 21 self.loaded = True
22 lock.release() 22 lock.release()
23 23
24 def play(self): 24 def is_playing(self):
25 self.channel = self.sound.play() 25 return self.channel is not None and self.channel.get_busy()
26
27 def play(self, fade_in = 0, volume = 100, start_at = 0):
28 self.set_volume(volume)
29
30 if start_at > 0:
31 song_duration = self.sound.get_length()
32 raw_data_length = len(self.raw_data)
33 start_offset = int((raw_data_length / song_duration) * start_at)
34 start_offset = start_offset - (start_offset % 2)
35 self.sound = pygame.mixer.Sound(self.raw_data[start_offset:])
36 else:
37 self.sound = pygame.mixer.Sound(self.raw_data)
38
39 self.channel = self.sound.play(fade_ms = fade_in * 1000)
26 40
27 def pause(self): 41 def pause(self):
28 if self.channel is not None: 42 if self.channel is not None:
29 self.channel.pause() 43 self.channel.pause()
30 44
31 def stop(self): 45 def stop(self, fade_out = 0):
32 self.channel = None 46 self.channel = None
33 self.sound.stop() 47 if fade_out > 0:
34 48 self.sound.fadeout(fade_out * 1000)
49 else:
50 self.sound.stop()
51
52 def set_volume(self, value):
53 if value < 0:
54 value = 0
55 if value > 100:
56 value = 100
57 self.sound.set_volume(value / 100)
35 58