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