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