Fix the build for older macOS.
[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<CinemaSoundProcessor const *> 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         return _cinema_sound_processors;
61 }
62
63
64 /** Set up the static _sound_processors vector; must be called before from_*
65  *  methods are used.
66  */
67 void
68 CinemaSoundProcessor::setup_cinema_sound_processors ()
69 {
70         _cinema_sound_processors.push_back (new DolbyCP750);
71         _cinema_sound_processors.push_back (new USL);
72         _cinema_sound_processors.push_back (new DatasatAP2x);
73 }
74
75
76 /** @param id One of our ids.
77  *  @return Corresponding sound processor, or 0.
78  */
79 CinemaSoundProcessor const *
80 CinemaSoundProcessor::from_id (string id)
81 {
82         auto i = _cinema_sound_processors.begin ();
83         while (i != _cinema_sound_processors.end() && (*i)->id() != id) {
84                 ++i;
85         }
86
87         if (i == _cinema_sound_processors.end ()) {
88                 return nullptr;
89         }
90
91         return *i;
92 }
93
94
95 /** @param s A sound processor from our static list.
96  *  @return Index of the sound processor with the list, or -1.
97  */
98 int
99 CinemaSoundProcessor::as_index (CinemaSoundProcessor const * s)
100 {
101         vector<CinemaSoundProcessor*>::size_type i = 0;
102         while (i < _cinema_sound_processors.size() && _cinema_sound_processors[i] != s) {
103                 ++i;
104         }
105
106         if (i == _cinema_sound_processors.size ()) {
107                 return -1;
108         }
109
110         return i;
111 }
112
113
114 /** @param i An index returned from as_index().
115  *  @return Corresponding sound processor.
116  */
117 CinemaSoundProcessor const *
118 CinemaSoundProcessor::from_index (int i)
119 {
120         DCPOMATIC_ASSERT (i >= 0 && i < int(_cinema_sound_processors.size()));
121         return _cinema_sound_processors[i];
122 }
123
124
125 float
126 CinemaSoundProcessor::db_for_fader_change (float from, float to) const
127 {
128         float db = 0;
129
130         if (from < to) {
131                 if (from <= _knee) {
132                         float const t = min (to, _knee);
133                         db += (t - from) * _below;
134                 }
135
136                 if (to > 4) {
137                         float const t = max (from, _knee);
138                         db += (to - t) * _above;
139                 }
140         } else {
141                 if (from >= _knee) {
142                         float const t = max (to, _knee);
143                         db -= (from - t) * _above;
144                 }
145
146                 if (to < _knee) {
147                         float const t = min (from, _knee);
148                         db -= (t - to) * _below;
149                 }
150         }
151
152         return db;
153 }