2 Copyright (C) 2012-2016 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 "lib/config.h"
28 #include "lib/ffmpeg_audio_stream.h"
29 #include "lib/ffmpeg_content.h"
30 #include "lib/cinema_sound_processor.h"
31 #include "lib/job_manager.h"
32 #include "lib/dcp_content.h"
33 #include "lib/audio_content.h"
34 #include <wx/spinctrl.h>
35 #include <boost/foreach.hpp>
43 using boost::dynamic_pointer_cast;
44 using boost::shared_ptr;
45 using boost::optional;
47 AudioPanel::AudioPanel (ContentPanel* p)
48 : ContentSubPanel (p, _("Audio"))
51 wxBoxSizer* reference_sizer = new wxBoxSizer (wxVERTICAL);
53 _reference = new wxCheckBox (this, wxID_ANY, _("Use this DCP's audio as OV and make VF"));
54 reference_sizer->Add (_reference, 0, wxLEFT | wxRIGHT | wxTOP, DCPOMATIC_SIZER_GAP);
56 _reference_note = new wxStaticText (this, wxID_ANY, _(""));
57 _reference_note->Wrap (200);
58 reference_sizer->Add (_reference_note, 0, wxLEFT | wxRIGHT, DCPOMATIC_SIZER_GAP);
59 wxFont font = _reference_note->GetFont();
60 font.SetStyle(wxFONTSTYLE_ITALIC);
61 font.SetPointSize(font.GetPointSize() - 1);
62 _reference_note->SetFont(font);
64 _sizer->Add (reference_sizer);
66 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
67 _sizer->Add (grid, 0, wxALL, 8);
71 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
72 grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
73 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
74 grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
77 add_label_to_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
78 _gain = new ContentSpinCtrlDouble<AudioContent> (
80 new wxSpinCtrlDouble (this),
81 AudioContentProperty::GAIN,
83 boost::mem_fn (&AudioContent::gain),
84 boost::mem_fn (&AudioContent::set_gain)
87 _gain->add (grid, wxGBPosition (r, 1));
88 add_label_to_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
89 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
90 grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
93 add_label_to_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
94 _delay = new ContentSpinCtrl<AudioContent> (
96 new wxSpinCtrl (this),
97 AudioContentProperty::DELAY,
99 boost::mem_fn (&AudioContent::delay),
100 boost::mem_fn (&AudioContent::set_delay)
103 _delay->add (grid, wxGBPosition (r, 1));
104 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
105 add_label_to_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
108 _mapping = new AudioMappingView (this);
109 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
112 _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
113 _sizer->Add (_description, 0, wxALL, 12);
114 _description->SetFont (font);
117 _gain->wrapped()->SetRange (-60, 60);
118 _gain->wrapped()->SetDigits (1);
119 _gain->wrapped()->SetIncrement (0.5);
120 _delay->wrapped()->SetRange (-1000, 1000);
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));
128 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
131 AudioPanel::~AudioPanel ()
134 _audio_dialog->Destroy ();
140 AudioPanel::film_changed (Film::Property property)
143 case Film::AUDIO_CHANNELS:
144 case Film::AUDIO_PROCESSOR:
145 _mapping->set_output_channels (_parent->film()->audio_output_names ());
148 case Film::VIDEO_FRAME_RATE:
149 setup_description ();
151 case Film::REEL_TYPE:
153 setup_sensitivity ();
161 AudioPanel::film_content_changed (int property)
163 ContentList ac = _parent->selected_audio ();
164 if (property == AudioContentProperty::STREAMS) {
165 if (ac.size() == 1) {
166 _mapping->set (ac.front()->audio->mapping());
167 _mapping->set_input_channels (ac.front()->audio->channel_names ());
169 vector<AudioMappingView::Group> groups;
171 BOOST_FOREACH (shared_ptr<const AudioStream> i, ac.front()->audio->streams()) {
172 shared_ptr<const FFmpegAudioStream> f = dynamic_pointer_cast<const FFmpegAudioStream> (i);
177 name += " (" + f->codec_name.get() + ")";
180 groups.push_back (AudioMappingView::Group (c, c + i->channels() - 1, name));
183 _mapping->set_input_groups (groups);
186 _mapping->set (AudioMapping ());
188 setup_description ();
191 } else if (property == AudioContentProperty::GAIN) {
193 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
194 if (ac.size() == 1) {
195 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
196 checked_set (_reference, dcp ? dcp->reference_audio () : false);
198 checked_set (_reference, false);
201 setup_sensitivity ();
202 } else if (property == ContentProperty::VIDEO_FRAME_RATE) {
203 setup_description ();
208 AudioPanel::gain_calculate_button_clicked ()
210 GainCalculatorDialog* d = new GainCalculatorDialog (this);
211 int const r = d->ShowModal ();
213 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
218 _gain->wrapped()->SetValue (
219 Config::instance()->cinema_sound_processor()->db_for_fader_change (
225 /* This appears to be necessary, as the change is not signalled,
228 _gain->view_changed ();
234 AudioPanel::setup_description ()
236 ContentList ac = _parent->selected_audio ();
237 if (ac.size () != 1) {
238 checked_set (_description, wxT (""));
242 checked_set (_description, ac.front()->audio->processing_description ());
246 AudioPanel::mapping_changed (AudioMapping m)
248 ContentList c = _parent->selected_audio ();
250 c.front()->audio->set_mapping (m);
255 AudioPanel::content_selection_changed ()
257 ContentList sel = _parent->selected_audio ();
259 _gain->set_content (sel);
260 _delay->set_content (sel);
262 film_content_changed (AudioContentProperty::STREAMS);
263 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
265 setup_sensitivity ();
269 AudioPanel::setup_sensitivity ()
271 ContentList sel = _parent->selected_audio ();
273 shared_ptr<DCPContent> dcp;
274 if (sel.size() == 1) {
275 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
279 bool const can_reference = dcp && dcp->can_reference_audio (why_not);
280 setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
282 if (_reference->GetValue ()) {
283 _gain->wrapped()->Enable (false);
284 _gain_calculate_button->Enable (false);
285 _show->Enable (true);
286 _peak->Enable (false);
287 _delay->wrapped()->Enable (false);
288 _mapping->Enable (false);
289 _description->Enable (false);
291 _gain->wrapped()->Enable (sel.size() == 1);
292 _gain_calculate_button->Enable (sel.size() == 1);
293 _show->Enable (sel.size() == 1);
294 _peak->Enable (sel.size() == 1);
295 _delay->wrapped()->Enable (sel.size() == 1);
296 _mapping->Enable (sel.size() == 1);
297 _description->Enable (sel.size() == 1);
302 AudioPanel::show_clicked ()
305 _audio_dialog->Destroy ();
309 ContentList ac = _parent->selected_audio ();
310 if (ac.size() != 1) {
314 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
315 _audio_dialog->Show ();
319 AudioPanel::setup_peak ()
321 ContentList sel = _parent->selected_audio ();
322 optional<float> peak_dB;
324 if (sel.size() != 1) {
325 _peak->SetLabel (wxT (""));
327 shared_ptr<Playlist> playlist (new Playlist);
328 playlist->add (sel.front ());
330 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
331 peak_dB = 20 * log10 (analysis->overall_sample_peak().first.peak) + analysis->gain_correction (playlist);
332 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), *peak_dB));
334 _peak->SetLabel (_("Peak: unknown"));
338 static wxColour normal = _peak->GetForegroundColour ();
340 if (peak_dB && *peak_dB > -0.5) {
341 _peak->SetForegroundColour (wxColour (255, 0, 0));
342 } else if (peak_dB && *peak_dB > -3) {
343 _peak->SetForegroundColour (wxColour (186, 120, 0));
345 _peak->SetForegroundColour (normal);
350 AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
352 if (old_active && *old_active == "analyse_audio") {
354 _mapping->Enable (true);
355 } else if (new_active && *new_active == "analyse_audio") {
356 _mapping->Enable (false);
361 AudioPanel::reference_clicked ()
363 ContentList c = _parent->selected ();
368 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
373 d->set_reference_audio (_reference->GetValue ());
377 AudioPanel::set_film (shared_ptr<Film>)
379 /* We are changing film, so destroy any audio dialog for the old one */
381 _audio_dialog->Destroy ();