aboutsummaryrefslogtreecommitdiff
path: root/helpers
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-14 23:44:25 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-14 23:44:25 +0200
commit6f4944c18398a7482297bd1d80fcd4ee926270ae (patch)
tree1980c46c32fee0d1544656bbaa8bd5a159893b6d /helpers
parent52d58baf7a39e5e09a0b4ce24b1e951d98e7618b (diff)
downloadMusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.tar.gz
MusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.tar.zst
MusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.zip
Add loop option for play
Diffstat (limited to 'helpers')
-rw-r--r--helpers/action.py15
-rw-r--r--helpers/music_file.py16
2 files changed, 22 insertions, 9 deletions
diff --git a/helpers/action.py b/helpers/action.py
index 3dc1b8d..792b123 100644
--- a/helpers/action.py
+++ b/helpers/action.py
@@ -64,15 +64,17 @@ class Action:
64 music.unpause() 64 music.unpause()
65 65
66 def play(self, music = None, fade_in = 0, start_at = 0, 66 def play(self, music = None, fade_in = 0, start_at = 0,
67 restart_if_running = False, volume = 100, **kwargs): 67 restart_if_running = False, volume = 100,
68 loop = 0,
69 **kwargs):
68 if music is not None: 70 if music is not None:
69 if restart_if_running: 71 if restart_if_running:
70 if music.is_not_stopped(): 72 if music.is_not_stopped():
71 music.stop() 73 music.stop()
72 music.play(volume = volume, fade_in = fade_in, start_at = start_at) 74 music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop)
73 else: 75 else:
74 if not music.is_not_stopped(): 76 if not music.is_not_stopped():
75 music.play(volume = volume, fade_in = fade_in, start_at = start_at) 77 music.play(volume = volume, fade_in = fade_in, start_at = start_at, loop = loop)
76 78
77 def seek(self, music = None, value = 0, delta = False, **kwargs): 79 def seek(self, music = None, value = 0, delta = False, **kwargs):
78 for music in self.music_list(music): 80 for music in self.music_list(music):
@@ -124,7 +126,7 @@ class Action:
124 return "unpausing all musics" 126 return "unpausing all musics"
125 127
126 def play_print(self, music = None, fade_in = 0, start_at = 0, 128 def play_print(self, music = None, fade_in = 0, start_at = 0,
127 restart_if_running = False, volume = 100, **kwargs): 129 restart_if_running = False, volume = 100, loop = 0, **kwargs):
128 message = "starting " 130 message = "starting "
129 if music is not None: 131 if music is not None:
130 message += "« {} »".format(music.name) 132 message += "« {} »".format(music.name)
@@ -139,6 +141,11 @@ class Action:
139 141
140 message += " at volume {}%".format(volume) 142 message += " at volume {}%".format(volume)
141 143
144 if loop > 0:
145 message += " {} times".format(loop + 1)
146 elif loop < 0:
147 message += " in loop"
148
142 if restart_if_running: 149 if restart_if_running:
143 message += " (restarting if already running)" 150 message += " (restarting if already running)"
144 151
diff --git a/helpers/music_file.py b/helpers/music_file.py
index 32869b6..0bfb1ec 100644
--- a/helpers/music_file.py
+++ b/helpers/music_file.py
@@ -78,19 +78,21 @@ class MusicFile(Machine):
78 else: 78 else:
79 return 0 79 return 0
80 80
81 def play(self, fade_in = 0, volume = 100, start_at = 0): 81 def play(self, fade_in = 0, volume = 100, loop = 0, start_at = 0):
82 db_gain = gain(volume) + self.mapping.master_gain 82 db_gain = gain(volume) + self.mapping.master_gain
83 self.volume = volume 83 self.volume = volume
84 self.loop = loop
84 85
85 ms = int(start_at * 1000) 86 ms = int(start_at * 1000)
86 ms_fi = max(1, int(fade_in * 1000)) 87 ms_fi = max(1, int(fade_in * 1000))
87 with self.music_lock: 88 with self.music_lock:
88 self.current_audio_segment = (self.audio_segment + db_gain).fade(from_gain=-120, duration=ms_fi, start=ms) 89 self.current_audio_segment = (self.audio_segment + db_gain).fade(from_gain=-120, duration=ms_fi, start=ms)
89 self.before_loaded_playing(initial_frame = int(start_at * self.audio_segment.frame_rate)) 90 self.initial_frame = int(start_at * self.audio_segment.frame_rate)
91 self.before_loaded_playing()
90 self.start_playing() 92 self.start_playing()
91 93
92 def before_loaded_playing(self, initial_frame = 0): 94 def before_loaded_playing(self):
93 self.current_frame = initial_frame 95 self.current_frame = self.initial_frame
94 with self.music_lock: 96 with self.music_lock:
95 segment = self.current_audio_segment 97 segment = self.current_audio_segment
96 98
@@ -125,7 +127,11 @@ class MusicFile(Machine):
125 ) 127 )
126 self.current_frame += frame_count 128 self.current_frame += frame_count
127 if len(audio_segment) == 0: 129 if len(audio_segment) == 0:
128 raise sd.CallbackStop 130 if self.is_loaded_playing() and self.loop != 0:
131 self.loop -= 1
132 self.current_frame = self.initial_frame
133 else:
134 raise sd.CallbackStop
129 135
130 out_data[:] = audio_segment.ljust(len(out_data), b'\0') 136 out_data[:] = audio_segment.ljust(len(out_data), b'\0')
131 137