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 ());
212 case FilmProperty::VIDEO_FRAME_RATE:
213 setup_description ();
215 case FilmProperty::REEL_TYPE:
216 case FilmProperty::INTEROP:
217 setup_sensitivity ();
226 AudioPanel::film_content_changed (int property)
228 auto ac = _parent->selected_audio ();
229 if (property == AudioContentProperty::STREAMS) {
230 if (ac.size() == 1) {
231 _mapping->set (ac.front()->audio->mapping());
232 _mapping->set_input_channels (ac.front()->audio->channel_names ());
234 vector<AudioMappingView::Group> groups;
236 for (auto i: ac.front()->audio->streams()) {
237 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
242 name += " (" + f->codec_name.get() + ")";
245 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
248 _mapping->set_input_groups (groups);
251 _mapping->set (AudioMapping ());
253 setup_description ();
256 } else if (property == AudioContentProperty::GAIN) {
257 /* This is a bit aggressive but probably not so bad */
260 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
261 if (ac.size() == 1) {
262 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
263 checked_set (_reference, dcp ? dcp->reference_audio () : false);
265 checked_set (_reference, false);
268 setup_sensitivity ();
269 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
270 setup_description ();
271 } else if (property == AudioContentProperty::FADE_IN) {
274 check.insert (i->audio->fade_in().get());
277 if (check.size() == 1) {
279 ac.front()->audio->fade_in(),
280 ac.front()->active_video_frame_rate(_parent->film())
285 } else if (property == AudioContentProperty::FADE_OUT) {
288 check.insert (i->audio->fade_out().get());
291 if (check.size() == 1) {
293 ac.front()->audio->fade_out(),
294 ac.front()->active_video_frame_rate(_parent->film())
299 } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
300 setup_sensitivity ();
306 AudioPanel::gain_calculate_button_clicked ()
308 GainCalculatorDialog dialog(this);
309 auto const r = dialog.ShowModal();
310 auto change = dialog.db_change();
312 if (r == wxID_CANCEL || !change) {
316 auto old_peak_dB = peak ();
317 auto old_value = _gain->wrapped()->GetValue();
318 _gain->wrapped()->SetValue(old_value + *change);
320 /* This appears to be necessary, as the change is not signalled,
323 _gain->view_changed ();
325 auto peak_dB = peak ();
326 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
327 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."));
328 _gain->wrapped()->SetValue (old_value);
329 _gain->view_changed ();
335 AudioPanel::setup_description ()
337 auto ac = _parent->selected_audio ();
338 if (ac.size () != 1) {
339 checked_set (_description, wxT (""));
343 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
348 AudioPanel::mapping_changed (AudioMapping m)
350 auto c = _parent->selected_audio ();
352 c.front()->audio->set_mapping (m);
358 AudioPanel::content_selection_changed ()
360 auto sel = _parent->selected_audio ();
362 _gain->set_content (sel);
363 _delay->set_content (sel);
365 film_content_changed (AudioContentProperty::STREAMS);
366 film_content_changed (AudioContentProperty::GAIN);
367 film_content_changed (AudioContentProperty::FADE_IN);
368 film_content_changed (AudioContentProperty::FADE_OUT);
369 film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
370 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
372 setup_sensitivity ();
377 AudioPanel::setup_sensitivity ()
379 auto sel = _parent->selected_audio ();
381 shared_ptr<DCPContent> dcp;
382 if (sel.size() == 1) {
383 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
387 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
389 if (why_not.empty()) {
390 cannot = _("Cannot reference this DCP's audio.");
392 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
394 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
396 auto const ref = _reference->GetValue();
397 auto const single = sel.size() == 1;
399 auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
401 _gain->wrapped()->Enable (!ref);
402 _gain_calculate_button->Enable (!ref && single);
403 _show->Enable (single);
404 _peak->Enable (!ref && single);
405 _delay->wrapped()->Enable (!ref);
406 _mapping->Enable (!ref && single);
407 _description->Enable (!ref && single);
408 _fade_in->Enable (!_use_same_fades_as_video->GetValue());
409 _fade_out->Enable (!_use_same_fades_as_video->GetValue());
410 _use_same_fades_as_video->Enable (!ref && all_have_video);
415 AudioPanel::show_clicked ()
417 _audio_dialog.reset();
419 auto ac = _parent->selected_audio ();
420 if (ac.size() != 1) {
424 _audio_dialog.reset(this, _parent->film(), _parent->film_viewer(), ac.front());
425 _audio_dialog->Show ();
429 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
431 AudioPanel::peak () const
433 optional<float> peak_dB;
435 auto sel = _parent->selected_audio ();
436 if (sel.size() == 1) {
437 auto playlist = make_shared<Playlist>();
438 playlist->add (_parent->film(), sel.front());
440 /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
441 * the content selection is changed, so cache it.
443 auto const path = _parent->film()->audio_analysis_path(playlist);
444 auto cached = _peak_cache.find(path);
445 if (cached != _peak_cache.end()) {
446 peak_dB = cached->second;
448 auto analysis = make_shared<AudioAnalysis>(path);
449 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
450 _peak_cache[path] = *peak_dB;
462 AudioPanel::setup_peak ()
464 auto sel = _parent->selected_audio ();
466 auto peak_dB = peak ();
467 if (sel.size() != 1) {
468 _peak->SetLabel (wxT(""));
472 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
474 _peak->SetLabel (_("Peak: unknown"));
478 static auto normal = _peak->GetForegroundColour ();
480 if (peak_dB && *peak_dB > -0.5) {
481 _peak->SetForegroundColour (wxColour (255, 0, 0));
482 } else if (peak_dB && *peak_dB > -3) {
483 _peak->SetForegroundColour (wxColour (186, 120, 0));
485 _peak->SetForegroundColour (normal);
491 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
493 if (old_active && *old_active == "analyse_audio") {
495 _mapping->Enable (true);
496 } else if (new_active && *new_active == "analyse_audio") {
497 _mapping->Enable (false);
503 AudioPanel::reference_clicked ()
505 auto c = _parent->selected ();
510 auto d = dynamic_pointer_cast<DCPContent>(c.front());
515 d->set_reference_audio (_reference->GetValue ());
520 AudioPanel::set_film (shared_ptr<Film>)
522 /* We are changing film, so destroy any audio dialog for the old one */
523 _audio_dialog.reset();
528 AudioPanel::fade_in_changed ()
530 auto const hmsf = _fade_in->get();
531 for (auto i: _parent->selected_audio()) {
532 auto const vfr = i->active_video_frame_rate(_parent->film());
533 i->audio->set_fade_in (dcpomatic::ContentTime(hmsf, vfr));
539 AudioPanel::fade_out_changed ()
541 auto const hmsf = _fade_out->get();
542 for (auto i: _parent->selected_audio()) {
543 auto const vfr = i->active_video_frame_rate (_parent->film());
544 i->audio->set_fade_out (dcpomatic::ContentTime(hmsf, vfr));
550 AudioPanel::use_same_fades_as_video_changed ()
552 for (auto content: _parent->selected_audio()) {
553 content->audio->set_use_same_fades_as_video(_use_same_fades_as_video->GetValue());