Update audio plot window when audio mapping is changed.
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /*
2     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
3
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.
8
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.
13
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.
17
18 */
19
20 #include <boost/filesystem.hpp>
21 #include "lib/audio_analysis.h"
22 #include "lib/film.h"
23 #include "lib/audio_content.h"
24 #include "audio_dialog.h"
25 #include "audio_plot.h"
26 #include "wx_util.h"
27
28 using boost::shared_ptr;
29 using boost::bind;
30 using boost::optional;
31
32 AudioDialog::AudioDialog (wxWindow* parent)
33         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
34         , _plot (0)
35 {
36         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
37
38         _plot = new AudioPlot (this);
39         sizer->Add (_plot, 1, wxALL | wxEXPAND, 12);
40
41         wxBoxSizer* side = new wxBoxSizer (wxVERTICAL);
42
43         {
44                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
45                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
46         }
47
48         for (int i = 0; i < MAX_DCP_AUDIO_CHANNELS; ++i) {
49                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
50                 side->Add (_channel_checkbox[i], 1, wxEXPAND | wxALL, 3);
51                 _channel_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::channel_clicked, this, _1));
52         }
53
54         {
55                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
56                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
57         }
58         
59         wxString const types[] = {
60                 _("Peak"),
61                 _("RMS")
62         };
63
64         for (int i = 0; i < AudioPoint::COUNT; ++i) {
65                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
66                 side->Add (_type_checkbox[i], 1, wxEXPAND | wxALL, 3);
67                 _type_checkbox[i]->Bind (wxEVT_COMMAND_CHECKBOX_CLICKED, boost::bind (&AudioDialog::type_clicked, this, _1));
68         }
69
70         {
71                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
72                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
73         }
74         
75         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
76         _smoothing->Bind (wxEVT_SCROLL_THUMBTRACK, boost::bind (&AudioDialog::smoothing_changed, this));
77         side->Add (_smoothing, 1, wxEXPAND);
78
79         sizer->Add (side, 0, wxALL, 12);
80
81         SetSizer (sizer);
82         sizer->Layout ();
83         sizer->SetSizeHints (this);
84 }
85
86 void
87 AudioDialog::set_content (shared_ptr<AudioContent> c)
88 {
89         _content_changed_connection.disconnect ();
90
91         _content = c;
92
93         try_to_load_analysis ();
94         _plot->set_gain (_content->audio_gain ());
95
96         _content_changed_connection = _content->Changed.connect (bind (&AudioDialog::content_changed, this, _2));
97
98         SetTitle (wxString::Format (_("DCP-o-matic audio - %s"), std_to_wx(_content->path_summary()).data()));
99 }
100
101 void
102 AudioDialog::try_to_load_analysis ()
103 {
104         if (!IsShown ()) {
105                 return;
106         }
107
108         if (!boost::filesystem::exists (_content->audio_analysis_path())) {
109                 _plot->set_analysis (shared_ptr<AudioAnalysis> ());
110                 _analysis_finished_connection = _content->analyse_audio (bind (&AudioDialog::analysis_finished, this));
111                 return;
112         }
113         
114         shared_ptr<AudioAnalysis> a;
115         
116         a.reset (new AudioAnalysis (_content->audio_analysis_path ()));
117         _plot->set_analysis (a);
118
119         /* Set up some defaults if no check boxes are checked */
120         
121         int i = 0;
122         while (i < MAX_DCP_AUDIO_CHANNELS && (!_channel_checkbox[i] || !_channel_checkbox[i]->GetValue ())) {
123                 ++i;
124         }
125
126         if (i == MAX_DCP_AUDIO_CHANNELS && _channel_checkbox[0]) {
127                 _channel_checkbox[0]->SetValue (true);
128                 _plot->set_channel_visible (0, true);
129         }
130
131         i = 0;
132         while (i < AudioPoint::COUNT && !_type_checkbox[i]->GetValue ()) {
133                 i++;
134         }
135
136         if (i == AudioPoint::COUNT) {
137                 for (int i = 0; i < AudioPoint::COUNT; ++i) {
138                         _type_checkbox[i]->SetValue (true);
139                         _plot->set_type_visible (i, true);
140                 }
141         }
142 }
143
144 void
145 AudioDialog::analysis_finished ()
146 {
147         if (!boost::filesystem::exists (_content->audio_analysis_path())) {
148                 /* We analysed and still nothing showed up, so maybe it was cancelled or it failed.
149                    Give up.
150                 */
151                 _plot->set_message (_("Could not analyse audio."));
152                 return;
153         }
154
155         try_to_load_analysis ();
156 }
157
158 void
159 AudioDialog::channel_clicked (wxCommandEvent& ev)
160 {
161         int c = 0;
162         while (c < MAX_DCP_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
163                 ++c;
164         }
165
166         assert (c < MAX_DCP_AUDIO_CHANNELS);
167
168         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
169 }
170
171 void
172 AudioDialog::content_changed (int p)
173 {
174         if (p == AudioContentProperty::AUDIO_GAIN) {
175                 _plot->set_gain (_content->audio_gain ());
176         } else if (p == AudioContentProperty::AUDIO_MAPPING) {
177                 try_to_load_analysis ();
178         }
179 }
180
181 void
182 AudioDialog::type_clicked (wxCommandEvent& ev)
183 {
184         int t = 0;
185         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
186                 ++t;
187         }
188
189         assert (t < AudioPoint::COUNT);
190
191         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
192 }
193
194 void
195 AudioDialog::smoothing_changed ()
196 {
197         _plot->set_smoothing (_smoothing->GetValue ());
198 }