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 <wx/spinctrl.h>
31 #include <boost/lexical_cast.hpp>
32 #include <boost/foreach.hpp>
40 using boost::dynamic_pointer_cast;
41 using boost::lexical_cast;
42 using boost::shared_ptr;
43 using boost::optional;
45 AudioPanel::AudioPanel (ContentPanel* p)
46 : ContentSubPanel (p, _("Audio"))
49 wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
50 _sizer->Add (grid, 0, wxALL, 8);
55 wxBoxSizer* s = new wxBoxSizer (wxHORIZONTAL);
56 _show = new wxButton (this, wxID_ANY, _("Show graph of audio levels..."));
58 _peak = new wxStaticText (this, wxID_ANY, wxT (""));
59 s->Add (_peak, 0, wxALIGN_CENTER_VERTICAL | wxLEFT | wxRIGHT, DCPOMATIC_SIZER_X_GAP);
60 grid->Add (s, wxGBPosition (r, 0), wxGBSpan (1, 2));
64 add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
65 _gain = new ContentSpinCtrlDouble<AudioContent> (
67 new wxSpinCtrlDouble (this),
68 AudioContentProperty::AUDIO_GAIN,
69 boost::mem_fn (&AudioContent::audio_gain),
70 boost::mem_fn (&AudioContent::set_audio_gain)
73 _gain->add (grid, wxGBPosition (r, 1));
74 add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
75 _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
76 grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
79 add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
80 _delay = new ContentSpinCtrl<AudioContent> (
82 new wxSpinCtrl (this),
83 AudioContentProperty::AUDIO_DELAY,
84 boost::mem_fn (&AudioContent::audio_delay),
85 boost::mem_fn (&AudioContent::set_audio_delay)
88 _delay->add (grid, wxGBPosition (r, 1));
89 /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
90 add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
93 _mapping = new AudioMappingView (this);
94 _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
97 _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
98 _sizer->Add (_description, 0, wxALL, 12);
99 wxFont font = _description->GetFont();
100 font.SetStyle (wxFONTSTYLE_ITALIC);
101 font.SetPointSize (font.GetPointSize() - 1);
102 _description->SetFont (font);
105 _gain->wrapped()->SetRange (-60, 60);
106 _gain->wrapped()->SetDigits (1);
107 _gain->wrapped()->SetIncrement (0.5);
108 _delay->wrapped()->SetRange (-1000, 1000);
110 _show->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::show_clicked, this));
111 _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED, boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
113 _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
115 JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1));
118 AudioPanel::~AudioPanel ()
121 _audio_dialog->Destroy ();
127 AudioPanel::film_changed (Film::Property property)
130 case Film::AUDIO_CHANNELS:
131 case Film::AUDIO_PROCESSOR:
132 _mapping->set_output_channels (_parent->film()->audio_output_names ());
135 case Film::VIDEO_FRAME_RATE:
136 setup_description ();
144 AudioPanel::film_content_changed (int property)
146 if (property == AudioContentProperty::AUDIO_STREAMS) {
147 AudioContentList ac = _parent->selected_audio ();
148 if (ac.size() == 1) {
149 _mapping->set (ac.front()->audio_mapping());
150 _mapping->set_input_channels (ac.front()->audio_channel_names ());
152 _mapping->set (AudioMapping ());
154 setup_description ();
157 } else if (property == AudioContentProperty::AUDIO_GAIN) {
163 AudioPanel::gain_calculate_button_clicked ()
165 GainCalculatorDialog* d = new GainCalculatorDialog (this);
166 int const r = d->ShowModal ();
168 if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
173 _gain->wrapped()->SetValue (
174 Config::instance()->cinema_sound_processor()->db_for_fader_change (
180 /* This appears to be necessary, as the change is not signalled,
183 _gain->view_changed ();
189 AudioPanel::setup_description ()
191 AudioContentList ac = _parent->selected_audio ();
192 if (ac.size () != 1) {
193 checked_set (_description, wxT (""));
197 checked_set (_description, ac.front()->processing_description ());
201 AudioPanel::mapping_changed (AudioMapping m)
203 AudioContentList c = _parent->selected_audio ();
205 c.front()->set_audio_mapping (m);
210 AudioPanel::content_selection_changed ()
212 AudioContentList sel = _parent->selected_audio ();
214 _gain->set_content (sel);
215 _delay->set_content (sel);
217 _gain_calculate_button->Enable (sel.size() == 1);
218 _mapping->Enable (sel.size() == 1);
220 film_content_changed (AudioContentProperty::AUDIO_STREAMS);
224 AudioPanel::show_clicked ()
227 _audio_dialog->Destroy ();
231 AudioContentList ac = _parent->selected_audio ();
232 if (ac.size() != 1) {
236 _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
237 _audio_dialog->Show ();
241 AudioPanel::setup_peak ()
243 AudioContentList sel = _parent->selected_audio ();
246 if (sel.size() != 1) {
247 _peak->SetLabel (wxT (""));
249 shared_ptr<Playlist> playlist (new Playlist);
250 playlist->add (sel.front ());
252 shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
253 if (analysis->peak ()) {
254 float const peak_dB = 20 * log10 (analysis->peak().get()) + analysis->gain_correction (playlist);
258 _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), peak_dB));
260 _peak->SetLabel (_("Peak: unknown"));
263 _peak->SetLabel (_("Peak: unknown"));
267 static wxColour normal = _peak->GetForegroundColour ();
270 _peak->SetForegroundColour (wxColour (255, 0, 0));
272 _peak->SetForegroundColour (normal);
277 AudioPanel::active_jobs_changed (optional<string> j)
279 if (j && *j == "analyse_audio") {