]> git.immae.eu Git - perso/Immae/Projets/Python/MusicSampler.git/blob - helpers/music_effect.py
Make seek work well with loops
[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, initial_loop, start, end,
7 **kwargs):
8 if effect in self.effect_types:
9 self.effect = effect
10 else:
11 raise Exception("Unknown effect {}".format(effect))
12
13 self.start = start
14 self.end = end
15 self.audio_segment = audio_segment
16 self.initial_loop = initial_loop
17 getattr(self, self.effect + "_init")(**kwargs)
18
19 def get_last_gain(self):
20 return getattr(self, self.effect + "_get_last_gain")()
21
22 def get_next_gain(self, current_frame, current_loop, frame_count):
23 # This returns two values:
24 # - The first one is the gain to apply on that frame
25 # - The last one is True or False depending on whether it is the last
26 # call to the function and the last gain should be saved permanently
27 return getattr(self, self.effect + "_get_next_gain")(
28 current_frame,
29 current_loop,
30 frame_count)
31
32 # Fading
33 def fade_init(self, gain=0, **kwargs):
34 self.audio_segment_frame_count = self.audio_segment.frame_count()
35 self.first_frame = int(
36 self.audio_segment_frame_count * self.initial_loop +\
37 self.audio_segment.frame_rate * self.start)
38 self.last_frame = int(
39 self.audio_segment_frame_count * self.initial_loop +\
40 self.audio_segment.frame_rate * self.end)
41 self.gain= gain
42
43 def fade_get_last_gain(self):
44 return self.gain
45
46 def fade_get_next_gain(self, current_frame, current_loop, frame_count):
47 current_frame = current_frame \
48 + (current_loop - self.initial_loop) \
49 * self.audio_segment_frame_count
50
51 if current_frame >= self.last_frame:
52 return [self.gain, True]
53 elif current_frame < self.first_frame:
54 return [0, False]
55 else:
56 return [
57 (current_frame - self.first_frame) / \
58 (self.last_frame - self.first_frame) * self.gain,
59 False
60 ]
61
62