]>
Commit | Line | Data |
---|---|---|
1 | class GainEffect: | |
2 | effect_types = [ | |
3 | 'noop', | |
4 | 'fade' | |
5 | ] | |
6 | ||
7 | def __init__(self, effect, audio_segment, initial_loop, start, end, | |
8 | **kwargs): | |
9 | if effect in self.effect_types: | |
10 | self.effect = effect | |
11 | else: | |
12 | self.effect = 'noop' | |
13 | ||
14 | self.start = start | |
15 | self.end = end | |
16 | self.audio_segment = audio_segment | |
17 | self.initial_loop = initial_loop | |
18 | getattr(self, self.effect + "_init")(**kwargs) | |
19 | ||
20 | def get_last_gain(self): | |
21 | return getattr(self, self.effect + "_get_last_gain")() | |
22 | ||
23 | def get_next_gain(self, current_frame, current_loop, frame_count): | |
24 | # This returns two values: | |
25 | # - The first one is the gain to apply on that frame | |
26 | # - The last one is True or False depending on whether it is the last | |
27 | # call to the function and the last gain should be saved permanently | |
28 | return getattr(self, self.effect + "_get_next_gain")( | |
29 | current_frame, | |
30 | current_loop, | |
31 | frame_count) | |
32 | ||
33 | # Noop | |
34 | def noop_init(self, **kwargs): | |
35 | pass | |
36 | ||
37 | def noop_get_last_gain(self, **kwargs): | |
38 | return 0 | |
39 | ||
40 | def noop_get_next_gain(self, **kwargs): | |
41 | return [0, True] | |
42 | ||
43 | # Fading | |
44 | def fade_init(self, gain=0, **kwargs): | |
45 | self.audio_segment_frame_count = self.audio_segment.frame_count() | |
46 | self.first_frame = int( | |
47 | self.audio_segment_frame_count * self.initial_loop +\ | |
48 | self.audio_segment.frame_rate * self.start) | |
49 | self.last_frame = int( | |
50 | self.audio_segment_frame_count * self.initial_loop +\ | |
51 | self.audio_segment.frame_rate * self.end) | |
52 | self.gain= gain | |
53 | ||
54 | def fade_get_last_gain(self): | |
55 | return self.gain | |
56 | ||
57 | def fade_get_next_gain(self, current_frame, current_loop, frame_count): | |
58 | current_frame = current_frame \ | |
59 | + (current_loop - self.initial_loop) \ | |
60 | * self.audio_segment_frame_count | |
61 | ||
62 | if current_frame >= self.last_frame: | |
63 | return [self.gain, True] | |
64 | elif current_frame < self.first_frame: | |
65 | return [0, False] | |
66 | else: | |
67 | return [ | |
68 | (current_frame - self.first_frame) / \ | |
69 | (self.last_frame - self.first_frame) * self.gain, | |
70 | False | |
71 | ] | |
72 | ||
73 |