aboutsummaryrefslogtreecommitdiff
path: root/helpers/music_effect.py
diff options
context:
space:
mode:
authorIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-18 21:17:12 +0200
committerIsmaël Bouya <ismael.bouya@normalesup.org>2016-07-20 21:48:54 +0200
commitaee1334ca47ff55c815eee204fe03683a572be0f (patch)
treeba9beef4beca1ab9ac86430016ff223eece31e75 /helpers/music_effect.py
parentb37c72a236806f02e5538ba7e607f6add0cc6fb6 (diff)
downloadMusicSampler-aee1334ca47ff55c815eee204fe03683a572be0f.tar.gz
MusicSampler-aee1334ca47ff55c815eee204fe03683a572be0f.tar.zst
MusicSampler-aee1334ca47ff55c815eee204fe03683a572be0f.zip
Add fading
Diffstat (limited to 'helpers/music_effect.py')
-rw-r--r--helpers/music_effect.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/helpers/music_effect.py b/helpers/music_effect.py
new file mode 100644
index 0000000..ef8dc90
--- /dev/null
+++ b/helpers/music_effect.py
@@ -0,0 +1,50 @@
1class 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