+ setup_sensitivity ();
+}
+
+void
+AudioPanel::setup_sensitivity ()
+{
+ ContentList sel = _parent->selected_audio ();
+
+ shared_ptr<DCPContent> dcp;
+ if (sel.size() == 1) {
+ dcp = dynamic_pointer_cast<DCPContent> (sel.front ());
+ }
+
+ string why_not;
+ bool const can_reference = dcp && dcp->can_reference_audio (_parent->film(), why_not);
+ setup_refer_button (_reference, _reference_note, dcp, can_reference, why_not);
+
+ if (_reference->GetValue ()) {
+ _gain->wrapped()->Enable (false);
+ _gain_calculate_button->Enable (false);
+ _show->Enable (true);
+ _peak->Enable (false);
+ _delay->wrapped()->Enable (false);
+ _mapping->Enable (false);
+ _description->Enable (false);
+ } else {
+ _gain->wrapped()->Enable (sel.size() == 1);
+ _gain_calculate_button->Enable (sel.size() == 1);
+ _show->Enable (sel.size() == 1);
+ _peak->Enable (sel.size() == 1);
+ _delay->wrapped()->Enable (sel.size() == 1);
+ _mapping->Enable (sel.size() == 1);
+ _description->Enable (sel.size() == 1);
+ }
+}
+
+void
+AudioPanel::show_clicked ()
+{
+ if (_audio_dialog) {
+ _audio_dialog->Destroy ();
+ _audio_dialog = 0;
+ }
+
+ ContentList ac = _parent->selected_audio ();
+ if (ac.size() != 1) {
+ return;
+ }
+
+ _audio_dialog = new AudioDialog (this, _parent->film (), ac.front ());
+ _audio_dialog->Show ();
+}
+
+void
+AudioPanel::setup_peak ()
+{
+ ContentList sel = _parent->selected_audio ();
+ optional<float> peak_dB;
+
+ if (sel.size() != 1) {
+ _peak->SetLabel (wxT (""));
+ } else {
+ shared_ptr<Playlist> playlist (new Playlist);
+ playlist->add (_parent->film(), sel.front());
+ try {
+ shared_ptr<AudioAnalysis> analysis (new AudioAnalysis (_parent->film()->audio_analysis_path (playlist)));
+ peak_dB = 20 * log10 (analysis->overall_sample_peak().first.peak) + analysis->gain_correction (playlist);
+ _peak->SetLabel (wxString::Format (_("Peak: %.2fdB"), *peak_dB));
+ } catch (...) {
+ _peak->SetLabel (_("Peak: unknown"));
+ }
+ }
+
+ static wxColour normal = _peak->GetForegroundColour ();
+
+ if (peak_dB && *peak_dB > -0.5) {
+ _peak->SetForegroundColour (wxColour (255, 0, 0));
+ } else if (peak_dB && *peak_dB > -3) {
+ _peak->SetForegroundColour (wxColour (186, 120, 0));
+ } else {
+ _peak->SetForegroundColour (normal);
+ }
+}
+
+void
+AudioPanel::active_jobs_changed (optional<string> old_active, optional<string> new_active)
+{
+ if (old_active && *old_active == "analyse_audio") {
+ setup_peak ();
+ _mapping->Enable (true);
+ } else if (new_active && *new_active == "analyse_audio") {
+ _mapping->Enable (false);
+ }
+}
+
+void
+AudioPanel::reference_clicked ()
+{
+ ContentList c = _parent->selected ();
+ if (c.size() != 1) {
+ return;
+ }
+
+ shared_ptr<DCPContent> d = dynamic_pointer_cast<DCPContent> (c.front ());
+ if (!d) {
+ return;
+ }
+
+ d->set_reference_audio (_reference->GetValue ());
+}
+
+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 = 0;
+ }