децибел
[ardour.git] / libs / ardour / value_as_string.cc
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 #include "ardour/value_as_string.h"
22
23 namespace ARDOUR {
24
25 std::string
26 value_as_string(const Evoral::ParameterDescriptor& desc,
27                 double                             v)
28 {
29         char buf[32];
30
31         if (desc.scale_points) {
32                 // Check if value is on a scale point
33                 for (Evoral::ScalePoints::const_iterator i = desc.scale_points->begin();
34                      i != desc.scale_points->end();
35                      ++i) {
36                         if (i->second == v) {
37                                 return i->first;  // Found it, return scale point label
38                         }
39                 }
40         }
41
42         // Value is not a scale point, print it normally
43         if (desc.unit == Evoral::ParameterDescriptor::MIDI_NOTE) {
44                 if (v >= 0 && v <= 127) {
45                         const int         num          = rint(v);
46                         static const char names[12][3] = {
47                                 "C", "C#", "D", "D#", "E", "F", "F#", "G", "G#", "A", "A#", "B"
48                         };
49                         snprintf(buf, sizeof(buf), "%s %d", names[num % 12], (num / 12) - 2);
50                 } else {
51                         // Odd, invalid range, just print the number
52                         snprintf(buf, sizeof(buf), "%.0f", v);
53                 }
54         } else if (!desc.print_fmt.empty()) {
55                 snprintf(buf, sizeof(buf), desc.print_fmt.c_str(), v);
56         } else if (desc.integer_step) {
57                 snprintf(buf, sizeof(buf), "%d", (int)v);
58         } else {
59                 snprintf(buf, sizeof(buf), "%.3f", v);
60         }
61         if (desc.print_fmt.empty() && desc.unit == Evoral::ParameterDescriptor::DB) {
62                 // TODO: Move proper dB printing from AutomationLine here
63                 return std::string(buf) + _(" dB");
64         }
65         return buf;
66 }
67
68 std::string
69 value_as_string(const Evoral::ParameterDescriptor& desc,
70                 const Evoral::Variant&             val)
71 {
72         // Only numeric support, for now
73         return value_as_string(desc, val.to_double());
74 }
75
76 }  // namespace ARDOUR