Move FFmpegStream classes into their own source files.
[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 "lib/ffmpeg_audio_stream.h"
26 #include "audio_dialog.h"
27 #include "audio_panel.h"
28 #include "audio_mapping_view.h"
29 #include "wx_util.h"
30 #include "gain_calculator_dialog.h"
31 #include "film_editor.h"
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using boost::dynamic_pointer_cast;
37 using boost::lexical_cast;
38 using boost::shared_ptr;
39
40 AudioPanel::AudioPanel (FilmEditor* e)
41         : FilmEditorPanel (e, _("Audio"))
42         , _audio_dialog (0)
43 {
44         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
45         _sizer->Add (grid, 0, wxALL, 8);
46
47         int r = 0;
48
49         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
50         grid->Add (_show, wxGBPosition (r, 0));
51         ++r;
52
53         add_label_to_grid_bag_sizer (grid, this, _("Audio Gain"), true, wxGBPosition (r, 0));
54         _gain = new ContentSpinCtrl<AudioContent> (
55                 this,
56                 new wxSpinCtrl (this),
57                 AudioContentProperty::AUDIO_GAIN,
58                 boost::mem_fn (&AudioContent::audio_gain),
59                 boost::mem_fn (&AudioContent::set_audio_gain)
60                 );
61         
62         _gain->add (grid, wxGBPosition (r, 1));
63         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
64         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
65         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
66         ++r;
67
68         add_label_to_grid_bag_sizer (grid, this, _("Audio Delay"), true, wxGBPosition (r, 0));
69         _delay = new ContentSpinCtrl<AudioContent> (
70                 this,
71                 new wxSpinCtrl (this),
72                 AudioContentProperty::AUDIO_DELAY,
73                 boost::mem_fn (&AudioContent::audio_delay),
74                 boost::mem_fn (&AudioContent::set_audio_delay)
75                 );
76         
77         _delay->add (grid, wxGBPosition (r, 1));
78         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
79         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
80         ++r;
81
82         add_label_to_grid_bag_sizer (grid, this, _("Audio Stream"), true, wxGBPosition (r, 0));
83         _stream = new wxChoice (this, wxID_ANY);
84         grid->Add (_stream, wxGBPosition (r, 1));
85         _description = add_label_to_grid_bag_sizer (grid, this, "", false, wxGBPosition (r, 3));
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 (property == AudioContentProperty::AUDIO_MAPPING) {
127                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
128                 _sizer->Layout ();
129         } else if (property == FFmpegContentProperty::AUDIO_STREAM) {
130                 setup_stream_description ();
131                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
132                 _sizer->Layout ();
133         } else if (property == FFmpegContentProperty::AUDIO_STREAMS) {
134                 _stream->Clear ();
135                 if (fcs) {
136                         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
137                         for (vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin(); i != a.end(); ++i) {
138                                 _stream->Append (std_to_wx ((*i)->name), new wxStringClientData (std_to_wx ((*i)->identifier ())));
139                         }
140                         
141                         if (fcs->audio_stream()) {
142                                 checked_set (_stream, fcs->audio_stream()->identifier ());
143                                 setup_stream_description ();
144                         }
145                 }
146         }
147 }
148
149 void
150 AudioPanel::gain_calculate_button_clicked ()
151 {
152         GainCalculatorDialog* d = new GainCalculatorDialog (this);
153         d->ShowModal ();
154
155         if (d->wanted_fader() == 0 || d->actual_fader() == 0) {
156                 d->Destroy ();
157                 return;
158         }
159         
160         _gain->wrapped()->SetValue (
161                 Config::instance()->sound_processor()->db_for_fader_change (
162                         d->wanted_fader (),
163                         d->actual_fader ()
164                         )
165                 );
166
167         /* This appears to be necessary, as the change is not signalled,
168            I think.
169         */
170         _gain->view_changed ();
171         
172         d->Destroy ();
173 }
174
175 void
176 AudioPanel::show_clicked ()
177 {
178         if (_audio_dialog) {
179                 _audio_dialog->Destroy ();
180                 _audio_dialog = 0;
181         }
182
183         AudioContentList ac = _editor->selected_audio_content ();
184         if (ac.size() != 1) {
185                 return;
186         }
187         
188         _audio_dialog = new AudioDialog (this);
189         _audio_dialog->Show ();
190         _audio_dialog->set_content (ac.front ());
191 }
192
193 void
194 AudioPanel::stream_changed ()
195 {
196         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
197         if (fc.size() != 1) {
198                 return;
199         }
200
201         shared_ptr<FFmpegContent> fcs = fc.front ();
202         
203         if (_stream->GetSelection() == -1) {
204                 return;
205         }
206         
207         vector<shared_ptr<FFmpegAudioStream> > a = fcs->audio_streams ();
208         vector<shared_ptr<FFmpegAudioStream> >::iterator i = a.begin ();
209         string const s = string_client_data (_stream->GetClientObject (_stream->GetSelection ()));
210         while (i != a.end() && (*i)->identifier () != s) {
211                 ++i;
212         }
213
214         if (i != a.end ()) {
215                 fcs->set_audio_stream (*i);
216         }
217
218         setup_stream_description ();
219 }
220
221 void
222 AudioPanel::setup_stream_description ()
223 {
224         FFmpegContentList fc = _editor->selected_ffmpeg_content ();
225         if (fc.size() != 1) {
226                 _description->SetLabel ("");
227                 return;
228         }
229
230         shared_ptr<FFmpegContent> fcs = fc.front ();
231
232         if (!fcs->audio_stream ()) {
233                 _description->SetLabel (wxT (""));
234         } else {
235                 wxString s;
236                 if (fcs->audio_channels() == 1) {
237                         s << _("1 channel");
238                 } else {
239                         s << fcs->audio_channels() << wxT (" ") << _("channels");
240                 }
241                 s << wxT (", ") << fcs->audio_frame_rate() << _("Hz");
242                 _description->SetLabel (s);
243         }
244 }
245
246 void
247 AudioPanel::mapping_changed (AudioMapping m)
248 {
249         AudioContentList c = _editor->selected_audio_content ();
250         if (c.size() == 1) {
251                 c.front()->set_audio_mapping (m);
252         }
253 }
254
255 void
256 AudioPanel::content_selection_changed ()
257 {
258         AudioContentList sel = _editor->selected_audio_content ();
259
260         if (_audio_dialog && sel.size() == 1) {
261                 _audio_dialog->set_content (sel.front ());
262         }
263         
264         _gain->set_content (sel);
265         _delay->set_content (sel);
266
267         _show->Enable (sel.size() == 1);
268         _stream->Enable (sel.size() == 1);
269         _mapping->Enable (sel.size() == 1);
270
271         film_content_changed (AudioContentProperty::AUDIO_MAPPING);
272         film_content_changed (FFmpegContentProperty::AUDIO_STREAM);
273         film_content_changed (FFmpegContentProperty::AUDIO_STREAMS);
274 }