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