aboutsummaryrefslogtreecommitdiff
path: root/helpers/music_file.py
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/music_file.py
parent52d58baf7a39e5e09a0b4ce24b1e951d98e7618b (diff)
downloadMusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.tar.gz
MusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.tar.zst
MusicSampler-6f4944c18398a7482297bd1d80fcd4ee926270ae.zip
Add loop option for play
Diffstat (limited to 'helpers/music_file.py')
-rw-r--r--helpers/music_file.py16
1 files changed, 11 insertions, 5 deletions
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