Fix resizing/redraw problems on Windows.
[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 "audio_dialog.h"
22 #include "audio_plot.h"
23 #include "audio_analysis.h"
24 #include "film.h"
25 #include "wx_util.h"
26
27 using boost::shared_ptr;
28 using boost::bind;
29 using boost::optional;
30
31 AudioDialog::AudioDialog (wxWindow* parent)
32         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
33         , _plot (0)
34 {
35         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
36
37         _plot = new AudioPlot (this);
38         sizer->Add (_plot, 1, wxALL | wxEXPAND, 12);
39
40         wxBoxSizer* side = new wxBoxSizer (wxVERTICAL);
41
42         {
43                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
44                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
45         }
46         
47
48         for (int i = 0; i < MAX_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]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::channel_clicked), 0, this);
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]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::type_clicked), 0, this);
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->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (AudioDialog::smoothing_changed), 0, 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_film (boost::shared_ptr<Film> f)
88 {
89         _film_changed_connection.disconnect ();
90         _film_audio_analysis_succeeded_connection.disconnect ();
91         
92         _film = f;
93
94         try_to_load_analysis ();
95         setup_channels ();
96         _plot->set_gain (_film->audio_gain ());
97
98         _film_changed_connection = _film->Changed.connect (bind (&AudioDialog::film_changed, this, _1));
99         _film_audio_analysis_succeeded_connection = _film->AudioAnalysisSucceeded.connect (bind (&AudioDialog::try_to_load_analysis, this));
100
101         SetTitle (wxString::Format (_("DVD-o-matic audio - %s"), std_to_wx(_film->name()).data()));
102 }
103
104 void
105 AudioDialog::setup_channels ()
106 {
107         if (!_film->audio_stream()) {
108                 return;
109         }
110
111         AudioMapping m (_film->audio_stream()->channels ());
112         
113         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
114                 if (m.dcp_to_source(static_cast<libdcp::Channel>(i))) {
115                         _channel_checkbox[i]->Show ();
116                 } else {
117                         _channel_checkbox[i]->Hide ();
118                 }
119         }
120 }       
121
122 void
123 AudioDialog::try_to_load_analysis ()
124 {
125         shared_ptr<AudioAnalysis> a;
126
127         if (boost::filesystem::exists (_film->audio_analysis_path())) {
128                 a.reset (new AudioAnalysis (_film->audio_analysis_path ()));
129         } else {
130                 if (IsShown ()) {
131                         _film->analyse_audio ();
132                 }
133         }
134                 
135         _plot->set_analysis (a);
136
137         AudioMapping m (_film->audio_stream()->channels ());
138         optional<libdcp::Channel> c = m.source_to_dcp (0);
139         if (c) {
140                 _channel_checkbox[c.get()]->SetValue (true);
141                 _plot->set_channel_visible (0, true);
142         }
143
144         for (int i = 0; i < AudioPoint::COUNT; ++i) {
145                 _type_checkbox[i]->SetValue (true);
146                 _plot->set_type_visible (i, true);
147         }
148 }
149
150 void
151 AudioDialog::channel_clicked (wxCommandEvent& ev)
152 {
153         int c = 0;
154         while (c < MAX_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
155                 ++c;
156         }
157
158         assert (c < MAX_AUDIO_CHANNELS);
159
160         AudioMapping m (_film->audio_stream()->channels ());
161         optional<int> s = m.dcp_to_source (static_cast<libdcp::Channel> (c));
162         if (s) {
163                 _plot->set_channel_visible (s.get(), _channel_checkbox[c]->GetValue ());
164         }
165 }
166
167 void
168 AudioDialog::film_changed (Film::Property p)
169 {
170         switch (p) {
171         case Film::AUDIO_GAIN:
172                 _plot->set_gain (_film->audio_gain ());
173                 break;
174         case Film::CONTENT_AUDIO_STREAM:
175         case Film::EXTERNAL_AUDIO:
176         case Film::USE_CONTENT_AUDIO:
177                 setup_channels ();
178                 break;
179         default:
180                 break;
181         }
182 }
183
184 void
185 AudioDialog::type_clicked (wxCommandEvent& ev)
186 {
187         int t = 0;
188         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
189                 ++t;
190         }
191
192         assert (t < AudioPoint::COUNT);
193
194         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
195 }
196
197 void
198 AudioDialog::smoothing_changed (wxScrollEvent &)
199 {
200         _plot->set_smoothing (_smoothing->GetValue ());
201 }