-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMidiFlasher.cpp
154 lines (141 loc) · 2.61 KB
/
MidiFlasher.cpp
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
#include "stdafx.h"
class MidiFlasher
{
public:
MidiFlasher(int midiport)
{
int numDevices = midiOutGetNumDevs();
if (midiport >= numDevices)
{
throw L"Invalid device number.";
}
int flag = midiOutOpen(&(this->device), midiport, 0, 0, CALLBACK_NULL);
if (flag != MMSYSERR_NOERROR) {
throw L"Error opening MIDI Output.";
}
this->turnOnInControl();
}
~MidiFlasher()
{
try
{
this->offFrom(0);
midiOutReset(device);
midiOutClose(device);
}
catch (const LPCWSTR msg)
{
// ignore
}
}
void Debug(const WCHAR* szFormat, ...)
{
WCHAR szBuff[1024];
va_list arg;
va_start(arg, szFormat);
_vsnwprintf_s(szBuff, sizeof(szBuff), szFormat, arg);
va_end(arg);
OutputDebugString(szBuff);
}
void SendMidi(float peak) {
if (peak == 0)
{
this->offFrom(0);
}
else if (peak < 0.1)
{
this->offFrom(1);
}
else if (peak < 0.3)
{
this->offFrom(2);
}
else if (peak < 0.45)
{
this->offFrom(3);
}
else if (peak < 0.6)
{
this->offFrom(4);
}
else if (peak < 0.7)
{
this->offFrom(5);
}
else if (peak < 0.8)
{
this->offFrom(6);
}
else if (peak < 0.9)
{
this->offFrom(7);
}
else if (peak < 0.95)
{
this->offFrom(8);
}
else
{
this->offFrom(9);
}
}
private:
int noteStates[256] = {};
HMIDIOUT device;
void sendMidiMessage(unsigned char note, int colour)
{
// dont send a signal if it is already set correctly
if (this->noteStates[note] == colour)
{
return;
}
this->noteStates[note] = colour;
union { unsigned long word; unsigned char data[4]; } message;
message.data[0] = 0x90; // MIDI note-on message (requires to data bytes)
message.data[1] = note; // MIDI note-on message: Key number (60 = middle C)
message.data[2] = colour; // MIDI note-on message: Key velocity (100 = loud)
message.data[3] = 0; // Unused parameter
int flag = midiOutShortMsg(this->device, message.word);
if (flag != MMSYSERR_NOERROR) {
throw L"MIDI Output is not open.";
}
}
void turnOnInControl()
{
this->sendMidiMessage(12, 127);
}
int getNoteColour(int note)
{
switch (note) {
case 0:
return 32;
case 1:
return 48;
case 2:
return 49;
case 3:
return 50;
case 4:
return 51;
case 5:
return 35;
case 6:
return 19;
case 7:
return 3;
default:
return 1;
}
}
void offFrom(int number)
{
for (int i = 0; i < number; ++i) {
this->sendMidiMessage(96 + i, this->getNoteColour(i));
this->sendMidiMessage(112 + i, this->getNoteColour(i));
}
for (int i = number; i < 9; ++i) {
this->sendMidiMessage(96 + i, 0);
this->sendMidiMessage(112 + i, 0);
}
}
};