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