Add a couple of audio properties to the multi-select list.
[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 ContentWidget<AudioContent, wxSpinCtrl> (
54                 this,
55                 new wxSpinCtrl (this),
56                 AudioContentProperty::AUDIO_GAIN,
57                 boost::mem_fn (&AudioContent::audio_gain),
58                 boost::mem_fn (&AudioContent::set_audio_gain)
59                 );
60         _gain->add (grid, wxGBPosition (r, 1));
61         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
62         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
63         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
64         ++r;
65
66         add_label_to_grid_bag_sizer (grid, this, _("Audio Delay"), false, wxGBPosition (r, 0));
67         _delay = new ContentWidget<AudioContent, wxSpinCtrl> (
68                 this,
69                 new wxSpinCtrl (this),
70                 AudioContentProperty::AUDIO_DELAY,
71                 boost::mem_fn (&AudioContent::audio_delay),
72                 boost::mem_fn (&AudioContent::set_audio_delay)
73                 );
74         _delay->add (grid, wxGBPosition (r,1 ));
75         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
76         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
77         ++r;
78
79         add_label_to_grid_bag_sizer (grid, this, _("Audio Stream"), true, wxGBPosition (r, 0));
80         _stream = new wxChoice (this, wxID_ANY);
81         grid->Add (_stream, wxGBPosition (r, 1));
82         ++r;
83         
84         _description = new wxStaticText (this, wxID_ANY, wxT (""));
85         grid->Add (_description, wxGBPosition (r, 0));
86         ++r;
87         
88         _mapping = new AudioMappingView (this);
89         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
90
91         _gain->wrapped()->SetRange (-60, 60);
92         _delay->wrapped()->SetRange (-1000, 1000);
93
94         _stream->Bind                (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&AudioPanel::stream_changed, this));
95         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
96         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
97
98         _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
99 }
100
101
102 void
103 AudioPanel::film_changed (Film::Property property)
104 {
105         switch (property) {
106         case Film::AUDIO_CHANNELS:
107                 _mapping->set_channels (_editor->film()->audio_channels ());
108                 _sizer->Layout ();
109                 break;
110         default:
111                 break;
112         }
113 }
114
115 void
116 AudioPanel::film_content_changed (int property)
117 {
118         AudioContentList ac = _editor->selected_audio_content ();
119         shared_ptr<AudioContent> acs;
120         shared_ptr<FFmpegContent> fcs;
121         if (ac.size() == 1) {
122                 acs = ac.front ();
123                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
124         }
125         
126         if (_audio_dialog && acs) {
127                 _audio_dialog->set_content (acs);
128         }
129         
130         if (property == AudioContentProperty::AUDIO_MAPPING) {
131                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
132                 _sizer->Layout ();
133         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
134                 setup_stream_description ();
135                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
136         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
137                 _stream->Clear ();
138                 if (fcs) {
139                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
140                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
141                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx (lexical_cast<string> ((*i)->id))));
142                         }
143                         
144                         if (fcs->audio_stream()) {
145                                 checked_set (_stream, lexical_cast<string> (fcs->audio_stream()->id));
146                                 setup_stream_description ();
147                         }
148                 }
149         }
150 }
151
152 void
153 AudioPanel::gain_calculate_button_clicked ()
154 {
155         GainCalculatorDialog* d = new GainCalculatorDialog (this);
156         d->ShowModal ();
157
158         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
159                 d->Destroy ();
160                 return;
161         }
162         
163         _gain->wrapped()->SetValue (
164                 Config::instance()->sound_processor()->db_for_fader_change (
165                         d->wanted_fader (),
166                         d->actual_fader ()
167                         )
168                 );
169
170         /* This appears to be necessary, as the change is not signalled,
171            I think.
172         */
173         _gain->update_from_model ();
174         
175         d->Destroy ();
176 }
177
178 void
179 AudioPanel::show_clicked ()
180 {
181         if (_audio_dialog) {
182                 _audio_dialog->Destroy ();
183                 _audio_dialog = 0;
184         }
185
186         AudioContentList ac = _editor->selected_audio_content ();
187         if (ac.size() != 1) {
188                 return;
189         }
190         
191         _audio_dialog = new AudioDialog (this);
192         _audio_dialog->Show ();
193         _audio_dialog->set_content (ac.front ());
194 }
195
196 void
197 AudioPanel::stream_changed ()
198 {
199         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
200         if (fc.size() != 1) {
201                 return;
202         }
203
204         shared_ptr<FFmpegContent> fcs = fc.front ();
205         
206         if (_stream->GetSelection() == -1) {
207                 return;
208         }
209         
210         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
211         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
212         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
213         while (i != a.end() && lexical_cast<string> ((*i)->id) != s) {
214                 ++i;
215         }
216
217         if (i != a.end ()) {
218                 fcs->set_audio_stream (*i);
219         }
220
221         setup_stream_description ();
222 }
223
224 void
225 AudioPanel::setup_stream_description ()
226 {
227         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
228         if (fc.size() != 1) {
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->content_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         _gain->set_content (sel);
263         _delay->set_content (sel);
264
265         _show->Enable (sel.size() == 1);
266         _stream->Enable (sel.size() == 1);
267         _mapping->Enable (sel.size() == 1);
268
269         film_content_changed (AudioContentProperty::AUDIO_MAPPING);
270         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
271         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
272 }