]> git.sesse.net Git - casparcg/blob - common/tweener.h
[CHANGELOG] Updated for release of 2.1.0 Beta 1
[casparcg] / common / tweener.h
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #pragma once
23
24 #include <functional>
25 #include <vector>
26
27 namespace caspar {
28
29 /**
30  * A tweener can be used for creating any kind of (image position, image fade
31  * in/out, audio volume etc) transition, by invoking it for each temporal
32  * timepoint when a tweened value is needed.
33  *
34  * For video the temporal resolution will usually be each frame or field (for
35  * interlaced material).
36  *
37  * For audio the smoothest transitions will be generated by using the samplerate
38  * as temporal resolution, but using the video frame/field rate is probably fine
39  * most of the times and much less time consuming.
40  */
41 class tweener
42 {
43 public:
44         /**
45          * Constructor.
46          *
47          * @param name The name of the tween function to use.
48          */
49         tweener(const std::wstring& name = L"linear");
50
51         /**
52          * @return The possible tween function names. Some of them may also support
53          *                 additional parameters appended to the name.
54          */
55         static const std::vector<std::wstring>& names();
56
57         /**
58          * Calculate a tweened value given a timepoint within the total duration
59          * and the starting value and the destination delta value.
60          *
61          * Usually b, c and d remains constant during a transition, while t changes
62          * for each temporal tweened value.
63          * 
64          * @param t     The timepoint within the total duration (0 <= n <= d).
65          * @param b     The starting value.
66          * @param c     The destination value delta from the starting value
67          *              (absolute destination value - b).
68          * @param d The total duration (when t = d, the destination value should
69          *              have been reached).
70          *
71          * @return The tweened value for the given timepoint. Can sometimes be less
72          *             than b or greater than b + c for some tweener functions.
73          */
74         double operator()(double t, double b , double c, double d) const;
75
76         bool operator==(const tweener& other) const;
77         bool operator!=(const tweener& other) const;
78 private:
79         std::function<double(double, double, double, double)>   func_;
80         std::wstring                                                                                    name_;
81 };
82
83 }