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