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