C++11 tidying.
[dcpomatic.git] / src / lib / mid_side_decoder.cc
1 /*
2     Copyright (C) 2014-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 "mid_side_decoder.h"
23 #include "audio_buffers.h"
24 #include "audio_mapping.h"
25
26 #include "i18n.h"
27
28
29 using std::make_shared;
30 using std::min;
31 using std::shared_ptr;
32 using std::string;
33 using std::vector;
34
35
36 string
37 MidSideDecoder::name () const
38 {
39         return _("Mid-side decoder");
40 }
41
42
43 string
44 MidSideDecoder::id () const
45 {
46         return N_("mid-side-decoder");
47 }
48
49
50 int
51 MidSideDecoder::out_channels () const
52 {
53         return 3;
54 }
55
56
57 shared_ptr<AudioProcessor>
58 MidSideDecoder::clone (int) const
59 {
60         return make_shared<MidSideDecoder>();
61 }
62
63
64 shared_ptr<AudioBuffers>
65 MidSideDecoder::run (shared_ptr<const AudioBuffers> in, int channels)
66 {
67         int const N = min (channels, 3);
68         auto out = make_shared<AudioBuffers>(channels, in->frames ());
69         for (int i = 0; i < in->frames(); ++i) {
70                 auto const left = in->data()[0][i];
71                 auto const right = in->data()[1][i];
72                 auto const mid = (left + right) / 2;
73                 if (N > 0) {
74                         out->data()[0][i] = left - mid;
75                 }
76                 if (N > 1) {
77                         out->data()[1][i] = right - mid;
78                 }
79                 if (N > 2) {
80                         out->data()[2][i] = mid;
81                 }
82         }
83
84         for (int i = N; i < channels; ++i) {
85                 out->make_silent (i);
86         }
87
88         return out;
89 }
90
91
92 void
93 MidSideDecoder::make_audio_mapping_default (AudioMapping& mapping) const
94 {
95         /* Just map the first two input channels to our M/S */
96         mapping.make_zero ();
97         for (int i = 0; i < min(2, mapping.input_channels()); ++i) {
98                 mapping.set (i, i, 1);
99         }
100 }
101
102
103 vector<NamedChannel>
104 MidSideDecoder::input_names () const
105 {
106         return {
107                 NamedChannel(_("Left"), 0),
108                 NamedChannel(_("Right"), 1)
109         };
110 }