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 "dcpomatic_button.h"
30 #include "lib/config.h"
31 #include "lib/ffmpeg_audio_stream.h"
32 #include "lib/ffmpeg_content.h"
33 #include "lib/cinema_sound_processor.h"
34 #include "lib/job_manager.h"
35 #include "lib/dcp_content.h"
36 #include "lib/audio_content.h"
37 #include <wx/spinctrl.h>
38 #include <boost/foreach.hpp>
46 using boost::dynamic_pointer_cast;
47 using boost::shared_ptr;
48 using boost::optional;
49 #if BOOST_VERSION >= 106100
50 using namespace boost::placeholders;
53 AudioPanel::AudioPanel (ContentPanel* p)
54 : ContentSubPanel (p, _("Audio"))
57 _reference = new CheckBox (this, _("Use this DCP's audio as OV and make VF"));
58 _reference_note = new StaticText (this, wxT(""));
59 _reference_note->Wrap (200);
60 wxFont font = _reference_note->GetFont();
61 font.SetStyle(wxFONTSTYLE_ITALIC);
62 font.SetPointSize(font.GetPointSize() - 1);
63 _reference_note->SetFont(font);
65 _show = new Button (this, _("Show graph of audio levels..."));
66 _peak = new StaticText (this, wxT (""));
68 _gain_label = create_label (this, _("Gain"), true);
69 _gain = new ContentSpinCtrlDouble<AudioContent> (
71 new wxSpinCtrlDouble (this),
72 AudioContentProperty::GAIN,
74 boost::mem_fn (&AudioContent::gain),
75 boost::mem_fn (&AudioContent::set_gain)
78 _gain_db_label = create_label (this, _("dB"), false);
79 _gain_calculate_button = new Button (this, _("Calculate..."));
81 _delay_label = create_label (this, _("Delay"), true);
82 _delay = new ContentSpinCtrl<AudioContent> (
84 new wxSpinCtrl (this),
85 AudioContentProperty::DELAY,
87 boost::mem_fn (&AudioContent::delay),
88 boost::mem_fn (&AudioContent::set_delay)
91 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
92 _delay_ms_label = create_label (this, _("ms"), false);
94 _mapping = new AudioMappingView (this);
95 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
97 _description = new StaticText (this, wxT(" \n"), wxDefaultPosition, wxDefaultSize);
98 _sizer->Add (_description, 0, wxALL, 12);
99 _description->SetFont (font);
101 _gain->wrapped()->SetRange (-60, 60);
102 _gain->wrapped()->SetDigits (1);
103 _gain->wrapped()->SetIncrement (0.5);
104 _delay->wrapped()->SetRange (-1000, 1000);
106 content_selection_changed ();
107 film_changed (Film::AUDIO_CHANNELS);
108 film_changed (Film::VIDEO_FRAME_RATE);
109 film_changed (Film::REEL_TYPE);
111 _reference->Bind (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
112 _show->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::show_clicked, this));
113 _gain_calculate_button->Bind (wxEVT_BUTTON, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
115 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
116 _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
122 AudioPanel::add_to_grid ()
124 bool const full = Config::instance()->interface_complexity() == Config::INTERFACE_FULL;
128 _reference->Show (full);
129 _reference_note->Show (full);
132 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
133 reference_sizer->Add (_reference, 0);
134 reference_sizer->Add (_reference_note, 0);
135 _grid->Add (reference_sizer, wxGBPosition(r, 0), wxGBSpan(1, 4));
139 _grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
140 _grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
143 add_label_to_sizer (_grid, _gain_label, true, wxGBPosition(r, 0));
145 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
146 s->Add (_gain->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
147 s->Add (_gain_db_label, 0, wxALIGN_CENTER_VERTICAL);
148 _grid->Add (s, wxGBPosition(r, 1));
151 _gain_calculate_button->Show (full);
154 _grid->Add (_gain_calculate_button, wxGBPosition(r, 2), wxDefaultSpan, wxALIGN_CENTER_VERTICAL);
158 _delay_label->Show (full);
160 _delay_ms_label->Show (full);
163 add_label_to_sizer (_grid, _delay_label, true, wxGBPosition(r, 0));
164 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
165 s->Add (_delay->wrapped(), 1, wxALIGN_CENTER_VERTICAL | wxTOP | wxBOTTOM | wxRIGHT, 6);
166 s->Add (_delay_ms_label, 0, wxALIGN_CENTER_VERTICAL);
167 _grid->Add (s, wxGBPosition(r, 1));
172 AudioPanel::~AudioPanel ()
175 _audio_dialog->Destroy ();
181 AudioPanel::film_changed (Film::Property property)
183 if (!_parent->film()) {
188 case Film::AUDIO_CHANNELS:
189 case Film::AUDIO_PROCESSOR:
190 _mapping->set_output_channels (_parent->film()->audio_output_names ());
193 case Film::VIDEO_FRAME_RATE:
194 setup_description ();
196 case Film::REEL_TYPE:
198 setup_sensitivity ();
206 AudioPanel::film_content_changed (int property)
208 ContentList ac = _parent->selected_audio ();
209 if (property == AudioContentProperty::STREAMS) {
210 if (ac.size() == 1) {
211 _mapping->set (ac.front()->audio->mapping());
212 _mapping->set_input_channels (ac.front()->audio->channel_names ());
214 vector<AudioMappingView::Group> groups;
216 BOOST_FOREACH (shared_ptr<const AudioStream> i, ac.front()->audio->streams()) {
217 shared_ptr<const FFmpegAudioStream> f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
222 name += " (" + f->codec_name.get() + ")";
225 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
228 _mapping->set_input_groups (groups);
231 _mapping->set (AudioMapping ());
233 setup_description ();
236 } else if (property == AudioContentProperty::GAIN) {
238 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
239 if (ac.size() == 1) {
240 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
241 checked_set (_reference, dcp ? dcp->reference_audio () : false);
243 checked_set (_reference, false);
246 setup_sensitivity ();
247 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
248 setup_description ();
253 AudioPanel::gain_calculate_button_clicked ()
255 GainCalculatorDialog* d = new GainCalculatorDialog (this);
256 int const r = d->ShowModal ();
258 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
263 _gain->wrapped()->SetValue (
264 Config::instance()->cinema_sound_processor()->db_for_fader_change (
270 /* This appears to be necessary, as the change is not signalled,
273 _gain->view_changed ();
279 AudioPanel::setup_description ()
281 ContentList 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 ContentList c = _parent->selected_audio ();
295 c.front()->audio->set_mapping (m);
300 AudioPanel::content_selection_changed ()
302 ContentList 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 ContentList 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);
326 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
328 if (_reference->GetValue ()) {
329 _gain->wrapped()->Enable (false);
330 _gain_calculate_button->Enable (false);
331 _show->Enable (true);
332 _peak->Enable (false);
333 _delay->wrapped()->Enable (false);
334 _mapping->Enable (false);
335 _description->Enable (false);
337 _gain->wrapped()->Enable (sel.size() == 1);
338 _gain_calculate_button->Enable (sel.size() == 1);
339 _show->Enable (sel.size() == 1);
340 _peak->Enable (sel.size() == 1);
341 _delay->wrapped()->Enable (sel.size() == 1);
342 _mapping->Enable (sel.size() == 1);
343 _description->Enable (sel.size() == 1);
348 AudioPanel::show_clicked ()
351 _audio_dialog->Destroy ();
355 ContentList ac = _parent->selected_audio ();
356 if (ac.size() != 1) {
360 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
361 _audio_dialog->Show ();
365 AudioPanel::setup_peak ()
367 ContentList sel = _parent->selected_audio ();
368 optional<float> peak_dB;
370 if (sel.size() != 1) {
371 _peak->SetLabel (wxT (""));
373 shared_ptr<Playlist> playlist (new Playlist);
374 playlist->add (_parent->film(), sel.front());
376 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
377 peak_dB = 20 * log10 (analysis->overall_sample_peak().first.peak) + analysis->gain_correction (playlist);
378 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), *peak_dB));
380 _peak->SetLabel (_("Peak: unknown"));
384 static wxColour normal = _peak->GetForegroundColour ();
386 if (peak_dB && *peak_dB > -0.5) {
387 _peak->SetForegroundColour (wxColour (255, 0, 0));
388 } else if (peak_dB && *peak_dB > -3) {
389 _peak->SetForegroundColour (wxColour (186, 120, 0));
391 _peak->SetForegroundColour (normal);
396 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
398 if (old_active && *old_active == "analyse_audio") {
400 _mapping->Enable (true);
401 } else if (new_active && *new_active == "analyse_audio") {
402 _mapping->Enable (false);
407 AudioPanel::reference_clicked ()
409 ContentList c = _parent->selected ();
414 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
419 d->set_reference_audio (_reference->GetValue ());
423 AudioPanel::set_film (shared_ptr<Film>)
425 /* We are changing film, so destroy any audio dialog for the old one */
427 _audio_dialog->Destroy ();