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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457
|
/*
==============================================================================
This file is part of the JUCE library.
Copyright (c) 2015 - ROLI Ltd.
Permission is granted to use this software under the terms of either:
a) the GPL v2 (or any later version)
b) the Affero GPL v3
Details of these licenses can be found at: www.gnu.org/licenses
JUCE 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.
------------------------------------------------------------------------------
To release a closed-source product which uses JUCE, commercial licenses are
available: visit www.juce.com for more information.
==============================================================================
*/
#include "MainComponent.h"
//==============================================================================
struct MidiDeviceListEntry : ReferenceCountedObject
{
MidiDeviceListEntry (const String& deviceName) : name (deviceName) {}
String name;
ScopedPointer<MidiInput> inDevice;
ScopedPointer<MidiOutput> outDevice;
typedef ReferenceCountedObjectPtr<MidiDeviceListEntry> Ptr;
};
//==============================================================================
struct MidiCallbackMessage : public Message
{
MidiCallbackMessage (const MidiMessage& msg) : message (msg) {}
MidiMessage message;
};
//==============================================================================
class MidiDeviceListBox : public ListBox,
private ListBoxModel
{
public:
//==============================================================================
MidiDeviceListBox (const String& name,
MainContentComponent& contentComponent,
bool isInputDeviceList)
: ListBox (name, this),
parent (contentComponent),
isInput (isInputDeviceList)
{
setOutlineThickness (1);
setMultipleSelectionEnabled (true);
setClickingTogglesRowSelection (true);
}
//==============================================================================
int getNumRows() override
{
return isInput ? parent.getNumMidiInputs()
: parent.getNumMidiOutputs();
}
//==============================================================================
void paintListBoxItem (int rowNumber, Graphics &g,
int width, int height, bool rowIsSelected) override
{
if (rowIsSelected)
g.fillAll (Colours::lightblue);
else if (rowNumber % 2)
g.fillAll (Colour (0xffeeeeee));
g.setColour (Colours::black);
g.setFont (height * 0.7f);
if (isInput)
{
if (rowNumber < parent.getNumMidiInputs())
g.drawText (parent.getMidiDevice (rowNumber, true)->name,
5, 0, width, height,
Justification::centredLeft, true);
}
else
{
if (rowNumber < parent.getNumMidiOutputs())
g.drawText (parent.getMidiDevice (rowNumber, false)->name,
5, 0, width, height,
Justification::centredLeft, true);
}
}
//==============================================================================
void selectedRowsChanged (int) override
{
SparseSet<int> newSelectedItems = getSelectedRows();
if (newSelectedItems != lastSelectedItems)
{
for (int i = 0; i < lastSelectedItems.size(); ++i)
{
if (! newSelectedItems.contains (lastSelectedItems[i]))
parent.closeDevice (isInput, lastSelectedItems[i]);
}
for (int i = 0; i < newSelectedItems.size(); ++i)
{
if (! lastSelectedItems.contains (newSelectedItems[i]))
parent.openDevice (isInput, newSelectedItems[i]);
}
lastSelectedItems = newSelectedItems;
}
}
//==============================================================================
void syncSelectedItemsWithDeviceList (const ReferenceCountedArray<MidiDeviceListEntry>& midiDevices)
{
SparseSet<int> selectedRows;
for (int i = 0; i < midiDevices.size(); ++i)
if (midiDevices[i]->inDevice != nullptr || midiDevices[i]->outDevice != nullptr)
selectedRows.addRange (Range<int> (i, i+1));
lastSelectedItems = selectedRows;
updateContent();
setSelectedRows (selectedRows, dontSendNotification);
}
private:
//==============================================================================
MainContentComponent& parent;
bool isInput;
SparseSet<int> lastSelectedItems;
};
//==============================================================================
MainContentComponent::MainContentComponent ()
: midiInputLabel ("Midi Input Label", "MIDI Input:"),
midiOutputLabel ("Midi Output Label", "MIDI Output:"),
incomingMidiLabel ("Incoming Midi Label", "Received MIDI messages:"),
outgoingMidiLabel ("Outgoing Midi Label", "Play the keyboard to send MIDI messages..."),
midiKeyboard (keyboardState, MidiKeyboardComponent::horizontalKeyboard),
midiMonitor ("MIDI Monitor"),
pairButton ("MIDI Bluetooth devices..."),
midiInputSelector (new MidiDeviceListBox ("Midi Input Selector", *this, true)),
midiOutputSelector (new MidiDeviceListBox ("Midi Input Selector", *this, false))
{
setSize (732, 520);
addLabelAndSetStyle (midiInputLabel);
addLabelAndSetStyle (midiOutputLabel);
addLabelAndSetStyle (incomingMidiLabel);
addLabelAndSetStyle (outgoingMidiLabel);
midiKeyboard.setName ("MIDI Keyboard");
addAndMakeVisible (midiKeyboard);
midiMonitor.setMultiLine (true);
midiMonitor.setReturnKeyStartsNewLine (false);
midiMonitor.setReadOnly (true);
midiMonitor.setScrollbarsShown (true);
midiMonitor.setCaretVisible (false);
midiMonitor.setPopupMenuEnabled (false);
midiMonitor.setText (String());
addAndMakeVisible (midiMonitor);
if (! BluetoothMidiDevicePairingDialogue::isAvailable())
pairButton.setEnabled (false);
addAndMakeVisible (pairButton);
pairButton.addListener (this);
keyboardState.addListener (this);
addAndMakeVisible (midiInputSelector);
addAndMakeVisible (midiOutputSelector);
startTimer (500);
}
//==============================================================================
void MainContentComponent::addLabelAndSetStyle (Label& label)
{
label.setFont (Font (15.00f, Font::plain));
label.setJustificationType (Justification::centredLeft);
label.setEditable (false, false, false);
label.setColour (TextEditor::textColourId, Colours::black);
label.setColour (TextEditor::backgroundColourId, Colour (0x00000000));
addAndMakeVisible (label);
}
//==============================================================================
MainContentComponent::~MainContentComponent()
{
stopTimer();
midiInputs.clear();
midiOutputs.clear();
keyboardState.removeListener (this);
midiInputSelector = nullptr;
midiOutputSelector = nullptr;
midiOutputSelector = nullptr;
}
//==============================================================================
void MainContentComponent::paint (Graphics& g)
{
g.fillAll (Colours::white);
}
//==============================================================================
void MainContentComponent::resized()
{
const int margin = 10;
midiInputLabel.setBounds (margin, margin,
(getWidth() / 2) - (2 * margin), 24);
midiOutputLabel.setBounds ((getWidth() / 2) + margin, margin,
(getWidth() / 2) - (2 * margin), 24);
midiInputSelector->setBounds (margin, (2 * margin) + 24,
(getWidth() / 2) - (2 * margin),
(getHeight() / 2) - ((4 * margin) + 24 + 24));
midiOutputSelector->setBounds ((getWidth() / 2) + margin, (2 * margin) + 24,
(getWidth() / 2) - (2 * margin),
(getHeight() / 2) - ((4 * margin) + 24 + 24));
pairButton.setBounds (margin, (getHeight() / 2) - (margin + 24),
getWidth() - (2 * margin), 24);
outgoingMidiLabel.setBounds (margin, getHeight() / 2, getWidth() - (2*margin), 24);
midiKeyboard.setBounds (margin, (getHeight() / 2) + (24 + margin), getWidth() - (2*margin), 64);
incomingMidiLabel.setBounds (margin, (getHeight() / 2) + (24 + (2 * margin) + 64),
getWidth() - (2*margin), 24);
int y = (getHeight() / 2) + ((2 * 24) + (3 * margin) + 64);
midiMonitor.setBounds (margin, y,
getWidth() - (2*margin), getHeight() - y - margin);
}
//==============================================================================
void MainContentComponent::buttonClicked (Button* buttonThatWasClicked)
{
if (buttonThatWasClicked == &pairButton)
RuntimePermissions::request (
RuntimePermissions::bluetoothMidi,
[] (bool wasGranted) { if (wasGranted) BluetoothMidiDevicePairingDialogue::open(); } );
}
//==============================================================================
bool MainContentComponent::hasDeviceListChanged (const StringArray& deviceNames, bool isInputDevice)
{
ReferenceCountedArray<MidiDeviceListEntry>& midiDevices = isInputDevice ? midiInputs
: midiOutputs;
if (deviceNames.size() != midiDevices.size())
return true;
for (int i = 0; i < deviceNames.size(); ++i)
if (deviceNames[i] != midiDevices[i]->name)
return true;
return false;
}
MidiDeviceListEntry::Ptr MainContentComponent::findDeviceWithName (const String& name, bool isInputDevice) const
{
const ReferenceCountedArray<MidiDeviceListEntry>& midiDevices = isInputDevice ? midiInputs
: midiOutputs;
for (int i = 0; i < midiDevices.size(); ++i)
if (midiDevices[i]->name == name)
return midiDevices[i];
return nullptr;
}
void MainContentComponent::closeUnpluggedDevices (StringArray& currentlyPluggedInDevices, bool isInputDevice)
{
ReferenceCountedArray<MidiDeviceListEntry>& midiDevices = isInputDevice ? midiInputs
: midiOutputs;
for (int i = midiDevices.size(); --i >= 0;)
{
MidiDeviceListEntry& d = *midiDevices[i];
if (! currentlyPluggedInDevices.contains (d.name))
{
if (isInputDevice ? d.inDevice != nullptr
: d.outDevice != nullptr)
closeDevice (isInputDevice, i);
midiDevices.remove (i);
}
}
}
void MainContentComponent::updateDeviceList (bool isInputDeviceList)
{
StringArray newDeviceNames = isInputDeviceList ? MidiInput::getDevices()
: MidiOutput::getDevices();
if (hasDeviceListChanged (newDeviceNames, isInputDeviceList))
{
ReferenceCountedArray<MidiDeviceListEntry>& midiDevices
= isInputDeviceList ? midiInputs : midiOutputs;
closeUnpluggedDevices (newDeviceNames, isInputDeviceList);
ReferenceCountedArray<MidiDeviceListEntry> newDeviceList;
// add all currently plugged-in devices to the device list
for (int i = 0; i < newDeviceNames.size(); ++i)
{
MidiDeviceListEntry::Ptr entry = findDeviceWithName (newDeviceNames[i], isInputDeviceList);
if (entry == nullptr)
entry = new MidiDeviceListEntry (newDeviceNames[i]);
newDeviceList.add (entry);
}
// actually update the device list
midiDevices = newDeviceList;
// update the selection status of the combo-box
if (MidiDeviceListBox* midiSelector = isInputDeviceList ? midiInputSelector : midiOutputSelector)
midiSelector->syncSelectedItemsWithDeviceList (midiDevices);
}
}
//==============================================================================
void MainContentComponent::timerCallback ()
{
updateDeviceList (true);
updateDeviceList (false);
}
//==============================================================================
void MainContentComponent::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
{
MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
sendToOutputs (m);
}
//==============================================================================
void MainContentComponent::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
{
MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
sendToOutputs (m);
}
//==============================================================================
void MainContentComponent::sendToOutputs(const MidiMessage& msg)
{
for (int i = 0; i < midiOutputs.size(); ++i)
if (midiOutputs[i]->outDevice != nullptr)
midiOutputs[i]->outDevice->sendMessageNow (msg);
}
//==============================================================================
void MainContentComponent::handleIncomingMidiMessage (MidiInput* /*source*/, const MidiMessage &message)
{
// This is called on the MIDI thread
if (message.isNoteOnOrOff())
postMessage (new MidiCallbackMessage (message));
}
//==============================================================================
void MainContentComponent::handleMessage (const Message& msg)
{
// This is called on the message loop
const MidiMessage& mm = dynamic_cast<const MidiCallbackMessage&> (msg).message;
String midiString;
midiString << (mm.isNoteOn() ? String ("Note on: ") : String ("Note off: "));
midiString << (MidiMessage::getMidiNoteName (mm.getNoteNumber(), true, true, true));
midiString << (String (" vel = "));
midiString << static_cast<int>(mm.getVelocity());
midiString << "\n";
midiMonitor.insertTextAtCaret (midiString);
}
//==============================================================================
void MainContentComponent::openDevice (bool isInput, int index)
{
if (isInput)
{
jassert (midiInputs[index]->inDevice == nullptr);
midiInputs[index]->inDevice = MidiInput::openDevice (index, this);
if (midiInputs[index]->inDevice == nullptr)
{
DBG ("MainContentComponent::openDevice: open input device for index = " << index << " failed!" );
return;
}
midiInputs[index]->inDevice->start();
}
else
{
jassert (midiOutputs[index]->outDevice == nullptr);
midiOutputs[index]->outDevice = MidiOutput::openDevice (index);
if (midiOutputs[index]->outDevice == nullptr)
DBG ("MainContentComponent::openDevice: open output device for index = " << index << " failed!" );
}
}
//==============================================================================
void MainContentComponent::closeDevice (bool isInput, int index)
{
if (isInput)
{
jassert (midiInputs[index]->inDevice != nullptr);
midiInputs[index]->inDevice->stop();
midiInputs[index]->inDevice = nullptr;
}
else
{
jassert (midiOutputs[index]->outDevice != nullptr);
midiOutputs[index]->outDevice = nullptr;
}
}
//==============================================================================
int MainContentComponent::getNumMidiInputs() const noexcept
{
return midiInputs.size();
}
//==============================================================================
int MainContentComponent::getNumMidiOutputs() const noexcept
{
return midiOutputs.size();
}
//==============================================================================
ReferenceCountedObjectPtr<MidiDeviceListEntry>
MainContentComponent::getMidiDevice (int index, bool isInput) const noexcept
{
return isInput ? midiInputs[index] : midiOutputs[index];
}
|