2 Copyright (C) 2012-2021 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/>.
22 #include "audio_dialog.h"
23 #include "audio_mapping_view.h"
24 #include "audio_panel.h"
25 #include "check_box.h"
26 #include "content_panel.h"
27 #include "dcpomatic_button.h"
28 #include "gain_calculator_dialog.h"
29 #include "language_tag_widget.h"
30 #include "static_text.h"
32 #include "lib/config.h"
33 #include "lib/ffmpeg_audio_stream.h"
34 #include "lib/ffmpeg_content.h"
35 #include "lib/cinema_sound_processor.h"
36 #include "lib/job_manager.h"
37 #include "lib/dcp_content.h"
38 #include "lib/audio_content.h"
39 #include <wx/spinctrl.h>
48 using std::make_shared;
49 using std::dynamic_pointer_cast;
50 using std::shared_ptr;
51 using boost::optional;
52 #if BOOST_VERSION >= 106100
53 using namespace boost::placeholders;
57 AudioPanel::AudioPanel (ContentPanel* p)
58 : ContentSubPanel (p, _("Audio"))
61 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
62 _reference_note = new StaticText (this, wxT(""));
63 _reference_note->Wrap (200);
64 auto font = _reference_note->GetFont();
65 font.SetStyle(wxFONTSTYLE_ITALIC);
66 font.SetPointSize(font.GetPointSize() - 1);
67 _reference_note->SetFont(font);
69 _show = new Button (this, _("Show graph of audio levels..."));
70 _peak = new StaticText (this, wxT (""));
72 _gain_label = create_label (this, _("Gain"), true);
73 _gain = new ContentSpinCtrlDouble<AudioContent> (
75 new wxSpinCtrlDouble (this),
76 AudioContentProperty::GAIN,
78 boost::mem_fn (&AudioContent::gain),
79 boost::mem_fn (&AudioContent::set_gain)
82 _gain_db_label = create_label (this, _("dB"), false);
83 _gain_calculate_button = new Button (this, _("Calculate..."));
85 _delay_label = create_label (this, _("Delay"), true);
86 _delay = new ContentSpinCtrl<AudioContent> (
88 new wxSpinCtrl (this),
89 AudioContentProperty::DELAY,
91 boost::mem_fn (&AudioContent::delay),
92 boost::mem_fn (&AudioContent::set_delay)
95 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
96 _delay_ms_label = create_label (this, _("ms"), false);
98 _enable_language = new wxCheckBox (this, wxID_ANY, _("Language"));
99 _language = new LanguageTagWidget (this, _("Language used for the dialogue in this content"), boost::none);
101 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
102 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
104 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
105 _sizer->Add (_description, 0, wxALL, 12);
106 _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 content_selection_changed ();
114 film_changed (Film::Property::AUDIO_CHANNELS);
115 film_changed (Film::Property::VIDEO_FRAME_RATE);
116 film_changed (Film::Property::REEL_TYPE);
118 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
119 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
120 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
121 _enable_language->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::enable_language_clicked, this));
122 _language->Changed.connect (boost::bind(&AudioPanel::language_changed, this));
124 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
125 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
131 AudioPanel::add_to_grid ()
135 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
136 reference_sizer->Add (_reference, 0);
137 reference_sizer->Add (_reference_note, 0);
138 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
141 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
142 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
145 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
147 auto s = new wxBoxSizer (wxHORIZONTAL);
148 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
149 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
150 _grid->Add (s, wxGBPosition(r, 1));
153 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
156 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
157 auto s = new wxBoxSizer (wxHORIZONTAL);
158 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
159 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
160 _grid->Add (s, wxGBPosition(r, 1));
163 s = new wxBoxSizer (wxHORIZONTAL);
164 s->Add (_enable_language, 0, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
165 s->Add (_language->sizer(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT);
166 _grid->Add (s, wxGBPosition(r, 0), wxGBSpan(1, 2), wxEXPAND);
170 AudioPanel::~AudioPanel ()
173 _audio_dialog->Destroy ();
174 _audio_dialog = nullptr;
179 AudioPanel::film_changed (Film::Property property)
181 if (!_parent->film()) {
186 case Film::Property::AUDIO_CHANNELS:
187 case Film::Property::AUDIO_PROCESSOR:
188 _mapping->set_output_channels (_parent->film()->audio_output_names ());
191 case Film::Property::VIDEO_FRAME_RATE:
192 setup_description ();
194 case Film::Property::REEL_TYPE:
195 case Film::Property::INTEROP:
196 setup_sensitivity ();
204 AudioPanel::film_content_changed (int property)
206 auto ac = _parent->selected_audio ();
207 if (property == AudioContentProperty::STREAMS) {
208 if (ac.size() == 1) {
209 _mapping->set (ac.front()->audio->mapping());
210 _mapping->set_input_channels (ac.front()->audio->channel_names ());
212 vector<AudioMappingView::Group> groups;
214 for (auto i: ac.front()->audio->streams()) {
215 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
220 name += " (" + f->codec_name.get() + ")";
223 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
226 _mapping->set_input_groups (groups);
229 _mapping->set (AudioMapping ());
231 setup_description ();
234 } else if (property == AudioContentProperty::GAIN) {
236 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
237 if (ac.size() == 1) {
238 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
239 checked_set (_reference, dcp ? dcp->reference_audio () : false);
241 checked_set (_reference, false);
244 setup_sensitivity ();
245 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
246 setup_description ();
247 } else if (property == AudioContentProperty::LANGUAGE) {
248 if (ac.size() == 1 && ac.front()->audio->language()) {
249 _enable_language->SetValue (true);
250 _language->set (ac.front()->audio->language());
252 _enable_language->SetValue (false);
253 _language->set (boost::none);
259 AudioPanel::gain_calculate_button_clicked ()
261 auto d = new GainCalculatorDialog (this);
262 auto const r = d->ShowModal ();
263 auto c = d->db_change();
265 if (r == wxID_CANCEL || !c) {
270 auto old_peak_dB = peak ();
271 auto old_value = _gain->wrapped()->GetValue();
272 _gain->wrapped()->SetValue(old_value + *c);
274 /* This appears to be necessary, as the change is not signalled,
277 _gain->view_changed ();
279 auto peak_dB = peak ();
280 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
281 error_dialog (this, _("It is not possible to adjust the content's gain for this fader change as it would cause the DCP's audio to clip. The gain has not been changed."));
282 _gain->wrapped()->SetValue (old_value);
283 _gain->view_changed ();
290 AudioPanel::setup_description ()
292 auto ac = _parent->selected_audio ();
293 if (ac.size () != 1) {
294 checked_set (_description, wxT (""));
298 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
302 AudioPanel::mapping_changed (AudioMapping m)
304 auto c = _parent->selected_audio ();
306 c.front()->audio->set_mapping (m);
311 AudioPanel::content_selection_changed ()
313 auto sel = _parent->selected_audio ();
315 _gain->set_content (sel);
316 _delay->set_content (sel);
318 film_content_changed (AudioContentProperty::STREAMS);
319 film_content_changed (AudioContentProperty::GAIN);
320 film_content_changed (AudioContentProperty::LANGUAGE);
321 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
323 setup_sensitivity ();
327 AudioPanel::setup_sensitivity ()
329 auto sel = _parent->selected_audio ();
331 shared_ptr<DCPContent> dcp;
332 if (sel.size() == 1) {
333 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
337 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
339 if (why_not.empty()) {
340 cannot = _("Cannot reference this DCP's audio.");
342 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
344 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
346 auto const ref = _reference->GetValue();
347 auto const single = sel.size() == 1;
349 _gain->wrapped()->Enable (!ref);
350 _gain_calculate_button->Enable (!ref && single);
351 _show->Enable (single);
352 _peak->Enable (!ref && single);
353 _delay->wrapped()->Enable (!ref);
354 _mapping->Enable (!ref && single);
355 _description->Enable (!ref && single);
356 _enable_language->Enable (!ref && single);
357 _language->enable (!ref && single && _enable_language->GetValue());
361 AudioPanel::show_clicked ()
364 _audio_dialog->Destroy ();
365 _audio_dialog = nullptr;
368 auto ac = _parent->selected_audio ();
369 if (ac.size() != 1) {
373 _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
374 _audio_dialog->Show ();
377 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
379 AudioPanel::peak () const
381 optional<float> peak_dB;
383 auto sel = _parent->selected_audio ();
384 if (sel.size() == 1) {
385 auto playlist = make_shared<Playlist>();
386 playlist->add (_parent->film(), sel.front());
388 auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
389 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
399 AudioPanel::setup_peak ()
401 auto sel = _parent->selected_audio ();
403 auto peak_dB = peak ();
404 if (sel.size() != 1) {
405 _peak->SetLabel (wxT(""));
409 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
411 _peak->SetLabel (_("Peak: unknown"));
415 static auto normal = _peak->GetForegroundColour ();
417 if (peak_dB && *peak_dB > -0.5) {
418 _peak->SetForegroundColour (wxColour (255, 0, 0));
419 } else if (peak_dB && *peak_dB > -3) {
420 _peak->SetForegroundColour (wxColour (186, 120, 0));
422 _peak->SetForegroundColour (normal);
427 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
429 if (old_active && *old_active == "analyse_audio") {
431 _mapping->Enable (true);
432 } else if (new_active && *new_active == "analyse_audio") {
433 _mapping->Enable (false);
438 AudioPanel::reference_clicked ()
440 auto c = _parent->selected ();
445 auto d = dynamic_pointer_cast<DCPContent>(c.front());
450 d->set_reference_audio (_reference->GetValue ());
454 AudioPanel::set_film (shared_ptr<Film>)
456 /* We are changing film, so destroy any audio dialog for the old one */
458 _audio_dialog->Destroy ();
459 _audio_dialog = nullptr;
465 AudioPanel::enable_language_clicked ()
467 setup_sensitivity ();
468 auto sel = _parent->selected_audio ();
469 if (sel.size() == 1) {
470 sel.front()->audio->set_language (_enable_language->GetValue() ? _language->get() : boost::none);
476 AudioPanel::language_changed ()
478 auto sel = _parent->selected_audio ();
479 if (sel.size() == 1) {
480 sel.front()->audio->set_language (_language->get());