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.

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