Re-add show audio button and analyse just that content when it is clicked.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2015 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 "lib/config.h"
21 #include "lib/ffmpeg_content.h"
22 #include "lib/cinema_sound_processor.h"
23 #include "audio_panel.h"
24 #include "audio_mapping_view.h"
25 #include "wx_util.h"
26 #include "gain_calculator_dialog.h"
27 #include "content_panel.h"
28 #include "audio_dialog.h"
29 #include <wx/spinctrl.h>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/foreach.hpp>
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using std::list;
37 using std::pair;
38 using boost::dynamic_pointer_cast;
39 using boost::lexical_cast;
40 using boost::shared_ptr;
41
42 AudioPanel::AudioPanel (ContentPanel* p)
43         : ContentSubPanel (p, _("Audio"))
44         , _audio_dialog (0)
45 {
46         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         int r = 0;
50
51         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
52         grid->Add (_show, wxGBPosition (r, 0));
53         ++r;
54
55         add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
56         _gain = new ContentSpinCtrlDouble<AudioContent> (
57                 this,
58                 new wxSpinCtrlDouble (this),
59                 AudioContentProperty::AUDIO_GAIN,
60                 boost::mem_fn (&AudioContent::audio_gain),
61                 boost::mem_fn (&AudioContent::set_audio_gain)
62                 );
63
64         _gain->add (grid, wxGBPosition (r, 1));
65         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
66         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
67         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
68         ++r;
69
70         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
71         _delay = new ContentSpinCtrl<AudioContent> (
72                 this,
73                 new wxSpinCtrl (this),
74                 AudioContentProperty::AUDIO_DELAY,
75                 boost::mem_fn (&AudioContent::audio_delay),
76                 boost::mem_fn (&AudioContent::set_audio_delay)
77                 );
78
79         _delay->add (grid, wxGBPosition (r, 1));
80         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
81         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
82         ++r;
83
84         _mapping = new AudioMappingView (this);
85         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
86         ++r;
87
88         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
89         _sizer->Add (_description, 0, wxALL, 12);
90         wxFont font = _description->GetFont();
91         font.SetStyle (wxFONTSTYLE_ITALIC);
92         font.SetPointSize (font.GetPointSize() - 1);
93         _description->SetFont (font);
94         ++r;
95
96         _gain->wrapped()->SetRange (-60, 60);
97         _gain->wrapped()->SetDigits (1);
98         _gain->wrapped()->SetIncrement (0.5);
99         _delay->wrapped()->SetRange (-1000, 1000);
100
101         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
102         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
103
104         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
105 }
106
107 AudioPanel::~AudioPanel ()
108 {
109         if (_audio_dialog) {
110                 _audio_dialog->Destroy ();
111                 _audio_dialog = 0;
112         }
113 }
114
115 void
116 AudioPanel::film_changed (Film::Property property)
117 {
118         switch (property) {
119         case Film::AUDIO_CHANNELS:
120         case Film::AUDIO_PROCESSOR:
121                 _mapping->set_output_channels (_parent->film()->audio_output_names ());
122                 break;
123         case Film::VIDEO_FRAME_RATE:
124                 setup_description ();
125                 break;
126         default:
127                 break;
128         }
129 }
130
131 void
132 AudioPanel::film_content_changed (int property)
133 {
134         if (property == AudioContentProperty::AUDIO_STREAMS) {
135                 AudioContentList ac = _parent->selected_audio ();
136                 if (ac.size() == 1) {
137                         _mapping->set (ac.front()->audio_mapping());
138                         _mapping->set_input_channels (ac.front()->audio_channel_names ());
139                 } else {
140                         _mapping->set (AudioMapping ());
141                 }
142                 setup_description ();
143                 _sizer->Layout ();
144         }
145 }
146
147 void
148 AudioPanel::gain_calculate_button_clicked ()
149 {
150         GainCalculatorDialog* d = new GainCalculatorDialog (this);
151         int const r = d->ShowModal ();
152
153         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
154                 d->Destroy ();
155                 return;
156         }
157
158         _gain->wrapped()->SetValue (
159                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
160                         d->wanted_fader (),
161                         d->actual_fader ()
162                         )
163                 );
164
165         /* This appears to be necessary, as the change is not signalled,
166            I think.
167         */
168         _gain->view_changed ();
169
170         d->Destroy ();
171 }
172
173 void
174 AudioPanel::setup_description ()
175 {
176         AudioContentList ac = _parent->selected_audio ();
177         if (ac.size () != 1) {
178                 checked_set (_description, wxT (""));
179                 return;
180         }
181
182         checked_set (_description, ac.front()->processing_description ());
183 }
184
185 void
186 AudioPanel::mapping_changed (AudioMapping m)
187 {
188         AudioContentList c = _parent->selected_audio ();
189         if (c.size() == 1) {
190                 c.front()->set_audio_mapping (m);
191         }
192 }
193
194 void
195 AudioPanel::content_selection_changed ()
196 {
197         AudioContentList sel = _parent->selected_audio ();
198
199         _gain->set_content (sel);
200         _delay->set_content (sel);
201
202         _gain_calculate_button->Enable (sel.size() == 1);
203         _mapping->Enable (sel.size() == 1);
204
205         film_content_changed (AudioContentProperty::AUDIO_STREAMS);
206 }
207
208 void
209 AudioPanel::show_clicked ()
210 {
211         if (_audio_dialog) {
212                 _audio_dialog->Destroy ();
213                 _audio_dialog = 0;
214         }
215
216         AudioContentList ac = _parent->selected_audio ();
217         if (ac.size() != 1) {
218                 return;
219         }
220
221         _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
222         _audio_dialog->Show ();
223 }