Support pretty display of arbitrary plugin parameter units.
[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         };
41
42         ParameterDescriptor(const Evoral::Parameter& parameter)
43                 : key((uint32_t)-1)
44                 , datatype(Variant::VOID)
45                 , normal(parameter.normal())
46                 , lower(parameter.min())
47                 , upper(parameter.max())
48                 , step((upper - lower) / 100.0f)
49                 , smallstep((upper - lower) / 1000.0f)
50                 , largestep((upper - lower) / 10.0f)
51                 , integer_step(parameter.type() >= MidiCCAutomation &&
52                                parameter.type() <= MidiChannelPressureAutomation)
53                 , toggled(parameter.toggled())
54                 , logarithmic(false)
55                 , sr_dependent(false)
56                 , min_unbound(0)
57                 , max_unbound(0)
58                 , enumeration(false)
59         {
60                 if (parameter.type() == GainAutomation) {
61                         unit = DB;
62                 }
63         }
64
65         ParameterDescriptor()
66                 : key((uint32_t)-1)
67                 , datatype(Variant::VOID)
68                 , normal(0)
69                 , lower(0)
70                 , upper(0)
71                 , step(0)
72                 , smallstep(0)
73                 , largestep(0)
74                 , integer_step(false)
75                 , toggled(false)
76                 , logarithmic(false)
77                 , sr_dependent(false)
78                 , min_unbound(0)
79                 , max_unbound(0)
80                 , enumeration(false)
81         {}
82
83         /// Set step, smallstep, and largestep, based on current description
84         void update_steps() {
85                 if (unit == ParameterDescriptor::MIDI_NOTE) {
86                         step      = smallstep = 1;  // semitone
87                         largestep = 12;             // octave
88                 } else {
89                         const float delta = upper - lower;
90
91                         step      = delta / 1000.0f;
92                         smallstep = delta / 10000.0f;
93                         largestep = delta / 10.0f;
94
95                         if (integer_step) {
96                                 step      = rint(step);
97                                 largestep = rint(largestep);
98                                 // leave smallstep alone for fine tuning
99                         }
100                 }
101         }
102
103         std::string                    label;
104         std::string                    print_fmt;  ///< format string for pretty printing
105         boost::shared_ptr<ScalePoints> scale_points;
106         uint32_t                       key;  ///< for properties
107         Variant::Type                  datatype;  ///< for properties
108         Unit                           unit;
109         float                          normal;
110         float                          lower;  ///< for frequencies, this is in Hz (not a fraction of the sample rate)
111         float                          upper;  ///< for frequencies, this is in Hz (not a fraction of the sample rate)
112         float                          step;
113         float                          smallstep;
114         float                          largestep;
115         bool                           integer_step;
116         bool                           toggled;
117         bool                           logarithmic;
118         bool                           sr_dependent;
119         bool                           min_unbound;
120         bool                           max_unbound;
121         bool                           enumeration;
122 };
123
124 } // namespace ARDOUR
125
126 #endif // __ardour_parameter_descriptor_h__