-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMIDIContentComponent.cpp
295 lines (225 loc) · 10.5 KB
/
MIDIContentComponent.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
==============================================================================
This is an automatically generated GUI class created by the Introjucer!
Be careful when adding custom code to these files, as only the code within
the "//[xyz]" and "//[/xyz]" sections will be retained when the file is loaded
and re-saved.
Created with Introjucer version: 3.2.0
------------------------------------------------------------------------------
The Introjucer is part of the JUCE library - "Jules' Utility Class Extensions"
Copyright (c) 2015 - ROLI Ltd.
==============================================================================
*/
//[Headers] You can add your own extra header files here...
//[/Headers]
#include "MIDIContentComponent.h"
//[MiscUserDefs] You can add your own user definitions and misc code here...
//==============================================================================
MIDIContentComponent::MIDIContentComponent ()
: lastInputIndex (0),
isAddingFromMidiInput (false),
startTime (Time::getMillisecondCounterHiRes() * 0.001)
{
//[Constructor_pre] You can add your own custom stuff here..
//[/Constructor_pre]
addAndMakeVisible (midiInputList = new ComboBox ("new combo box"));
midiInputList->setTextWhenNoChoicesAvailable ("No MIDI Inputs Enabled");
const StringArray midiInputs (MidiInput::getDevices());
midiInputList->addItemList (midiInputs, 1);
midiInputList->setEditableText (false);
midiInputList->setJustificationType (Justification::centredLeft);
midiInputList->setTextWhenNothingSelected (String::empty);
midiInputList->addListener (this);
for (int i = 0; i < midiInputs.size(); ++i)
{
if (deviceManager.isMidiInputEnabled (midiInputs[i]))
{
setMidiInput (i);
break;
}
}
// if no enabled devices were found just use the first one in the list
if (midiInputList->getSelectedId() == 0)
setMidiInput (0);
/*addAndMakeVisible (keyboardComponent);
keyboardState.addListener (this);*/
addAndMakeVisible (midiMessagesBox = new TextEditor ("new text editor"));
midiMessagesBox->setMultiLine (false);
midiMessagesBox->setReturnKeyStartsNewLine (false);
midiMessagesBox->setReadOnly (false);
midiMessagesBox->setScrollbarsShown (true);
midiMessagesBox->setCaretVisible (true);
midiMessagesBox->setPopupMenuEnabled (true);
midiMessagesBox->setText (String::empty);
}
MIDIContentComponent::~MIDIContentComponent()
{
//[Destructor_pre]. You can add your own custom destruction code here..
//[/Destructor_pre]
//[Destructor]. You can add your own custom destruction code here..
// keyboardState.removeListener (this);
// deviceManager.removeMidiInputCallback (MidiInput::getDevices()[midiInputList.getSelectedItemIndex()], this);
// midiInputList.removeListener (this);
//[/Destructor]
}
//[/MiscUserDefs]
//==============================================================================
//==============================================================================
void MIDIContentComponent::paint (Graphics& g)
{
//[UserPrePaint] Add your own custom painting code here..
//[/UserPrePaint]
g.fillAll (Colours::white);
//[UserPaint] Add your own custom painting code here..
//[/UserPaint]
}
void MIDIContentComponent::resized()
{
//[UserPreResize] Add your own custom resize code here..
//[/UserPreResize]
midiInputList->setBounds (32, 80, 150, 24);
midiMessagesBox->setBounds (32, 40, 150, 24);
//[UserResized] Add your own custom resize handling here..
//[/UserResized]
}
void MIDIContentComponent::comboBoxChanged (ComboBox* comboBoxThatHasChanged)
{
//[UsercomboBoxChanged_Pre]
//[/UsercomboBoxChanged_Pre]
if (comboBoxThatHasChanged == midiInputList)
{
//[UserComboBoxCode_midiInputList] -- add your combo box handling code here..
// mi metto in ascolto sulla periferica selezionata
setMidiInput (midiInputList->getSelectedItemIndex());
//[/UserComboBoxCode_midiInputList]
}
//[UsercomboBoxChanged_Post]
//[/UsercomboBoxChanged_Post]
}
//[MiscUserCode] You can add your own definitions of your custom methods or any other code here...
class IncomingMessageCallback : public CallbackMessage
{
public:
IncomingMessageCallback (MIDIContentComponent* o, const MidiMessage& m, const String& s)
: owner (o), message (m), source (s)
{}
void messageCallback() override
{
if (owner != nullptr)
owner->addMessageToList (message, source);
}
Component::SafePointer<MIDIContentComponent> owner;
MidiMessage message;
String source;
};
void MIDIContentComponent::postMessageToList (const MidiMessage& message, const String& source)
{
(new IncomingMessageCallback (this, message, source))->post();
}
static String getMidiMessageDescription (const MidiMessage& m)
{
if (m.isNoteOn()) return "Note on " + MidiMessage::getMidiNoteName (m.getNoteNumber(), true, true, 3);
if (m.isNoteOff()) return "Note off " + MidiMessage::getMidiNoteName (m.getNoteNumber(), true, true, 3);
if (m.isProgramChange()) return "Program change " + String (m.getProgramChangeNumber());
if (m.isPitchWheel()) return "Pitch wheel " + String (m.getPitchWheelValue());
if (m.isAftertouch()) return "After touch " + MidiMessage::getMidiNoteName (m.getNoteNumber(), true, true, 3) + ": " + String (m.getAfterTouchValue());
if (m.isChannelPressure()) return "Channel pressure " + String (m.getChannelPressureValue());
if (m.isAllNotesOff()) return "All notes off";
if (m.isAllSoundOff()) return "All sound off";
if (m.isMetaEvent()) return "Meta event";
if (m.isController())
{
String name (MidiMessage::getControllerName (m.getControllerNumber()));
if (name.isEmpty())
name = "[" + String (m.getControllerNumber()) + "]";
return "Controller " + name + ": " + String (m.getControllerValue());
}
return String::toHexString (m.getRawData(), m.getRawDataSize());
}
void MIDIContentComponent::addMessageToList (const MidiMessage& message, const String& source)
{
/*message.NOTE(MIDITOFREQ)*/
const double time = message.getTimeStamp() - startTime;
const int hours = ((int) (time / 3600.0)) % 24;
const int minutes = ((int) (time / 60.0)) % 60;
const int seconds = ((int) time) % 60;
const int millis = ((int) (time * 1000.0)) % 1000;
const String timecode (String::formatted ("%02d:%02d:%02d.%03d",
hours,
minutes,
seconds,
millis));
const String description (getMidiMessageDescription (message));
//const String midiMessageString (timecode + " - " + description + " (" + source + ")");
//const String midiMessageString (String(MidiMessage::getMidiNoteInHertz(message.getPitchWheelValue())));
//const String midiMessageString (String(message.getNoteNumber()));
const String midiMessageString (String((440.0 * pow(2.0, (message.getNoteNumber() - 69.0)/12.0))));
logMessage (midiMessageString);
}
void MIDIContentComponent::setMidiInput (int index)
{
const StringArray list (MidiInput::getDevices());
deviceManager.removeMidiInputCallback (list[lastInputIndex], this);
const String newInput (list[index]);
if (!deviceManager.isMidiInputEnabled (newInput))
deviceManager.setMidiInputEnabled (newInput, true);
deviceManager.addMidiInputCallback (newInput, this);
midiInputList->setSelectedId (index + 1, dontSendNotification);
lastInputIndex = index;
}
void MIDIContentComponent::handleIncomingMidiMessage (MidiInput* source, const MidiMessage& message)
{
const ScopedValueSetter<bool> scopedInputFlag (isAddingFromMidiInput, true);
keyboardState.processNextMidiEvent (message);
postMessageToList (message, source->getName());
}
void MIDIContentComponent::logMessage (const String& m)
{
midiMessagesBox->moveCaretToEnd();
midiMessagesBox->insertTextAtCaret (m + newLine);
}
/*override*/
void MIDIContentComponent::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
{
if (! isAddingFromMidiInput)
{
MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
postMessageToList (m, "On-Screen Keybaord");
}
}
void MIDIContentComponent::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber)
{
if (! isAddingFromMidiInput)
{
MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber));
m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
postMessageToList (m, "On-Screen Keyboard");
}
}
// This is used to dispach an incoming message to the message thread
//[/MiscUserCode]
//==============================================================================
#if 0
/* -- Introjucer information section --
This is where the Introjucer stores the metadata that describe this GUI layout, so
make changes in here at your peril!
BEGIN_JUCER_METADATA
<JUCER_COMPONENT documentType="Component" className="MIDIContentComponent" componentName=""
parentClasses="public Component" constructorParams="" variableInitialisers=""
snapPixels="8" snapActive="1" snapShown="1" overlayOpacity="0.330"
fixedSize="0" initialWidth="600" initialHeight="400">
<BACKGROUND backgroundColour="ffffffff"/>
<COMBOBOX name="new combo box" id="6b588d7fd78cbafc" memberName="midiInputList"
virtualName="" explicitFocusOrder="0" pos="32 80 150 24" editable="0"
layout="33" items="" textWhenNonSelected="" textWhenNoItems="(no choices)"/>
<TEXTEDITOR name="new text editor" id="7b52cb17d932ef17" memberName="midiMessagesBox"
virtualName="" explicitFocusOrder="0" pos="32 40 150 24" initialText=""
multiline="0" retKeyStartsLine="0" readonly="0" scrollbars="1"
caret="1" popupmenu="1"/>
</JUCER_COMPONENT>
END_JUCER_METADATA
*/
#endif
//[EndFile] You can add extra defines here...
//[/EndFile]