Ardour  9.2-79-gba93f2fe52
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 (auto const & [label,val] : *desc.scale_points) {
40  if (val == v) {
41  return label;
42  }
43  }
44  }
45 
46  if (desc.toggled) {
47  return v > 0 ? _("on") : _("off");
48  }
49 
50  // Value is not a scale point, print it normally
52  snprintf(buf, sizeof(buf), "%s", ParameterDescriptor::midi_note_name (rint(v)).c_str());
53  } else if (desc.type == GainAutomation
54  || desc.type == BusSendLevel
55  || desc.type == TrimAutomation
56  || desc.type == EnvelopeAutomation
57  || desc.type == MainOutVolume
58  || desc.type == SurroundSendLevel
59  || desc.type == InsertReturnLevel) {
60 #ifdef PLATFORM_WINDOWS
61  if (v < GAIN_COEFF_SMALL) {
62  snprintf(buf, sizeof(buf), "-inf dB");
63  } else {
64  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
65  }
66 #else
67  snprintf(buf, sizeof(buf), "%.2f dB", accurate_coefficient_to_dB (v));
68 #endif
69  } else if (desc.type == PanWidthAutomation) {
70  snprintf (buf, sizeof (buf), "%d%%", (int) floor (100.0 * v));
71  } else if (desc.type == MidiPitchBenderAutomation) {
72  snprintf (buf, sizeof (buf), "%d", (int) v - 8192);
73  } else if (!desc.print_fmt.empty()) {
74  snprintf(buf, sizeof(buf), desc.print_fmt.c_str(), v);
75  } else if (desc.integer_step) {
76  snprintf(buf, sizeof(buf), "%d", (int)v);
77  } else if (desc.upper - desc.lower >= 1000) {
78  snprintf(buf, sizeof(buf), "%.1f", v);
79  } else if (desc.upper - desc.lower >= 100) {
80  snprintf(buf, sizeof(buf), "%.2f", v);
81  } else {
82  snprintf(buf, sizeof(buf), "%.3f", v);
83  }
84  if (desc.print_fmt.empty() && desc.unit == ARDOUR::ParameterDescriptor::DB) {
85  // TODO: Move proper dB printing from AutomationLine here
86  return std::string(buf) + " dB";
87  }
88  return buf;
89 }
90 
91 inline std::string
93  const ARDOUR::Variant& val)
94 {
95  // Only numeric support, for now
96  return value_as_string(desc, val.to_double());
97 }
98 
99 inline double
101  std::string const & str,
102  bool& legal)
103 {
104  legal = true; /* be optimistic */
105 
106  if (desc.scale_points) {
107  // Check if label matches a scale point
108  for (auto const & [label,value] : *desc.scale_points) {
109  if (label == str) {
110  return value; // Found it, return scale point value
111  }
112  }
113  legal = false;
114  return 0.;
115  }
116 
117  if (desc.toggled) {
118  if (str == _("on") || str == _("yes") || str == "1") {
119  return 1.0;
120  } else if (str == _("off") || str == _("no") || str == "0") {
121  return 0.0;
122  } else {
123  legal = false;
124  return 0.;
125  }
126  }
127 
128  // Value is not a scale point, print it normally
130 
132  legal = (nn == 255);
133  return nn;
134 
135  } else if (desc.type == GainAutomation ||
136  desc.type == TrimAutomation ||
137  desc.type == BusSendLevel ||
138  desc.type == EnvelopeAutomation ||
139  desc.type == MainOutVolume ||
140  desc.type == SurroundSendLevel ||
141  desc.type == InsertReturnLevel ||
143 
144  float f;
145  legal = (sscanf (str.c_str(), "%f", &f) == 1);
146  if (!legal) {
147  return 0.;
148  }
149 
150  /* clamp to range */
151 
152  float max_dB = accurate_coefficient_to_dB (desc.upper);
153  float min_dB = accurate_coefficient_to_dB (desc.lower);
154 
155  f = std::max (std::min (f, max_dB), min_dB);
156 
157  return dB_to_coefficient(f);
158 
159  } else if (desc.type == PanWidthAutomation) {
160  int tmp;
161  legal = (sscanf (str.c_str(), "%d", &tmp) == 1);
162  } else if (desc.type == MidiPitchBenderAutomation) {
163  int tmp;
164  legal = (sscanf (str.c_str(), "%d", &tmp) == 1);
165  return tmp + 8192;
166  } else if (desc.integer_step) {
167  float tmp;
168  legal = (sscanf (str.c_str(), "%g", &tmp) == 1);
169  return (int) tmp;
170  }
171 
172  legal = false;
173  return 0.;
174 }
175 
176 } // namespace ARDOUR
double to_double() const
Definition: variant.h:104
static float dB_to_coefficient(float dB)
Definition: dB.h:32
static float accurate_coefficient_to_dB(float coeff)
Definition: dB.h:40
#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)
@ MidiPitchBenderAutomation
double string_as_value(const ARDOUR::ParameterDescriptor &desc, std::string const &str, bool &legal)
std::string print_fmt
format string for pretty printing
static uint8_t midi_note_num(const std::string &name)
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.