update_steps(), properly handle integer ranges
[ardour.git] / libs / ardour / ardour / parameter_descriptor.h
1 /*
2     Copyright (C) 2014 Paul Davis
3     Author: David Robillard
4
5     This program is free software; you can redistribute it and/or modify it
6     under the terms of the GNU General Public License as published by the Free
7     Software Foundation; either version 2 of the License, or (at your option)
8     any later version.
9
10     This program is distributed in the hope that it will be useful, but WITHOUT
11     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12     FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
13     for more details.
14
15     You should have received a copy of the GNU General Public License along
16     with this program; if not, write to the Free Software Foundation, Inc.,
17     675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #ifndef __ardour_parameter_descriptor_h__
21 #define __ardour_parameter_descriptor_h__
22
23 #include "ardour/variant.h"
24 #include "evoral/Parameter.hpp"
25
26 namespace ARDOUR {
27
28 typedef std::map<const std::string, const float> ScalePoints;
29
30 /** Descriptor of a parameter or control.
31  *
32  * Essentially a union of LADSPA, VST and LV2 info.
33  */
34 struct ParameterDescriptor
35 {
36         enum Unit {
37                 NONE,       ///< No unit
38                 DB,         ///< Decibels
39                 MIDI_NOTE,  ///< MIDI note number
40                 HZ,         ///< Frequency in Hertz
41         };
42
43         ParameterDescriptor(const Evoral::Parameter& parameter)
44                 : key((uint32_t)-1)
45                 , datatype(Variant::NOTHING)
46                 , unit(NONE)
47                 , normal(parameter.normal())
48                 , lower(parameter.min())
49                 , upper(parameter.max())
50                 , step(0)
51                 , smallstep(0)
52                 , largestep(0)
53                 , integer_step(parameter.type() >= MidiCCAutomation &&
54                                parameter.type() <= MidiChannelPressureAutomation)
55                 , toggled(parameter.toggled())
56                 , logarithmic(false)
57                 , sr_dependent(false)
58                 , min_unbound(0)
59                 , max_unbound(0)
60                 , enumeration(false)
61         {
62                 if (parameter.type() == GainAutomation) {
63                         unit = DB;
64                 }
65                 update_steps();
66         }
67
68         ParameterDescriptor()
69                 : key((uint32_t)-1)
70                 , datatype(Variant::NOTHING)
71                 , unit(NONE)
72                 , normal(0)
73                 , lower(0)
74                 , upper(0)
75                 , step(0)
76                 , smallstep(0)
77                 , largestep(0)
78                 , integer_step(false)
79                 , toggled(false)
80                 , logarithmic(false)
81                 , sr_dependent(false)
82                 , min_unbound(0)
83                 , max_unbound(0)
84                 , enumeration(false)
85         {}
86
87         /* Set step, smallstep, and largestep, based on current description */
88         void update_steps() {
89                 if (unit == ParameterDescriptor::MIDI_NOTE) {
90                         step      = smallstep = 1;  // semitone
91                         largestep = 12;             // octave
92                 } else if (integer_step) {
93                         const float delta = upper - lower;
94
95                         smallstep = delta / 10000.0f;
96                         step      = delta / 1000.0f;
97                         largestep = delta / 40.0f;
98
99                         smallstep = std::max(1.0, rint(smallstep));
100                         step      = std::max(1.0, rint(step));
101                         largestep = std::max(1.0, rint(largestep));
102                 }
103                 /* else: leave all others as default '0'
104                  * in that case the UI (eg. AutomationController::create)
105                  * uses internal_to_interface() to map the value
106                  * to an appropriate interface range
107                  */
108         }
109
110         std::string                    label;
111         std::string                    print_fmt;  ///< format string for pretty printing
112         boost::shared_ptr<ScalePoints> scale_points;
113         uint32_t                       key;  ///< for properties
114         Variant::Type                  datatype;  ///< for properties
115         Unit                           unit;
116         float                          normal;
117         float                          lower;  ///< for frequencies, this is in Hz (not a fraction of the sample rate)
118         float                          upper;  ///< for frequencies, this is in Hz (not a fraction of the sample rate)
119         float                          step;
120         float                          smallstep;
121         float                          largestep;
122         bool                           integer_step;
123         bool                           toggled;
124         bool                           logarithmic;
125         bool                           sr_dependent;
126         bool                           min_unbound;
127         bool                           max_unbound;
128         bool                           enumeration;
129 };
130
131 } // namespace ARDOUR
132
133 #endif // __ardour_parameter_descriptor_h__