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