Fix confusion with AUDIO_STREAMS property.
[dcpomatic.git] / src / wx / audio_panel.cc
1 /*
2     Copyright (C) 2012-2015 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 "lib/config.h"
21 #include "lib/ffmpeg_content.h"
22 #include "lib/cinema_sound_processor.h"
23 #include "audio_dialog.h"
24 #include "audio_panel.h"
25 #include "audio_mapping_view.h"
26 #include "wx_util.h"
27 #include "gain_calculator_dialog.h"
28 #include "content_panel.h"
29 #include <wx/spinctrl.h>
30 #include <boost/lexical_cast.hpp>
31 #include <boost/foreach.hpp>
32
33 using std::vector;
34 using std::cout;
35 using std::string;
36 using std::list;
37 using std::pair;
38 using boost::dynamic_pointer_cast;
39 using boost::lexical_cast;
40 using boost::shared_ptr;
41
42 AudioPanel::AudioPanel (ContentPanel* p)
43         : ContentSubPanel (p, _("Audio"))
44         , _audio_dialog (0)
45 {
46         wxGridBagSizer* grid = new wxGridBagSizer (DCPOMATIC_SIZER_X_GAP, DCPOMATIC_SIZER_Y_GAP);
47         _sizer->Add (grid, 0, wxALL, 8);
48
49         int r = 0;
50
51         _show = new wxButton (this, wxID_ANY, _("Show Audio..."));
52         grid->Add (_show, wxGBPosition (r, 0));
53         ++r;
54
55         add_label_to_grid_bag_sizer (grid, this, _("Gain"), true, wxGBPosition (r, 0));
56         _gain = new ContentSpinCtrlDouble<AudioContent> (
57                 this,
58                 new wxSpinCtrlDouble (this),
59                 AudioContentProperty::AUDIO_GAIN,
60                 boost::mem_fn (&AudioContent::audio_gain),
61                 boost::mem_fn (&AudioContent::set_audio_gain)
62                 );
63         
64         _gain->add (grid, wxGBPosition (r, 1));
65         add_label_to_grid_bag_sizer (grid, this, _("dB"), false, wxGBPosition (r, 2));
66         _gain_calculate_button = new wxButton (this, wxID_ANY, _("Calculate..."));
67         grid->Add (_gain_calculate_button, wxGBPosition (r, 3));
68         ++r;
69
70         add_label_to_grid_bag_sizer (grid, this, _("Delay"), true, wxGBPosition (r, 0));
71         _delay = new ContentSpinCtrl<AudioContent> (
72                 this,
73                 new wxSpinCtrl (this),
74                 AudioContentProperty::AUDIO_DELAY,
75                 boost::mem_fn (&AudioContent::audio_delay),
76                 boost::mem_fn (&AudioContent::set_audio_delay)
77                 );
78         
79         _delay->add (grid, wxGBPosition (r, 1));
80         /// TRANSLATORS: this is an abbreviation for milliseconds, the unit of time
81         add_label_to_grid_bag_sizer (grid, this, _("ms"), false, wxGBPosition (r, 2));
82         ++r;
83
84         _mapping = new AudioMappingView (this);
85         _sizer->Add (_mapping, 1, wxEXPAND | wxALL, 6);
86         ++r;
87
88         _description = new wxStaticText (this, wxID_ANY, wxT (" \n"), wxDefaultPosition, wxDefaultSize);
89         _sizer->Add (_description, 0, wxALL, 12);
90         wxFont font = _description->GetFont();
91         font.SetStyle (wxFONTSTYLE_ITALIC);
92         font.SetPointSize (font.GetPointSize() - 1);
93         _description->SetFont (font);
94         ++r;
95
96         _gain->wrapped()->SetRange (-60, 60);
97         _gain->wrapped()->SetDigits (1);
98         _gain->wrapped()->SetIncrement (0.5);
99         _delay->wrapped()->SetRange (-1000, 1000);
100
101         _show->Bind                  (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::show_clicked, this));
102         _gain_calculate_button->Bind (wxEVT_COMMAND_BUTTON_CLICKED,  boost::bind (&AudioPanel::gain_calculate_button_clicked, this));
103
104         _mapping_connection = _mapping->Changed.connect (boost::bind (&AudioPanel::mapping_changed, this, _1));
105 }
106
107
108 void
109 AudioPanel::film_changed (Film::Property property)
110 {
111         switch (property) {
112         case Film::AUDIO_CHANNELS:
113                 _mapping->set_channels (_parent->film()->audio_channels ());
114                 _sizer->Layout ();
115                 break;
116         case Film::VIDEO_FRAME_RATE:
117                 setup_description ();
118                 break;
119         default:
120                 break;
121         }
122 }
123
124 void
125 AudioPanel::film_content_changed (int property)
126 {
127         AudioContentList ac = _parent->selected_audio ();
128         shared_ptr<AudioContent> acs;
129         shared_ptr<FFmpegContent> fcs;
130         if (ac.size() == 1) {
131                 acs = ac.front ();
132                 fcs = dynamic_pointer_cast<FFmpegContent> (acs);
133         }
134         
135         if (property == AudioContentProperty::AUDIO_STREAMS) {
136                 _mapping->set (acs ? acs->audio_mapping () : AudioMapping ());
137                 _sizer->Layout ();
138                 setup_description ();
139         }
140 }
141
142 void
143 AudioPanel::gain_calculate_button_clicked ()
144 {
145         GainCalculatorDialog* d = new GainCalculatorDialog (this);
146         int const r = d->ShowModal ();
147
148         if (r == wxID_CANCEL || d->wanted_fader() == 0 || d->actual_fader() == 0) {
149                 d->Destroy ();
150                 return;
151         }
152         
153         _gain->wrapped()->SetValue (
154                 Config::instance()->cinema_sound_processor()->db_for_fader_change (
155                         d->wanted_fader (),
156                         d->actual_fader ()
157                         )
158                 );
159
160         /* This appears to be necessary, as the change is not signalled,
161            I think.
162         */
163         _gain->view_changed ();
164         
165         d->Destroy ();
166 }
167
168 void
169 AudioPanel::show_clicked ()
170 {
171         if (_audio_dialog) {
172                 _audio_dialog->Destroy ();
173                 _audio_dialog = 0;
174         }
175
176         AudioContentList ac = _parent->selected_audio ();
177         if (ac.size() != 1) {
178                 return;
179         }
180         
181         _audio_dialog = new AudioDialog (this, _parent->film ());
182         _audio_dialog->Show ();
183         _audio_dialog->set_content (ac.front ());
184 }
185
186 void
187 AudioPanel::setup_description ()
188 {
189         AudioContentList ac = _parent->selected_audio ();
190         if (ac.size () != 1) {
191                 checked_set (_description, wxT (""));
192                 return;
193         }
194
195         checked_set (_description, ac.front()->processing_description ());
196 }
197
198 void
199 AudioPanel::mapping_changed (AudioMapping m)
200 {
201         AudioContentList c = _parent->selected_audio ();
202         if (c.size() == 1) {
203                 c.front()->set_audio_mapping (m);
204         }
205 }
206
207 void
208 AudioPanel::content_selection_changed ()
209 {
210         AudioContentList sel = _parent->selected_audio ();
211
212         if (_audio_dialog && sel.size() == 1) {
213                 _audio_dialog->set_content (sel.front ());
214         }
215         
216         _gain->set_content (sel);
217         _delay->set_content (sel);
218
219         _gain_calculate_button->Enable (sel.size() == 1);
220         _show->Enable (sel.size() == 1);
221         _mapping->Enable (sel.size() == 1);
222
223         film_content_changed (AudioContentProperty::AUDIO_STREAMS);
224 }