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 "static_text.h"
31 #include "lib/audio_content.h"
32 #include "lib/cinema_sound_processor.h"
33 #include "lib/config.h"
34 #include "lib/dcp_content.h"
35 #include "lib/ffmpeg_audio_stream.h"
36 #include "lib/ffmpeg_content.h"
37 #include "lib/job_manager.h"
38 #include "lib/maths_util.h"
39 #include <dcp/warnings.h>
40 LIBDCP_DISABLE_WARNINGS
41 #include <wx/spinctrl.h>
42 LIBDCP_ENABLE_WARNINGS
45 using std::dynamic_pointer_cast;
47 using std::make_shared;
50 using std::shared_ptr;
53 using boost::optional;
54 #if BOOST_VERSION >= 106100
55 using namespace boost::placeholders;
57 using namespace dcpomatic;
60 std::map<boost::filesystem::path, float> AudioPanel::_peak_cache;
63 AudioPanel::AudioPanel (ContentPanel* p)
64 : ContentSubPanel (p, _("Audio"))
73 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
74 _reference_note = new StaticText (this, wxT(""));
75 _reference_note->Wrap (200);
76 auto font = _reference_note->GetFont();
77 font.SetStyle(wxFONTSTYLE_ITALIC);
78 font.SetPointSize(font.GetPointSize() - 1);
79 _reference_note->SetFont(font);
81 _show = new Button (this, _("Show graph of audio levels..."));
82 _peak = new StaticText (this, wxT (""));
84 _gain_label = create_label (this, _("Gain"), true);
85 _gain = new ContentSpinCtrlDouble<AudioContent> (
87 new wxSpinCtrlDouble (this),
88 AudioContentProperty::GAIN,
90 boost::mem_fn (&AudioContent::gain),
91 boost::mem_fn (&AudioContent::set_gain)
94 _gain_db_label = create_label (this, _("dB"), false);
95 _gain_calculate_button = new Button (this, _("Calculate..."));
97 _delay_label = create_label (this, _("Delay"), true);
98 _delay = new ContentSpinCtrl<AudioContent> (
100 new wxSpinCtrl (this),
101 AudioContentProperty::DELAY,
103 boost::mem_fn (&AudioContent::delay),
104 boost::mem_fn (&AudioContent::set_delay)
107 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
108 _delay_ms_label = create_label (this, _("ms"), false);
110 _fade_in_label = create_label (this, _("Fade in"), true);
111 _fade_in = new Timecode<ContentTime> (this);
113 _fade_out_label = create_label (this, _("Fade out"), true);
114 _fade_out = new Timecode<ContentTime> (this);
116 _use_same_fades_as_video = new wxCheckBox (this, wxID_ANY, _("Use same fades as video"));
118 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
119 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
121 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
122 _sizer->Add (_description, 0, wxALL, 12);
123 _description->SetFont (font);
125 _gain->wrapped()->SetRange (-60, 60);
126 _gain->wrapped()->SetDigits (1);
127 _gain->wrapped()->SetIncrement (0.5);
128 _delay->wrapped()->SetRange (-1000, 1000);
130 content_selection_changed ();
131 film_changed (Film::Property::AUDIO_CHANNELS);
132 film_changed (Film::Property::VIDEO_FRAME_RATE);
133 film_changed (Film::Property::REEL_TYPE);
135 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
136 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
137 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
139 _fade_in->Changed.connect (boost::bind(&AudioPanel::fade_in_changed, this));
140 _fade_out->Changed.connect (boost::bind(&AudioPanel::fade_out_changed, this));
141 _use_same_fades_as_video->Bind (wxEVT_CHECKBOX, boost::bind(&AudioPanel::use_same_fades_as_video_changed, this));
143 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
144 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
153 AudioPanel::add_to_grid ()
157 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
158 reference_sizer->Add (_reference, 0);
159 reference_sizer->Add (_reference_note, 0);
160 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
163 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
164 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
167 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
169 auto s = new wxBoxSizer (wxHORIZONTAL);
170 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
171 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
172 _grid->Add (s, wxGBPosition(r, 1));
175 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
178 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
179 auto s = new wxBoxSizer (wxHORIZONTAL);
180 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
181 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
182 _grid->Add (s, wxGBPosition(r, 1));
185 add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition(r, 0));
186 _grid->Add (_fade_in, wxGBPosition(r, 1), wxGBSpan(1, 3));
189 add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition(r, 0));
190 _grid->Add (_fade_out, wxGBPosition(r, 1), wxGBSpan(1, 3));
193 _grid->Add (_use_same_fades_as_video, wxGBPosition(r, 0), wxGBSpan(1, 4));
198 AudioPanel::~AudioPanel ()
201 _audio_dialog->Destroy ();
202 _audio_dialog = nullptr;
207 AudioPanel::film_changed (Film::Property property)
209 if (!_parent->film()) {
214 case Film::Property::AUDIO_CHANNELS:
215 case Film::Property::AUDIO_PROCESSOR:
216 _mapping->set_output_channels (_parent->film()->audio_output_names ());
219 case Film::Property::VIDEO_FRAME_RATE:
220 setup_description ();
222 case Film::Property::REEL_TYPE:
223 case Film::Property::INTEROP:
224 setup_sensitivity ();
233 AudioPanel::film_content_changed (int property)
235 auto ac = _parent->selected_audio ();
236 if (property == AudioContentProperty::STREAMS) {
237 if (ac.size() == 1) {
238 _mapping->set (ac.front()->audio->mapping());
239 _mapping->set_input_channels (ac.front()->audio->channel_names ());
241 vector<AudioMappingView::Group> groups;
243 for (auto i: ac.front()->audio->streams()) {
244 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
249 name += " (" + f->codec_name.get() + ")";
252 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
255 _mapping->set_input_groups (groups);
258 _mapping->set (AudioMapping ());
260 setup_description ();
263 } else if (property == AudioContentProperty::GAIN) {
264 /* This is a bit aggressive but probably not so bad */
267 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
268 if (ac.size() == 1) {
269 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
270 checked_set (_reference, dcp ? dcp->reference_audio () : false);
272 checked_set (_reference, false);
275 setup_sensitivity ();
276 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
277 setup_description ();
278 } else if (property == AudioContentProperty::FADE_IN) {
281 check.insert (i->audio->fade_in().get());
284 if (check.size() == 1) {
286 ac.front()->audio->fade_in(),
287 ac.front()->active_video_frame_rate(_parent->film())
292 } else if (property == AudioContentProperty::FADE_OUT) {
295 check.insert (i->audio->fade_out().get());
298 if (check.size() == 1) {
300 ac.front()->audio->fade_out(),
301 ac.front()->active_video_frame_rate(_parent->film())
306 } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
307 setup_sensitivity ();
313 AudioPanel::gain_calculate_button_clicked ()
315 auto d = new GainCalculatorDialog (this);
316 auto const r = d->ShowModal ();
317 auto c = d->db_change();
319 if (r == wxID_CANCEL || !c) {
324 auto old_peak_dB = peak ();
325 auto old_value = _gain->wrapped()->GetValue();
326 _gain->wrapped()->SetValue(old_value + *c);
328 /* This appears to be necessary, as the change is not signalled,
331 _gain->view_changed ();
333 auto peak_dB = peak ();
334 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
335 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."));
336 _gain->wrapped()->SetValue (old_value);
337 _gain->view_changed ();
345 AudioPanel::setup_description ()
347 auto ac = _parent->selected_audio ();
348 if (ac.size () != 1) {
349 checked_set (_description, wxT (""));
353 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
358 AudioPanel::mapping_changed (AudioMapping m)
360 auto c = _parent->selected_audio ();
362 c.front()->audio->set_mapping (m);
368 AudioPanel::content_selection_changed ()
370 auto sel = _parent->selected_audio ();
372 _gain->set_content (sel);
373 _delay->set_content (sel);
375 film_content_changed (AudioContentProperty::STREAMS);
376 film_content_changed (AudioContentProperty::GAIN);
377 film_content_changed (AudioContentProperty::FADE_IN);
378 film_content_changed (AudioContentProperty::FADE_OUT);
379 film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
380 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
382 setup_sensitivity ();
387 AudioPanel::setup_sensitivity ()
389 auto sel = _parent->selected_audio ();
391 shared_ptr<DCPContent> dcp;
392 if (sel.size() == 1) {
393 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
397 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
399 if (why_not.empty()) {
400 cannot = _("Cannot reference this DCP's audio.");
402 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
404 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
406 auto const ref = _reference->GetValue();
407 auto const single = sel.size() == 1;
409 auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
411 _gain->wrapped()->Enable (!ref);
412 _gain_calculate_button->Enable (!ref && single);
413 _show->Enable (single);
414 _peak->Enable (!ref && single);
415 _delay->wrapped()->Enable (!ref);
416 _mapping->Enable (!ref && single);
417 _description->Enable (!ref && single);
418 _fade_in->Enable (!_use_same_fades_as_video->GetValue());
419 _fade_out->Enable (!_use_same_fades_as_video->GetValue());
420 _use_same_fades_as_video->Enable (!ref && all_have_video);
425 AudioPanel::show_clicked ()
428 _audio_dialog->Destroy ();
429 _audio_dialog = nullptr;
432 auto ac = _parent->selected_audio ();
433 if (ac.size() != 1) {
437 _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
438 _audio_dialog->Show ();
442 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
444 AudioPanel::peak () const
446 optional<float> peak_dB;
448 auto sel = _parent->selected_audio ();
449 if (sel.size() == 1) {
450 auto playlist = make_shared<Playlist>();
451 playlist->add (_parent->film(), sel.front());
453 /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
454 * the content selection is changed, so cache it.
456 auto const path = _parent->film()->audio_analysis_path(playlist);
457 auto cached = _peak_cache.find(path);
458 if (cached != _peak_cache.end()) {
459 peak_dB = cached->second;
461 auto analysis = make_shared<AudioAnalysis>(path);
462 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
463 _peak_cache[path] = *peak_dB;
475 AudioPanel::setup_peak ()
477 auto sel = _parent->selected_audio ();
479 auto peak_dB = peak ();
480 if (sel.size() != 1) {
481 _peak->SetLabel (wxT(""));
485 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
487 _peak->SetLabel (_("Peak: unknown"));
491 static auto normal = _peak->GetForegroundColour ();
493 if (peak_dB && *peak_dB > -0.5) {
494 _peak->SetForegroundColour (wxColour (255, 0, 0));
495 } else if (peak_dB && *peak_dB > -3) {
496 _peak->SetForegroundColour (wxColour (186, 120, 0));
498 _peak->SetForegroundColour (normal);
504 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
506 if (old_active && *old_active == "analyse_audio") {
508 _mapping->Enable (true);
509 } else if (new_active && *new_active == "analyse_audio") {
510 _mapping->Enable (false);
516 AudioPanel::reference_clicked ()
518 auto c = _parent->selected ();
523 auto d = dynamic_pointer_cast<DCPContent>(c.front());
528 d->set_reference_audio (_reference->GetValue ());
533 AudioPanel::set_film (shared_ptr<Film>)
535 /* We are changing film, so destroy any audio dialog for the old one */
537 _audio_dialog->Destroy ();
538 _audio_dialog = nullptr;
544 AudioPanel::fade_in_changed ()
546 auto const hmsf = _fade_in->get();
547 for (auto i: _parent->selected_audio()) {
548 auto const vfr = i->active_video_frame_rate(_parent->film());
549 i->audio->set_fade_in (dcpomatic::ContentTime(hmsf, vfr));
555 AudioPanel::fade_out_changed ()
557 auto const hmsf = _fade_out->get();
558 for (auto i: _parent->selected_audio()) {
559 auto const vfr = i->active_video_frame_rate (_parent->film());
560 i->audio->set_fade_out (dcpomatic::ContentTime(hmsf, vfr));
566 AudioPanel::use_same_fades_as_video_changed ()
568 for (auto content: _parent->selected_audio()) {
569 content->audio->set_use_same_fades_as_video(_use_same_fades_as_video->GetValue());