Basics of per-channel audio gain.
[dcpomatic.git] / src / wx / audio_mapping_view.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 <wx/wx.h>
21 #include <wx/renderer.h>
22 #include <wx/grid.h>
23 #include <libdcp/types.h>
24 #include "lib/audio_mapping.h"
25 #include "lib/util.h"
26 #include "audio_mapping_view.h"
27 #include "wx_util.h"
28 #include "audio_gain_dialog.h"
29
30 using std::cout;
31 using std::list;
32 using std::string;
33 using std::max;
34 using boost::shared_ptr;
35 using boost::lexical_cast;
36
37 #define INDICATOR_SIZE 20
38
39 enum {
40         ID_off = 1,
41         ID_full = 2,
42         ID_minus3dB = 3,
43         ID_edit = 4
44 };
45
46 class NoSelectionStringRenderer : public wxGridCellStringRenderer
47 {
48 public:
49         void Draw (wxGrid& grid, wxGridCellAttr& attr, wxDC& dc, const wxRect& rect, int row, int col, bool)
50         {
51                 wxGridCellStringRenderer::Draw (grid, attr, dc, rect, row, col, false);
52         }
53 };
54
55 class ValueRenderer : public wxGridCellRenderer
56 {
57 public:
58
59         void Draw (wxGrid& grid, wxGridCellAttr &, wxDC& dc, const wxRect& rect, int row, int col, bool)
60         {
61                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (255, 255, 255), 1, wxPENSTYLE_SOLID));
62                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
63                 dc.DrawRectangle (rect);
64
65                 int const xo = (rect.GetWidth() - INDICATOR_SIZE) / 2;
66                 int const yo = (rect.GetHeight() - INDICATOR_SIZE) / 2;
67
68                 dc.SetPen (*wxThePenList->FindOrCreatePen (wxColour (0, 0, 0), 1, wxPENSTYLE_SOLID));
69                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (255, 255, 255), wxBRUSHSTYLE_SOLID));
70                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo, INDICATOR_SIZE, INDICATOR_SIZE));
71
72                 float const value = lexical_cast<float> (wx_to_std (grid.GetCellValue (row, col)));
73                 float const value_dB = 20 * log10 (value);
74                 int const range = 18;
75                 int height = 0;
76                 if (value_dB > -range) {
77                         height = INDICATOR_SIZE * (1 + value_dB / range);
78                 }
79
80                 height = max (0, height);
81                 
82                 if (value > 0) {
83                         /* Make sure we get a little bit of the marker if there is any gain */
84                         height = max (3, height);
85                 }
86
87                 dc.SetBrush (*wxTheBrushList->FindOrCreateBrush (wxColour (0, 255, 0), wxBRUSHSTYLE_SOLID));
88                 dc.DrawRectangle (wxRect (rect.GetLeft() + xo, rect.GetTop() + yo + INDICATOR_SIZE - height, INDICATOR_SIZE, height));
89         }
90
91         wxSize GetBestSize (wxGrid &, wxGridCellAttr &, wxDC &, int, int)
92         {
93                 return wxSize (INDICATOR_SIZE + 4, INDICATOR_SIZE + 4);
94         }
95         
96         wxGridCellRenderer* Clone () const
97         {
98                 return new ValueRenderer;
99         }
100 };
101
102
103 AudioMappingView::AudioMappingView (wxWindow* parent)
104         : wxPanel (parent, wxID_ANY)
105         , _menu_row (0)
106         , _menu_column (1)
107 {
108         _grid = new wxGrid (this, wxID_ANY);
109
110         _grid->CreateGrid (0, MAX_AUDIO_CHANNELS + 1);
111         _grid->HideRowLabels ();
112         _grid->DisableDragRowSize ();
113         _grid->DisableDragColSize ();
114         _grid->EnableEditing (false);
115         _grid->SetCellHighlightPenWidth (0);
116         _grid->SetDefaultRenderer (new NoSelectionStringRenderer);
117
118         set_column_labels ();
119
120         _sizer = new wxBoxSizer (wxVERTICAL);
121         _sizer->Add (_grid, 1, wxEXPAND | wxALL);
122         SetSizerAndFit (_sizer);
123
124         Bind (wxEVT_GRID_CELL_LEFT_CLICK, boost::bind (&AudioMappingView::left_click, this, _1));
125         Bind (wxEVT_GRID_CELL_RIGHT_CLICK, boost::bind (&AudioMappingView::right_click, this, _1));
126
127         _menu = new wxMenu;
128         _menu->Append (ID_off, _("Off"));
129         _menu->Append (ID_full, _("Full"));
130         _menu->Append (ID_minus3dB, _("-3dB"));
131         _menu->Append (ID_edit, _("Edit..."));
132
133         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::off, this), ID_off);
134         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::full, this), ID_full);
135         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::minus3dB, this), ID_minus3dB);
136         Bind (wxEVT_COMMAND_MENU_SELECTED, boost::bind (&AudioMappingView::edit, this), ID_edit);
137 }
138
139 void
140 AudioMappingView::left_click (wxGridEvent& ev)
141 {
142         if (ev.GetCol() == 0) {
143                 return;
144         }
145
146         libdcp::Channel d = static_cast<libdcp::Channel> (ev.GetCol() - 1);
147         
148         if (_map.get (ev.GetRow(), d) > 0) {
149                 _map.set (ev.GetRow(), d, 0);
150         } else {
151                 _map.set (ev.GetRow(), d, 1);
152         }
153
154         update_cells ();
155         Changed (_map);
156 }
157
158 void
159 AudioMappingView::right_click (wxGridEvent& ev)
160 {
161         if (ev.GetCol() == 0) {
162                 return;
163         }
164
165         _menu_row = ev.GetRow ();
166         _menu_column = ev.GetCol ();
167         PopupMenu (_menu, ev.GetPosition ());
168 }
169
170 void
171 AudioMappingView::off ()
172 {
173         _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 0);
174         update_cells ();
175         Changed (_map);
176 }
177
178 void
179 AudioMappingView::full ()
180 {
181         _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1);
182         update_cells ();
183         Changed (_map);
184 }
185
186 void
187 AudioMappingView::minus3dB ()
188 {
189         _map.set (_menu_row, static_cast<libdcp::Channel> (_menu_column - 1), 1 / sqrt (2));
190         update_cells ();
191         Changed (_map);
192 }
193
194 void
195 AudioMappingView::edit ()
196 {
197         libdcp::Channel d = static_cast<libdcp::Channel> (_menu_column - 1);
198         
199         AudioGainDialog* dialog = new AudioGainDialog (this, _menu_row, _menu_column - 1, _map.get (_menu_row, d));
200         if (dialog->ShowModal () == wxID_OK) {
201                 _map.set (_menu_row, d, dialog->value ());
202                 update_cells ();
203                 Changed (_map);
204         }
205         
206         dialog->Destroy ();
207 }
208
209 void
210 AudioMappingView::set (AudioMapping map)
211 {
212         _map = map;
213         update_cells ();
214 }
215
216 void
217 AudioMappingView::update_cells ()
218 {
219         if (_grid->GetNumberRows ()) {
220                 _grid->DeleteRows (0, _grid->GetNumberRows ());
221         }
222
223         _grid->InsertRows (0, _map.content_channels ());
224
225         for (int i = 0; i < _map.content_channels(); ++i) {
226                 for (int j = 0; j < MAX_AUDIO_CHANNELS; ++j) {
227                         _grid->SetCellRenderer (i, j + 1, new ValueRenderer);
228                 }
229         }
230         
231         for (int i = 0; i < _map.content_channels(); ++i) {
232                 _grid->SetCellValue (i, 0, wxString::Format (wxT("%d"), i + 1));
233
234                 for (int j = 1; j < _grid->GetNumberCols(); ++j) {
235                         _grid->SetCellValue (i, j, std_to_wx (lexical_cast<string> (_map.get (i, static_cast<libdcp::Channel> (j - 1)))));
236                 }
237         }
238
239         _grid->AutoSize ();
240 }
241
242 /** @param c Number of DCP channels */
243 void
244 AudioMappingView::set_channels (int c)
245 {
246         c++;
247
248         if (c < _grid->GetNumberCols ()) {
249                 _grid->DeleteCols (c, _grid->GetNumberCols() - c);
250         } else if (c > _grid->GetNumberCols ()) {
251                 _grid->InsertCols (_grid->GetNumberCols(), c - _grid->GetNumberCols());
252                 set_column_labels ();
253         }
254
255         update_cells ();
256 }
257
258 void
259 AudioMappingView::set_column_labels ()
260 {
261         int const c = _grid->GetNumberCols ();
262         
263         _grid->SetColLabelValue (0, _("Content channel"));
264         
265         if (c > 0) {
266                 _grid->SetColLabelValue (1, _("L"));
267         }
268         
269         if (c > 1) {
270                 _grid->SetColLabelValue (2, _("R"));
271         }
272         
273         if (c > 2) {
274                 _grid->SetColLabelValue (3, _("C"));
275         }
276         
277         if (c > 3) {
278                 _grid->SetColLabelValue (4, _("Lfe"));
279         }
280         
281         if (c > 4) {
282                 _grid->SetColLabelValue (5, _("Ls"));
283         }
284         
285         if (c > 5) {
286                 _grid->SetColLabelValue (6, _("Rs"));
287         }
288
289         _grid->AutoSize ();
290 }