Basics of content dialogs.
[dcpomatic.git] / src / lib / audio_mapping.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 "audio_mapping.h"
21
22 using std::map;
23 using boost::optional;
24
25 AutomaticAudioMapping::AutomaticAudioMapping (int c)
26         : _source_channels (c)
27 {
28
29 }
30
31 optional<libdcp::Channel>
32 AutomaticAudioMapping::source_to_dcp (int c) const
33 {
34         if (c >= _source_channels) {
35                 return optional<libdcp::Channel> ();
36         }
37
38         if (_source_channels == 1) {
39                 /* mono sources to centre */
40                 return libdcp::CENTRE;
41         }
42         
43         return static_cast<libdcp::Channel> (c);
44 }
45
46 optional<int>
47 AutomaticAudioMapping::dcp_to_source (libdcp::Channel c) const
48 {
49         if (_source_channels == 1) {
50                 if (c == libdcp::CENTRE) {
51                         return 0;
52                 } else {
53                         return optional<int> ();
54                 }
55         }
56
57         if (static_cast<int> (c) >= _source_channels) {
58                 return optional<int> ();
59         }
60         
61         return static_cast<int> (c);
62 }
63
64 int
65 AutomaticAudioMapping::dcp_channels () const
66 {
67         if (_source_channels == 1) {
68                 /* The source is mono, so to put the mono channel into
69                    the centre we need to generate a 5.1 soundtrack.
70                 */
71                 return 6;
72         }
73
74         return _source_channels;
75 }
76
77 optional<int>
78 ConfiguredAudioMapping::dcp_to_source (libdcp::Channel c) const
79 {
80         map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.begin ();
81         while (i != _source_to_dcp.end() && i->second != c) {
82                 ++i;
83         }
84
85         if (i == _source_to_dcp.end ()) {
86                 return boost::none;
87         }
88
89         return i->first;
90 }
91
92 optional<libdcp::Channel>
93 ConfiguredAudioMapping::source_to_dcp (int c) const
94 {
95         map<int, libdcp::Channel>::const_iterator i = _source_to_dcp.find (c);
96         if (i == _source_to_dcp.end ()) {
97                 return boost::none;
98         }
99
100         return i->second;
101 }
102
103