move all code to construct MIDI messages into relevant Control/Strip/Surface object...
[ardour.git] / libs / surfaces / mackie / meter.cc
1 /*
2         Copyright (C) 2006,2007 John Anderson
3         Copyright (C) 2012 Paul Davis
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 #include <cmath>
21
22 #include "pbd/compose.h"
23 #include "ardour/debug.h"
24
25 #include "meter.h"
26 #include "surface.h"
27 #include "surface_port.h"
28 #include "control_group.h"
29
30 using namespace Mackie;
31 using namespace PBD;
32
33 Control*
34 Meter::factory (Surface& surface, int id, const char* name, Group& group)
35 {
36         Meter* m = new Meter (id, name, group);
37         surface.meters[id] = m;
38         surface.controls.push_back (m);
39         group.add (*m);
40         return m;
41 }
42
43 MidiByteArray
44 Meter::update_message (float dB)
45 {
46         float def = 0.0f; /* Meter deflection %age */
47
48         // DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Meter ID %1 dB %2\n", raw_id(), dB));
49         
50         if (dB < -70.0f) {
51                 def = 0.0f;
52         } else if (dB < -60.0f) {
53                 def = (dB + 70.0f) * 0.25f;
54         } else if (dB < -50.0f) {
55                 def = (dB + 60.0f) * 0.5f + 2.5f;
56         } else if (dB < -40.0f) {
57                 def = (dB + 50.0f) * 0.75f + 7.5f;
58         } else if (dB < -30.0f) {
59                 def = (dB + 40.0f) * 1.5f + 15.0f;
60         } else if (dB < -20.0f) {
61                 def = (dB + 30.0f) * 2.0f + 30.0f;
62         } else if (dB < 6.0f) {
63                 def = (dB + 20.0f) * 2.5f + 50.0f;
64         } else {
65                 def = 115.0f;
66         }
67         
68         /* 115 is the deflection %age that would be
69            when dB=6.0. this is an arbitrary
70            endpoint for our scaling.
71         */
72
73         MidiByteArray msg;
74         
75         if (def > 100.0f) {
76                 if (!overload_on) {
77                         overload_on = true;
78                         msg << MidiByteArray (2, 0xd0, (raw_id() << 4) | 0xe);
79                 }
80         } else {
81                 if (overload_on) {
82                         overload_on = false;
83                         msg << MidiByteArray (2, 0xd0, (raw_id() << 4) | 0xf);
84                 }
85         }
86         
87         /* we can use up to 13 segments */
88
89         int segment = lrintf ((def/115.0) * 13.0);
90         
91         if (last_segment_value_sent != segment) {
92                 last_segment_value_sent = segment;
93                 msg << MidiByteArray (2, 0xD0, (raw_id()<<4) | segment);
94         }
95
96         return msg;
97 }
98