]>
Commit | Line | Data |
---|---|---|
aee1334c IB |
1 | class GainEffect: |
2 | effect_types = [ | |
3 | 'fade' | |
4 | ] | |
5 | ||
9925ce3b IB |
6 | def __init__(self, effect, audio_segment, initial_loop, start, end, |
7 | **kwargs): | |
aee1334c IB |
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 | |
1e44fe7e | 16 | self.initial_loop = initial_loop |
aee1334c IB |
17 | getattr(self, self.effect + "_init")(**kwargs) |
18 | ||
19 | def get_last_gain(self): | |
20 | return getattr(self, self.effect + "_get_last_gain")() | |
21 | ||
1e44fe7e | 22 | def get_next_gain(self, current_frame, current_loop, frame_count): |
aee1334c IB |
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, | |
1e44fe7e | 29 | current_loop, |
aee1334c IB |
30 | frame_count) |
31 | ||
32 | # Fading | |
33 | def fade_init(self, gain=0, **kwargs): | |
1e44fe7e IB |
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) | |
aee1334c IB |
41 | self.gain= gain |
42 | ||
43 | def fade_get_last_gain(self): | |
44 | return self.gain | |
45 | ||
1e44fe7e IB |
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 | ||
aee1334c IB |
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 |