Redesign Session+Route Template Meta Script API
[ardour.git] / libs / ardour / ardour / value_as_string.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
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation; either version 2 of the License, or
8     (at your option) any later version.
9
10     This program is distributed in the hope that it will be useful,
11     but WITHOUT ANY WARRANTY; without even the implied warranty of
12     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13     GNU General Public License for more details.
14
15     You should have received a copy of the GNU General Public License
16     along with this program; if not, write to the Free Software
17     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
19 */
20
21 #ifndef __ardour_value_as_string_h__
22 #define __ardour_value_as_string_h__
23
24 #include <stddef.h>
25
26 #include "ardour/dB.h"
27 #include "ardour/parameter_descriptor.h"
28
29 #include "pbd/i18n.h"
30
31 namespace ARDOUR {
32
33 inline std::string
34 value_as_string(const ARDOUR::ParameterDescriptor& desc,
35                 double                             v)
36 {
37         char buf[32];
38
39         if (desc.scale_points) {
40                 // Check if value is on a scale point
41                 for (ARDOUR::ScalePoints::const_iterator i = desc.scale_points->begin();
42                      i != desc.scale_points->end();
43                      ++i) {
44                         if (i->second == v) {
45                                 return i->first;  // Found it, return scale point label
46                         }
47                 }
48         }
49
50         if (desc.toggled) {
51                 return v > 0 ? _("on") : _("off");
52         }
53
54         // Value is not a scale point, print it normally
55         if (desc.unit == ARDOUR::ParameterDescriptor::MIDI_NOTE) {
56                 snprintf(buf, sizeof(buf), "%s", ParameterDescriptor::midi_note_name (rint(v)).c_str());
57         } else if (desc.type == GainAutomation || desc.type == TrimAutomation || desc.type == EnvelopeAutomation) {
58                 snprintf(buf, sizeof(buf), "%.1f dB", accurate_coefficient_to_dB (v));
59         } else if (desc.type == PanWidthAutomation) {
60                 snprintf (buf, sizeof (buf), "%d%%", (int) floor (100.0 * v));
61         } else if (!desc.print_fmt.empty()) {
62                 snprintf(buf, sizeof(buf), desc.print_fmt.c_str(), v);
63         } else if (desc.integer_step) {
64                 snprintf(buf, sizeof(buf), "%d", (int)v);
65         } else if (desc.upper - desc.lower >= 1000) {
66                 snprintf(buf, sizeof(buf), "%.1f", v);
67         } else if (desc.upper - desc.lower >= 100) {
68                 snprintf(buf, sizeof(buf), "%.2f", v);
69         } else {
70                 snprintf(buf, sizeof(buf), "%.3f", v);
71         }
72         if (desc.print_fmt.empty() && desc.unit == ARDOUR::ParameterDescriptor::DB) {
73                 // TODO: Move proper dB printing from AutomationLine here
74                 return std::string(buf) + " dB";
75         }
76         return buf;
77 }
78
79 inline std::string
80 value_as_string(const ARDOUR::ParameterDescriptor& desc,
81                 const ARDOUR::Variant&             val)
82 {
83         // Only numeric support, for now
84         return value_as_string(desc, val.to_double());
85 }
86
87 }  // namespace ARDOUR
88
89 #endif /* __ardour_value_as_string_h__ */