Merge master.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2014 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 <boost/lexical_cast.hpp>
21 #include <wx/spinctrl.h>
22 #include "lib/config.h"
23 #include "lib/ffmpeg_content.h"
24 #include "lib/ffmpeg_audio_stream.h"
25 #include "lib/audio_processor.h"
26 #include "lib/cinema_sound_processor.h"
27 #include "audio_dialog.h"
28 #include "audio_panel.h"
29 #include "audio_mapping_view.h"
30 #include "wx_util.h"
31 #include "gain_calculator_dialog.h"
32 #include "content_panel.h"
33
34 using std::vector;
35 using std::cout;
36 using std::string;
37 using std::list;
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         add_label_to_grid_bag_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
85         _stream = new wxChoice (this, wxID_ANY);
86         grid->Add (_stream, wxGBPosition (r, 1), wxGBSpan (1, 3), wxEXPAND);
87         ++r;
88
89         add_label_to_grid_bag_sizer (grid, this, _("Process with"), true, wxGBPosition (r, 0));
90         _processor = new wxChoice (this, wxID_ANY);
91         setup_processors ();
92         grid->Add (_processor, wxGBPosition (r, 1), wxGBSpan (1, 3), wxEXPAND);
93         ++r;
94         
95         _mapping = new AudioMappingView (this);
96         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
97         ++r;
98
99         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
100         _sizer->Add (_description, 0, wxALL, 12);
101         wxFont font = _description->GetFont();
102         font.SetStyle (wxFONTSTYLE_ITALIC);
103         font.SetPointSize (font.GetPointSize() - 1);
104         _description->SetFont (font);
105         ++r;
106
107         _gain->wrapped()->SetRange (-60, 60);
108         _gain->wrapped()->SetDigits (1);
109         _gain->wrapped()->SetIncrement (0.5);
110         _delay->wrapped()->SetRange (-1000, 1000);
111
112         _stream->Bind                (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&AudioPanel::stream_changed, this));
113         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::show_clicked, this));
114         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
115         _processor->Bind             (wxEVT_COMMAND_CHOICE_SELECTED, boost::bind (&AudioPanel::processor_changed, this));
116
117         _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
118 }
119
120
121 void
122 AudioPanel::film_changed (Film::Property property)
123 {
124         switch (property) {
125         case Film::AUDIO_CHANNELS:
126                 _mapping->set_channels (_parent->film()->audio_channels ());
127                 _sizer->Layout ();
128                 break;
129         case Film::VIDEO_FRAME_RATE:
130                 setup_description ();
131                 break;
132         default:
133                 break;
134         }
135 }
136
137 void
138 AudioPanel::film_content_changed (int property)
139 {
140         AudioContentList ac = _parent->selected_audio ();
141         shared_ptr<AudioContent> acs;
142         shared_ptr<FFmpegContent> fcs;
143         if (ac.size() == 1) {
144                 acs = ac.front ();
145                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
146         }
147         
148         if (property == AudioContentProperty::AUDIO_MAPPING) {
149                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
150                 _sizer->Layout ();
151         } else if (property == AudioContentProperty::AUDIO_FRAME_RATE) {
152                 setup_description ();
153         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
154                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
155                 _sizer->Layout ();
156         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
157                 _stream->Clear ();
158                 if (fcs) {
159                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
160                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
161                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
162                         }
163                         
164                         if (fcs->audio_stream()) {
165                                 checked_set (_stream, fcs->audio_stream()->identifier ());
166                         }
167                 }
168         } else if (property == AudioContentProperty::AUDIO_PROCESSOR) {
169                 if (acs) {
170                         checked_set (_processor, acs->audio_processor() ? acs->audio_processor()->id() : N_("none"));
171                 } else {
172                         checked_set (_processor, N_("none"));
173                 }
174         }
175 }
176
177 void
178 AudioPanel::gain_calculate_button_clicked ()
179 {
180         GainCalculatorDialog* d = new GainCalculatorDialog (this);
181         d->ShowModal ();
182
183         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
184                 d->Destroy ();
185                 return;
186         }
187         
188         _gain->wrapped()->SetValue (
189                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
190                         d->wanted_fader (),
191                         d->actual_fader ()
192                         )
193                 );
194
195         /* This appears to be necessary, as the change is not signalled,
196            I think.
197         */
198         _gain->view_changed ();
199         
200         d->Destroy ();
201 }
202
203 void
204 AudioPanel::show_clicked ()
205 {
206         if (_audio_dialog) {
207                 _audio_dialog->Destroy ();
208                 _audio_dialog = 0;
209         }
210
211         AudioContentList ac = _parent->selected_audio ();
212         if (ac.size() != 1) {
213                 return;
214         }
215         
216         _audio_dialog = new AudioDialog (this);
217         _audio_dialog->Show ();
218         _audio_dialog->set_content (ac.front ());
219 }
220
221 void
222 AudioPanel::stream_changed ()
223 {
224         FFmpegContentList fc = _parent->selected_ffmpeg ();
225         if (fc.size() != 1) {
226                 return;
227         }
228
229         shared_ptr<FFmpegContent> fcs = fc.front ();
230         
231         if (_stream->GetSelection() == -1) {
232                 return;
233         }
234         
235         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
236         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
237         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
238         while (i != a.end() && (*i)->identifier () != s) {
239                 ++i;
240         }
241
242         if (i != a.end ()) {
243                 fcs->set_audio_stream (*i);
244         }
245 }
246
247 void
248 AudioPanel::processor_changed ()
249 {
250         string const s = string_client_data (_processor->GetClientObject (_processor->GetSelection ()));
251         AudioProcessor const * p = 0;
252         if (s != wx_to_std (N_("none"))) {
253                 p = AudioProcessor::from_id (s);
254         }
255                 
256         AudioContentList c = _parent->selected_audio ();
257         for (AudioContentList::const_iterator i = c.begin(); i != c.end(); ++i) {
258                 (*i)->set_audio_processor (p);
259         }
260 }
261
262 void
263 AudioPanel::setup_description ()
264 {
265         AudioContentList ac = _parent->selected_audio ();
266         if (ac.size () != 1) {
267                 _description->SetLabel ("");
268                 return;
269         }
270
271         shared_ptr<AudioContent> acs = ac.front ();
272         if (acs->audio_frame_rate() != acs->resampled_audio_frame_rate ()) {
273                 _description->SetLabel (wxString::Format (
274                                                 _("Audio will be resampled from %.3fkHz to %.3fkHz."),
275                                                 acs->audio_frame_rate() / 1000.0,
276                                                 acs->resampled_audio_frame_rate() / 1000.0
277                                                 ));
278         } else {
279                 _description->SetLabel (_("Audio will not be resampled."));
280         }
281 }
282
283 void
284 AudioPanel::mapping_changed (AudioMapping m)
285 {
286         AudioContentList c = _parent->selected_audio ();
287         if (c.size() == 1) {
288                 c.front()->set_audio_mapping (m);
289         }
290 }
291
292 void
293 AudioPanel::content_selection_changed ()
294 {
295         AudioContentList sel = _parent->selected_audio ();
296
297         if (_audio_dialog && sel.size() == 1) {
298                 _audio_dialog->set_content (sel.front ());
299         }
300         
301         _gain->set_content (sel);
302         _delay->set_content (sel);
303
304         _gain_calculate_button->Enable (sel.size() == 1);
305         _show->Enable (sel.size() == 1);
306         _stream->Enable (sel.size() == 1);
307         _processor->Enable (!sel.empty());
308         _mapping->Enable (sel.size() == 1);
309
310         setup_processors ();
311
312         film_content_changed (AudioContentProperty::AUDIO_MAPPING);
313         film_content_changed (AudioContentProperty::AUDIO_PROCESSOR);
314         film_content_changed (AudioContentProperty::AUDIO_FRAME_RATE);
315         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
316         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
317 }
318
319 void
320 AudioPanel::setup_processors ()
321 {
322         AudioContentList sel = _parent->selected_audio ();
323
324         _processor->Clear ();
325         list<AudioProcessor const *> ap = AudioProcessor::all ();
326         _processor->Append (_("None"), new wxStringClientData (N_("none")));
327         for (list<AudioProcessor const *>::const_iterator i = ap.begin(); i != ap.end(); ++i) {
328
329                 AudioContentList::const_iterator j = sel.begin();
330                 while (j != sel.end() && (*i)->in_channels().includes ((*j)->audio_channels ())) {
331                         ++j;
332                 }
333
334                 if (j == sel.end ()) {
335                         _processor->Append (std_to_wx ((*i)->name ()), new wxStringClientData (std_to_wx ((*i)->id ())));
336                 }
337         }
338 }