Merge master.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2013 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/sound_processor.h"
24 #include "lib/ffmpeg_content.h"
25 #include "lib/ffmpeg_audio_stream.h"
26 #include "audio_dialog.h"
27 #include "audio_panel.h"
28 #include "audio_mapping_view.h"
29 #include "wx_util.h"
30 #include "gain_calculator_dialog.h"
31 #include "film_editor.h"
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using boost::dynamic_pointer_cast;
37 using boost::lexical_cast;
38 using boost::shared_ptr;
39
40 AudioPanel::AudioPanel (FilmEditor* e)
41         : FilmEditorPanel (e, _("Audio"))
42         , _audio_dialog (0)
43 {
44         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45         _sizer->Add (grid, 0, wxALL, 8);
46
47         int r = 0;
48
49         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
50         grid->Add (_show, wxGBPosition (r, 0));
51         ++r;
52
53         add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
54         _gain = new ContentSpinCtrlDouble<AudioContent> (
55                 this,
56                 new wxSpinCtrlDouble (this),
57                 AudioContentProperty::AUDIO_GAIN,
58                 boost::mem_fn (&AudioContent::audio_gain),
59                 boost::mem_fn (&AudioContent::set_audio_gain)
60                 );
61         
62         _gain->add (grid, wxGBPosition (r, 1));
63         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
64         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
65         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
66         ++r;
67
68         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
69         _delay = new ContentSpinCtrl<AudioContent> (
70                 this,
71                 new wxSpinCtrl (this),
72                 AudioContentProperty::AUDIO_DELAY,
73                 boost::mem_fn (&AudioContent::audio_delay),
74                 boost::mem_fn (&AudioContent::set_audio_delay)
75                 );
76         
77         _delay->add (grid, wxGBPosition (r, 1));
78         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
79         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
80         ++r;
81
82         add_label_to_grid_bag_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
83         _stream = new wxChoice (this, wxID_ANY);
84         grid->Add (_stream, wxGBPosition (r, 1));
85         _description = add_label_to_grid_bag_sizer (grid, this, "", false, wxGBPosition (r, 3));
86         ++r;
87         
88         _mapping = new AudioMappingView (this);
89         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
90
91         _gain->wrapped()->SetRange (-60, 60);
92         _gain->wrapped()->SetDigits (1);
93         _gain->wrapped()->SetIncrement (0.5);
94         _delay->wrapped()->SetRange (-1000, 1000);
95
96         _stream->Bind                (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&AudioPanel::stream_changed, this));
97         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
98         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
99
100         _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
101 }
102
103
104 void
105 AudioPanel::film_changed (Film::Property property)
106 {
107         switch (property) {
108         case Film::AUDIO_CHANNELS:
109                 _mapping->set_channels (_editor->film()->audio_channels ());
110                 _sizer->Layout ();
111                 break;
112         default:
113                 break;
114         }
115 }
116
117 void
118 AudioPanel::film_content_changed (int property)
119 {
120         AudioContentList ac = _editor->selected_audio_content ();
121         shared_ptr<AudioContent> acs;
122         shared_ptr<FFmpegContent> fcs;
123         if (ac.size() == 1) {
124                 acs = ac.front ();
125                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
126         }
127         
128         if (property == AudioContentProperty::AUDIO_MAPPING) {
129                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
130                 _sizer->Layout ();
131         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
132                 setup_stream_description ();
133                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
134                 _sizer->Layout ();
135         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
136                 _stream->Clear ();
137                 if (fcs) {
138                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
139                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
140                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
141                         }
142                         
143                         if (fcs->audio_stream()) {
144                                 checked_set (_stream, fcs->audio_stream()->identifier ());
145                                 setup_stream_description ();
146                         }
147                 }
148         }
149 }
150
151 void
152 AudioPanel::gain_calculate_button_clicked ()
153 {
154         GainCalculatorDialog* d = new GainCalculatorDialog (this);
155         d->ShowModal ();
156
157         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
158                 d->Destroy ();
159                 return;
160         }
161         
162         _gain->wrapped()->SetValue (
163                 Config::instance()->sound_processor()->db_for_fader_change (
164                         d->wanted_fader (),
165                         d->actual_fader ()
166                         )
167                 );
168
169         /* This appears to be necessary, as the change is not signalled,
170            I think.
171         */
172         _gain->view_changed ();
173         
174         d->Destroy ();
175 }
176
177 void
178 AudioPanel::show_clicked ()
179 {
180         if (_audio_dialog) {
181                 _audio_dialog->Destroy ();
182                 _audio_dialog = 0;
183         }
184
185         AudioContentList ac = _editor->selected_audio_content ();
186         if (ac.size() != 1) {
187                 return;
188         }
189         
190         _audio_dialog = new AudioDialog (this);
191         _audio_dialog->Show ();
192         _audio_dialog->set_content (ac.front ());
193 }
194
195 void
196 AudioPanel::stream_changed ()
197 {
198         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
199         if (fc.size() != 1) {
200                 return;
201         }
202
203         shared_ptr<FFmpegContent> fcs = fc.front ();
204         
205         if (_stream->GetSelection() == -1) {
206                 return;
207         }
208         
209         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
210         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
211         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
212         while (i != a.end() && (*i)->identifier () != s) {
213                 ++i;
214         }
215
216         if (i != a.end ()) {
217                 fcs->set_audio_stream (*i);
218         }
219
220         setup_stream_description ();
221 }
222
223 void
224 AudioPanel::setup_stream_description ()
225 {
226         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
227         if (fc.size() != 1) {
228                 _description->SetLabel ("");
229                 return;
230         }
231
232         shared_ptr<FFmpegContent> fcs = fc.front ();
233
234         if (!fcs->audio_stream ()) {
235                 _description->SetLabel (wxT (""));
236         } else {
237                 wxString s;
238                 if (fcs->audio_channels() == 1) {
239                         s << _("1 channel");
240                 } else {
241                         s << fcs->audio_channels() << wxT (" ") << _("channels");
242                 }
243                 s << wxT (", ") << fcs->audio_frame_rate() << _("Hz");
244                 _description->SetLabel (s);
245         }
246 }
247
248 void
249 AudioPanel::mapping_changed (AudioMapping m)
250 {
251         AudioContentList c = _editor->selected_audio_content ();
252         if (c.size() == 1) {
253                 c.front()->set_audio_mapping (m);
254         }
255 }
256
257 void
258 AudioPanel::content_selection_changed ()
259 {
260         AudioContentList sel = _editor->selected_audio_content ();
261
262         if (_audio_dialog && sel.size() == 1) {
263                 _audio_dialog->set_content (sel.front ());
264         }
265         
266         _gain->set_content (sel);
267         _delay->set_content (sel);
268
269         _show->Enable (sel.size() == 1);
270         _stream->Enable (sel.size() == 1);
271         _mapping->Enable (sel.size() == 1);
272
273         film_content_changed (AudioContentProperty::AUDIO_MAPPING);
274         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
275         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
276 }