Missed update to private test repo version.
[dcpomatic.git] / src / lib / cinema_sound_processor.cc
1 /*
2     Copyright (C) 2012-2021 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
22 /** @file src/sound_processor.cc
23  *  @brief CinemaSoundProcessor class.
24  */
25
26
27 #include "cinema_sound_processor.h"
28 #include "dolby_cp750.h"
29 #include "usl.h"
30 #include "datasat_ap2x.h"
31 #include "dcpomatic_assert.h"
32 #include <iostream>
33 #include <cassert>
34
35
36 using namespace std;
37
38
39 vector<unique_ptr<const CinemaSoundProcessor>> CinemaSoundProcessor::_cinema_sound_processors;
40
41
42 /** @param i Our id.
43  *  @param n User-visible name.
44  */
45 CinemaSoundProcessor::CinemaSoundProcessor (string i, string n, float knee, float below, float above)
46         : _id (i)
47         , _name (n)
48         , _knee (knee)
49         , _below (below)
50         , _above (above)
51 {
52
53 }
54
55
56 /** @return All available sound processors */
57 vector<CinemaSoundProcessor const *>
58 CinemaSoundProcessor::all ()
59 {
60         vector<CinemaSoundProcessor const *> raw;
61         for (auto& processor: _cinema_sound_processors) {
62                 raw.push_back (processor.get());
63         }
64         return raw;
65 }
66
67
68 /** Set up the static _sound_processors vector; must be called before from_*
69  *  methods are used.
70  */
71 void
72 CinemaSoundProcessor::setup_cinema_sound_processors ()
73 {
74         _cinema_sound_processors.push_back (unique_ptr<CinemaSoundProcessor>(new DolbyCP750));
75         _cinema_sound_processors.push_back (unique_ptr<CinemaSoundProcessor>(new USL));
76         _cinema_sound_processors.push_back (unique_ptr<CinemaSoundProcessor>(new DatasatAP2x));
77 }
78
79
80 /** @param id One of our ids.
81  *  @return Corresponding sound processor, or 0.
82  */
83 CinemaSoundProcessor const *
84 CinemaSoundProcessor::from_id (string id)
85 {
86         auto i = _cinema_sound_processors.begin ();
87         while (i != _cinema_sound_processors.end() && (*i)->id() != id) {
88                 ++i;
89         }
90
91         if (i == _cinema_sound_processors.end ()) {
92                 return nullptr;
93         }
94
95         return i->get();
96 }
97
98
99 /** @param i An index returned from as_index().
100  *  @return Corresponding sound processor.
101  */
102 CinemaSoundProcessor const *
103 CinemaSoundProcessor::from_index (int i)
104 {
105         DCPOMATIC_ASSERT (i >= 0 && i < int(_cinema_sound_processors.size()));
106         return _cinema_sound_processors[i].get();
107 }
108
109
110 float
111 CinemaSoundProcessor::db_for_fader_change (float from, float to) const
112 {
113         float db = 0;
114
115         if (from < to) {
116                 if (from <= _knee) {
117                         float const t = min (to, _knee);
118                         db += (t - from) * _below;
119                 }
120
121                 if (to > 4) {
122                         float const t = max (from, _knee);
123                         db += (to - t) * _above;
124                 }
125         } else {
126                 if (from >= _knee) {
127                         float const t = max (to, _knee);
128                         db -= (from - t) * _above;
129                 }
130
131                 if (to < _knee) {
132                         float const t = min (from, _knee);
133                         db -= (t - to) * _below;
134                 }
135         }
136
137         return db;
138 }