Credit Gerald Maruccia for the upmixer algorithm.
[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.02, 150.0 / sampling_rate, 1900.0 / sampling_rate)
35         , _lfe (0.02, 20.0 / sampling_rate, 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 (GĂ©rald Maruccia)");
46 }
47
48
49 string
50 UpmixerA::id () const
51 {
52         return N_("stereo-5.1-upmix-a");
53 }
54
55 ChannelCount
56 UpmixerA::in_channels () const
57 {
58         return ChannelCount (2);
59 }
60
61 int
62 UpmixerA::out_channels () const
63 {
64         return 6;
65 }
66
67 shared_ptr<AudioProcessor>
68 UpmixerA::clone (int sampling_rate) const
69 {
70         return shared_ptr<AudioProcessor> (new UpmixerA (sampling_rate));
71 }
72
73 shared_ptr<AudioBuffers>
74 UpmixerA::run (shared_ptr<const AudioBuffers> in)
75 {
76         /* Input L and R */
77         shared_ptr<AudioBuffers> in_L = in->channel (0);
78         shared_ptr<AudioBuffers> in_R = in->channel (1);
79
80         /* Mix of L and R */
81         shared_ptr<AudioBuffers> in_LR = in_L->clone ();
82         in_LR->accumulate_frames (in_R.get(), 0, 0, in_R->frames ());
83         in_LR->apply_gain (0.5);
84
85         /* Run filters */
86         shared_ptr<AudioBuffers> L = _left.run (in_L);
87         shared_ptr<AudioBuffers> R = _right.run (in_R);
88         shared_ptr<AudioBuffers> C = _centre.run (in_LR);
89         shared_ptr<AudioBuffers> Lfe = _lfe.run (in_LR);
90         shared_ptr<AudioBuffers> Ls = _ls.run (in_L);
91         shared_ptr<AudioBuffers> Rs = _rs.run (in_R);
92
93         shared_ptr<AudioBuffers> out (new AudioBuffers (6, in->frames ()));
94         out->copy_channel_from (L.get(), 0, 0);
95         out->copy_channel_from (R.get(), 0, 1);
96         out->copy_channel_from (C.get(), 0, 2);
97         out->copy_channel_from (Lfe.get(), 0, 3);
98         out->copy_channel_from (Ls.get(), 0, 4);
99         out->copy_channel_from (Rs.get(), 0, 5);
100         return out;
101 }
102
103 void
104 UpmixerA::flush ()
105 {
106         _left.flush ();
107         _right.flush ();
108         _centre.flush ();
109         _lfe.flush ();
110         _ls.flush ();
111         _rs.flush ();
112 }
113
114 void
115 UpmixerA::make_audio_mapping_default (AudioMapping& mapping) const
116 {
117         /* Just map the first two input channels to our L/R */
118         mapping.make_zero ();
119         for (int i = 0; i < min (2, mapping.input_channels()); ++i) {
120                 mapping.set (i, i, 1);
121         }
122 }
123
124 vector<string>
125 UpmixerA::input_names () const
126 {
127         vector<string> n;
128         n.push_back (_("Upmix L"));
129         n.push_back (_("Upmix R"));
130         return n;
131 }