2 Copyright (C) 2012-2018 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/>.
21 #include "audio_panel.h"
22 #include "audio_mapping_view.h"
24 #include "gain_calculator_dialog.h"
25 #include "content_panel.h"
26 #include "audio_dialog.h"
27 #include "static_text.h"
28 #include "check_box.h"
29 #include "lib/config.h"
30 #include "lib/ffmpeg_audio_stream.h"
31 #include "lib/ffmpeg_content.h"
32 #include "lib/cinema_sound_processor.h"
33 #include "lib/job_manager.h"
34 #include "lib/dcp_content.h"
35 #include "lib/audio_content.h"
36 #include <wx/spinctrl.h>
37 #include <boost/foreach.hpp>
45 using boost::dynamic_pointer_cast;
46 using boost::shared_ptr;
47 using boost::optional;
49 AudioPanel::AudioPanel (ContentPanel* p)
50 : ContentSubPanel (p, _("Audio"))
53 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
54 _reference_note = new StaticText (this, wxT(""));
55 _reference_note->Wrap (200);
56 wxFont font = _reference_note->GetFont();
57 font.SetStyle(wxFONTSTYLE_ITALIC);
58 font.SetPointSize(font.GetPointSize() - 1);
59 _reference_note->SetFont(font);
61 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
62 _peak = new StaticText (this, wxT (""));
64 _gain_label = create_label (this, _("Gain"), true);
65 _gain = new ContentSpinCtrlDouble<AudioContent> (
67 new wxSpinCtrlDouble (this),
68 AudioContentProperty::GAIN,
70 boost::mem_fn (&AudioContent::gain),
71 boost::mem_fn (&AudioContent::set_gain)
74 _gain_db_label = create_label (this, _("dB"), false);
75 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
77 _delay_label = create_label (this, _("Delay"), true);
78 _delay = new ContentSpinCtrl<AudioContent> (
80 new wxSpinCtrl (this),
81 AudioContentProperty::DELAY,
83 boost::mem_fn (&AudioContent::delay),
84 boost::mem_fn (&AudioContent::set_delay)
87 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
88 _delay_ms_label = create_label (this, _("ms"), false);
90 _mapping = new AudioMappingView (this);
91 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
93 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
94 _sizer->Add (_description, 0, wxALL, 12);
95 _description->SetFont (font);
97 _gain->wrapped()->SetRange (-60, 60);
98 _gain->wrapped()->SetDigits (1);
99 _gain->wrapped()->SetIncrement (0.5);
100 _delay->wrapped()->SetRange (-1000, 1000);
102 content_selection_changed ();
103 film_changed (Film::AUDIO_CHANNELS);
104 film_changed (Film::VIDEO_FRAME_RATE);
105 film_changed (Film::REEL_TYPE);
107 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
108 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
109 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
111 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
112 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
118 AudioPanel::add_to_grid ()
120 Config::Interface const interface = Config::instance()->interface_complexity();
124 _reference->Show (interface == Config::INTERFACE_FULL);
125 _reference_note->Show (interface == Config::INTERFACE_FULL);
127 if (interface == Config::INTERFACE_FULL) {
128 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
129 reference_sizer->Add (_reference, 0);
130 reference_sizer->Add (_reference_note, 0);
131 _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 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
142 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
143 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
144 _grid->Add (s, wxGBPosition(r, 1));
146 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
150 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
152 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
153 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
154 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
155 _grid->Add (s, wxGBPosition(r, 1));
160 AudioPanel::~AudioPanel ()
163 _audio_dialog->Destroy ();
169 AudioPanel::film_changed (Film::Property property)
172 case Film::AUDIO_CHANNELS:
173 case Film::AUDIO_PROCESSOR:
174 _mapping->set_output_channels (_parent->film()->audio_output_names ());
177 case Film::VIDEO_FRAME_RATE:
178 setup_description ();
180 case Film::REEL_TYPE:
182 setup_sensitivity ();
190 AudioPanel::film_content_changed (int property)
192 ContentList ac = _parent->selected_audio ();
193 if (property == AudioContentProperty::STREAMS) {
194 if (ac.size() == 1) {
195 _mapping->set (ac.front()->audio->mapping());
196 _mapping->set_input_channels (ac.front()->audio->channel_names ());
198 vector<AudioMappingView::Group> groups;
200 BOOST_FOREACH (shared_ptr<const AudioStream> i, ac.front()->audio->streams()) {
201 shared_ptr<const FFmpegAudioStream> f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
206 name += " (" + f->codec_name.get() + ")";
209 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
212 _mapping->set_input_groups (groups);
215 _mapping->set (AudioMapping ());
217 setup_description ();
220 } else if (property == AudioContentProperty::GAIN) {
222 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
223 if (ac.size() == 1) {
224 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
225 checked_set (_reference, dcp ? dcp->reference_audio () : false);
227 checked_set (_reference, false);
230 setup_sensitivity ();
231 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
232 setup_description ();
237 AudioPanel::gain_calculate_button_clicked ()
239 GainCalculatorDialog* d = new GainCalculatorDialog (this);
240 int const r = d->ShowModal ();
242 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
247 _gain->wrapped()->SetValue (
248 Config::instance()->cinema_sound_processor()->db_for_fader_change (
254 /* This appears to be necessary, as the change is not signalled,
257 _gain->view_changed ();
263 AudioPanel::setup_description ()
265 ContentList ac = _parent->selected_audio ();
266 if (ac.size () != 1) {
267 checked_set (_description, wxT (""));
271 checked_set (_description, ac.front()->audio->processing_description(_parent->film()));
275 AudioPanel::mapping_changed (AudioMapping m)
277 ContentList c = _parent->selected_audio ();
279 c.front()->audio->set_mapping (m);
284 AudioPanel::content_selection_changed ()
286 ContentList sel = _parent->selected_audio ();
288 _gain->set_content (sel);
289 _delay->set_content (sel);
291 film_content_changed (AudioContentProperty::STREAMS);
292 film_content_changed (AudioContentProperty::GAIN);
293 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
295 setup_sensitivity ();
299 AudioPanel::setup_sensitivity ()
301 ContentList sel = _parent->selected_audio ();
303 shared_ptr<DCPContent> dcp;
304 if (sel.size() == 1) {
305 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
309 bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
310 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
312 if (_reference->GetValue ()) {
313 _gain->wrapped()->Enable (false);
314 _gain_calculate_button->Enable (false);
315 _show->Enable (true);
316 _peak->Enable (false);
317 _delay->wrapped()->Enable (false);
318 _mapping->Enable (false);
319 _description->Enable (false);
321 _gain->wrapped()->Enable (sel.size() == 1);
322 _gain_calculate_button->Enable (sel.size() == 1);
323 _show->Enable (sel.size() == 1);
324 _peak->Enable (sel.size() == 1);
325 _delay->wrapped()->Enable (sel.size() == 1);
326 _mapping->Enable (sel.size() == 1);
327 _description->Enable (sel.size() == 1);
332 AudioPanel::show_clicked ()
335 _audio_dialog->Destroy ();
339 ContentList ac = _parent->selected_audio ();
340 if (ac.size() != 1) {
344 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
345 _audio_dialog->Show ();
349 AudioPanel::setup_peak ()
351 ContentList sel = _parent->selected_audio ();
352 optional<float> peak_dB;
354 if (sel.size() != 1) {
355 _peak->SetLabel (wxT (""));
357 shared_ptr<Playlist> playlist (new Playlist);
358 playlist->add (_parent->film(), sel.front());
360 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
361 peak_dB = 20 * log10 (analysis->overall_sample_peak().first.peak) + analysis->gain_correction (playlist);
362 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), *peak_dB));
364 _peak->SetLabel (_("Peak: unknown"));
368 static wxColour normal = _peak->GetForegroundColour ();
370 if (peak_dB && *peak_dB > -0.5) {
371 _peak->SetForegroundColour (wxColour (255, 0, 0));
372 } else if (peak_dB && *peak_dB > -3) {
373 _peak->SetForegroundColour (wxColour (186, 120, 0));
375 _peak->SetForegroundColour (normal);
380 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
382 if (old_active && *old_active == "analyse_audio") {
384 _mapping->Enable (true);
385 } else if (new_active && *new_active == "analyse_audio") {
386 _mapping->Enable (false);
391 AudioPanel::reference_clicked ()
393 ContentList c = _parent->selected ();
398 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
403 d->set_reference_audio (_reference->GetValue ());
407 AudioPanel::set_film (shared_ptr<Film>)
409 /* We are changing film, so destroy any audio dialog for the old one */
411 _audio_dialog->Destroy ();