Cleanup: use layout() wrapper.
[dcpomatic.git] / src / wx / audio_panel.cc
index 15b18828db50c5349669b68e7493c67b2196f9eb..50831ec4076607e5833a64d7cc5f983a02ae44c3 100644 (file)
 #include "lib/ffmpeg_content.h"
 #include "lib/job_manager.h"
 #include "lib/maths_util.h"
+#include <dcp/warnings.h>
+LIBDCP_DISABLE_WARNINGS
 #include <wx/spinctrl.h>
-#include <iostream>
+LIBDCP_ENABLE_WARNINGS
 
 
-using std::cout;
 using std::dynamic_pointer_cast;
 using std::list;
 using std::make_shared;
@@ -56,6 +57,9 @@ using namespace boost::placeholders;
 using namespace dcpomatic;
 
 
+std::map<boost::filesystem::path, float> AudioPanel::_peak_cache;
+
+
 AudioPanel::AudioPanel (ContentPanel* p)
        : ContentSubPanel (p, _("Audio"))
 {
@@ -109,6 +113,8 @@ AudioPanel::create ()
        _fade_out_label = create_label (this, _("Fade out"), true);
        _fade_out = new Timecode<ContentTime> (this);
 
+       _use_same_fades_as_video = new CheckBox(this, _("Use same fades as video"));
+
        _mapping = new AudioMappingView (this, _("Content"), _("content"), _("DCP"), _("DCP"));
        _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
 
@@ -126,19 +132,20 @@ AudioPanel::create ()
        film_changed (Film::Property::VIDEO_FRAME_RATE);
        film_changed (Film::Property::REEL_TYPE);
 
-       _reference->Bind             (wxEVT_CHECKBOX, boost::bind (&AudioPanel::reference_clicked, this));
+       _reference->bind(&AudioPanel::reference_clicked, this);
        _show->Bind                  (wxEVT_BUTTON,   boost::bind (&AudioPanel::show_clicked, this));
        _gain_calculate_button->Bind (wxEVT_BUTTON,   boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
 
        _fade_in->Changed.connect (boost::bind(&AudioPanel::fade_in_changed, this));
        _fade_out->Changed.connect (boost::bind(&AudioPanel::fade_out_changed, this));
+       _use_same_fades_as_video->bind(&AudioPanel::use_same_fades_as_video_changed, this);
 
        _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
        _active_jobs_connection = JobManager::instance()->ActiveJobsChanged.connect (boost::bind (&AudioPanel::active_jobs_changed, this, _1, _2));
 
        add_to_grid ();
 
-       _sizer->Layout ();
+       layout();
 }
 
 
@@ -182,17 +189,12 @@ AudioPanel::add_to_grid ()
        add_label_to_sizer (_grid, _fade_out_label, true, wxGBPosition(r, 0));
        _grid->Add (_fade_out, wxGBPosition(r, 1), wxGBSpan(1, 3));
        ++r;
-}
-
 
-AudioPanel::~AudioPanel ()
-{
-       if (_audio_dialog) {
-               _audio_dialog->Destroy ();
-               _audio_dialog = nullptr;
-       }
+       _grid->Add (_use_same_fades_as_video, wxGBPosition(r, 0), wxGBSpan(1, 4));
+       ++r;
 }
 
+
 void
 AudioPanel::film_changed (Film::Property property)
 {
@@ -251,6 +253,8 @@ AudioPanel::film_content_changed (int property)
                setup_peak ();
                layout ();
        } else if (property == AudioContentProperty::GAIN) {
+               /* This is a bit aggressive but probably not so bad */
+               _peak_cache.clear();
                setup_peak ();
        } else if (property == DCPContentProperty::REFERENCE_AUDIO) {
                if (ac.size() == 1) {
@@ -291,6 +295,8 @@ AudioPanel::film_content_changed (int property)
                } else {
                        _fade_out->clear ();
                }
+       } else if (property == AudioContentProperty::USE_SAME_FADES_AS_VIDEO) {
+               setup_sensitivity ();
        }
 }
 
@@ -298,18 +304,17 @@ AudioPanel::film_content_changed (int property)
 void
 AudioPanel::gain_calculate_button_clicked ()
 {
-       auto d = new GainCalculatorDialog (this);
-       auto const r = d->ShowModal ();
-       auto c = d->db_change();
+       GainCalculatorDialog dialog(this);
+       auto const r = dialog.ShowModal();
+       auto change = dialog.db_change();
 
-       if (r == wxID_CANCEL || !c) {
-               d->Destroy ();
+       if (r == wxID_CANCEL || !change) {
                return;
        }
 
        auto old_peak_dB = peak ();
        auto old_value = _gain->wrapped()->GetValue();
-       _gain->wrapped()->SetValue(old_value + *c);
+       _gain->wrapped()->SetValue(old_value + *change);
 
        /* This appears to be necessary, as the change is not signalled,
           I think.
@@ -322,8 +327,6 @@ AudioPanel::gain_calculate_button_clicked ()
                _gain->wrapped()->SetValue (old_value);
                _gain->view_changed ();
        }
-
-       d->Destroy ();
 }
 
 
@@ -362,6 +365,7 @@ AudioPanel::content_selection_changed ()
        film_content_changed (AudioContentProperty::GAIN);
        film_content_changed (AudioContentProperty::FADE_IN);
        film_content_changed (AudioContentProperty::FADE_OUT);
+       film_content_changed (AudioContentProperty::USE_SAME_FADES_AS_VIDEO);
        film_content_changed (DCPContentProperty::REFERENCE_AUDIO);
 
        setup_sensitivity ();
@@ -391,6 +395,8 @@ AudioPanel::setup_sensitivity ()
        auto const ref = _reference->GetValue();
        auto const single = sel.size() == 1;
 
+       auto const all_have_video = std::all_of(sel.begin(), sel.end(), [](shared_ptr<const Content> c) { return static_cast<bool>(c->video); });
+
        _gain->wrapped()->Enable (!ref);
        _gain_calculate_button->Enable (!ref && single);
        _show->Enable (single);
@@ -398,23 +404,23 @@ AudioPanel::setup_sensitivity ()
        _delay->wrapped()->Enable (!ref);
        _mapping->Enable (!ref && single);
        _description->Enable (!ref && single);
+       _fade_in->Enable (!_use_same_fades_as_video->GetValue());
+       _fade_out->Enable (!_use_same_fades_as_video->GetValue());
+       _use_same_fades_as_video->Enable (!ref && all_have_video);
 }
 
 
 void
 AudioPanel::show_clicked ()
 {
-       if (_audio_dialog) {
-               _audio_dialog->Destroy ();
-               _audio_dialog = nullptr;
-       }
+       _audio_dialog.reset();
 
        auto ac = _parent->selected_audio ();
        if (ac.size() != 1) {
                return;
        }
 
-       _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
+       _audio_dialog.reset(this, _parent->film(), _parent->film_viewer(), ac.front());
        _audio_dialog->Show ();
 }
 
@@ -430,8 +436,18 @@ AudioPanel::peak () const
                auto playlist = make_shared<Playlist>();
                playlist->add (_parent->film(), sel.front());
                try {
-                       auto analysis = make_shared<AudioAnalysis>(_parent->film()->audio_analysis_path(playlist));
-                       peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
+                       /* Loading the audio analysis file is slow, and this ::peak() is called a few times when
+                        * the content selection is changed, so cache it.
+                        */
+                       auto const path = _parent->film()->audio_analysis_path(playlist);
+                       auto cached = _peak_cache.find(path);
+                       if (cached != _peak_cache.end()) {
+                               peak_dB = cached->second;
+                       } else {
+                               auto analysis = make_shared<AudioAnalysis>(path);
+                               peak_dB = linear_to_db(analysis->overall_sample_peak().first.peak) + analysis->gain_correction(playlist);
+                               _peak_cache[path] = *peak_dB;
+                       }
                } catch (...) {
 
                }
@@ -503,10 +519,7 @@ void
 AudioPanel::set_film (shared_ptr<Film>)
 {
        /* We are changing film, so destroy any audio dialog for the old one */
-       if (_audio_dialog) {
-               _audio_dialog->Destroy ();
-               _audio_dialog = nullptr;
-       }
+       _audio_dialog.reset();
 }
 
 
@@ -531,3 +544,12 @@ AudioPanel::fade_out_changed ()
        }
 }
 
+
+void
+AudioPanel::use_same_fades_as_video_changed ()
+{
+       for (auto content: _parent->selected_audio()) {
+               content->audio->set_use_same_fades_as_video(_use_same_fades_as_video->GetValue());
+       }
+}
+