The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. namespace juce
  18. {
  19. struct MIDIDeviceDetector : public PhysicalTopologySource::DeviceDetector
  20. {
  21. MIDIDeviceDetector() {}
  22. juce::StringArray scanForDevices() override
  23. {
  24. juce::StringArray result;
  25. for (auto& pair : findDevices())
  26. result.add (pair.inputName + " & " + pair.outputName);
  27. return result;
  28. }
  29. PhysicalTopologySource::DeviceConnection* openDevice (int index) override
  30. {
  31. auto pair = findDevices()[index];
  32. if (pair.inputIndex >= 0 && pair.outputIndex >= 0)
  33. {
  34. std::unique_ptr<MIDIDeviceConnection> dev (new MIDIDeviceConnection());
  35. if (dev->lockAgainstOtherProcesses (pair.inputName, pair.outputName))
  36. {
  37. lockedFromOutside = false;
  38. dev->midiInput.reset (juce::MidiInput::openDevice (pair.inputIndex, dev.get()));
  39. dev->midiOutput.reset (juce::MidiOutput::openDevice (pair.outputIndex));
  40. if (dev->midiInput != nullptr)
  41. {
  42. dev->midiInput->start();
  43. return dev.release();
  44. }
  45. }
  46. else
  47. {
  48. lockedFromOutside = true;
  49. }
  50. }
  51. return nullptr;
  52. }
  53. bool isLockedFromOutside() const override
  54. {
  55. return lockedFromOutside && ! findDevices().isEmpty();
  56. }
  57. static bool isBlocksMidiDeviceName (const juce::String& name)
  58. {
  59. return name.indexOf (" BLOCK") > 0 || name.indexOf (" Block") > 0;
  60. }
  61. static String cleanBlocksDeviceName (juce::String name)
  62. {
  63. name = name.trim();
  64. if (name.endsWith (" IN)"))
  65. return name.dropLastCharacters (4);
  66. if (name.endsWith (" OUT)"))
  67. return name.dropLastCharacters (5);
  68. const int openBracketPosition = name.lastIndexOfChar ('[');
  69. if (openBracketPosition != -1 && name.endsWith ("]"))
  70. return name.dropLastCharacters (name.length() - openBracketPosition);
  71. return name;
  72. }
  73. struct MidiInputOutputPair
  74. {
  75. juce::String outputName, inputName;
  76. int outputIndex = -1, inputIndex = -1;
  77. };
  78. static juce::Array<MidiInputOutputPair> findDevices()
  79. {
  80. juce::Array<MidiInputOutputPair> result;
  81. auto midiInputs = juce::MidiInput::getDevices();
  82. auto midiOutputs = juce::MidiOutput::getDevices();
  83. for (int j = 0; j < midiInputs.size(); ++j)
  84. {
  85. if (isBlocksMidiDeviceName (midiInputs[j]))
  86. {
  87. MidiInputOutputPair pair;
  88. pair.inputName = midiInputs[j];
  89. pair.inputIndex = j;
  90. String cleanedInputName = cleanBlocksDeviceName (pair.inputName);
  91. for (int i = 0; i < midiOutputs.size(); ++i)
  92. {
  93. if (cleanBlocksDeviceName (midiOutputs[i]) == cleanedInputName)
  94. {
  95. pair.outputName = midiOutputs[i];
  96. pair.outputIndex = i;
  97. break;
  98. }
  99. }
  100. result.add (pair);
  101. }
  102. }
  103. return result;
  104. }
  105. private:
  106. bool lockedFromOutside = true;
  107. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MIDIDeviceDetector)
  108. };
  109. } // namespace juce