Merge branch '1.0' of ssh://houllier/home/carl/git/dvdomatic into 1.0
[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 "audio_dialog.h"
24 #include "audio_plot.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         for (int i = 0; i < MAX_AUDIO_CHANNELS; ++i) {
48                 _channel_checkbox[i] = new wxCheckBox (this, wxID_ANY, std_to_wx (audio_channel_name (i)));
49                 side->Add (_channel_checkbox[i], 1, wxEXPAND | wxALL, 3);
50                 _channel_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::channel_clicked), 0, this);
51         }
52
53         {
54                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Type"));
55                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
56         }
57         
58         wxString const types[] = {
59                 _("Peak"),
60                 _("RMS")
61         };
62
63         for (int i = 0; i < AudioPoint::COUNT; ++i) {
64                 _type_checkbox[i] = new wxCheckBox (this, wxID_ANY, types[i]);
65                 side->Add (_type_checkbox[i], 1, wxEXPAND | wxALL, 3);
66                 _type_checkbox[i]->Connect (wxID_ANY, wxEVT_COMMAND_CHECKBOX_CLICKED, wxCommandEventHandler (AudioDialog::type_clicked), 0, this);
67         }
68
69         {
70                 wxStaticText* m = new wxStaticText (this, wxID_ANY, _("Smoothing"));
71                 side->Add (m, 1, wxALIGN_CENTER_VERTICAL | wxTOP, 16);
72         }
73         
74         _smoothing = new wxSlider (this, wxID_ANY, AudioPlot::max_smoothing / 2, 1, AudioPlot::max_smoothing);
75         _smoothing->Connect (wxID_ANY, wxEVT_SCROLL_THUMBTRACK, wxScrollEventHandler (AudioDialog::smoothing_changed), 0, this);
76         side->Add (_smoothing, 1, wxEXPAND);
77
78         sizer->Add (side, 0, wxALL, 12);
79
80         SetSizer (sizer);
81         sizer->Layout ();
82         sizer->SetSizeHints (this);
83 }
84
85 void
86 AudioDialog::set_content (shared_ptr<AudioContent> c)
87 {
88         _content_changed_connection.disconnect ();
89         
90         _content = c;
91
92         try_to_load_analysis ();
93         _plot->set_gain (_content->audio_gain ());
94
95         _content_changed_connection = _content->Changed.connect (bind (&AudioDialog::content_changed, this, _2));
96
97         SetTitle (wxString::Format (_("DCP-o-matic audio - %s"), std_to_wx(_content->file().filename().string()).data()));
98 }
99
100
101 void
102 AudioDialog::try_to_load_analysis ()
103 {
104         shared_ptr<AudioAnalysis> a;
105
106         if (boost::filesystem::exists (_content->audio_analysis_path())) {
107                 a.reset (new AudioAnalysis (_content->audio_analysis_path ()));
108         } else {
109                 if (IsShown ()) {
110                         _content->analyse_audio (bind (&AudioDialog::try_to_load_analysis, this));
111                 }
112         }
113                 
114         _plot->set_analysis (a);
115
116         if (_channel_checkbox[0]) {
117                 _channel_checkbox[0]->SetValue (true);
118         }
119         _plot->set_channel_visible (0, true);
120
121         for (int i = 0; i < AudioPoint::COUNT; ++i) {
122                 _type_checkbox[i]->SetValue (true);
123                 _plot->set_type_visible (i, true);
124         }
125 }
126
127 void
128 AudioDialog::channel_clicked (wxCommandEvent& ev)
129 {
130         int c = 0;
131         while (c < MAX_AUDIO_CHANNELS && ev.GetEventObject() != _channel_checkbox[c]) {
132                 ++c;
133         }
134
135         assert (c < MAX_AUDIO_CHANNELS);
136
137         _plot->set_channel_visible (c, _channel_checkbox[c]->GetValue ());
138 }
139
140 void
141 AudioDialog::content_changed (int p)
142 {
143         if (p == AudioContentProperty::AUDIO_GAIN) {
144                 _plot->set_gain (_content->audio_gain ());
145         }
146 }
147
148 void
149 AudioDialog::type_clicked (wxCommandEvent& ev)
150 {
151         int t = 0;
152         while (t < AudioPoint::COUNT && ev.GetEventObject() != _type_checkbox[t]) {
153                 ++t;
154         }
155
156         assert (t < AudioPoint::COUNT);
157
158         _plot->set_type_visible (t, _type_checkbox[t]->GetValue ());
159 }
160
161 void
162 AudioDialog::smoothing_changed (wxScrollEvent &)
163 {
164         _plot->set_smoothing (_smoothing->GetValue ());
165 }