2 Copyright (C) 2012-2016 Carl Hetherington <cth@carlh.net>
4 This file is part of DCP-o-matic.
6 DCP-o-matic is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 DCP-o-matic is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with DCP-o-matic. If not, see <http://www.gnu.org/licenses/>.
21 #include "audio_panel.h"
22 #include "audio_mapping_view.h"
24 #include "gain_calculator_dialog.h"
25 #include "content_panel.h"
26 #include "audio_dialog.h"
27 #include "lib/config.h"
28 #include "lib/ffmpeg_content.h"
29 #include "lib/cinema_sound_processor.h"
30 #include "lib/job_manager.h"
31 #include "lib/dcp_content.h"
32 #include "lib/audio_content.h"
33 #include <wx/spinctrl.h>
34 #include <boost/foreach.hpp>
42 using boost::dynamic_pointer_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));
59 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
60 grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
61 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
62 grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
65 add_label_to_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
66 _gain = new ContentSpinCtrlDouble<AudioContent> (
68 new wxSpinCtrlDouble (this),
69 AudioContentProperty::GAIN,
71 boost::mem_fn (&AudioContent::gain),
72 boost::mem_fn (&AudioContent::set_gain)
75 _gain->add (grid, wxGBPosition (r, 1));
76 add_label_to_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
77 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
78 grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
81 add_label_to_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
82 _delay = new ContentSpinCtrl<AudioContent> (
84 new wxSpinCtrl (this),
85 AudioContentProperty::DELAY,
87 boost::mem_fn (&AudioContent::delay),
88 boost::mem_fn (&AudioContent::set_delay)
91 _delay->add (grid, wxGBPosition (r, 1));
92 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
93 add_label_to_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
96 _mapping = new AudioMappingView (this);
97 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
100 _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
101 _sizer->Add (_description, 0, wxALL, 12);
102 wxFont font = _description->GetFont();
103 font.SetStyle (wxFONTSTYLE_ITALIC);
104 font.SetPointSize (font.GetPointSize() - 1);
105 _description->SetFont (font);
108 _gain->wrapped()->SetRange (-60, 60);
109 _gain->wrapped()->SetDigits (1);
110 _gain->wrapped()->SetIncrement (0.5);
111 _delay->wrapped()->SetRange (-1000, 1000);
113 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
114 _show->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
115 _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
117 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
119 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
122 AudioPanel::~AudioPanel ()
125 _audio_dialog->Destroy ();
131 AudioPanel::film_changed (Film::Property property)
134 case Film::AUDIO_CHANNELS:
135 case Film::AUDIO_PROCESSOR:
136 _mapping->set_output_channels (_parent->film()->audio_output_names ());
139 case Film::VIDEO_FRAME_RATE:
140 setup_description ();
142 case Film::REEL_TYPE:
143 setup_sensitivity ();
150 AudioPanel::film_content_changed (int property)
152 ContentList ac = _parent->selected_audio ();
153 if (property == AudioContentProperty::STREAMS) {
154 if (ac.size() == 1) {
155 _mapping->set (ac.front()->audio->mapping());
156 _mapping->set_input_channels (ac.front()->audio->channel_names ());
158 _mapping->set (AudioMapping ());
160 setup_description ();
163 } else if (property == AudioContentProperty::GAIN) {
165 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
166 if (ac.size() == 1) {
167 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
168 checked_set (_reference, dcp ? dcp->reference_audio () : false);
170 checked_set (_reference, false);
173 setup_sensitivity ();
174 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
175 setup_description ();
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 ContentList ac = _parent->selected_audio ();
209 if (ac.size () != 1) {
210 checked_set (_description, wxT (""));
214 checked_set (_description, ac.front()->audio->processing_description ());
218 AudioPanel::mapping_changed (AudioMapping m)
220 ContentList c = _parent->selected_audio ();
222 c.front()->audio->set_mapping (m);
227 AudioPanel::content_selection_changed ()
229 ContentList sel = _parent->selected_audio ();
231 _gain->set_content (sel);
232 _delay->set_content (sel);
234 film_content_changed (AudioContentProperty::STREAMS);
235 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
237 setup_sensitivity ();
241 AudioPanel::setup_sensitivity ()
243 ContentList sel = _parent->selected_audio ();
245 shared_ptr<DCPContent> dcp;
246 if (sel.size() == 1) {
247 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
250 list<string> why_not;
251 bool const can_reference = dcp && dcp->can_reference_audio (why_not);
252 setup_refer_button (_reference, dcp, can_reference, why_not);
254 if (_reference->GetValue ()) {
255 _gain->wrapped()->Enable (false);
256 _gain_calculate_button->Enable (false);
257 _peak->Enable (false);
258 _delay->wrapped()->Enable (false);
259 _mapping->Enable (false);
260 _description->Enable (false);
262 _gain->wrapped()->Enable (true);
263 _gain_calculate_button->Enable (sel.size() == 1);
264 _peak->Enable (true);
265 _delay->wrapped()->Enable (true);
266 _mapping->Enable (sel.size() == 1);
267 _description->Enable (true);
272 AudioPanel::show_clicked ()
275 _audio_dialog->Destroy ();
279 ContentList ac = _parent->selected_audio ();
280 if (ac.size() != 1) {
284 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
285 _audio_dialog->Show ();
289 AudioPanel::setup_peak ()
291 ContentList sel = _parent->selected_audio ();
294 if (sel.size() != 1) {
295 _peak->SetLabel (wxT (""));
297 shared_ptr<Playlist> playlist (new Playlist);
298 playlist->add (sel.front ());
300 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
301 if (analysis->sample_peak ()) {
302 float const peak_dB = 20 * log10 (analysis->sample_peak().get()) + analysis->gain_correction (playlist);
306 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
308 _peak->SetLabel (_("Peak: unknown"));
311 _peak->SetLabel (_("Peak: unknown"));
315 static wxColour normal = _peak->GetForegroundColour ();
318 _peak->SetForegroundColour (wxColour (255, 0, 0));
320 _peak->SetForegroundColour (normal);
325 AudioPanel::active_jobs_changed (optional<string> j)
327 if (j && *j == "analyse_audio") {
333 AudioPanel::reference_clicked ()
335 ContentList c = _parent->selected ();
340 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
345 d->set_reference_audio (_reference->GetValue ());