From 1e44fe7e6c1464b49582a163615e8cb3ed279d3e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Isma=C3=ABl=20Bouya?= Date: Wed, 20 Jul 2016 22:31:17 +0200 Subject: [PATCH] Remove a_s_with_effect in profit to GainEffect Use loop number in the effect --- helpers/music_effect.py | 21 ++++++++++++----- helpers/music_file.py | 50 ++++++++++++++--------------------------- 2 files changed, 33 insertions(+), 38 deletions(-) diff --git a/helpers/music_effect.py b/helpers/music_effect.py index ef8dc90..23583cd 100644 --- a/helpers/music_effect.py +++ b/helpers/music_effect.py @@ -3,7 +3,7 @@ class GainEffect: 'fade' ] - def __init__(self, effect, audio_segment, start, end, **kwargs): + def __init__(self, effect, audio_segment, initial_loop, start, end, **kwargs): if effect in self.effect_types: self.effect = effect else: @@ -12,30 +12,41 @@ class GainEffect: self.start = start self.end = end self.audio_segment = audio_segment + self.initial_loop = initial_loop getattr(self, self.effect + "_init")(**kwargs) def get_last_gain(self): return getattr(self, self.effect + "_get_last_gain")() - def get_next_gain(self, current_frame, frame_count): + def get_next_gain(self, current_frame, current_loop, frame_count): # This returns two values: # - The first one is the gain to apply on that frame # - The last one is True or False depending on whether it is the last # call to the function and the last gain should be saved permanently return getattr(self, self.effect + "_get_next_gain")( current_frame, + current_loop, frame_count) # Fading def fade_init(self, gain=0, **kwargs): - self.first_frame = int(self.audio_segment.frame_rate * self.start) - self.last_frame = int(self.audio_segment.frame_rate * self.end) + self.audio_segment_frame_count = self.audio_segment.frame_count() + self.first_frame = int( + self.audio_segment_frame_count * self.initial_loop +\ + self.audio_segment.frame_rate * self.start) + self.last_frame = int( + self.audio_segment_frame_count * self.initial_loop +\ + self.audio_segment.frame_rate * self.end) self.gain= gain def fade_get_last_gain(self): return self.gain - def fade_get_next_gain(self, current_frame, frame_count): + def fade_get_next_gain(self, current_frame, current_loop, frame_count): + current_frame = current_frame \ + + (current_loop - self.initial_loop) \ + * self.audio_segment_frame_count + if current_frame >= self.last_frame: return [self.gain, True] elif current_frame < self.first_frame: diff --git a/helpers/music_file.py b/helpers/music_file.py index 810bc22..a37a708 100644 --- a/helpers/music_file.py +++ b/helpers/music_file.py @@ -127,20 +127,22 @@ class MusicFile(Machine): def play(self, fade_in=0, volume=100, loop=0, start_at=0): self.set_gain(gain(volume) + self.mapping.master_gain, absolute=True) self.volume = volume - self.loop = loop + self.current_loop = 0 + self.loop_left = loop - ms = int(start_at * 1000) - ms_fi = int(fade_in * 1000) with self.music_lock: self.current_audio_segment = self.audio_segment self.current_frame = int(start_at * self.audio_segment.frame_rate) - if ms_fi > 0: - self.a_s_with_effect = self \ - .current_audio_segment[ms : ms+ms_fi] \ - .fade_in(ms_fi) - self.current_frame_with_effect = 0 - else: - self.a_s_with_effect = None + if fade_in > 0: + db_gain = gain(self.volume, 0)[0] + self.set_gain(-db_gain) + self.gain_effects.append(GainEffect( + "fade", + self.current_audio_segment, + self.current_loop, + self.sound_position, + self.sound_position + fade_in, + gain=db_gain)) self.start_playing() @@ -164,8 +166,9 @@ 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 != 0: - self.loop -= 1 + if self.is_loaded_playing() and self.loop_left != 0: + self.loop_left -= 1 + self.current_loop += 1 self.current_frame = 0 [new_data, new_nb_frames] = self.get_next_sample( frame_count - nb_frames) @@ -184,25 +187,6 @@ class MusicFile(Machine): data = b"" nb_frames = 0 - if self.a_s_with_effect is not None: - segment = self.a_s_with_effect - max_val = int(segment.frame_count()) - - start_i = max(self.current_frame_with_effect, 0) - end_i = min(self.current_frame_with_effect + frame_count, max_val) - - data += segment._data[start_i*fw : end_i*fw] - - frame_count = max( - 0, - self.current_frame_with_effect + frame_count - max_val) - - self.current_frame_with_effect += end_i - start_i - self.current_frame += end_i - start_i - nb_frames += end_i - start_i - - if frame_count > 0: - self.a_s_with_effect = None segment = self.current_audio_segment max_val = int(segment.frame_count()) @@ -213,7 +197,6 @@ class MusicFile(Machine): nb_frames += end_i - start_i self.current_frame += end_i - start_i - # FIXME: self.effects_next_gain should take into account the loop number volume_factor = self.volume_factor(self.effects_next_gain(nb_frames)) data = audioop.mul(data, Config.sample_width, volume_factor) @@ -226,7 +209,6 @@ class MusicFile(Machine): return with self.music_lock: self.abandon_all_effects() - self.a_s_with_effect = None self.current_frame = max( 0, int(delta) * self.current_frame @@ -238,6 +220,7 @@ class MusicFile(Machine): for gain_effect in self.gain_effects: [new_gain, last_gain] = gain_effect.get_next_gain( self.current_frame, + self.current_loop, frame_count) if last_gain: self.set_gain(new_gain) @@ -289,6 +272,7 @@ class MusicFile(Machine): self.gain_effects.append(GainEffect( "fade", self.current_audio_segment, + self.current_loop, self.sound_position, self.sound_position + fade, gain=db_gain)) -- 2.41.0