Barely-functioning GL playback with new arrangement.
[dcpomatic.git] / src / lib / cinema_sound_processor.cc
1 /*
2     Copyright (C) 2012-2014 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 /** @file src/sound_processor.cc
22  *  @brief CinemaSoundProcessor class.
23  */
24
25 #include "cinema_sound_processor.h"
26 #include "dolby_cp750.h"
27 #include "usl.h"
28 #include "datasat_ap2x.h"
29 #include "dcpomatic_assert.h"
30 #include <iostream>
31 #include <cassert>
32
33 using namespace std;
34
35 vector<CinemaSoundProcessor const *> CinemaSoundProcessor::_cinema_sound_processors;
36
37 /** @param i Our id.
38  *  @param n User-visible name.
39  */
40 CinemaSoundProcessor::CinemaSoundProcessor (string i, string n, float knee, float below, float above)
41         : _id (i)
42         , _name (n)
43         , _knee (knee)
44         , _below (below)
45         , _above (above)
46 {
47
48 }
49
50 /** @return All available sound processors */
51 vector<CinemaSoundProcessor const *>
52 CinemaSoundProcessor::all ()
53 {
54         return _cinema_sound_processors;
55 }
56
57 /** Set up the static _sound_processors vector; must be called before from_*
58  *  methods are used.
59  */
60 void
61 CinemaSoundProcessor::setup_cinema_sound_processors ()
62 {
63         _cinema_sound_processors.push_back (new DolbyCP750);
64         _cinema_sound_processors.push_back (new USL);
65         _cinema_sound_processors.push_back (new DatasatAP2x);
66 }
67
68 /** @param id One of our ids.
69  *  @return Corresponding sound processor, or 0.
70  */
71 CinemaSoundProcessor const *
72 CinemaSoundProcessor::from_id (string id)
73 {
74         vector<CinemaSoundProcessor const *>::iterator i = _cinema_sound_processors.begin ();
75         while (i != _cinema_sound_processors.end() && (*i)->id() != id) {
76                 ++i;
77         }
78
79         if (i == _cinema_sound_processors.end ()) {
80                 return 0;
81         }
82
83         return *i;
84 }
85
86 /** @param s A sound processor from our static list.
87  *  @return Index of the sound processor with the list, or -1.
88  */
89 int
90 CinemaSoundProcessor::as_index (CinemaSoundProcessor const * s)
91 {
92         vector<CinemaSoundProcessor*>::size_type i = 0;
93         while (i < _cinema_sound_processors.size() && _cinema_sound_processors[i] != s) {
94                 ++i;
95         }
96
97         if (i == _cinema_sound_processors.size ()) {
98                 return -1;
99         }
100
101         return i;
102 }
103
104 /** @param i An index returned from as_index().
105  *  @return Corresponding sound processor.
106  */
107 CinemaSoundProcessor const *
108 CinemaSoundProcessor::from_index (int i)
109 {
110         DCPOMATIC_ASSERT (i <= int(_cinema_sound_processors.size ()));
111         return _cinema_sound_processors[i];
112 }
113
114 float
115 CinemaSoundProcessor::db_for_fader_change (float from, float to) const
116 {
117         float db = 0;
118
119         if (from < to) {
120                 if (from <= _knee) {
121                         float const t = min (to, _knee);
122                         db += (t - from) * _below;
123                 }
124
125                 if (to > 4) {
126                         float const t = max (from, _knee);
127                         db += (to - t) * _above;
128                 }
129         } else {
130                 if (from >= _knee) {
131                         float const t = max (to, _knee);
132                         db -= (from - t) * _above;
133                 }
134
135                 if (to < _knee) {
136                         float const t = min (from, _knee);
137                         db -= (t - to) * _below;
138                 }
139         }
140
141         return db;
142 }