aboutsummaryrefslogtreecommitdiff
path: root/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'helpers')
-rw-r--r--helpers/action.py26
-rw-r--r--helpers/music_file.py33
2 files changed, 48 insertions, 11 deletions
diff --git a/helpers/action.py b/helpers/action.py
index a68de90..8a69fae 100644
--- a/helpers/action.py
+++ b/helpers/action.py
@@ -43,23 +43,37 @@ class Action:
43 def play(self, music = None, fade_in = 0, start_at = 0, 43 def play(self, music = None, fade_in = 0, start_at = 0,
44 restart_if_running = False, volume = 100, **kwargs): 44 restart_if_running = False, volume = 100, **kwargs):
45 if music is not None: 45 if music is not None:
46 music.play() 46 if restart_if_running:
47 if music.is_playing():
48 music.stop()
49 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
50 else:
51 if not music.is_playing():
52 music.play(volume = volume, fade_in = fade_in, start_at = start_at)
47 else: 53 else:
48 pygame.mixer.unpause() 54 pygame.mixer.unpause()
49 55
50 def stop(self, music = None, fade_out = 0, **kwargs): 56 def stop(self, music = None, fade_out = 0, **kwargs):
51 if music is not None: 57 if music is not None:
52 music.stop() 58 music.stop(fade_out = fade_out)
53 else: 59 else:
54 pygame.mixer.stop() 60 if fade_out > 0:
61 pygame.fadeout(fade_out * 1000)
62 else:
63 pygame.mixer.stop()
55 64
56 def stop_all_actions(self, **kwargs): 65 def stop_all_actions(self, **kwargs):
57 self.key.mapping.stop_all_running() 66 self.key.mapping.stop_all_running()
58 67
59 def volume(self, music = None, value = 100, **kwargs): 68 def volume(self, music = None, value = 100, **kwargs):
60 pass 69 if music is not None:
70 music.set_volume(value)
71 else:
72 pass
61 73
62 def wait(self, duration = 0, **kwargs): 74 def wait(self, duration = 0, **kwargs):
75 # FIXME: Make it stoppable
76 # http://stackoverflow.com/questions/29082268/python-time-sleep-vs-event-wait
63 time.sleep(duration) 77 time.sleep(duration)
64 78
65 def command_print(self, command = "", **kwargs): 79 def command_print(self, command = "", **kwargs):
@@ -104,10 +118,10 @@ class Action:
104 else: 118 else:
105 return "stopping all musics with {}s fadeout".format(fade_out) 119 return "stopping all musics with {}s fadeout".format(fade_out)
106 120
107 def stop_all_actions_print(self): 121 def stop_all_actions_print(self, **kwargs):
108 return "stopping all actions" 122 return "stopping all actions"
109 123
110 def volume_print(self, music = None, value = 100, *kwargs): 124 def volume_print(self, music = None, value = 100, **kwargs):
111 if music is not None: 125 if music is not None:
112 return "setting volume of {} to {}%".format(music.filename, value) 126 return "setting volume of {} to {}%".format(music.filename, value)
113 else: 127 else:
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