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"))
60 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
61 _reference_note = new StaticText (this, wxT(""));
62 _reference_note->Wrap (200);
63 auto font = _reference_note->GetFont();
64 font.SetStyle(wxFONTSTYLE_ITALIC);
65 font.SetPointSize(font.GetPointSize() - 1);
66 _reference_note->SetFont(font);
68 _show = new Button (this, _("Show graph of audio levels..."));
69 _peak = new StaticText (this, wxT (""));
71 _gain_label = create_label (this, _("Gain"), true);
72 _gain = new ContentSpinCtrlDouble<AudioContent> (
74 new wxSpinCtrlDouble (this),
75 AudioContentProperty::GAIN,
77 boost::mem_fn (&AudioContent::gain),
78 boost::mem_fn (&AudioContent::set_gain)
81 _gain_db_label = create_label (this, _("dB"), false);
82 _gain_calculate_button = new Button (this, _("Calculate..."));
84 _delay_label = create_label (this, _("Delay"), true);
85 _delay = new ContentSpinCtrl<AudioContent> (
87 new wxSpinCtrl (this),
88 AudioContentProperty::DELAY,
90 boost::mem_fn (&AudioContent::delay),
91 boost::mem_fn (&AudioContent::set_delay)
94 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
95 _delay_ms_label = create_label (this, _("ms"), false);
97 _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
98 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
100 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
101 _sizer->Add (_description, 0, wxALL, 12);
102 _description->SetFont (font);
104 _gain->wrapped()->SetRange (-60, 60);
105 _gain->wrapped()->SetDigits (1);
106 _gain->wrapped()->SetIncrement (0.5);
107 _delay->wrapped()->SetRange (-1000, 1000);
109 content_selection_changed ();
110 film_changed (Film::Property::AUDIO_CHANNELS);
111 film_changed (Film::Property::VIDEO_FRAME_RATE);
112 film_changed (Film::Property::REEL_TYPE);
114 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
115 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
116 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
118 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
119 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
125 AudioPanel::add_to_grid ()
129 auto reference_sizer = new wxBoxSizer (wxVERTICAL);
130 reference_sizer->Add (_reference, 0);
131 reference_sizer->Add (_reference_note, 0);
132 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
135 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
136 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
139 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
141 auto s = new wxBoxSizer (wxHORIZONTAL);
142 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
143 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
144 _grid->Add (s, wxGBPosition(r, 1));
147 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
150 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
151 auto s = new wxBoxSizer (wxHORIZONTAL);
152 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxRIGHT, DCPOMATIC_SIZER_GAP);
153 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
154 _grid->Add (s, wxGBPosition(r, 1));
158 AudioPanel::~AudioPanel ()
161 _audio_dialog->Destroy ();
162 _audio_dialog = nullptr;
167 AudioPanel::film_changed (Film::Property property)
169 if (!_parent->film()) {
174 case Film::Property::AUDIO_CHANNELS:
175 case Film::Property::AUDIO_PROCESSOR:
176 _mapping->set_output_channels (_parent->film()->audio_output_names ());
179 case Film::Property::VIDEO_FRAME_RATE:
180 setup_description ();
182 case Film::Property::REEL_TYPE:
183 case Film::Property::INTEROP:
184 setup_sensitivity ();
192 AudioPanel::film_content_changed (int property)
194 auto ac = _parent->selected_audio ();
195 if (property == AudioContentProperty::STREAMS) {
196 if (ac.size() == 1) {
197 _mapping->set (ac.front()->audio->mapping());
198 _mapping->set_input_channels (ac.front()->audio->channel_names ());
200 vector<AudioMappingView::Group> groups;
202 for (auto i: ac.front()->audio->streams()) {
203 auto f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
208 name += " (" + f->codec_name.get() + ")";
211 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
214 _mapping->set_input_groups (groups);
217 _mapping->set (AudioMapping ());
219 setup_description ();
222 } else if (property == AudioContentProperty::GAIN) {
224 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
225 if (ac.size() == 1) {
226 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
227 checked_set (_reference, dcp ? dcp->reference_audio () : false);
229 checked_set (_reference, false);
232 setup_sensitivity ();
233 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
234 setup_description ();
239 AudioPanel::gain_calculate_button_clicked ()
241 auto d = new GainCalculatorDialog (this);
242 auto const r = d->ShowModal ();
243 auto c = d->db_change();
245 if (r == wxID_CANCEL || !c) {
250 auto old_peak_dB = peak ();
251 auto old_value = _gain->wrapped()->GetValue();
252 _gain->wrapped()->SetValue(old_value + *c);
254 /* This appears to be necessary, as the change is not signalled,
257 _gain->view_changed ();
259 auto peak_dB = peak ();
260 if (old_peak_dB && *old_peak_dB < -0.5 && peak_dB && *peak_dB > -0.5) {
261 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."));
262 _gain->wrapped()->SetValue (old_value);
263 _gain->view_changed ();
270 AudioPanel::setup_description ()
272 auto ac = _parent->selected_audio ();
273 if (ac.size () != 1) {
274 checked_set (_description, wxT (""));
278 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
282 AudioPanel::mapping_changed (AudioMapping m)
284 auto c = _parent->selected_audio ();
286 c.front()->audio->set_mapping (m);
291 AudioPanel::content_selection_changed ()
293 auto sel = _parent->selected_audio ();
295 _gain->set_content (sel);
296 _delay->set_content (sel);
298 film_content_changed (AudioContentProperty::STREAMS);
299 film_content_changed (AudioContentProperty::GAIN);
300 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
302 setup_sensitivity ();
306 AudioPanel::setup_sensitivity ()
308 auto sel = _parent->selected_audio ();
310 shared_ptr<DCPContent> dcp;
311 if (sel.size() == 1) {
312 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
316 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
318 if (why_not.empty()) {
319 cannot = _("Cannot reference this DCP's audio.");
321 cannot = _("Cannot reference this DCP's audio: ") + std_to_wx(why_not);
323 setup_refer_button (_reference, _reference_note, dcp, can_reference, cannot);
325 auto const ref = _reference->GetValue();
326 auto const single = sel.size() == 1;
328 _gain->wrapped()->Enable (!ref);
329 _gain_calculate_button->Enable (!ref && single);
330 _show->Enable (single);
331 _peak->Enable (!ref && single);
332 _delay->wrapped()->Enable (!ref);
333 _mapping->Enable (!ref && single);
334 _description->Enable (!ref && single);
338 AudioPanel::show_clicked ()
341 _audio_dialog->Destroy ();
342 _audio_dialog = nullptr;
345 auto ac = _parent->selected_audio ();
346 if (ac.size() != 1) {
350 _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
351 _audio_dialog->Show ();
354 /** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
356 AudioPanel::peak () const
358 optional<float> peak_dB;
360 auto sel = _parent->selected_audio ();
361 if (sel.size() == 1) {
362 auto playlist = make_shared<Playlist>();
363 playlist->add (_parent->film(), sel.front());
365 auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
366 peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
376 AudioPanel::setup_peak ()
378 auto sel = _parent->selected_audio ();
380 auto peak_dB = peak ();
381 if (sel.size() != 1) {
382 _peak->SetLabel (wxT(""));
386 _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
388 _peak->SetLabel (_("Peak: unknown"));
392 static auto normal = _peak->GetForegroundColour ();
394 if (peak_dB && *peak_dB > -0.5) {
395 _peak->SetForegroundColour (wxColour (255, 0, 0));
396 } else if (peak_dB && *peak_dB > -3) {
397 _peak->SetForegroundColour (wxColour (186, 120, 0));
399 _peak->SetForegroundColour (normal);
404 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
406 if (old_active && *old_active == "analyse_audio") {
408 _mapping->Enable (true);
409 } else if (new_active && *new_active == "analyse_audio") {
410 _mapping->Enable (false);
415 AudioPanel::reference_clicked ()
417 auto c = _parent->selected ();
422 auto d = dynamic_pointer_cast<DCPContent>(c.front());
427 d->set_reference_audio (_reference->GetValue ());
431 AudioPanel::set_film (shared_ptr<Film>)
433 /* We are changing film, so destroy any audio dialog for the old one */
435 _audio_dialog->Destroy ();
436 _audio_dialog = nullptr;