+
+void
+AudioPanel::show_clicked ()
+{
+ if (_audio_dialog) {
+ _audio_dialog->Destroy ();
+ _audio_dialog = nullptr;
+ }
+
+ auto ac = _parent->selected_audio ();
+ if (ac.size() != 1) {
+ return;
+ }
+
+ _audio_dialog = new AudioDialog (this, _parent->film(), _parent->film_viewer(), ac.front());
+ _audio_dialog->Show ();
+}
+
+/** @return If there is one selected piece of audio content, return its peak value in dB (if known) */
+optional<float>
+AudioPanel::peak () const
+{
+ optional<float> peak_dB;
+
+ auto sel = _parent->selected_audio ();
+ if (sel.size() == 1) {
+ 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);
+ } catch (...) {
+
+ }
+ }
+
+ return peak_dB;
+}
+
+void
+AudioPanel::setup_peak ()
+{
+ auto sel = _parent->selected_audio ();
+
+ auto peak_dB = peak ();
+ if (sel.size() != 1) {
+ _peak->SetLabel (wxT(""));
+ } else {
+ peak_dB = peak ();
+ if (peak_dB) {
+ _peak->SetLabel (wxString::Format(_("Peak: %.2fdB"), *peak_dB));
+ } else {
+ _peak->SetLabel (_("Peak: unknown"));
+ }
+ }
+
+ static auto 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 ()
+{
+ auto c = _parent->selected ();
+ if (c.size() != 1) {
+ return;
+ }
+
+ auto 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 = nullptr;
+ }
+}
+