Merge master.
[dcpomatic.git] / src / wx / audio_dialog.cc
1 /* -*- c-basic-offset: 8; default-tab-width: 8; -*- */
2
3 /*
4     Copyright (C) 2013 Carl Hetherington <cth@carlh.net>
5
6     This program is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     This program is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with this program; if not, write to the Free Software
18     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19
20 */
21
22 #include <boost/filesystem.hpp>
23 #include "lib/audio_analysis.h"
24 #include "lib/film.h"
25 #include "audio_dialog.h"
26 #include "audio_plot.h"
27 #include "wx_util.h"
28
29 using boost::shared_ptr;
30 using boost::bind;
31 using boost::optional;
32
33 AudioDialog::AudioDialog (wxWindow* parent)
34         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER | wxFULL_REPAINT_ON_RESIZE)
35         , _plot (0)
36 {
37         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
38
39         _plot = new AudioPlot (this);
40         sizer->Add (_plot, 1, wxALL | wxEXPAND, 12);
41
42         wxBoxSizer* side = new wxBoxSizer (wxVERTICAL);
43
44         {
45                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Channels"));
46                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
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 (shared_ptr<Film> f)
89 {
90         _film_changed_connection.disconnect ();
91         _film_audio_analysis_succeeded_connection.disconnect ();
92         
93         _film = f;
94
95         try_to_load_analysis ();
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 (_("DCP-o-matic audio - %s"), std_to_wx(_film->name()).data()));
102 }
103
104
105 void
106 AudioDialog::try_to_load_analysis ()
107 {
108         shared_ptr<AudioAnalysis> a;
109
110         if (boost::filesystem::exists (_film->audio_analysis_path())) {
111                 a.reset (new AudioAnalysis (_film->audio_analysis_path ()));
112         } else {
113                 if (IsShown ()) {
114                         _film->analyse_audio ();
115                 }
116         }
117                 
118         _plot->set_analysis (a);
119
120         if (_channel_checkbox[0]) {
121                 _channel_checkbox[0]->SetValue (true);
122         }
123         _plot->set_channel_visible (0, true);
124
125         for (int i = 0; i < AudioPoint::COUNT; ++i) {
126                 _type_checkbox[i]->SetValue (true);
127                 _plot->set_type_visible (i, true);
128         }
129 }
130
131 void
132 AudioDialog::channel_clicked (wxCommandEvent& ev)
133 {
134         int c = 0;
135         while (c < MAX_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
136                 ++c;
137         }
138
139         assert (c < MAX_AUDIO_CHANNELS);
140
141         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
142 }
143
144 void
145 AudioDialog::film_changed (Film::Property p)
146 {
147         switch (p) {
148         case Film::AUDIO_GAIN:
149                 _plot->set_gain (_film->audio_gain ());
150                 break;
151         default:
152                 break;
153         }
154 }
155
156 void
157 AudioDialog::type_clicked (wxCommandEvent& ev)
158 {
159         int t = 0;
160         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
161                 ++t;
162         }
163
164         assert (t < AudioPoint::COUNT);
165
166         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
167 }
168
169 void
170 AudioDialog::smoothing_changed (wxScrollEvent &)
171 {
172         _plot->set_smoothing (_smoothing->GetValue ());
173 }