File: fade_functions.py

package info (click to toggle)
linux-show-player 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,896 kB
  • sloc: python: 12,408; sh: 154; makefile: 17; xml: 8
file content (76 lines) | stat: -rw-r--r-- 2,113 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# -*- coding: utf-8 -*-
#
# Python porting of qtractor fade functions
#
# This file is part of Linux Show Player
#
# Copyright 2012-2016 Francesco Ceruti <ceppofrancy@gmail.com>
#
# Linux Show Player is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Linux Show Player is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Linux Show Player.  If not, see <http://www.gnu.org/licenses/>

"""
Functor arguments normalization:

t = (x - x0) / (x1 - x0)   where 'x' is the time  (x0 initial, x1 final)
a = y1 - y0                where 'y' is the value (y0 initial, y1 final)
b = y0
"""

from enum import Enum

from lisp.core.util import FunctionProxy


def fade_linear(t, a, b):
    """Linear fade."""
    return a * t + b


def fadein_quad(t, a, b):
    """Quadratic (t^2) fade in: accelerating from zero velocity."""
    return a * (t ** 2) + b


def fadeout_quad(t, a, b):
    """Quadratic (t^2) fade out: decelerating to zero velocity."""
    return a * (t * (2 - t)) + b


def fade_inout_quad(t, a, b):
    """Quadratic (t^2) fade in-out: acceleration until halfway,
    then deceleration.
    """
    t *= 2
    if t < 1.0:
        return 0.5 * a * (t ** 2) + b
    else:
        t -= 1
        return 0.5 * a * (1 - (t * (t - 2))) + b


def ntime(time, begin, duration):
    """Return normalized time."""
    return (time - begin) / (duration - begin)


class FadeInType(Enum):
    Linear = FunctionProxy(fade_linear)
    Quadratic = FunctionProxy(fadein_quad)
    Quadratic2 = FunctionProxy(fade_inout_quad)


class FadeOutType(Enum):
    Linear = FunctionProxy(fade_linear)
    Quadratic = FunctionProxy(fadeout_quad)
    Quadratic2 = FunctionProxy(fade_inout_quad)