]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/music_effect.py
Add fading
[perso/Immae/Projets/Python/MusicSampler.git] / helpers / music_effect.py
1 class GainEffect:
2 effect_types = [
3 'fade'
4 ]
5
6 def __init__(self, effect, audio_segment, start, end, **kwargs):
7 if effect in self.effect_types:
8 self.effect = effect
9 else:
10 raise Exception("Unknown effect {}".format(effect))
11
12 self.start = start
13 self.end = end
14 self.audio_segment = audio_segment
15 getattr(self, self.effect + "_init")(**kwargs)
16
17 def get_last_gain(self):
18 return getattr(self, self.effect + "_get_last_gain")()
19
20 def get_next_gain(self, current_frame, frame_count):
21 # This returns two values:
22 # - The first one is the gain to apply on that frame
23 # - The last one is True or False depending on whether it is the last
24 # call to the function and the last gain should be saved permanently
25 return getattr(self, self.effect + "_get_next_gain")(
26 current_frame,
27 frame_count)
28
29 # Fading
30 def fade_init(self, gain=0, **kwargs):
31 self.first_frame = int(self.audio_segment.frame_rate * self.start)
32 self.last_frame = int(self.audio_segment.frame_rate * self.end)
33 self.gain= gain
34
35 def fade_get_last_gain(self):
36 return self.gain
37
38 def fade_get_next_gain(self, current_frame, frame_count):
39 if current_frame >= self.last_frame:
40 return [self.gain, True]
41 elif current_frame < self.first_frame:
42 return [0, False]
43 else:
44 return [
45 (current_frame - self.first_frame) / \
46 (self.last_frame - self.first_frame) * self.gain,
47 False
48 ]
49
50