89b04409a816d90899c6209844da93a094280fa6
[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         add_label_to_sizer (table, this, _("Channel"));
42         _channel = new wxChoice (this, wxID_ANY);
43         table->Add (_channel, 1, wxEXPAND | wxALL, 6);
44
45         sizer->Add (table);
46
47         SetSizer (sizer);
48         sizer->Layout ();
49         sizer->SetSizeHints (this);
50
51         _channel->Connect (wxID_ANY, wxEVT_COMMAND_CHOICE_SELECTED, wxCommandEventHandler (AudioDialog::channel_changed), 0, this);
52 }
53
54 void
55 AudioDialog::set_film (boost::shared_ptr<Film> f)
56 {
57         _film_changed_connection.disconnect ();
58         _film_audio_analysis_finished_connection.disconnect ();
59         
60         _film = f;
61
62         try_to_load_analysis ();
63         setup_channels ();
64
65         _channel->SetSelection (0);
66
67         _film_changed_connection = _film->Changed.connect (bind (&AudioDialog::film_changed, this, _1));
68         _film_audio_analysis_finished_connection = _film->AudioAnalysisFinished.connect (bind (&AudioDialog::try_to_load_analysis, this));
69 }
70
71 void
72 AudioDialog::setup_channels ()
73 {
74         _channel->Clear ();
75
76         if (!_film->audio_stream()) {
77                 return;
78         }
79         
80         for (int i = 0; i < _film->audio_stream()->channels(); ++i) {
81                 _channel->Append (audio_channel_name (i));
82         }
83 }       
84
85 void
86 AudioDialog::try_to_load_analysis ()
87 {
88         shared_ptr<AudioAnalysis> a;
89
90         if (boost::filesystem::exists (_film->audio_analysis_path())) {
91                 a.reset (new AudioAnalysis (_film->audio_analysis_path ()));
92         } else {
93                 if (IsShown ()) {
94                         _film->analyse_audio ();
95                 }
96         }
97                 
98         _plot->set_analysis (a);
99 }
100
101 void
102 AudioDialog::channel_changed (wxCommandEvent &)
103 {
104         _plot->set_channel (_channel->GetSelection ());
105 }
106
107 void
108 AudioDialog::film_changed (Film::Property p)
109 {
110         switch (p) {
111         case Film::AUDIO_GAIN:
112                 _plot->set_gain (_film->audio_gain ());
113                 break;
114         case Film::CONTENT_AUDIO_STREAM:
115         case Film::EXTERNAL_AUDIO:
116         case Film::USE_CONTENT_AUDIO:
117                 setup_channels ();
118                 break;
119         default:
120                 break;
121         }
122 }