Optimise audio filters; tweak order of the LPFs in the upmixers.
[dcpomatic.git] / src / lib / upmixer_a.cc
1 /*
2     Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "upmixer_a.h"
21 #include "audio_buffers.h"
22 #include "audio_mapping.h"
23
24 #include "i18n.h"
25
26 using std::string;
27 using std::min;
28 using std::vector;
29 using boost::shared_ptr;
30
31 UpmixerA::UpmixerA (int sampling_rate)
32         : _left (0.02, 1900.0 / sampling_rate, 4800.0 / sampling_rate)
33         , _right (0.02, 1900.0 / sampling_rate, 4800.0 / sampling_rate)
34         , _centre (0.01, 150.0 / sampling_rate, 1900.0 / sampling_rate)
35         , _lfe (0.01, 150.0 / sampling_rate)
36         , _ls (0.02, 4800.0 / sampling_rate, 20000.0 / sampling_rate)
37         , _rs (0.02, 4800.0 / sampling_rate, 20000.0 / sampling_rate)
38 {
39
40 }
41
42 string
43 UpmixerA::name () const
44 {
45         return _("Stereo to 5.1 up-mixer A");
46 }
47
48 string
49 UpmixerA::id () const
50 {
51         return N_("stereo-5.1-upmix-a");
52 }
53
54 int
55 UpmixerA::out_channels () const
56 {
57         return 6;
58 }
59
60 shared_ptr<AudioProcessor>
61 UpmixerA::clone (int sampling_rate) const
62 {
63         return shared_ptr<AudioProcessor> (new UpmixerA (sampling_rate));
64 }
65
66 shared_ptr<AudioBuffers>
67 UpmixerA::run (shared_ptr<const AudioBuffers> in, int channels)
68 {
69         /* Input L and R */
70         shared_ptr<AudioBuffers> in_L = in->channel (0);
71         shared_ptr<AudioBuffers> in_R = in->channel (1);
72
73         /* Mix of L and R; -6dB down in amplitude (3dB in terms of power) */
74         shared_ptr<AudioBuffers> in_LR = in_L->clone ();
75         in_LR->accumulate_frames (in_R.get(), 0, 0, in_R->frames ());
76         in_LR->apply_gain (-6);
77
78         /* Run filters */
79         vector<shared_ptr<AudioBuffers> > all_out;
80         all_out.push_back (_left.run (in_L));
81         all_out.push_back (_right.run (in_R));
82         all_out.push_back (_centre.run (in_LR));
83         all_out.push_back (_lfe.run (in_LR));
84         all_out.push_back (_ls.run (in_L));
85         all_out.push_back (_rs.run (in_R));
86
87         shared_ptr<AudioBuffers> out (new AudioBuffers (channels, in->frames ()));
88         int const N = min (channels, 6);
89
90         for (int i = 0; i < N; ++i) {
91                 out->copy_channel_from (all_out[i].get(), 0, i);
92         }
93
94         for (int i = N; i < channels; ++i) {
95                 out->make_silent (i);
96         }
97
98         return out;
99 }
100
101 void
102 UpmixerA::flush ()
103 {
104         _left.flush ();
105         _right.flush ();
106         _centre.flush ();
107         _lfe.flush ();
108         _ls.flush ();
109         _rs.flush ();
110 }
111
112 void
113 UpmixerA::make_audio_mapping_default (AudioMapping& mapping) const
114 {
115         /* Just map the first two input channels to our L/R */
116         mapping.make_zero ();
117         for (int i = 0; i < min (2, mapping.input_channels()); ++i) {
118                 mapping.set (i, i, 1);
119         }
120 }
121
122 vector<string>
123 UpmixerA::input_names () const
124 {
125         vector<string> n;
126         n.push_back (_("Upmix L"));
127         n.push_back (_("Upmix R"));
128         return n;
129 }