X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=helpers%2Fmusic_file.py;h=017fc596e236e14c700bff5bb88c746e56d38372;hb=54887692c36a97c3310a41f73ba1a53bcdec63b8;hp=a37a708cb5365ae519a76186ce5768a60280be08;hpb=1e44fe7e6c1464b49582a163615e8cb3ed279d3e;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git diff --git a/helpers/music_file.py b/helpers/music_file.py index a37a708..017fc59 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -14,7 +14,7 @@ from .music_effect import GainEffect file_lock = Lock("file") -class MusicFile(Machine): +class MusicFile: def __init__(self, filename, mapping, name=None, gain=1): states = [ 'initial', @@ -22,7 +22,13 @@ class MusicFile(Machine): 'failed', { 'name': 'loaded', - 'children': ['stopped', 'playing', 'paused', 'stopping'] + 'children': [ + 'stopped', + 'playing', + 'paused', + 'stopping', + 'stopped' + ] } ] transitions = [ @@ -69,8 +75,9 @@ class MusicFile(Machine): } ] - Machine.__init__(self, states=states, - transitions=transitions, initial='initial') + Machine(model=self, states=states, + transitions=transitions, initial='initial', + ignore_invalid_triggers=True) self.volume = 100 self.mapping = mapping @@ -125,10 +132,15 @@ class MusicFile(Machine): return 0 def play(self, fade_in=0, volume=100, loop=0, start_at=0): + # FIXME: create a "reinitialize" method + self.gain_effects = [] self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) self.volume = volume self.current_loop = 0 - self.loop_left = loop + if loop < 0: + self.last_loop = float('inf') + else: + self.last_loop = loop with self.music_lock: self.current_audio_segment = self.audio_segment @@ -166,8 +178,8 @@ class MusicFile(Machine): with self.music_lock: [data, nb_frames] = self.get_next_sample(frame_count) if nb_frames < frame_count: - if self.is_loaded_playing() and self.loop_left != 0: - self.loop_left -= 1 + if self.is_loaded_playing() and\ + self.current_loop < self.last_loop: self.current_loop += 1 self.current_frame = 0 [new_data, new_nb_frames] = self.get_next_sample( @@ -175,7 +187,7 @@ class MusicFile(Machine): data += new_data nb_frames += new_nb_frames elif nb_frames == 0: - # FIXME: too slow + # FIXME: too slow when mixing multiple streams threading.Thread( name="MSFinishedCallback", target=self.finished_callback).start() @@ -209,11 +221,26 @@ class MusicFile(Machine): return with self.music_lock: self.abandon_all_effects() - self.current_frame = max( - 0, - int(delta) * self.current_frame - + int(value * self.audio_segment.frame_rate)) - # FIXME: si on fait un seek + delta, adapter le "loop" + if delta: + frame_count = int(self.audio_segment.frame_count()) + frame_diff = int(value * self.audio_segment.frame_rate) + self.current_frame += frame_diff + while self.current_frame < 0: + self.current_loop -= 1 + self.current_frame += frame_count + while self.current_frame > frame_count: + self.current_loop += 1 + self.current_frame -= frame_count + if self.current_loop < 0: + self.current_loop = 0 + self.current_frame = 0 + if self.current_loop > self.last_loop: + self.current_loop = self.last_loop + self.current_frame = frame_count + else: + self.current_frame = max( + 0, + int(value * self.audio_segment.frame_rate)) def effects_next_gain(self, frame_count): db_gain = 0 @@ -238,7 +265,7 @@ class MusicFile(Machine): self.gain_effects = [] self.set_gain(db_gain) - def stop(self, fade_out=0, wait=False): + def stop(self, fade_out=0, wait=False, set_wait_id=None): if self.is_loaded_playing(): ms = int(self.sound_position * 1000) ms_fo = max(1, int(fade_out * 1000)) @@ -249,6 +276,8 @@ class MusicFile(Machine): self.current_audio_segment = new_audio_segment self.stop_playing() if wait: + if set_wait_id is not None: + self.mapping.add_wait_id(set_wait_id, self.wait_event) self.wait_end() else: self.stop_playing() @@ -268,6 +297,9 @@ class MusicFile(Machine): value + int(delta) * self.volume, self.volume) + if not (self.is_loaded_playing() or self.is_loaded_paused()): + return + if fade > 0: self.gain_effects.append(GainEffect( "fade",