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"
38 #include "lib/job_manager.h"
39 #include "lib/maths_util.h"
40 #include <dcp/warnings.h>
41 LIBDCP_DISABLE_WARNINGS
42 #include <wx/spinctrl.h>
43 LIBDCP_ENABLE_WARNINGS
46 using std::dynamic_pointer_cast;
48 using std::make_shared;
51 using std::shared_ptr;
54 using boost::optional;
55 #if BOOST_VERSION >= 106100
56 using namespace boost::placeholders;
58 using namespace dcpomatic;
61 std::map<boost::filesystem::path, float> AudioPanel::_peak_cache;
64 AudioPanel::AudioPanel (ContentPanel* p)
65 : ContentSubPanel (p, _("Audio"))
74 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
75 _reference_note = new StaticText (this, wxT(""));
76 _reference_note->Wrap (200);
77 auto font = _reference_note->GetFont();
78 font.SetStyle(wxFONTSTYLE_ITALIC);
79 font.SetPointSize(font.GetPointSize() - 1);
80 _reference_note->SetFont(font);
82 _show = new Button (this, _("Show graph of audio levels..."));
83 _peak = new StaticText (this, wxT (""));
85 _gain_label = create_label (this, _("Gain"), true);
86 _gain = new ContentSpinCtrlDouble<AudioContent> (
88 new wxSpinCtrlDouble (this),
89 AudioContentProperty::GAIN,
91 boost::mem_fn (&AudioContent::gain),
92 boost::mem_fn (&AudioContent::set_gain)
95 _gain_db_label = create_label (this, _("dB"), false);
96 _gain_calculate_button = new Button (this, _("Calculate..."));
98 _delay_label = create_label (this, _("Delay"), true);
99 _delay = new ContentSpinCtrl<AudioContent> (
101 new wxSpinCtrl (this),
102 AudioContentProperty::DELAY,
104 boost::mem_fn (&AudioContent::delay),
105 boost::mem_fn (&AudioContent::set_delay)
108 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
109 _delay_ms_label = create_label (this, _("ms"), false);
111 _fade_in_label = create_label (this, _("Fade in"), true);
112 _fade_in = new Timecode<ContentTime> (this);
114 _fade_out_label = create_label (this, _("Fade out"), true);
115 _fade_out = new Timecode<ContentTime> (this);
117 _use_same_fades_as_video = new CheckBox(this, _("Use same fades as video"));
119 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
120 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
122 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
123 _sizer->Add (_description, 0, wxALL, 12);
124 _description->SetFont (font);
126 _gain->wrapped()->SetRange (-60, 60);
127 _gain->wrapped()->SetDigits (1);
128 _gain->wrapped()->SetIncrement (0.5);
129 _delay->wrapped()->SetRange (-1000, 1000);
131 content_selection_changed ();
132 film_changed(FilmProperty::AUDIO_CHANNELS);
133 film_changed(FilmProperty::VIDEO_FRAME_RATE);
134 film_changed(FilmProperty::REEL_TYPE);
136 _reference->bind(&AudioPanel::reference_clicked, this);
137 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
138 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
140 _fade_in->Changed.connect (boost::bind(&AudioPanel::fade_in_changed, this));
141 _fade_out->Changed.connect (boost::bind(&AudioPanel::fade_out_changed, this));
142 _use_same_fades_as_video->bind(&AudioPanel::use_same_fades_as_video_changed, this);
144 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
145 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
154 AudioPanel::add_to_grid ()
158 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
159 reference_sizer->Add (_reference, 0);
160 reference_sizer->Add (_reference_note, 0);
161 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
164 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
165 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
168 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
170 auto s = new wxBoxSizer (wxHORIZONTAL);
171 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
172 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
173 _grid->Add (s, wxGBPosition(r, 1));
176 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
179 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
180 auto s = new wxBoxSizer (wxHORIZONTAL);
181 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
182 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
183 _grid->Add (s, wxGBPosition(r, 1));
186 add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition(r, 0));
187 _grid->Add (_fade_in, wxGBPosition(r, 1), wxGBSpan(1, 3));
190 add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition(r, 0));
191 _grid->Add (_fade_out, wxGBPosition(r, 1), wxGBSpan(1, 3));
194 _grid->Add (_use_same_fades_as_video, wxGBPosition(r, 0), wxGBSpan(1, 4));
200 AudioPanel::film_changed (FilmProperty property)
202 if (!_parent->film()) {
207 case FilmProperty::AUDIO_CHANNELS:
208 case FilmProperty::AUDIO_PROCESSOR:
209 _mapping->set_output_channels (_parent->film()->audio_output_names ());
213 case FilmProperty::VIDEO_FRAME_RATE:
214 setup_description ();
216 case FilmProperty::REEL_TYPE:
217 case FilmProperty::INTEROP:
218 setup_sensitivity ();
227 AudioPanel::film_content_changed (int property)
229 auto ac = _parent->selected_audio ();
230 if (property == AudioContentProperty::STREAMS) {
231 if (ac.size() == 1) {
232 _mapping->set (ac.front()->audio->mapping());
233 _mapping->set_input_channels (ac.front()->audio->channel_names ());
235 vector<AudioMappingView::Group> groups;
237 for (auto i: ac.front()->audio->streams()) {
238 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
243 name += " (" + f->codec_name.get() + ")";
246 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
249 _mapping->set_input_groups (groups);
252 _mapping->set (AudioMapping ());
254 setup_description ();
257 } else if (property == AudioContentProperty::GAIN) {
258 /* This is a bit aggressive but probably not so bad */
261 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
262 if (ac.size() == 1) {
263 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
264 checked_set (_reference, dcp ? dcp->reference_audio () : false);
266 checked_set (_reference, false);
269 setup_sensitivity ();
270 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
271 setup_description ();
272 } else if (property == AudioContentProperty::FADE_IN) {
275 check.insert (i->audio->fade_in().get());
278 if (check.size() == 1) {
280 ac.front()->audio->fade_in(),
281 ac.front()->active_video_frame_rate(_parent->film())
286 } else if (property == AudioContentProperty::FADE_OUT) {
289 check.insert (i->audio->fade_out().get());
292 if (check.size() == 1) {
294 ac.front()->audio->fade_out(),
295 ac.front()->active_video_frame_rate(_parent->film())
300 } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
301 setup_sensitivity ();
307 AudioPanel::gain_calculate_button_clicked ()
309 GainCalculatorDialog dialog(this);
310 auto const r = dialog.ShowModal();
311 auto change = dialog.db_change();
313 if (r == wxID_CANCEL || !change) {
317 auto old_peak_dB = peak ();
318 auto old_value = _gain->wrapped()->GetValue();
319 _gain->wrapped()->SetValue(old_value + *change);
321 /* This appears to be necessary, as the change is not signalled,
324 _gain->view_changed ();
326 auto peak_dB = peak ();
327 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
328 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."));
329 _gain->wrapped()->SetValue (old_value);
330 _gain->view_changed ();
336 AudioPanel::setup_description ()
338 auto ac = _parent->selected_audio ();
339 if (ac.size () != 1) {
340 checked_set (_description, wxT (""));
344 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
349 AudioPanel::mapping_changed (AudioMapping m)
351 auto c = _parent->selected_audio ();
353 c.front()->audio->set_mapping (m);
359 AudioPanel::content_selection_changed ()
361 auto sel = _parent->selected_audio ();
363 _gain->set_content (sel);
364 _delay->set_content (sel);
366 film_content_changed (AudioContentProperty::STREAMS);
367 film_content_changed (AudioContentProperty::GAIN);
368 film_content_changed (AudioContentProperty::FADE_IN);
369 film_content_changed (AudioContentProperty::FADE_OUT);
370 film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
371 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
373 setup_sensitivity ();
378 AudioPanel::setup_sensitivity ()
380 auto sel = _parent->selected_audio ();
382 shared_ptr<DCPContent> dcp;
383 if (sel.size() == 1) {
384 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
388 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
390 if (why_not.empty()) {
391 cannot = _("Cannot reference this DCP's audio.");
393 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
395 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
397 auto const ref = _reference->GetValue();
398 auto const single = sel.size() == 1;
400 auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
402 _gain->wrapped()->Enable (!ref);
403 _gain_calculate_button->Enable (!ref && single);
404 _show->Enable (single);
405 _peak->Enable (!ref && single);
406 _delay->wrapped()->Enable (!ref);
407 _mapping->Enable (!ref && single);
408 _description->Enable (!ref && single);
409 _fade_in->Enable (!_use_same_fades_as_video->GetValue());
410 _fade_out->Enable (!_use_same_fades_as_video->GetValue());
411 _use_same_fades_as_video->Enable (!ref && all_have_video);
416 AudioPanel::show_clicked ()
418 _audio_dialog.reset();
420 auto ac = _parent->selected_audio ();
421 if (ac.size() != 1) {
425 _audio_dialog.reset(this, _parent->film(), _parent->film_viewer(), ac.front());
426 _audio_dialog->Show ();
430 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
432 AudioPanel::peak () const
434 optional<float> peak_dB;
436 auto sel = _parent->selected_audio ();
437 if (sel.size() == 1) {
438 auto playlist = make_shared<Playlist>();
439 playlist->add (_parent->film(), sel.front());
441 /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
442 * the content selection is changed, so cache it.
444 auto const path = _parent->film()->audio_analysis_path(playlist);
445 auto cached = _peak_cache.find(path);
446 if (cached != _peak_cache.end()) {
447 peak_dB = cached->second;
449 auto analysis = make_shared<AudioAnalysis>(path);
450 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
451 _peak_cache[path] = *peak_dB;
463 AudioPanel::setup_peak ()
465 auto sel = _parent->selected_audio ();
467 auto peak_dB = peak ();
468 if (sel.size() != 1) {
469 _peak->SetLabel (wxT(""));
473 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
475 _peak->SetLabel (_("Peak: unknown"));
479 static auto normal = _peak->GetForegroundColour ();
481 if (peak_dB && *peak_dB > -0.5) {
482 _peak->SetForegroundColour (wxColour (255, 0, 0));
483 } else if (peak_dB && *peak_dB > -3) {
484 _peak->SetForegroundColour (wxColour (186, 120, 0));
486 _peak->SetForegroundColour (normal);
492 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
494 if (old_active && *old_active == "analyse_audio") {
496 _mapping->Enable (true);
497 } else if (new_active && *new_active == "analyse_audio") {
498 _mapping->Enable (false);
504 AudioPanel::reference_clicked ()
506 auto c = _parent->selected ();
511 auto d = dynamic_pointer_cast<DCPContent>(c.front());
516 d->set_reference_audio (_reference->GetValue ());
521 AudioPanel::set_film (shared_ptr<Film>)
523 /* We are changing film, so destroy any audio dialog for the old one */
524 _audio_dialog.reset();
529 AudioPanel::fade_in_changed ()
531 auto const hmsf = _fade_in->get();
532 for (auto i: _parent->selected_audio()) {
533 auto const vfr = i->active_video_frame_rate(_parent->film());
534 i->audio->set_fade_in (dcpomatic::ContentTime(hmsf, vfr));
540 AudioPanel::fade_out_changed ()
542 auto const hmsf = _fade_out->get();
543 for (auto i: _parent->selected_audio()) {
544 auto const vfr = i->active_video_frame_rate (_parent->film());
545 i->audio->set_fade_out (dcpomatic::ContentTime(hmsf, vfr));
551 AudioPanel::use_same_fades_as_video_changed ()
553 for (auto content: _parent->selected_audio()) {
554 content->audio->set_use_same_fades_as_video(_use_same_fades_as_video->GetValue());