Use make_shared<>.
[dcpomatic.git] / src / lib / upmixer_a.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 "upmixer_a.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 UpmixerA::UpmixerA (int sampling_rate)
35         : _left (0.02, 1900.0 / sampling_rate, 4800.0 / sampling_rate)
36         , _right (0.02, 1900.0 / sampling_rate, 4800.0 / sampling_rate)
37         , _centre (0.01, 150.0 / sampling_rate, 1900.0 / sampling_rate)
38         , _lfe (0.01, 150.0 / sampling_rate)
39         , _ls (0.02, 4800.0 / sampling_rate, 20000.0 / sampling_rate)
40         , _rs (0.02, 4800.0 / sampling_rate, 20000.0 / sampling_rate)
41 {
42
43 }
44
45 string
46 UpmixerA::name () const
47 {
48         return _("Stereo to 5.1 up-mixer A");
49 }
50
51 string
52 UpmixerA::id () const
53 {
54         return N_("stereo-5.1-upmix-a");
55 }
56
57 int
58 UpmixerA::out_channels () const
59 {
60         return 6;
61 }
62
63 shared_ptr<AudioProcessor>
64 UpmixerA::clone (int sampling_rate) const
65 {
66         return make_shared<UpmixerA> (sampling_rate);
67 }
68
69 shared_ptr<AudioBuffers>
70 UpmixerA::run (shared_ptr<const AudioBuffers> in, int channels)
71 {
72         /* Input L and R */
73         shared_ptr<AudioBuffers> in_L = in->channel (0);
74         shared_ptr<AudioBuffers> in_R = in->channel (1);
75
76         /* Mix of L and R; -6dB down in amplitude (3dB in terms of power) */
77         shared_ptr<AudioBuffers> in_LR = in_L->clone ();
78         in_LR->accumulate_frames (in_R.get(), 0, 0, in_R->frames ());
79         in_LR->apply_gain (-6);
80
81         /* Run filters */
82         vector<shared_ptr<AudioBuffers> > all_out;
83         all_out.push_back (_left.run (in_L));
84         all_out.push_back (_right.run (in_R));
85         all_out.push_back (_centre.run (in_LR));
86         all_out.push_back (_lfe.run (in_LR));
87         all_out.push_back (_ls.run (in_L));
88         all_out.push_back (_rs.run (in_R));
89
90         shared_ptr<AudioBuffers> out = make_shared<AudioBuffers> (channels, in->frames ());
91         int const N = min (channels, 6);
92
93         for (int i = 0; i < N; ++i) {
94                 out->copy_channel_from (all_out[i].get(), 0, i);
95         }
96
97         for (int i = N; i < channels; ++i) {
98                 out->make_silent (i);
99         }
100
101         return out;
102 }
103
104 void
105 UpmixerA::flush ()
106 {
107         _left.flush ();
108         _right.flush ();
109         _centre.flush ();
110         _lfe.flush ();
111         _ls.flush ();
112         _rs.flush ();
113 }
114
115 void
116 UpmixerA::make_audio_mapping_default (AudioMapping& mapping) const
117 {
118         /* Just map the first two input channels to our L/R */
119         mapping.make_zero ();
120         for (int i = 0; i < min (2, mapping.input_channels()); ++i) {
121                 mapping.set (i, i, 1);
122         }
123 }
124
125 vector<string>
126 UpmixerA::input_names () const
127 {
128         vector<string> n;
129         n.push_back (_("Upmix L"));
130         n.push_back (_("Upmix R"));
131         return n;
132 }