Fix parameter range stuff and automation time axis height (somewhat...).
[ardour.git] / libs / evoral / evoral / Parameter.hpp
1 /* This file is part of Evoral.
2  * Copyright (C) 2008 Dave Robillard <http://drobilla.net>
3  * Copyright (C) 2000-2008 Paul Davis
4  * 
5  * Evoral is free software; you can redistribute it and/or modify it under the
6  * terms of the GNU General Public License as published by the Free Software
7  * Foundation; either version 2 of the License, or (at your option) any later
8  * version.
9  * 
10  * Evoral is distributed in the hope that it will be useful, but WITHOUT ANY
11  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for details.
13  * 
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #ifndef EVORAL_PARAMETER_HPP
20 #define EVORAL_PARAMETER_HPP
21
22 #include <string>
23 #include <map>
24 #include <boost/shared_ptr.hpp>
25 #include <boost/format.hpp>
26 #include <iostream>
27
28 namespace Evoral {
29
30
31 /** ID of a [play|record|automate]able parameter.
32  *
33  * A parameter is defined by (type, id, channel).  Type is an integer which
34  * can be used in any way by the application (e.g. cast to a custom enum,
35  * map to/from a URI, etc).  ID is type specific (e.g. MIDI controller #).
36  *
37  * This class defines a < operator which is a strict weak ordering, so
38  * Parameter may be stored in a std::set, used as a std::map key, etc.
39  */
40 class Parameter
41 {
42 public:
43         Parameter(uint32_t type, uint8_t channel, uint32_t id=0)
44                 : _type(type), _id(id), _channel(channel)
45         {}
46         
47         Parameter(const std::string& str) {
48                 int channel;
49                 if (sscanf(str.c_str(), "%d_c%d_n%d", &_type, &channel, &_id) == 3) {
50                         if (channel >= 0 && channel <= 127) {
51                                 _channel = channel;
52                         } else {
53                                 std::cerr << "WARNING: Channel out of range: " << channel << std::endl;
54                         }
55                 }
56                 std::cerr << "WARNING: Unable to create parameter from string: " << str << std::endl;
57         }
58
59         inline uint32_t type()    const { return _type; }
60         inline uint32_t id()      const { return _id; }
61         inline uint8_t  channel() const { return _channel; }
62
63         /** Equivalence operator
64          * It is obvious from the definition that this operator
65          * is transitive, as required by stict weak ordering
66          * (see: http://www.sgi.com/tech/stl/StrictWeakOrdering.html)
67          */
68         inline bool operator==(const Parameter& id) const {
69                 return (_type == id._type && _id == id._id && _channel == id._channel);
70         }
71         
72         /** Strict weak ordering
73          * See: http://www.sgi.com/tech/stl/StrictWeakOrdering.html
74          * Sort Parameters first according to type then to id and lastly to channel.
75          *  
76          * Proof:
77          * <ol>
78          * <li>Irreflexivity: f(x, x) is false because of the irreflexivity of \c < in each branch.</li>
79          * <li>Antisymmetry: given x != y, f(x, y) implies !f(y, x) because of the same 
80          *     property of \c < in each branch and the symmetry of operator==. </li>
81          * <li>Transitivity: let f(x, y) and f(y, z) be true.
82          *    We prove by contradiction, assuming the contrary (f(x, z) is false).
83          *    That would imply exactly one of the following:
84          *        <ol>
85          *      <li> x == z which contradicts the assumption f(x, y) and f(y, x)
86          *                 because of antisymmetry.
87          *      </li>
88          *      <li> f(z, x) is true. That would imply that one of the ivars (we call it i) 
89          *           of x is greater than the same ivar in z while all "previous" ivars
90          *           are equal. That would imply that also in y all those "previous"
91          *           ivars are equal and because if x.i > z.i it is impossible
92          *           that there is an y that satisfies x.i < y.i < z.i at the same
93          *           time which contradicts the assumption.
94          *      </li>
95          *      Therefore f(x, z) is true (transitivity)
96          *    </ol> 
97          * </li>
98          * </ol>
99          */
100         inline bool operator<(const Parameter& id) const {
101                 if (_type < id._type) {
102                         return true;
103                 } else if (_type == id._type && _id < id._id) {
104                         return true;
105                 } else if (_id == id._id && _channel < id._channel) {
106                         return true;
107                 }
108                 
109                 return false;
110         }
111         
112         inline operator bool() const { return (_type != 0); }
113         
114         virtual std::string symbol() const {
115                 return (boost::format("%1%_c%2%_n%3%") % _type % (int)_channel % _id).str();
116         }
117
118         /** Not used in indentity/comparison */
119         struct Metadata {
120                 Metadata(double low=0.0, double high=1.0, double mid=0.0)
121                         : min(low), max(high), normal(mid)
122                 {}
123                 double min;
124                 double max;
125                 double normal;
126         };
127         
128         inline static void set_range(uint32_t type, double min, double max, double normal) {
129                 _type_metadata[type] = Metadata(min, max, normal);
130         }
131         
132         inline void set_range(double min, double max, double normal) {
133                 _metadata = boost::shared_ptr<Metadata>(new Metadata(min, max, normal));
134         }
135
136         inline Metadata& metadata() const {
137                 if (_metadata)
138                         return *_metadata.get();
139                 else
140                         return _type_metadata[_type];
141         }
142
143         inline const double min()    const { return metadata().min; }
144         inline const double max()    const { return metadata().max; }
145         inline const double normal() const { return metadata().normal; }
146
147 protected:
148         // Default copy constructor is ok
149         
150         // ID (used in comparison)
151         uint32_t _type;
152         uint32_t _id;
153         uint8_t  _channel;
154         
155         boost::shared_ptr<Metadata> _metadata;
156
157         typedef std::map<uint32_t, Metadata> TypeMetadata;
158         static TypeMetadata _type_metadata;
159 };
160
161
162 } // namespace Evoral
163
164 #endif // EVORAL_PARAMETER_HPP
165