summaryrefslogtreecommitdiff
path: root/libs/surfaces/us2400/meter.cc
blob: d72c48d01d95ad4b05f84b8fd01b45ebd7bc1af2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * Copyright (C) 2017 Ben Loftis <ben@harrisonconsoles.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <cmath>

#include "pbd/compose.h"
#include "ardour/debug.h"

#include "meter.h"
#include "surface.h"
#include "surface_port.h"
#include "control_group.h"
#include "us2400_control_protocol.h"

using namespace PBD;
using namespace ArdourSurface;
using namespace US2400;

Control*
Meter::factory (Surface& surface, int id, const char* name, Group& group)
{
	Meter* m = new Meter (id, name, group);
	surface.meters[id] = m;
	surface.controls.push_back (m);
	group.add (*m);
	return m;
}

void
Meter::notify_metering_state_changed(Surface& surface, bool transport_is_rolling, bool metering_active)
{
return;

	MidiByteArray msg;

	// sysex header
	msg << surface.sysex_hdr();

	// code for Channel Meter Enable Message
	msg << 0x20;

	// Channel identification
	msg << id();

	// Enable (0x07) / Disable (0x00) level meter on LCD, peak hold display on horizontal meter and signal LED
	_enabled = ((surface.mcp().device_info().has_separate_meters() || transport_is_rolling) && metering_active);
	msg << (_enabled ? 0x07 : 0x00);

	// sysex trailer
	msg << MIDI::eox;

	surface.write (msg);
}

void
Meter::send_update (Surface& surface, float dB)
{

	float def = 0.0f; /* Meter deflection %age */

	// DEBUG_TRACE (DEBUG::US2400, string_compose ("Meter ID %1 dB %2\n", id(), dB));

	if (dB < -70.0f) {
		def = 0.0f;
	} else if (dB < -60.0f) {
		def = (dB + 70.0f) * 0.25f;
	} else if (dB < -50.0f) {
		def = (dB + 60.0f) * 0.5f + 2.5f;
	} else if (dB < -40.0f) {
		def = (dB + 50.0f) * 0.75f + 7.5f;
	} else if (dB < -30.0f) {
		def = (dB + 40.0f) * 1.5f + 15.0f;
	} else if (dB < -20.0f) {
		def = (dB + 30.0f) * 2.0f + 30.0f;
	} else if (dB < 6.0f) {
		def = (dB + 20.0f) * 2.5f + 50.0f;
	} else {
		def = 115.0f;
	}

	/* 115 is the deflection %age that would be
	   when dB=6.0. this is an arbitrary
	   endpoint for our scaling.
	*/

	MidiByteArray msg;

	if (def > 100.0f) {
		if (!overload_on) {
			overload_on = true;
			surface.write (MidiByteArray (2, 0xd0, (id() << 4) | 0xe));
		}
	} else {
		if (overload_on) {
			overload_on = false;
			surface.write (MidiByteArray (2, 0xd0, (id() << 4) | 0xf));
		}
	}

	/* we can use up to 13 segments */

	int segment = lrintf ((def/115.0) * 13.0);

	//only send an update 'twice'
	if (segment == last_update_segment) {
		if (segment == llast_update_segment) {
			return;
		}
	}
	llast_update_segment = last_update_segment;
	last_update_segment = segment;


	surface.write (MidiByteArray (2, 0xd0, (id()<<4) | segment));
}

MidiByteArray
Meter::zero ()
{
	return MidiByteArray (2, 0xD0, (id()<<4 | 0));
}