X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=helpers%2Fmusic_effect.py;fp=helpers%2Fmusic_effect.py;h=ef8dc906025a61ea71a5a1a93540532cec666516;hb=aee1334ca47ff55c815eee204fe03683a572be0f;hp=0000000000000000000000000000000000000000;hpb=b37c72a236806f02e5538ba7e607f6add0cc6fb6;p=perso%2FImmae%2FProjets%2FPython%2FMusicSampler.git diff --git a/helpers/music_effect.py b/helpers/music_effect.py new file mode 100644 index 0000000..ef8dc90 --- /dev/null +++ b/helpers/music_effect.py @@ -0,0 +1,50 @@ +class GainEffect: + effect_types = [ + 'fade' + ] + + def __init__(self, effect, audio_segment, start, end, **kwargs): + if effect in self.effect_types: + self.effect = effect + else: + raise Exception("Unknown effect {}".format(effect)) + + self.start = start + self.end = end + self.audio_segment = audio_segment + 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): + # 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, + 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.gain= gain + + def fade_get_last_gain(self): + return self.gain + + def fade_get_next_gain(self, current_frame, frame_count): + if current_frame >= self.last_frame: + return [self.gain, True] + elif current_frame < self.first_frame: + return [0, False] + else: + return [ + (current_frame - self.first_frame) / \ + (self.last_frame - self.first_frame) * self.gain, + False + ] + +