Audio plugin host https://kx.studio/carla
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.

juce_MPEUtils.h 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - Raw Material Software Limited
  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. //==============================================================================
  20. /**
  21. This class handles the assignment of new MIDI notes to member channels of an active
  22. MPE zone.
  23. To use it, create an instance passing in the MPE zone that it should operate on
  24. and then call use the findMidiChannelForNewNote() method for all note-on messages
  25. and the noteOff() method for all note-off messages.
  26. @tags{Audio}
  27. */
  28. class MPEChannelAssigner
  29. {
  30. public:
  31. /** Constructor.
  32. This will assign channels within the range of the specified MPE zone.
  33. */
  34. MPEChannelAssigner (MPEZoneLayout::Zone zoneToUse);
  35. /** Legacy mode constructor.
  36. This will assign channels within the specified range.
  37. */
  38. MPEChannelAssigner (Range<int> channelRange = Range<int> (1, 17));
  39. /** This method will use a set of rules recommended in the MPE specification to
  40. determine which member channel the specified MIDI note should be assigned to
  41. and will return this channel number.
  42. The rules have the following precedence:
  43. - find a free channel on which the last note played was the same as the one specified
  44. - find the next free channel in round-robin assignment
  45. - find the channel number that is currently playing the closest note (but not the same)
  46. @param noteNumber the MIDI note number to be assigned to a channel
  47. @returns the zone's member channel that this note should be assigned to
  48. */
  49. int findMidiChannelForNewNote (int noteNumber) noexcept;
  50. /** If a note has been added using findMidiChannelForNewNote() this will return the channel
  51. to which it was assigned, otherwise it will return -1.
  52. */
  53. int findMidiChannelForExistingNote (int initialNoteOnNumber) noexcept;
  54. /** You must call this method for all note-offs that you receive so that this class
  55. can keep track of the currently playing notes internally.
  56. You can specify the channel number the note off happened on. If you don't, it will
  57. look through all channels to find the registered midi note matching the given note number.
  58. */
  59. void noteOff (int noteNumber, int midiChannel = -1);
  60. /** Call this to clear all currently playing notes. */
  61. void allNotesOff();
  62. private:
  63. bool isLegacy = false;
  64. std::unique_ptr<MPEZoneLayout::Zone> zone;
  65. int channelIncrement, numChannels, firstChannel, lastChannel, midiChannelLastAssigned;
  66. //==============================================================================
  67. struct MidiChannel
  68. {
  69. Array<int> notes;
  70. int lastNotePlayed = -1;
  71. bool isFree() const noexcept { return notes.isEmpty(); }
  72. };
  73. std::array<MidiChannel, 17> midiChannels;
  74. //==============================================================================
  75. int findMidiChannelPlayingClosestNonequalNote (int noteNumber) noexcept;
  76. };
  77. //==============================================================================
  78. /**
  79. This class handles the logic for remapping MIDI note messages from multiple MPE
  80. sources onto a specified MPE zone.
  81. @tags{Audio}
  82. */
  83. class MPEChannelRemapper
  84. {
  85. public:
  86. /** Used to indicate that a particular source & channel combination is not currently using MPE. */
  87. static const uint32 notMPE = 0;
  88. /** Constructor */
  89. MPEChannelRemapper (MPEZoneLayout::Zone zoneToRemap);
  90. //==============================================================================
  91. /** Remaps the MIDI channel of the specified MIDI message (if necessary).
  92. Note that the MidiMessage object passed in will have it's channel changed if it
  93. needs to be remapped.
  94. @param message the message to be remapped
  95. @param mpeSourceID the ID of the MPE source of the message. This is up to the
  96. user to define and keep constant
  97. */
  98. void remapMidiChannelIfNeeded (MidiMessage& message, uint32 mpeSourceID) noexcept;
  99. //==============================================================================
  100. /** Resets all the source & channel combinations. */
  101. void reset() noexcept;
  102. /** Clears a specified channel of this MPE zone. */
  103. void clearChannel (int channel) noexcept;
  104. /** Clears all channels in use by a specified source. */
  105. void clearSource (uint32 mpeSourceID);
  106. private:
  107. MPEZoneLayout::Zone zone;
  108. int channelIncrement;
  109. int firstChannel, lastChannel;
  110. uint32 sourceAndChannel[17];
  111. uint32 lastUsed[17];
  112. uint32 counter = 0;
  113. //==============================================================================
  114. bool applyRemapIfExisting (int channel, uint32 sourceAndChannelID, MidiMessage& m) noexcept;
  115. int getBestChanToReuse() const noexcept;
  116. void zeroArrays();
  117. //==============================================================================
  118. bool messageIsNoteData (const MidiMessage& m) { return (*m.getRawData() & 0xf0) != 0xf0; }
  119. };
  120. } // namespace juce