Ardour  9.0-pre0-582-g084a23a80d
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 #pragma once
21 
22 #include <stddef.h>
23 
24 #include "ardour/dB.h"
26 
27 #include "pbd/i18n.h"
28 
29 namespace ARDOUR {
30 
31 inline std::string
33  double v)
34 {
35  char buf[32];
36 
37  if (desc.scale_points) {
38  // Check if value is on a scale point
39  for (ARDOUR::ScalePoints::const_iterator i = desc.scale_points->begin();
40  i != desc.scale_points->end();
41  ++i) {
42  if (i->second == v) {
43  return i->first; // Found it, return scale point label
44  }
45  }
46  }
47 
48  if (desc.toggled) {
49  return v > 0 ? _("on") : _("off");
50  }
51 
52  // Value is not a scale point, print it normally
54  snprintf(buf, sizeof(buf), "%s", ParameterDescriptor::midi_note_name (rint(v)).c_str());
55  } else if (desc.type == GainAutomation || desc.type == BusSendLevel || desc.type == TrimAutomation || desc.type == EnvelopeAutomation || desc.type == MainOutVolume || desc.type == SurroundSendLevel || desc.type == InsertReturnLevel) {
56 #ifdef PLATFORM_WINDOWS
57  if (v < GAIN_COEFF_SMALL) {
58  snprintf(buf, sizeof(buf), "-inf dB");
59  } else {
60  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
61  }
62 #else
63  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
64 #endif
65  } else if (desc.type == PanWidthAutomation) {
66  snprintf (buf, sizeof (buf), "%d%%", (int) floor (100.0 * v));
67  } else if (!desc.print_fmt.empty()) {
68  snprintf(buf, sizeof(buf), desc.print_fmt.c_str(), v);
69  } else if (desc.integer_step) {
70  snprintf(buf, sizeof(buf), "%d", (int)v);
71  } else if (desc.upper - desc.lower >= 1000) {
72  snprintf(buf, sizeof(buf), "%.1f", v);
73  } else if (desc.upper - desc.lower >= 100) {
74  snprintf(buf, sizeof(buf), "%.2f", v);
75  } else {
76  snprintf(buf, sizeof(buf), "%.3f", v);
77  }
78  if (desc.print_fmt.empty() && desc.unit == ARDOUR::ParameterDescriptor::DB) {
79  // TODO: Move proper dB printing from AutomationLine here
80  return std::string(buf) + " dB";
81  }
82  return buf;
83 }
84 
85 inline std::string
87  const ARDOUR::Variant& val)
88 {
89  // Only numeric support, for now
90  return value_as_string(desc, val.to_double());
91 }
92 
93 } // namespace ARDOUR
94 
double to_double() const
Definition: variant.h:104
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.