C++11 tidying.
[dcpomatic.git] / src / lib / upmixer_b.cc
1 /*
2     Copyright (C) 2015-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 #include "upmixer_b.h"
23 #include "audio_buffers.h"
24 #include "audio_mapping.h"
25
26 #include "i18n.h"
27
28
29 using std::string;
30 using std::make_shared;
31 using std::min;
32 using std::vector;
33 using std::shared_ptr;
34
35
36 UpmixerB::UpmixerB (int sampling_rate)
37         : _lfe (0.01, 150.0 / sampling_rate)
38         , _delay (0.02 * sampling_rate)
39 {
40
41 }
42
43
44 string
45 UpmixerB::name () const
46 {
47         return _("Stereo to 5.1 up-mixer B");
48 }
49
50
51 string
52 UpmixerB::id () const
53 {
54         return N_("stereo-5.1-upmix-b");
55 }
56
57
58 int
59 UpmixerB::out_channels () const
60 {
61         return 6;
62 }
63
64
65 shared_ptr<AudioProcessor>
66 UpmixerB::clone (int sampling_rate) const
67 {
68         return make_shared<UpmixerB>(sampling_rate);
69 }
70
71
72 shared_ptr<AudioBuffers>
73 UpmixerB::run (shared_ptr<const AudioBuffers> in, int channels)
74 {
75         auto out = make_shared<AudioBuffers>(channels, in->frames());
76
77         /* L + R minus 6dB (in terms of amplitude) */
78         auto in_LR = in->channel(0);
79         in_LR->accumulate_frames (in->channel(1).get(), in->frames(), 0, 0);
80         in_LR->apply_gain (-6);
81
82         if (channels > 0) {
83                 /* L = Lt */
84                 out->copy_channel_from (in.get(), 0, 0);
85         }
86
87         if (channels > 1) {
88                 /* R = Rt */
89                 out->copy_channel_from (in.get(), 1, 1);
90         }
91
92         if (channels > 2) {
93                 /* C = L + R minus 3dB */
94                 out->copy_channel_from (in_LR.get(), 0, 2);
95         }
96
97         if (channels > 3) {
98                 /* Lfe is filtered C */
99                 out->copy_channel_from (_lfe.run(in_LR).get(), 0, 3);
100         }
101
102         shared_ptr<AudioBuffers> S;
103         if (channels > 4) {
104                 /* Ls is L - R with some delay */
105                 auto sub = make_shared<AudioBuffers>(1, in->frames());
106                 sub->copy_channel_from (in.get(), 0, 0);
107                 float* p = sub->data (0);
108                 float const * q = in->data (1);
109                 for (int i = 0; i < in->frames(); ++i) {
110                         *p++ -= *q++;
111                 }
112                 S = _delay.run (sub);
113                 out->copy_channel_from (S.get(), 0, 4);
114         }
115
116         if (channels > 5) {
117                 /* Rs = Ls */
118                 out->copy_channel_from (S.get(), 0, 5);
119         }
120
121         return out;
122 }
123
124
125 void
126 UpmixerB::flush ()
127 {
128         _lfe.flush ();
129         _delay.flush ();
130 }
131
132
133 void
134 UpmixerB::make_audio_mapping_default (AudioMapping& mapping) const
135 {
136         /* Just map the first two input channels to our L/R */
137         mapping.make_zero ();
138         for (int i = 0; i < min (2, mapping.input_channels()); ++i) {
139                 mapping.set (i, i, 1);
140         }
141 }
142
143
144 vector<NamedChannel>
145 UpmixerB::input_names () const
146 {
147         return {
148                 NamedChannel(_("Upmix L"), 0),
149                 NamedChannel(_("Upmix R"), 1)
150         };
151 }