Multiple simultaneous plots.
[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
30 AudioDialog::AudioDialog (wxWindow* parent)
31         : wxDialog (parent, wxID_ANY, _("Audio"), wxDefaultPosition, wxSize (640, 512), wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
32         , _plot (0)
33 {
34         wxBoxSizer* sizer = new wxBoxSizer (wxHORIZONTAL);
35
36         _plot = new AudioPlot (this);
37         sizer->Add (_plot, 1);
38
39         wxFlexGridSizer* table = new wxFlexGridSizer (2, 6, 6);
40
41         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
42                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, audio_channel_name (i));
43                 table->Add (_channel_checkbox[i], 1, wxEXPAND);
44                 table->AddSpacer (0);
45                 _channel_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::channel_clicked), 0, this);
46         }
47
48         table->AddSpacer (0);
49         table->AddSpacer (0);
50
51         wxString const types[] = {
52                 _("Peak"),
53                 _("RMS")
54         };
55
56         for (int i = 0; i < AudioPoint::COUNT; ++i) {
57                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
58                 table->Add (_type_checkbox[i], 1, wxEXPAND);
59                 table->AddSpacer (0);
60                 _type_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::type_clicked), 0, this);
61         }
62
63         sizer->Add (table);
64
65         SetSizer (sizer);
66         sizer->Layout ();
67         sizer->SetSizeHints (this);
68 }
69
70 void
71 AudioDialog::set_film (boost::shared_ptr<Film> f)
72 {
73         _film_changed_connection.disconnect ();
74         _film_audio_analysis_finished_connection.disconnect ();
75         
76         _film = f;
77
78         try_to_load_analysis ();
79         setup_channels ();
80         _plot->set_gain (_film->audio_gain ());
81
82         _film_changed_connection = _film->Changed.connect (bind (&AudioDialog::film_changed, this, _1));
83         _film_audio_analysis_finished_connection = _film->AudioAnalysisFinished.connect (bind (&AudioDialog::try_to_load_analysis, this));
84 }
85
86 void
87 AudioDialog::setup_channels ()
88 {
89         if (!_film->audio_stream()) {
90                 return;
91         }
92         
93         for (int i = 0; i < _film->audio_stream()->channels(); ++i) {
94                 _channel_checkbox[i]->Show ();
95         }
96
97         for (int i = _film->audio_stream()->channels(); i < MAX_AUDIO_CHANNELS; ++i) {
98                 _channel_checkbox[i]->Hide ();
99         }
100 }       
101
102 void
103 AudioDialog::try_to_load_analysis ()
104 {
105         shared_ptr<AudioAnalysis> a;
106
107         if (boost::filesystem::exists (_film->audio_analysis_path())) {
108                 a.reset (new AudioAnalysis (_film->audio_analysis_path ()));
109         } else {
110                 if (IsShown ()) {
111                         _film->analyse_audio ();
112                 }
113         }
114                 
115         _plot->set_analysis (a);
116         _channel_checkbox[0]->SetValue (true);
117         _plot->set_channel_visible (0, true);
118
119         for (int i = 0; i < AudioPoint::COUNT; ++i) {
120                 _type_checkbox[i]->SetValue (true);
121                 _plot->set_type_visible (i, true);
122         }
123 }
124
125 void
126 AudioDialog::channel_clicked (wxCommandEvent& ev)
127 {
128         int c = 0;
129         while (c < MAX_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
130                 ++c;
131         }
132
133         assert (c < MAX_AUDIO_CHANNELS);
134         
135         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
136 }
137
138 void
139 AudioDialog::film_changed (Film::Property p)
140 {
141         switch (p) {
142         case Film::AUDIO_GAIN:
143                 _plot->set_gain (_film->audio_gain ());
144                 break;
145         case Film::CONTENT_AUDIO_STREAM:
146         case Film::EXTERNAL_AUDIO:
147         case Film::USE_CONTENT_AUDIO:
148                 setup_channels ();
149                 break;
150         default:
151                 break;
152         }
153 }
154
155 void
156 AudioDialog::type_clicked (wxCommandEvent& ev)
157 {
158         int t = 0;
159         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
160                 ++t;
161         }
162
163         assert (t < AudioPoint::COUNT);
164
165         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
166 }