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