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