Mackie Control: Shift/Select now toggles individual channels within group. Makes...
[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 #include "mackie_control_protocol.h"
30
31 using namespace PBD;
32 using namespace ArdourSurface;
33 using namespace Mackie;
34
35 Control*
36 Meter::factory (Surface& surface, int id, const char* name, Group& group)
37 {
38         Meter* m = new Meter (id, name, group);
39         surface.meters[id] = m;
40         surface.controls.push_back (m);
41         group.add (*m);
42         return m;
43 }
44
45 void
46 Meter::notify_metering_state_changed(Surface& surface, bool transport_is_rolling, bool metering_active)
47 {
48         MidiByteArray msg;
49
50         // sysex header
51         msg << surface.sysex_hdr();
52
53         // code for Channel Meter Enable Message
54         msg << 0x20;
55
56         // Channel identification
57         msg << id();
58
59         // Enable (0x07) / Disable (0x00) level meter on LCD, peak hold display on horizontal meter and signal LED
60         _enabled = ((surface.mcp().device_info().has_separate_meters() || transport_is_rolling) && metering_active);
61         msg << (_enabled ? 0x07 : 0x00);
62
63         // sysex trailer
64         msg << MIDI::eox;
65
66         surface.write (msg);
67 }
68
69 void
70 Meter::send_update (Surface& surface, float dB)
71 {
72         float def = 0.0f; /* Meter deflection %age */
73
74         // DEBUG_TRACE (DEBUG::MackieControl, string_compose ("Meter ID %1 dB %2\n", id(), dB));
75
76         if (dB < -70.0f) {
77                 def = 0.0f;
78         } else if (dB < -60.0f) {
79                 def = (dB + 70.0f) * 0.25f;
80         } else if (dB < -50.0f) {
81                 def = (dB + 60.0f) * 0.5f + 2.5f;
82         } else if (dB < -40.0f) {
83                 def = (dB + 50.0f) * 0.75f + 7.5f;
84         } else if (dB < -30.0f) {
85                 def = (dB + 40.0f) * 1.5f + 15.0f;
86         } else if (dB < -20.0f) {
87                 def = (dB + 30.0f) * 2.0f + 30.0f;
88         } else if (dB < 6.0f) {
89                 def = (dB + 20.0f) * 2.5f + 50.0f;
90         } else {
91                 def = 115.0f;
92         }
93
94         /* 115 is the deflection %age that would be
95            when dB=6.0. this is an arbitrary
96            endpoint for our scaling.
97         */
98
99         MidiByteArray msg;
100
101         if (def > 100.0f) {
102                 if (!overload_on) {
103                         overload_on = true;
104                         surface.write (MidiByteArray (2, 0xd0, (id() << 4) | 0xe));
105
106                 }
107         } else {
108                 if (overload_on) {
109                         overload_on = false;
110                         surface.write (MidiByteArray (2, 0xd0, (id() << 4) | 0xf));
111                 }
112         }
113
114         /* we can use up to 13 segments */
115
116         int segment = lrintf ((def/115.0) * 13.0);
117
118         surface.write (MidiByteArray (2, 0xd0, (id()<<4) | segment));
119 }
120
121 MidiByteArray
122 Meter::zero ()
123 {
124         return MidiByteArray (2, 0xD0, (id()<<4 | 0));
125 }