2 Copyright (C) 2012-2015 Carl Hetherington <cth@carlh.net>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include "audio_panel.h"
21 #include "audio_mapping_view.h"
23 #include "gain_calculator_dialog.h"
24 #include "content_panel.h"
25 #include "audio_dialog.h"
26 #include "lib/config.h"
27 #include "lib/ffmpeg_content.h"
28 #include "lib/cinema_sound_processor.h"
29 #include "lib/job_manager.h"
30 #include "lib/dcp_content.h"
31 #include <wx/spinctrl.h>
32 #include <boost/lexical_cast.hpp>
33 #include <boost/foreach.hpp>
41 using boost::dynamic_pointer_cast;
42 using boost::lexical_cast;
43 using boost::shared_ptr;
44 using boost::optional;
46 AudioPanel::AudioPanel (ContentPanel* p)
47 : ContentSubPanel (p, _("Audio"))
50 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
51 _sizer->Add (grid, 0, wxALL, 8);
55 _reference = new wxCheckBox (this, wxID_ANY, _("Refer to existing DCP"));
56 grid->Add (_reference, wxGBPosition (r, 0), wxGBSpan (1, 2));
59 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
60 grid->Add (_show, wxGBPosition (r, 0), wxGBSpan (1, 2));
61 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
62 grid->Add (_peak, wxGBPosition (r, 2), wxGBSpan (1, 2), wxALIGN_CENTER_VERTICAL);
65 add_label_to_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
66 _gain = new ContentSpinCtrlDouble<AudioContent> (
68 new wxSpinCtrlDouble (this),
69 AudioContentProperty::AUDIO_GAIN,
70 boost::mem_fn (&AudioContent::audio_gain),
71 boost::mem_fn (&AudioContent::set_audio_gain)
74 _gain->add (grid, wxGBPosition (r, 1));
75 add_label_to_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
76 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
77 grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
80 add_label_to_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
81 _delay = new ContentSpinCtrl<AudioContent> (
83 new wxSpinCtrl (this),
84 AudioContentProperty::AUDIO_DELAY,
85 boost::mem_fn (&AudioContent::audio_delay),
86 boost::mem_fn (&AudioContent::set_audio_delay)
89 _delay->add (grid, wxGBPosition (r, 1));
90 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
91 add_label_to_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
94 _mapping = new AudioMappingView (this);
95 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
98 _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
99 _sizer->Add (_description, 0, wxALL, 12);
100 wxFont font = _description->GetFont();
101 font.SetStyle (wxFONTSTYLE_ITALIC);
102 font.SetPointSize (font.GetPointSize() - 1);
103 _description->SetFont (font);
106 _gain->wrapped()->SetRange (-60, 60);
107 _gain->wrapped()->SetDigits (1);
108 _gain->wrapped()->SetIncrement (0.5);
109 _delay->wrapped()->SetRange (-1000, 1000);
111 _reference->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioPanel::reference_clicked, this));
112 _show->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
113 _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
115 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
117 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
120 AudioPanel::~AudioPanel ()
123 _audio_dialog->Destroy ();
129 AudioPanel::film_changed (Film::Property property)
132 case Film::AUDIO_CHANNELS:
133 case Film::AUDIO_PROCESSOR:
134 _mapping->set_output_channels (_parent->film()->audio_output_names ());
137 case Film::VIDEO_FRAME_RATE:
138 setup_description ();
140 case Film::REEL_TYPE:
141 setup_sensitivity ();
148 AudioPanel::film_content_changed (int property)
150 AudioContentList ac = _parent->selected_audio ();
151 if (property == AudioContentProperty::AUDIO_STREAMS) {
152 if (ac.size() == 1) {
153 _mapping->set (ac.front()->audio_mapping());
154 _mapping->set_input_channels (ac.front()->audio_channel_names ());
156 _mapping->set (AudioMapping ());
158 setup_description ();
161 } else if (property == AudioContentProperty::AUDIO_GAIN) {
163 } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
164 if (ac.size() == 1) {
165 shared_ptr<DCPContent> dcp = dynamic_pointer_cast<DCPContent> (ac.front ());
166 checked_set (_reference, dcp ? dcp->reference_audio () : false);
168 checked_set (_reference, false);
171 setup_sensitivity ();
176 AudioPanel::gain_calculate_button_clicked ()
178 GainCalculatorDialog* d = new GainCalculatorDialog (this);
179 int const r = d->ShowModal ();
181 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
186 _gain->wrapped()->SetValue (
187 Config::instance()->cinema_sound_processor()->db_for_fader_change (
193 /* This appears to be necessary, as the change is not signalled,
196 _gain->view_changed ();
202 AudioPanel::setup_description ()
204 AudioContentList ac = _parent->selected_audio ();
205 if (ac.size () != 1) {
206 checked_set (_description, wxT (""));
210 checked_set (_description, ac.front()->processing_description ());
214 AudioPanel::mapping_changed (AudioMapping m)
216 AudioContentList c = _parent->selected_audio ();
218 c.front()->set_audio_mapping (m);
223 AudioPanel::content_selection_changed ()
225 AudioContentList sel = _parent->selected_audio ();
227 _gain->set_content (sel);
228 _delay->set_content (sel);
230 film_content_changed (AudioContentProperty::AUDIO_STREAMS);
231 film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
233 setup_sensitivity ();
237 AudioPanel::setup_sensitivity ()
239 AudioContentList sel = _parent->selected_audio ();
241 shared_ptr<DCPContent> dcp;
242 if (sel.size() == 1) {
243 dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
246 list<string> why_not;
247 bool const can_reference = dcp && dcp->can_reference_audio (why_not);
248 setup_refer_button (_reference, dcp, can_reference, why_not);
250 if (_reference->GetValue ()) {
251 _gain->wrapped()->Enable (false);
252 _gain_calculate_button->Enable (false);
253 _peak->Enable (false);
254 _delay->wrapped()->Enable (false);
255 _mapping->Enable (false);
256 _description->Enable (false);
258 _gain->wrapped()->Enable (true);
259 _gain_calculate_button->Enable (sel.size() == 1);
260 _peak->Enable (true);
261 _delay->wrapped()->Enable (true);
262 _mapping->Enable (sel.size() == 1);
263 _description->Enable (true);
268 AudioPanel::show_clicked ()
271 _audio_dialog->Destroy ();
275 AudioContentList ac = _parent->selected_audio ();
276 if (ac.size() != 1) {
280 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
281 _audio_dialog->Show ();
285 AudioPanel::setup_peak ()
287 AudioContentList sel = _parent->selected_audio ();
290 if (sel.size() != 1) {
291 _peak->SetLabel (wxT (""));
293 shared_ptr<Playlist> playlist (new Playlist);
294 playlist->add (sel.front ());
296 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
297 if (analysis->sample_peak ()) {
298 float const peak_dB = 20 * log10 (analysis->sample_peak().get()) + analysis->gain_correction (playlist);
302 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
304 _peak->SetLabel (_("Peak: unknown"));
307 _peak->SetLabel (_("Peak: unknown"));
311 static wxColour normal = _peak->GetForegroundColour ();
314 _peak->SetForegroundColour (wxColour (255, 0, 0));
316 _peak->SetForegroundColour (normal);
321 AudioPanel::active_jobs_changed (optional<string> j)
323 if (j && *j == "analyse_audio") {
329 AudioPanel::reference_clicked ()
331 ContentList c = _parent->selected ();
336 shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
341 d->set_reference_audio (_reference->GetValue ());