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 <wx/spinctrl.h>
47 using std::make_shared;
48 using std::dynamic_pointer_cast;
49 using std::shared_ptr;
50 using boost::optional;
51 #if BOOST_VERSION >= 106100
52 using namespace boost::placeholders;
56 AudioPanel::AudioPanel (ContentPanel* p)
57 : ContentSubPanel (p, _("Audio"))
67 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
68 _reference_note = new StaticText (this, wxT(""));
69 _reference_note->Wrap (200);
70 auto font = _reference_note->GetFont();
71 font.SetStyle(wxFONTSTYLE_ITALIC);
72 font.SetPointSize(font.GetPointSize() - 1);
73 _reference_note->SetFont(font);
75 _show = new Button (this, _("Show graph of audio levels..."));
76 _peak = new StaticText (this, wxT (""));
78 _gain_label = create_label (this, _("Gain"), true);
79 _gain = new ContentSpinCtrlDouble<AudioContent> (
81 new wxSpinCtrlDouble (this),
82 AudioContentProperty::GAIN,
84 boost::mem_fn (&AudioContent::gain),
85 boost::mem_fn (&AudioContent::set_gain)
88 _gain_db_label = create_label (this, _("dB"), false);
89 _gain_calculate_button = new Button (this, _("Calculate..."));
91 _delay_label = create_label (this, _("Delay"), true);
92 _delay = new ContentSpinCtrl<AudioContent> (
94 new wxSpinCtrl (this),
95 AudioContentProperty::DELAY,
97 boost::mem_fn (&AudioContent::delay),
98 boost::mem_fn (&AudioContent::set_delay)
101 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
102 _delay_ms_label = create_label (this, _("ms"), false);
104 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
105 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
107 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
108 _sizer->Add (_description, 0, wxALL, 12);
109 _description->SetFont (font);
111 _gain->wrapped()->SetRange (-60, 60);
112 _gain->wrapped()->SetDigits (1);
113 _gain->wrapped()->SetIncrement (0.5);
114 _delay->wrapped()->SetRange (-1000, 1000);
116 content_selection_changed ();
117 film_changed (Film::Property::AUDIO_CHANNELS);
118 film_changed (Film::Property::VIDEO_FRAME_RATE);
119 film_changed (Film::Property::REEL_TYPE);
121 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
122 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
123 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
125 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
126 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
134 AudioPanel::add_to_grid ()
138 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
139 reference_sizer->Add (_reference, 0);
140 reference_sizer->Add (_reference_note, 0);
141 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
144 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
145 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
148 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
150 auto s = new wxBoxSizer (wxHORIZONTAL);
151 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
152 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
153 _grid->Add (s, wxGBPosition(r, 1));
156 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
159 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
160 auto s = new wxBoxSizer (wxHORIZONTAL);
161 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
162 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
163 _grid->Add (s, wxGBPosition(r, 1));
167 AudioPanel::~AudioPanel ()
170 _audio_dialog->Destroy ();
171 _audio_dialog = nullptr;
176 AudioPanel::film_changed (Film::Property property)
178 if (!_parent->film()) {
183 case Film::Property::AUDIO_CHANNELS:
184 case Film::Property::AUDIO_PROCESSOR:
185 _mapping->set_output_channels (_parent->film()->audio_output_names ());
188 case Film::Property::VIDEO_FRAME_RATE:
189 setup_description ();
191 case Film::Property::REEL_TYPE:
192 case Film::Property::INTEROP:
193 setup_sensitivity ();
201 AudioPanel::film_content_changed (int property)
203 auto ac = _parent->selected_audio ();
204 if (property == AudioContentProperty::STREAMS) {
205 if (ac.size() == 1) {
206 _mapping->set (ac.front()->audio->mapping());
207 _mapping->set_input_channels (ac.front()->audio->channel_names ());
209 vector<AudioMappingView::Group> groups;
211 for (auto i: ac.front()->audio->streams()) {
212 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
217 name += " (" + f->codec_name.get() + ")";
220 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
223 _mapping->set_input_groups (groups);
226 _mapping->set (AudioMapping ());
228 setup_description ();
231 } else if (property == AudioContentProperty::GAIN) {
233 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
234 if (ac.size() == 1) {
235 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
236 checked_set (_reference, dcp ? dcp->reference_audio () : false);
238 checked_set (_reference, false);
241 setup_sensitivity ();
242 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
243 setup_description ();
248 AudioPanel::gain_calculate_button_clicked ()
250 auto d = new GainCalculatorDialog (this);
251 auto const r = d->ShowModal ();
252 auto c = d->db_change();
254 if (r == wxID_CANCEL || !c) {
259 auto old_peak_dB = peak ();
260 auto old_value = _gain->wrapped()->GetValue();
261 _gain->wrapped()->SetValue(old_value + *c);
263 /* This appears to be necessary, as the change is not signalled,
266 _gain->view_changed ();
268 auto peak_dB = peak ();
269 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
270 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."));
271 _gain->wrapped()->SetValue (old_value);
272 _gain->view_changed ();
279 AudioPanel::setup_description ()
281 auto ac = _parent->selected_audio ();
282 if (ac.size () != 1) {
283 checked_set (_description, wxT (""));
287 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
291 AudioPanel::mapping_changed (AudioMapping m)
293 auto c = _parent->selected_audio ();
295 c.front()->audio->set_mapping (m);
300 AudioPanel::content_selection_changed ()
302 auto sel = _parent->selected_audio ();
304 _gain->set_content (sel);
305 _delay->set_content (sel);
307 film_content_changed (AudioContentProperty::STREAMS);
308 film_content_changed (AudioContentProperty::GAIN);
309 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
311 setup_sensitivity ();
315 AudioPanel::setup_sensitivity ()
317 auto sel = _parent->selected_audio ();
319 shared_ptr<DCPContent> dcp;
320 if (sel.size() == 1) {
321 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
325 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
327 if (why_not.empty()) {
328 cannot = _("Cannot reference this DCP's audio.");
330 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
332 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
334 auto const ref = _reference->GetValue();
335 auto const single = sel.size() == 1;
337 _gain->wrapped()->Enable (!ref);
338 _gain_calculate_button->Enable (!ref && single);
339 _show->Enable (single);
340 _peak->Enable (!ref && single);
341 _delay->wrapped()->Enable (!ref);
342 _mapping->Enable (!ref && single);
343 _description->Enable (!ref && single);
347 AudioPanel::show_clicked ()
350 _audio_dialog->Destroy ();
351 _audio_dialog = nullptr;
354 auto ac = _parent->selected_audio ();
355 if (ac.size() != 1) {
359 _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
360 _audio_dialog->Show ();
363 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
365 AudioPanel::peak () const
367 optional<float> peak_dB;
369 auto sel = _parent->selected_audio ();
370 if (sel.size() == 1) {
371 auto playlist = make_shared<Playlist>();
372 playlist->add (_parent->film(), sel.front());
374 auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
375 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
385 AudioPanel::setup_peak ()
387 auto sel = _parent->selected_audio ();
389 auto peak_dB = peak ();
390 if (sel.size() != 1) {
391 _peak->SetLabel (wxT(""));
395 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
397 _peak->SetLabel (_("Peak: unknown"));
401 static auto normal = _peak->GetForegroundColour ();
403 if (peak_dB && *peak_dB > -0.5) {
404 _peak->SetForegroundColour (wxColour (255, 0, 0));
405 } else if (peak_dB && *peak_dB > -3) {
406 _peak->SetForegroundColour (wxColour (186, 120, 0));
408 _peak->SetForegroundColour (normal);
413 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
415 if (old_active && *old_active == "analyse_audio") {
417 _mapping->Enable (true);
418 } else if (new_active && *new_active == "analyse_audio") {
419 _mapping->Enable (false);
424 AudioPanel::reference_clicked ()
426 auto c = _parent->selected ();
431 auto d = dynamic_pointer_cast<DCPContent>(c.front());
436 d->set_reference_audio (_reference->GetValue ());
440 AudioPanel::set_film (shared_ptr<Film>)
442 /* We are changing film, so destroy any audio dialog for the old one */
444 _audio_dialog->Destroy ();
445 _audio_dialog = nullptr;