Basic multiple selection for cropping.
[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         wxFlexGridSizer* grid = new wxFlexGridSizer (3, DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
44         _sizer->Add (grid, 0, wxALL, 8);
45
46         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
47         grid->Add (_show, 1);
48         grid->AddSpacer (0);
49         grid->AddSpacer (0);
50
51         add_label_to_sizer (grid, this, _("Audio Gain"), true);
52         {
53                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
54                 _gain = new wxSpinCtrl (this);
55                 s->Add (_gain, 1);
56                 add_label_to_sizer (s, this, _("dB"), false);
57                 grid->Add (s, 1);
58         }
59         
60         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
61         grid->Add (_gain_calculate_button);
62
63         add_label_to_sizer (grid, this, _("Audio Delay"), false);
64         {
65                 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
66                 _delay = new wxSpinCtrl (this);
67                 s->Add (_delay, 1);
68                 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
69                 add_label_to_sizer (s, this, _("ms"), false);
70                 grid->Add (s);
71         }
72
73         grid->AddSpacer (0);
74
75         add_label_to_sizer (grid, this, _("Audio Stream"), true);
76         _stream = new wxChoice (this, wxID_ANY);
77         grid->Add (_stream, 1, wxEXPAND);
78         _description = new wxStaticText (this, wxID_ANY, wxT (""));
79         grid->AddSpacer (0);
80         
81         grid->Add (_description, 1, wxALIGN_CENTER_VERTICAL | wxLEFT, 8);
82         grid->AddSpacer (0);
83         grid->AddSpacer (0);
84         
85         _mapping = new AudioMappingView (this);
86         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
87
88         _gain->SetRange (-60, 60);
89         _delay->SetRange (-1000, 1000);
90
91         _delay->Bind                 (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&AudioPanel::delay_changed, this));
92         _stream->Bind                (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&AudioPanel::stream_changed, this));
93         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
94         _gain->Bind                  (wxEVT_COMMAND_SPINCTRL_UPDATED, boost::bind (&AudioPanel::gain_changed, this));
95         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
96
97         _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
98 }
99
100
101 void
102 AudioPanel::film_changed (Film::Property property)
103 {
104         switch (property) {
105         case Film::AUDIO_CHANNELS:
106                 _mapping->set_channels (_editor->film()->audio_channels ());
107                 _sizer->Layout ();
108                 break;
109         default:
110                 break;
111         }
112 }
113
114 void
115 AudioPanel::film_content_changed (int property)
116 {
117         AudioContentList ac = _editor->selected_audio_content ();
118         shared_ptr<AudioContent> acs;
119         shared_ptr<FFmpegContent> fcs;
120         if (ac.size() == 1) {
121                 acs = ac.front ();
122                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
123         }
124         
125         if (_audio_dialog && acs) {
126                 _audio_dialog->set_content (acs);
127         }
128         
129         if (property == AudioContentProperty::AUDIO_GAIN) {
130                 checked_set (_gain, acs ? acs->audio_gain() : 0);
131         } else if (property == AudioContentProperty::AUDIO_DELAY) {
132                 checked_set (_delay, acs ? acs->audio_delay() : 0);
133         } else if (property == AudioContentProperty::AUDIO_MAPPING) {
134                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
135                 _sizer->Layout ();
136         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
137                 setup_stream_description ();
138                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
139         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
140                 _stream->Clear ();
141                 if (fcs) {
142                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
143                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
144                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
145                         }
146                         
147                         if (fcs->audio_stream()) {
148                                 checked_set (_stream, lexical_cast<string> (fcs->audio_stream()->id));
149                                 setup_stream_description ();
150                         }
151                 }
152         }
153 }
154
155 void
156 AudioPanel::gain_changed ()
157 {
158         AudioContentList ac = _editor->selected_audio_content ();
159         if (ac.size() != 1) {
160                 return;
161         }
162
163         ac.front()->set_audio_gain (_gain->GetValue ());
164 }
165
166 void
167 AudioPanel::delay_changed ()
168 {
169         AudioContentList ac = _editor->selected_audio_content ();
170         if (ac.size() != 1) {
171                 return;
172         }
173
174         ac.front()->set_audio_delay (_delay->GetValue ());
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->SetValue (
189                 Config::instance()->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_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 = _editor->selected_audio_content ();
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 = _editor->selected_ffmpeg_content ();
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() && lexical_cast<string> ((*i)->id) != s) {
239                 ++i;
240         }
241
242         if (i != a.end ()) {
243                 fcs->set_audio_stream (*i);
244         }
245
246         setup_stream_description ();
247 }
248
249 void
250 AudioPanel::setup_stream_description ()
251 {
252         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
253         if (fc.size() != 1) {
254                 return;
255         }
256
257         shared_ptr<FFmpegContent> fcs = fc.front ();
258
259         if (!fcs->audio_stream ()) {
260                 _description->SetLabel (wxT (""));
261         } else {
262                 wxString s;
263                 if (fcs->audio_channels() == 1) {
264                         s << _("1 channel");
265                 } else {
266                         s << fcs->audio_channels() << wxT (" ") << _("channels");
267                 }
268                 s << wxT (", ") << fcs->content_audio_frame_rate() << _("Hz");
269                 _description->SetLabel (s);
270         }
271 }
272
273 void
274 AudioPanel::mapping_changed (AudioMapping m)
275 {
276         AudioContentList c = _editor->selected_audio_content ();
277         if (c.size() == 1) {
278                 c.front()->set_audio_mapping (m);
279         }
280 }
281
282 void
283 AudioPanel::content_selection_changed ()
284 {
285
286 }