Bump version
[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, _("Gain"), true, wxGBPosition (r, 0));
53         _gain = new ContentSpinCtrlDouble<AudioContent> (
54                 this,
55                 new wxSpinCtrlDouble (this),
56                 AudioContentProperty::AUDIO_GAIN,
57                 boost::mem_fn (&AudioContent::audio_gain),
58                 boost::mem_fn (&AudioContent::set_audio_gain)
59                 );
60         
61         _gain->add (grid, wxGBPosition (r, 1));
62         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
63         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
64         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
65         ++r;
66
67         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
68         _delay = new ContentSpinCtrl<AudioContent> (
69                 this,
70                 new wxSpinCtrl (this),
71                 AudioContentProperty::AUDIO_DELAY,
72                 boost::mem_fn (&AudioContent::audio_delay),
73                 boost::mem_fn (&AudioContent::set_audio_delay)
74                 );
75         
76         _delay->add (grid, wxGBPosition (r, 1));
77         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
78         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
79         ++r;
80
81         add_label_to_grid_bag_sizer (grid, this, _("Stream"), true, wxGBPosition (r, 0));
82         _stream = new wxChoice (this, wxID_ANY);
83         grid->Add (_stream, wxGBPosition (r, 1));
84         _stream_description = add_label_to_grid_bag_sizer (grid, this, "", false, wxGBPosition (r, 3));
85         ++r;
86         
87         _mapping = new AudioMappingView (this);
88         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
89         ++r;
90
91         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
92         _sizer->Add (_description, 0, wxALL, 12);
93         wxFont font = _description->GetFont();
94         font.SetStyle (wxFONTSTYLE_ITALIC);
95         font.SetPointSize (font.GetPointSize() - 1);
96         _description->SetFont (font);
97         ++r;
98
99         _gain->wrapped()->SetRange (-60, 60);
100         _gain->wrapped()->SetDigits (1);
101         _gain->wrapped()->SetIncrement (0.5);
102         _delay->wrapped()->SetRange (-1000, 1000);
103
104         _stream->Bind                (wxEVT_COMMAND_CHOICE_SELECTED,  boost::bind (&AudioPanel::stream_changed, this));
105         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::show_clicked, this));
106         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
107
108         _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
109 }
110
111
112 void
113 AudioPanel::film_changed (Film::Property property)
114 {
115         switch (property) {
116         case Film::AUDIO_CHANNELS:
117                 _mapping->set_channels (_editor->film()->audio_channels ());
118                 _sizer->Layout ();
119                 break;
120         case Film::VIDEO_FRAME_RATE:
121                 setup_description ();
122                 break;
123         default:
124                 break;
125         }
126 }
127
128 void
129 AudioPanel::film_content_changed (int property)
130 {
131         AudioContentList ac = _editor->selected_audio_content ();
132         shared_ptr<AudioContent> acs;
133         shared_ptr<FFmpegContent> fcs;
134         if (ac.size() == 1) {
135                 acs = ac.front ();
136                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
137         }
138         
139         if (property == AudioContentProperty::AUDIO_MAPPING) {
140                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
141                 _sizer->Layout ();
142         } else if (property == AudioContentProperty::AUDIO_FRAME_RATE) {
143                 setup_description ();
144         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
145                 setup_stream_description ();
146                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
147                 _sizer->Layout ();
148         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
149                 _stream->Clear ();
150                 if (fcs) {
151                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
152                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
153                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
154                         }
155                         
156                         if (fcs->audio_stream()) {
157                                 checked_set (_stream, fcs->audio_stream()->identifier ());
158                                 setup_stream_description ();
159                         }
160                 }
161         }
162 }
163
164 void
165 AudioPanel::gain_calculate_button_clicked ()
166 {
167         GainCalculatorDialog* d = new GainCalculatorDialog (this);
168         d->ShowModal ();
169
170         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
171                 d->Destroy ();
172                 return;
173         }
174         
175         _gain->wrapped()->SetValue (
176                 Config::instance()->sound_processor()->db_for_fader_change (
177                         d->wanted_fader (),
178                         d->actual_fader ()
179                         )
180                 );
181
182         /* This appears to be necessary, as the change is not signalled,
183            I think.
184         */
185         _gain->view_changed ();
186         
187         d->Destroy ();
188 }
189
190 void
191 AudioPanel::show_clicked ()
192 {
193         if (_audio_dialog) {
194                 _audio_dialog->Destroy ();
195                 _audio_dialog = 0;
196         }
197
198         AudioContentList ac = _editor->selected_audio_content ();
199         if (ac.size() != 1) {
200                 return;
201         }
202         
203         _audio_dialog = new AudioDialog (this);
204         _audio_dialog->Show ();
205         _audio_dialog->set_content (ac.front ());
206 }
207
208 void
209 AudioPanel::stream_changed ()
210 {
211         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
212         if (fc.size() != 1) {
213                 return;
214         }
215
216         shared_ptr<FFmpegContent> fcs = fc.front ();
217         
218         if (_stream->GetSelection() == -1) {
219                 return;
220         }
221         
222         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
223         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
224         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
225         while (i != a.end() && (*i)->identifier () != s) {
226                 ++i;
227         }
228
229         if (i != a.end ()) {
230                 fcs->set_audio_stream (*i);
231         }
232
233         setup_stream_description ();
234 }
235
236 void
237 AudioPanel::setup_description ()
238 {
239         AudioContentList ac = _editor->selected_audio_content ();
240         if (ac.size () != 1) {
241                 _description->SetLabel ("");
242                 return;
243         }
244
245         shared_ptr<AudioContent> acs = ac.front ();
246         if (acs->content_audio_frame_rate() != acs->output_audio_frame_rate ()) {
247                 _description->SetLabel (wxString::Format (
248                                                 _("Audio will be resampled from %.3fkHz to %.3fkHz."),
249                                                 acs->content_audio_frame_rate() / 1000.0,
250                                                 acs->output_audio_frame_rate() / 1000.0
251                                                 ));
252         } else {
253                 _description->SetLabel (_("Audio will not be resampled."));
254         }
255 }
256
257 void
258 AudioPanel::setup_stream_description ()
259 {
260         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
261         if (fc.size() != 1) {
262                 _stream_description->SetLabel ("");
263                 return;
264         }
265
266         shared_ptr<FFmpegContent> fcs = fc.front ();
267
268         if (!fcs->audio_stream ()) {
269                 _stream_description->SetLabel (wxT (""));
270         } else {
271                 wxString s;
272                 if (fcs->audio_channels() == 1) {
273                         s << _("1 channel");
274                 } else {
275                         s << fcs->audio_channels() << wxT (" ") << _("channels");
276                 }
277                 s << wxT (", ") << fcs->content_audio_frame_rate() << _("Hz");
278                 _stream_description->SetLabel (s);
279         }
280 }
281
282 void
283 AudioPanel::mapping_changed (AudioMapping m)
284 {
285         AudioContentList c = _editor->selected_audio_content ();
286         if (c.size() == 1) {
287                 c.front()->set_audio_mapping (m);
288         }
289 }
290
291 void
292 AudioPanel::content_selection_changed ()
293 {
294         AudioContentList sel = _editor->selected_audio_content ();
295
296         if (_audio_dialog && sel.size() == 1) {
297                 _audio_dialog->set_content (sel.front ());
298         }
299         
300         _gain->set_content (sel);
301         _delay->set_content (sel);
302
303         _show->Enable (sel.size() == 1);
304         _stream->Enable (sel.size() == 1);
305         _mapping->Enable (sel.size() == 1);
306
307         film_content_changed (AudioContentProperty::AUDIO_MAPPING);
308         film_content_changed (AudioContentProperty::AUDIO_FRAME_RATE);
309         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
310         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
311 }