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 _show = new Button (this, _("Show graph of audio levels..."));
75 _peak = new StaticText (this, wxT (""));
77 _gain_label = create_label (this, _("Gain"), true);
78 _gain = new ContentSpinCtrlDouble<AudioContent> (
80 new wxSpinCtrlDouble (this),
81 AudioContentProperty::GAIN,
83 boost::mem_fn (&AudioContent::gain),
84 boost::mem_fn (&AudioContent::set_gain)
87 _gain_db_label = create_label (this, _("dB"), false);
88 _gain_calculate_button = new Button (this, _("Calculate..."));
90 _delay_label = create_label (this, _("Delay"), true);
91 _delay = new ContentSpinCtrl<AudioContent> (
93 new wxSpinCtrl (this),
94 AudioContentProperty::DELAY,
96 boost::mem_fn (&AudioContent::delay),
97 boost::mem_fn (&AudioContent::set_delay)
100 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
101 _delay_ms_label = create_label (this, _("ms"), false);
103 _fade_in_label = create_label (this, _("Fade in"), true);
104 _fade_in = new Timecode<ContentTime> (this);
106 _fade_out_label = create_label (this, _("Fade out"), true);
107 _fade_out = new Timecode<ContentTime> (this);
109 _use_same_fades_as_video = new CheckBox(this, _("Use same fades as video"));
111 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
112 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
114 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
115 _sizer->Add (_description, 0, wxALL, 12);
116 auto font = _description->GetFont();
117 font.SetStyle(wxFONTSTYLE_ITALIC);
118 font.SetPointSize(font.GetPointSize() - 1);
119 _description->SetFont (font);
121 _gain->wrapped()->SetRange (-60, 60);
122 _gain->wrapped()->SetDigits (1);
123 _gain->wrapped()->SetIncrement (0.5);
124 _delay->wrapped()->SetRange (-1000, 1000);
126 content_selection_changed ();
127 film_changed(FilmProperty::AUDIO_CHANNELS);
128 film_changed(FilmProperty::VIDEO_FRAME_RATE);
129 film_changed(FilmProperty::REEL_TYPE);
131 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
132 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
134 _fade_in->Changed.connect (boost::bind(&AudioPanel::fade_in_changed, this));
135 _fade_out->Changed.connect (boost::bind(&AudioPanel::fade_out_changed, this));
136 _use_same_fades_as_video->bind(&AudioPanel::use_same_fades_as_video_changed, this);
138 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
139 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
148 AudioPanel::add_to_grid ()
152 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
153 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
156 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
158 auto s = new wxBoxSizer (wxHORIZONTAL);
159 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
160 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
161 _grid->Add (s, wxGBPosition(r, 1));
164 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
167 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
168 auto s = new wxBoxSizer (wxHORIZONTAL);
169 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
170 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
171 _grid->Add (s, wxGBPosition(r, 1));
174 add_label_to_sizer (_grid, _fade_in_label, true, wxGBPosition(r, 0));
175 _grid->Add (_fade_in, wxGBPosition(r, 1), wxGBSpan(1, 3));
178 add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition(r, 0));
179 _grid->Add (_fade_out, wxGBPosition(r, 1), wxGBSpan(1, 3));
182 _grid->Add (_use_same_fades_as_video, wxGBPosition(r, 0), wxGBSpan(1, 4));
188 AudioPanel::film_changed (FilmProperty property)
190 if (!_parent->film()) {
195 case FilmProperty::AUDIO_CHANNELS:
196 case FilmProperty::AUDIO_PROCESSOR:
197 _mapping->set_output_channels (_parent->film()->audio_output_names ());
201 case FilmProperty::VIDEO_FRAME_RATE:
202 setup_description ();
204 case FilmProperty::REEL_TYPE:
205 case FilmProperty::INTEROP:
206 setup_sensitivity ();
215 AudioPanel::film_content_changed (int property)
217 auto ac = _parent->selected_audio ();
218 if (property == AudioContentProperty::STREAMS) {
219 if (ac.size() == 1) {
220 _mapping->set (ac.front()->audio->mapping());
221 _mapping->set_input_channels (ac.front()->audio->channel_names ());
223 vector<AudioMappingView::Group> groups;
225 for (auto i: ac.front()->audio->streams()) {
226 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
231 name += " (" + f->codec_name.get() + ")";
234 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
237 _mapping->set_input_groups (groups);
240 _mapping->set (AudioMapping ());
242 setup_description ();
245 } else if (property == AudioContentProperty::GAIN) {
246 /* This is a bit aggressive but probably not so bad */
249 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
250 setup_description ();
251 } else if (property == AudioContentProperty::FADE_IN) {
254 check.insert (i->audio->fade_in().get());
257 if (check.size() == 1) {
259 ac.front()->audio->fade_in(),
260 ac.front()->active_video_frame_rate(_parent->film())
265 } else if (property == AudioContentProperty::FADE_OUT) {
268 check.insert (i->audio->fade_out().get());
271 if (check.size() == 1) {
273 ac.front()->audio->fade_out(),
274 ac.front()->active_video_frame_rate(_parent->film())
279 } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
280 setup_sensitivity ();
286 AudioPanel::gain_calculate_button_clicked ()
288 GainCalculatorDialog dialog(this);
289 auto const r = dialog.ShowModal();
290 auto change = dialog.db_change();
292 if (r == wxID_CANCEL || !change) {
296 auto old_peak_dB = peak ();
297 auto old_value = _gain->wrapped()->GetValue();
298 _gain->wrapped()->SetValue(old_value + *change);
300 /* This appears to be necessary, as the change is not signalled,
303 _gain->view_changed ();
305 auto peak_dB = peak ();
306 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
307 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."));
308 _gain->wrapped()->SetValue (old_value);
309 _gain->view_changed ();
315 AudioPanel::setup_description ()
317 auto ac = _parent->selected_audio ();
318 if (ac.size () != 1) {
319 checked_set (_description, wxT (""));
323 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
328 AudioPanel::mapping_changed (AudioMapping m)
330 auto c = _parent->selected_audio ();
332 c.front()->audio->set_mapping (m);
338 AudioPanel::content_selection_changed ()
340 auto sel = _parent->selected_audio ();
342 _gain->set_content (sel);
343 _delay->set_content (sel);
345 film_content_changed (AudioContentProperty::STREAMS);
346 film_content_changed (AudioContentProperty::GAIN);
347 film_content_changed (AudioContentProperty::FADE_IN);
348 film_content_changed (AudioContentProperty::FADE_OUT);
349 film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
350 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
352 setup_sensitivity ();
357 AudioPanel::setup_sensitivity ()
359 auto sel = _parent->selected_audio ();
361 shared_ptr<DCPContent> dcp;
362 if (sel.size() == 1) {
363 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
366 auto const ref = dcp && dcp->reference_audio();
367 auto const single = sel.size() == 1;
368 auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
370 _gain->wrapped()->Enable (!ref);
371 _gain_calculate_button->Enable (!ref && single);
372 _show->Enable (single);
373 _peak->Enable (!ref && single);
374 _delay->wrapped()->Enable (!ref);
375 _mapping->Enable (!ref && single);
376 _description->Enable (!ref && single);
377 _fade_in->Enable (!_use_same_fades_as_video->GetValue());
378 _fade_out->Enable (!_use_same_fades_as_video->GetValue());
379 _use_same_fades_as_video->Enable (!ref && all_have_video);
384 AudioPanel::show_clicked ()
386 _audio_dialog.reset();
388 auto ac = _parent->selected_audio ();
389 if (ac.size() != 1) {
393 _audio_dialog.reset(this, _parent->film(), _parent->film_viewer(), ac.front());
394 _audio_dialog->Show ();
398 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
400 AudioPanel::peak () const
402 optional<float> peak_dB;
404 auto sel = _parent->selected_audio ();
405 if (sel.size() == 1) {
406 auto playlist = make_shared<Playlist>();
407 playlist->add (_parent->film(), sel.front());
409 /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
410 * the content selection is changed, so cache it.
412 auto const path = _parent->film()->audio_analysis_path(playlist);
413 auto cached = _peak_cache.find(path);
414 if (cached != _peak_cache.end()) {
415 peak_dB = cached->second;
417 auto analysis = make_shared<AudioAnalysis>(path);
418 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
419 _peak_cache[path] = *peak_dB;
431 AudioPanel::setup_peak ()
433 auto sel = _parent->selected_audio ();
435 auto peak_dB = peak ();
436 if (sel.size() != 1) {
437 _peak->SetLabel (wxT(""));
441 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
443 _peak->SetLabel (_("Peak: unknown"));
447 static auto normal = _peak->GetForegroundColour ();
449 if (peak_dB && *peak_dB > -0.5) {
450 _peak->SetForegroundColour (wxColour (255, 0, 0));
451 } else if (peak_dB && *peak_dB > -3) {
452 _peak->SetForegroundColour (wxColour (186, 120, 0));
454 _peak->SetForegroundColour (normal);
460 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
462 if (old_active && *old_active == "analyse_audio") {
464 _mapping->Enable (true);
465 } else if (new_active && *new_active == "analyse_audio") {
466 _mapping->Enable (false);
472 AudioPanel::set_film (shared_ptr<Film>)
474 /* We are changing film, so destroy any audio dialog for the old one */
475 _audio_dialog.reset();
480 AudioPanel::fade_in_changed ()
482 auto const hmsf = _fade_in->get();
483 for (auto i: _parent->selected_audio()) {
484 auto const vfr = i->active_video_frame_rate(_parent->film());
485 i->audio->set_fade_in (dcpomatic::ContentTime(hmsf, vfr));
491 AudioPanel::fade_out_changed ()
493 auto const hmsf = _fade_out->get();
494 for (auto i: _parent->selected_audio()) {
495 auto const vfr = i->active_video_frame_rate (_parent->film());
496 i->audio->set_fade_out (dcpomatic::ContentTime(hmsf, vfr));
502 AudioPanel::use_same_fades_as_video_changed ()
504 for (auto content: _parent->selected_audio()) {
505 content->audio->set_use_same_fades_as_video(_use_same_fades_as_video->GetValue());