Fixes for wx 2.8.
[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)
33         , _plot (0)
34 {
35         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
36
37         _plot = new AudioPlot (this);
38         sizer->Add (_plot, 1, wxALL, 12);
39
40         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
41
42         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
43                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
44                 table->Add (_channel_checkbox[i], 1, wxEXPAND);
45                 table->AddSpacer (0);
46                 _channel_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::channel_clicked), 0, this);
47         }
48
49         table->AddSpacer (0);
50         table->AddSpacer (0);
51
52         wxString const types[] = {
53                 _("Peak"),
54                 _("RMS")
55         };
56
57         for (int i = 0; i < AudioPoint::COUNT; ++i) {
58                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
59                 table->Add (_type_checkbox[i], 1, wxEXPAND);
60                 table->AddSpacer (0);
61                 _type_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::type_clicked), 0, this);
62         }
63
64         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
65         _smoothing->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (AudioDialog::smoothing_changed), 0, this);
66         table->Add (_smoothing, 1, wxEXPAND);
67         table->AddSpacer (0);
68
69         sizer->Add (table, 0, wxALL, 12);
70
71         SetSizer (sizer);
72         sizer->Layout ();
73         sizer->SetSizeHints (this);
74 }
75
76 void
77 AudioDialog::set_film (boost::shared_ptr<Film> f)
78 {
79         _film_changed_connection.disconnect ();
80         _film_audio_analysis_finished_connection.disconnect ();
81         
82         _film = f;
83
84         try_to_load_analysis ();
85         setup_channels ();
86         _plot->set_gain (_film->audio_gain ());
87
88         _film_changed_connection = _film->Changed.connect (bind (&AudioDialog::film_changed, this, _1));
89         _film_audio_analysis_finished_connection = _film->AudioAnalysisFinished.connect (bind (&AudioDialog::try_to_load_analysis, this));
90
91         SetTitle (std_to_wx (String::compose ("DVD-o-matic audio - %1", _film->name())));
92 }
93
94 void
95 AudioDialog::setup_channels ()
96 {
97         if (!_film->audio_stream()) {
98                 return;
99         }
100
101         AudioMapping m (_film->audio_stream()->channels ());
102         
103         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
104                 if (m.dcp_to_source(static_cast<libdcp::Channel>(i))) {
105                         _channel_checkbox[i]->Show ();
106                 } else {
107                         _channel_checkbox[i]->Hide ();
108                 }
109         }
110 }       
111
112 void
113 AudioDialog::try_to_load_analysis ()
114 {
115         shared_ptr<AudioAnalysis> a;
116
117         if (boost::filesystem::exists (_film->audio_analysis_path())) {
118                 a.reset (new AudioAnalysis (_film->audio_analysis_path ()));
119         } else {
120                 if (IsShown ()) {
121                         _film->analyse_audio ();
122                 }
123         }
124                 
125         _plot->set_analysis (a);
126
127         AudioMapping m (_film->audio_stream()->channels ());
128         optional<libdcp::Channel> c = m.source_to_dcp (0);
129         if (c) {
130                 _channel_checkbox[c.get()]->SetValue (true);
131                 _plot->set_channel_visible (0, true);
132         }
133
134         for (int i = 0; i < AudioPoint::COUNT; ++i) {
135                 _type_checkbox[i]->SetValue (true);
136                 _plot->set_type_visible (i, true);
137         }
138 }
139
140 void
141 AudioDialog::channel_clicked (wxCommandEvent& ev)
142 {
143         int c = 0;
144         while (c < MAX_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
145                 ++c;
146         }
147
148         assert (c < MAX_AUDIO_CHANNELS);
149
150         AudioMapping m (_film->audio_stream()->channels ());
151         optional<int> s = m.dcp_to_source (static_cast<libdcp::Channel> (c));
152         if (s) {
153                 _plot->set_channel_visible (s.get(), _channel_checkbox[c]->GetValue ());
154         }
155 }
156
157 void
158 AudioDialog::film_changed (Film::Property p)
159 {
160         switch (p) {
161         case Film::AUDIO_GAIN:
162                 _plot->set_gain (_film->audio_gain ());
163                 break;
164         case Film::CONTENT_AUDIO_STREAM:
165         case Film::EXTERNAL_AUDIO:
166         case Film::USE_CONTENT_AUDIO:
167                 setup_channels ();
168                 break;
169         default:
170                 break;
171         }
172 }
173
174 void
175 AudioDialog::type_clicked (wxCommandEvent& ev)
176 {
177         int t = 0;
178         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
179                 ++t;
180         }
181
182         assert (t < AudioPoint::COUNT);
183
184         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
185 }
186
187 void
188 AudioDialog::smoothing_changed (wxScrollEvent &)
189 {
190         _plot->set_smoothing (_smoothing->GetValue ());
191 }