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