Various work on audio mapping.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-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 "lib/config.h"
21 #include "lib/ffmpeg_content.h"
22 #include "lib/cinema_sound_processor.h"
23 #include "audio_dialog.h"
24 #include "audio_panel.h"
25 #include "audio_mapping_view.h"
26 #include "wx_util.h"
27 #include "gain_calculator_dialog.h"
28 #include "content_panel.h"
29 #include <wx/spinctrl.h>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/foreach.hpp>
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using std::list;
37 using std::pair;
38 using boost::dynamic_pointer_cast;
39 using boost::lexical_cast;
40 using boost::shared_ptr;
41
42 AudioPanel::AudioPanel (ContentPanel* p)
43         : ContentSubPanel (p, _("Audio"))
44         , _audio_dialog (0)
45 {
46         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         int r = 0;
50
51         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
52         grid->Add (_show, wxGBPosition (r, 0));
53         ++r;
54
55         add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
56         _gain = new ContentSpinCtrlDouble<AudioContent> (
57                 this,
58                 new wxSpinCtrlDouble (this),
59                 AudioContentProperty::AUDIO_GAIN,
60                 boost::mem_fn (&AudioContent::audio_gain),
61                 boost::mem_fn (&AudioContent::set_audio_gain)
62                 );
63         
64         _gain->add (grid, wxGBPosition (r, 1));
65         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
66         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
67         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
68         ++r;
69
70         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
71         _delay = new ContentSpinCtrl<AudioContent> (
72                 this,
73                 new wxSpinCtrl (this),
74                 AudioContentProperty::AUDIO_DELAY,
75                 boost::mem_fn (&AudioContent::audio_delay),
76                 boost::mem_fn (&AudioContent::set_audio_delay)
77                 );
78         
79         _delay->add (grid, wxGBPosition (r, 1));
80         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
81         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
82         ++r;
83
84         _mapping = new AudioMappingView (this);
85         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
86         ++r;
87
88         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
89         _sizer->Add (_description, 0, wxALL, 12);
90         wxFont font = _description->GetFont();
91         font.SetStyle (wxFONTSTYLE_ITALIC);
92         font.SetPointSize (font.GetPointSize() - 1);
93         _description->SetFont (font);
94         ++r;
95
96         _gain->wrapped()->SetRange (-60, 60);
97         _gain->wrapped()->SetDigits (1);
98         _gain->wrapped()->SetIncrement (0.5);
99         _delay->wrapped()->SetRange (-1000, 1000);
100
101         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::show_clicked, this));
102         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
103
104         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
105 }
106
107
108 void
109 AudioPanel::film_changed (Film::Property property)
110 {
111         switch (property) {
112         case Film::AUDIO_CHANNELS:
113         case Film::AUDIO_PROCESSOR:
114                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
115                 break;
116         case Film::VIDEO_FRAME_RATE:
117                 setup_description ();
118                 break;
119         default:
120                 break;
121         }
122 }
123
124 void
125 AudioPanel::film_content_changed (int property)
126 {
127         if (property == AudioContentProperty::AUDIO_STREAMS) {
128                 AudioContentList ac = _parent->selected_audio ();
129                 if (ac.size() == 1) {
130                         _mapping->set (ac.front()->audio_mapping());
131                         _mapping->set_input_channels (ac.front()->audio_channel_names ());
132                 } else {
133                         _mapping->set (AudioMapping ());
134                 }
135                 setup_description ();
136         }
137 }
138
139 void
140 AudioPanel::gain_calculate_button_clicked ()
141 {
142         GainCalculatorDialog* d = new GainCalculatorDialog (this);
143         int const r = d->ShowModal ();
144
145         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
146                 d->Destroy ();
147                 return;
148         }
149         
150         _gain->wrapped()->SetValue (
151                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
152                         d->wanted_fader (),
153                         d->actual_fader ()
154                         )
155                 );
156
157         /* This appears to be necessary, as the change is not signalled,
158            I think.
159         */
160         _gain->view_changed ();
161         
162         d->Destroy ();
163 }
164
165 void
166 AudioPanel::show_clicked ()
167 {
168         if (_audio_dialog) {
169                 _audio_dialog->Destroy ();
170                 _audio_dialog = 0;
171         }
172
173         AudioContentList ac = _parent->selected_audio ();
174         if (ac.size() != 1) {
175                 return;
176         }
177         
178         _audio_dialog = new AudioDialog (this, _parent->film ());
179         _audio_dialog->Show ();
180         _audio_dialog->set_content (ac.front ());
181 }
182
183 void
184 AudioPanel::setup_description ()
185 {
186         AudioContentList ac = _parent->selected_audio ();
187         if (ac.size () != 1) {
188                 checked_set (_description, wxT (""));
189                 return;
190         }
191
192         checked_set (_description, ac.front()->processing_description ());
193 }
194
195 void
196 AudioPanel::mapping_changed (AudioMapping m)
197 {
198         AudioContentList c = _parent->selected_audio ();
199         if (c.size() == 1) {
200                 c.front()->set_audio_mapping (m);
201         }
202 }
203
204 void
205 AudioPanel::content_selection_changed ()
206 {
207         AudioContentList sel = _parent->selected_audio ();
208
209         if (_audio_dialog && sel.size() == 1) {
210                 _audio_dialog->set_content (sel.front ());
211         }
212         
213         _gain->set_content (sel);
214         _delay->set_content (sel);
215
216         _gain_calculate_button->Enable (sel.size() == 1);
217         _show->Enable (sel.size() == 1);
218         _mapping->Enable (sel.size() == 1);
219
220         film_content_changed (AudioContentProperty::AUDIO_STREAMS);
221 }