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/config.h"
32 #include "lib/ffmpeg_audio_stream.h"
33 #include "lib/ffmpeg_content.h"
34 #include "lib/cinema_sound_processor.h"
35 #include "lib/job_manager.h"
36 #include "lib/dcp_content.h"
37 #include "lib/audio_content.h"
38 #include "lib/maths_util.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"))
68 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
69 _reference_note = new StaticText (this, wxT(""));
70 _reference_note->Wrap (200);
71 auto font = _reference_note->GetFont();
72 font.SetStyle(wxFONTSTYLE_ITALIC);
73 font.SetPointSize(font.GetPointSize() - 1);
74 _reference_note->SetFont(font);
76 _show = new Button (this, _("Show graph of audio levels..."));
77 _peak = new StaticText (this, wxT (""));
79 _gain_label = create_label (this, _("Gain"), true);
80 _gain = new ContentSpinCtrlDouble<AudioContent> (
82 new wxSpinCtrlDouble (this),
83 AudioContentProperty::GAIN,
85 boost::mem_fn (&AudioContent::gain),
86 boost::mem_fn (&AudioContent::set_gain)
89 _gain_db_label = create_label (this, _("dB"), false);
90 _gain_calculate_button = new Button (this, _("Calculate..."));
92 _delay_label = create_label (this, _("Delay"), true);
93 _delay = new ContentSpinCtrl<AudioContent> (
95 new wxSpinCtrl (this),
96 AudioContentProperty::DELAY,
98 boost::mem_fn (&AudioContent::delay),
99 boost::mem_fn (&AudioContent::set_delay)
102 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
103 _delay_ms_label = create_label (this, _("ms"), false);
105 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
106 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
108 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
109 _sizer->Add (_description, 0, wxALL, 12);
110 _description->SetFont (font);
112 _gain->wrapped()->SetRange (-60, 60);
113 _gain->wrapped()->SetDigits (1);
114 _gain->wrapped()->SetIncrement (0.5);
115 _delay->wrapped()->SetRange (-1000, 1000);
117 content_selection_changed ();
118 film_changed (Film::Property::AUDIO_CHANNELS);
119 film_changed (Film::Property::VIDEO_FRAME_RATE);
120 film_changed (Film::Property::REEL_TYPE);
122 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
123 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
124 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
126 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
127 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
135 AudioPanel::add_to_grid ()
139 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
140 reference_sizer->Add (_reference, 0);
141 reference_sizer->Add (_reference_note, 0);
142 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
145 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
146 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
149 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
151 auto s = new wxBoxSizer (wxHORIZONTAL);
152 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
153 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
154 _grid->Add (s, wxGBPosition(r, 1));
157 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
160 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
161 auto s = new wxBoxSizer (wxHORIZONTAL);
162 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
163 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
164 _grid->Add (s, wxGBPosition(r, 1));
168 AudioPanel::~AudioPanel ()
171 _audio_dialog->Destroy ();
172 _audio_dialog = nullptr;
177 AudioPanel::film_changed (Film::Property property)
179 if (!_parent->film()) {
184 case Film::Property::AUDIO_CHANNELS:
185 case Film::Property::AUDIO_PROCESSOR:
186 _mapping->set_output_channels (_parent->film()->audio_output_names ());
189 case Film::Property::VIDEO_FRAME_RATE:
190 setup_description ();
192 case Film::Property::REEL_TYPE:
193 case Film::Property::INTEROP:
194 setup_sensitivity ();
202 AudioPanel::film_content_changed (int property)
204 auto ac = _parent->selected_audio ();
205 if (property == AudioContentProperty::STREAMS) {
206 if (ac.size() == 1) {
207 _mapping->set (ac.front()->audio->mapping());
208 _mapping->set_input_channels (ac.front()->audio->channel_names ());
210 vector<AudioMappingView::Group> groups;
212 for (auto i: ac.front()->audio->streams()) {
213 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
218 name += " (" + f->codec_name.get() + ")";
221 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
224 _mapping->set_input_groups (groups);
227 _mapping->set (AudioMapping ());
229 setup_description ();
232 } else if (property == AudioContentProperty::GAIN) {
234 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
235 if (ac.size() == 1) {
236 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
237 checked_set (_reference, dcp ? dcp->reference_audio () : false);
239 checked_set (_reference, false);
242 setup_sensitivity ();
243 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
244 setup_description ();
249 AudioPanel::gain_calculate_button_clicked ()
251 auto d = new GainCalculatorDialog (this);
252 auto const r = d->ShowModal ();
253 auto c = d->db_change();
255 if (r == wxID_CANCEL || !c) {
260 auto old_peak_dB = peak ();
261 auto old_value = _gain->wrapped()->GetValue();
262 _gain->wrapped()->SetValue(old_value + *c);
264 /* This appears to be necessary, as the change is not signalled,
267 _gain->view_changed ();
269 auto peak_dB = peak ();
270 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
271 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."));
272 _gain->wrapped()->SetValue (old_value);
273 _gain->view_changed ();
280 AudioPanel::setup_description ()
282 auto ac = _parent->selected_audio ();
283 if (ac.size () != 1) {
284 checked_set (_description, wxT (""));
288 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
292 AudioPanel::mapping_changed (AudioMapping m)
294 auto c = _parent->selected_audio ();
296 c.front()->audio->set_mapping (m);
301 AudioPanel::content_selection_changed ()
303 auto sel = _parent->selected_audio ();
305 _gain->set_content (sel);
306 _delay->set_content (sel);
308 film_content_changed (AudioContentProperty::STREAMS);
309 film_content_changed (AudioContentProperty::GAIN);
310 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
312 setup_sensitivity ();
316 AudioPanel::setup_sensitivity ()
318 auto sel = _parent->selected_audio ();
320 shared_ptr<DCPContent> dcp;
321 if (sel.size() == 1) {
322 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
326 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
328 if (why_not.empty()) {
329 cannot = _("Cannot reference this DCP's audio.");
331 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
333 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
335 auto const ref = _reference->GetValue();
336 auto const single = sel.size() == 1;
338 _gain->wrapped()->Enable (!ref);
339 _gain_calculate_button->Enable (!ref && single);
340 _show->Enable (single);
341 _peak->Enable (!ref && single);
342 _delay->wrapped()->Enable (!ref);
343 _mapping->Enable (!ref && single);
344 _description->Enable (!ref && single);
348 AudioPanel::show_clicked ()
351 _audio_dialog->Destroy ();
352 _audio_dialog = nullptr;
355 auto ac = _parent->selected_audio ();
356 if (ac.size() != 1) {
360 _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
361 _audio_dialog->Show ();
364 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
366 AudioPanel::peak () const
368 optional<float> peak_dB;
370 auto sel = _parent->selected_audio ();
371 if (sel.size() == 1) {
372 auto playlist = make_shared<Playlist>();
373 playlist->add (_parent->film(), sel.front());
375 auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
376 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
386 AudioPanel::setup_peak ()
388 auto sel = _parent->selected_audio ();
390 auto peak_dB = peak ();
391 if (sel.size() != 1) {
392 _peak->SetLabel (wxT(""));
396 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
398 _peak->SetLabel (_("Peak: unknown"));
402 static auto normal = _peak->GetForegroundColour ();
404 if (peak_dB && *peak_dB > -0.5) {
405 _peak->SetForegroundColour (wxColour (255, 0, 0));
406 } else if (peak_dB && *peak_dB > -3) {
407 _peak->SetForegroundColour (wxColour (186, 120, 0));
409 _peak->SetForegroundColour (normal);
414 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
416 if (old_active && *old_active == "analyse_audio") {
418 _mapping->Enable (true);
419 } else if (new_active && *new_active == "analyse_audio") {
420 _mapping->Enable (false);
425 AudioPanel::reference_clicked ()
427 auto c = _parent->selected ();
432 auto d = dynamic_pointer_cast<DCPContent>(c.front());
437 d->set_reference_audio (_reference->GetValue ());
441 AudioPanel::set_film (shared_ptr<Film>)
443 /* We are changing film, so destroy any audio dialog for the old one */
445 _audio_dialog->Destroy ();
446 _audio_dialog = nullptr;