Ardour  8.12
value_as_string.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 David Robillard <d@drobilla.net>
3  * Copyright (C) 2016-2017 Robin Gareus <robin@gareus.org>
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 along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19 
20 #ifndef __ardour_value_as_string_h__
21 #define __ardour_value_as_string_h__
22 
23 #include <stddef.h>
24 
25 #include "ardour/dB.h"
27 
28 #include "pbd/i18n.h"
29 
30 namespace ARDOUR {
31 
32 inline std::string
34  double v)
35 {
36  char buf[32];
37 
38  if (desc.scale_points) {
39  // Check if value is on a scale point
40  for (ARDOUR::ScalePoints::const_iterator i = desc.scale_points->begin();
41  i != desc.scale_points->end();
42  ++i) {
43  if (i->second == v) {
44  return i->first; // Found it, return scale point label
45  }
46  }
47  }
48 
49  if (desc.toggled) {
50  return v > 0 ? _("on") : _("off");
51  }
52 
53  // Value is not a scale point, print it normally
55  snprintf(buf, sizeof(buf), "%s", ParameterDescriptor::midi_note_name (rint(v)).c_str());
56  } else if (desc.type == GainAutomation || desc.type == BusSendLevel || desc.type == TrimAutomation || desc.type == EnvelopeAutomation || desc.type == MainOutVolume || desc.type == SurroundSendLevel || desc.type == InsertReturnLevel) {
57 #ifdef PLATFORM_WINDOWS
58  if (v < GAIN_COEFF_SMALL) {
59  snprintf(buf, sizeof(buf), "-inf dB");
60  } else {
61  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
62  }
63 #else
64  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
65 #endif
66  } else if (desc.type == PanWidthAutomation) {
67  snprintf (buf, sizeof (buf), "%d%%", (int) floor (100.0 * v));
68  } else if (!desc.print_fmt.empty()) {
69  snprintf(buf, sizeof(buf), desc.print_fmt.c_str(), v);
70  } else if (desc.integer_step) {
71  snprintf(buf, sizeof(buf), "%d", (int)v);
72  } else if (desc.upper - desc.lower >= 1000) {
73  snprintf(buf, sizeof(buf), "%.1f", v);
74  } else if (desc.upper - desc.lower >= 100) {
75  snprintf(buf, sizeof(buf), "%.2f", v);
76  } else {
77  snprintf(buf, sizeof(buf), "%.3f", v);
78  }
79  if (desc.print_fmt.empty() && desc.unit == ARDOUR::ParameterDescriptor::DB) {
80  // TODO: Move proper dB printing from AutomationLine here
81  return std::string(buf) + " dB";
82  }
83  return buf;
84 }
85 
86 inline std::string
88  const ARDOUR::Variant& val)
89 {
90  // Only numeric support, for now
91  return value_as_string(desc, val.to_double());
92 }
93 
94 } // namespace ARDOUR
95 
96 #endif /* __ardour_value_as_string_h__ */
double to_double() const
Definition: variant.h:105
static float accurate_coefficient_to_dB(float coeff)
Definition: dB.h:39
#define GAIN_COEFF_SMALL
Definition: dB.h:28
#define _(Text)
Definition: i18n.h:29
std::string value_as_string(const ARDOUR::ParameterDescriptor &desc, double v)
std::string print_fmt
format string for pretty printing
std::shared_ptr< ScalePoints > scale_points
static std::string midi_note_name(uint8_t, bool translate=true)
float upper
Maximum value (in Hz, for frequencies)
float lower
Minimum value (in Hz, for frequencies)
bool toggled
True iff parameter is boolean.