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.

159 lines
5.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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. MidiMessageCollector::MidiMessageCollector()
  20. {
  21. }
  22. MidiMessageCollector::~MidiMessageCollector()
  23. {
  24. }
  25. //==============================================================================
  26. void MidiMessageCollector::reset (const double newSampleRate)
  27. {
  28. const ScopedLock sl (midiCallbackLock);
  29. jassert (newSampleRate > 0);
  30. #if JUCE_DEBUG
  31. hasCalledReset = true;
  32. #endif
  33. sampleRate = newSampleRate;
  34. incomingMessages.clear();
  35. lastCallbackTime = Time::getMillisecondCounterHiRes();
  36. }
  37. void MidiMessageCollector::addMessageToQueue (const MidiMessage& message)
  38. {
  39. const ScopedLock sl (midiCallbackLock);
  40. #if JUCE_DEBUG
  41. jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
  42. #endif
  43. // the messages that come in here need to be time-stamped correctly - see MidiInput
  44. // for details of what the number should be.
  45. jassert (message.getTimeStamp() != 0);
  46. auto sampleNumber = (int) ((message.getTimeStamp() - 0.001 * lastCallbackTime) * sampleRate);
  47. incomingMessages.addEvent (message, sampleNumber);
  48. // if the messages don't get used for over a second, we'd better
  49. // get rid of any old ones to avoid the queue getting too big
  50. if (sampleNumber > sampleRate)
  51. incomingMessages.clear (0, sampleNumber - (int) sampleRate);
  52. }
  53. void MidiMessageCollector::removeNextBlockOfMessages (MidiBuffer& destBuffer,
  54. const int numSamples)
  55. {
  56. const ScopedLock sl (midiCallbackLock);
  57. #if JUCE_DEBUG
  58. jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
  59. #endif
  60. jassert (numSamples > 0);
  61. auto timeNow = Time::getMillisecondCounterHiRes();
  62. auto msElapsed = timeNow - lastCallbackTime;
  63. lastCallbackTime = timeNow;
  64. if (! incomingMessages.isEmpty())
  65. {
  66. int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));
  67. int startSample = 0;
  68. int scale = 1 << 16;
  69. if (numSourceSamples > numSamples)
  70. {
  71. // if our list of events is longer than the buffer we're being
  72. // asked for, scale them down to squeeze them all in..
  73. const int maxBlockLengthToUse = numSamples << 5;
  74. auto iter = incomingMessages.cbegin();
  75. if (numSourceSamples > maxBlockLengthToUse)
  76. {
  77. startSample = numSourceSamples - maxBlockLengthToUse;
  78. numSourceSamples = maxBlockLengthToUse;
  79. iter = incomingMessages.findNextSamplePosition (startSample);
  80. }
  81. scale = (numSamples << 10) / numSourceSamples;
  82. std::for_each (iter, incomingMessages.cend(), [&] (const MidiMessageMetadata& meta)
  83. {
  84. const auto pos = ((meta.samplePosition - startSample) * scale) >> 10;
  85. destBuffer.addEvent (meta.data, meta.numBytes, jlimit (0, numSamples - 1, pos));
  86. });
  87. }
  88. else
  89. {
  90. // if our event list is shorter than the number we need, put them
  91. // towards the end of the buffer
  92. startSample = numSamples - numSourceSamples;
  93. for (const auto metadata : incomingMessages)
  94. destBuffer.addEvent (metadata.data, metadata.numBytes,
  95. jlimit (0, numSamples - 1, metadata.samplePosition + startSample));
  96. }
  97. incomingMessages.clear();
  98. }
  99. }
  100. void MidiMessageCollector::ensureStorageAllocated (size_t bytes)
  101. {
  102. incomingMessages.ensureSize (bytes);
  103. }
  104. //==============================================================================
  105. void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
  106. {
  107. MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
  108. m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
  109. addMessageToQueue (m);
  110. }
  111. void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
  112. {
  113. MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
  114. m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
  115. addMessageToQueue (m);
  116. }
  117. void MidiMessageCollector::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
  118. {
  119. addMessageToQueue (message);
  120. }
  121. } // namespace juce