2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
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.
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.
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.
20 #include "audio_panel.h"
21 #include "audio_mapping_view.h"
23 #include "gain_calculator_dialog.h"
24 #include "content_panel.h"
25 #include "audio_dialog.h"
26 #include "lib/config.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/cinema_sound_processor.h"
29 #include "lib/job_manager.h"
30 #include "lib/dcp_content.h"
31 #include <wx/spinctrl.h>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/foreach.hpp>
41 using boost::dynamic_pointer_cast;
42 using boost::lexical_cast;
43 using boost::shared_ptr;
44 using boost::optional;
46 AudioPanel::AudioPanel (ContentPanel* p)
47 : ContentSubPanel (p, _("Audio"))
50 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
51 _sizer->Add (grid, 0, wxALL, 8);
55 _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
56 grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
60 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
61 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
63 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
64 s->Add (_peak, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
65 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
69 add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
70 _gain = new ContentSpinCtrlDouble<AudioContent> (
72 new wxSpinCtrlDouble (this),
73 AudioContentProperty::AUDIO_GAIN,
74 boost::mem_fn (&AudioContent::audio_gain),
75 boost::mem_fn (&AudioContent::set_audio_gain)
78 _gain->add (grid, wxGBPosition (r, 1));
79 add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
80 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
81 grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
84 add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
85 _delay = new ContentSpinCtrl<AudioContent> (
87 new wxSpinCtrl (this),
88 AudioContentProperty::AUDIO_DELAY,
89 boost::mem_fn (&AudioContent::audio_delay),
90 boost::mem_fn (&AudioContent::set_audio_delay)
93 _delay->add (grid, wxGBPosition (r, 1));
94 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
95 add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
98 _mapping = new AudioMappingView (this);
99 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
102 _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
103 _sizer->Add (_description, 0, wxALL, 12);
104 wxFont font = _description->GetFont();
105 font.SetStyle (wxFONTSTYLE_ITALIC);
106 font.SetPointSize (font.GetPointSize() - 1);
107 _description->SetFont (font);
110 _gain->wrapped()->SetRange (-60, 60);
111 _gain->wrapped()->SetDigits (1);
112 _gain->wrapped()->SetIncrement (0.5);
113 _delay->wrapped()->SetRange (-1000, 1000);
115 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
116 _show->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
117 _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
119 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
121 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
124 AudioPanel::~AudioPanel ()
127 _audio_dialog->Destroy ();
133 AudioPanel::film_changed (Film::Property property)
136 case Film::AUDIO_CHANNELS:
137 case Film::AUDIO_PROCESSOR:
138 _mapping->set_output_channels (_parent->film()->audio_output_names ());
141 case Film::VIDEO_FRAME_RATE:
142 setup_description ();
144 case Film::REEL_TYPE:
145 setup_sensitivity ();
152 AudioPanel::film_content_changed (int property)
154 AudioContentList ac = _parent->selected_audio ();
155 if (property == AudioContentProperty::AUDIO_STREAMS) {
156 if (ac.size() == 1) {
157 _mapping->set (ac.front()->audio_mapping());
158 _mapping->set_input_channels (ac.front()->audio_channel_names ());
160 _mapping->set (AudioMapping ());
162 setup_description ();
165 } else if (property == AudioContentProperty::AUDIO_GAIN) {
167 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
168 if (ac.size() == 1) {
169 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
170 checked_set (_reference, dcp ? dcp->reference_audio () : false);
172 checked_set (_reference, false);
175 setup_sensitivity ();
180 AudioPanel::gain_calculate_button_clicked ()
182 GainCalculatorDialog* d = new GainCalculatorDialog (this);
183 int const r = d->ShowModal ();
185 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
190 _gain->wrapped()->SetValue (
191 Config::instance()->cinema_sound_processor()->db_for_fader_change (
197 /* This appears to be necessary, as the change is not signalled,
200 _gain->view_changed ();
206 AudioPanel::setup_description ()
208 AudioContentList ac = _parent->selected_audio ();
209 if (ac.size () != 1) {
210 checked_set (_description, wxT (""));
214 checked_set (_description, ac.front()->processing_description ());
218 AudioPanel::mapping_changed (AudioMapping m)
220 AudioContentList c = _parent->selected_audio ();
222 c.front()->set_audio_mapping (m);
227 AudioPanel::content_selection_changed ()
229 AudioContentList sel = _parent->selected_audio ();
231 _gain->set_content (sel);
232 _delay->set_content (sel);
234 film_content_changed (AudioContentProperty::AUDIO_STREAMS);
236 setup_sensitivity ();
240 AudioPanel::setup_sensitivity ()
242 AudioContentList sel = _parent->selected_audio ();
244 shared_ptr<DCPContent> dcp;
245 if (sel.size() == 1) {
246 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
249 list<string> why_not;
250 bool const can_reference = dcp && dcp->can_reference_audio (why_not);
251 _reference->Enable (can_reference);
254 if (!can_reference) {
255 s = _("Cannot reference this DCP. ");
256 BOOST_FOREACH (string i, why_not) {
257 s += std_to_wx(i) + wxT(" ");
260 _reference->SetToolTip (s);
262 if (_reference->GetValue ()) {
263 _gain->wrapped()->Enable (false);
264 _gain_calculate_button->Enable (false);
265 _peak->Enable (false);
266 _delay->wrapped()->Enable (false);
267 _mapping->Enable (false);
268 _description->Enable (false);
270 _gain->wrapped()->Enable (true);
271 _gain_calculate_button->Enable (sel.size() == 1);
272 _peak->Enable (true);
273 _delay->wrapped()->Enable (true);
274 _mapping->Enable (sel.size() == 1);
275 _description->Enable (true);
280 AudioPanel::show_clicked ()
283 _audio_dialog->Destroy ();
287 AudioContentList ac = _parent->selected_audio ();
288 if (ac.size() != 1) {
292 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
293 _audio_dialog->Show ();
297 AudioPanel::setup_peak ()
299 AudioContentList sel = _parent->selected_audio ();
302 if (sel.size() != 1) {
303 _peak->SetLabel (wxT (""));
305 shared_ptr<Playlist> playlist (new Playlist);
306 playlist->add (sel.front ());
308 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
309 if (analysis->peak ()) {
310 float const peak_dB = 20 * log10 (analysis->peak().get()) + analysis->gain_correction (playlist);
314 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
316 _peak->SetLabel (_("Peak: unknown"));
319 _peak->SetLabel (_("Peak: unknown"));
323 static wxColour normal = _peak->GetForegroundColour ();
326 _peak->SetForegroundColour (wxColour (255, 0, 0));
328 _peak->SetForegroundColour (normal);
333 AudioPanel::active_jobs_changed (optional<string> j)
335 if (j && *j == "analyse_audio") {
341 AudioPanel::reference_clicked ()
343 ContentList c = _parent->selected ();
348 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
353 d->set_reference_audio (_reference->GetValue ());