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.

160 lines
5.8KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. MidiMessageCollector::MidiMessageCollector()
  24. : lastCallbackTime (0),
  25. sampleRate (44100.0001)
  26. {
  27. }
  28. MidiMessageCollector::~MidiMessageCollector()
  29. {
  30. }
  31. //==============================================================================
  32. void MidiMessageCollector::reset (const double newSampleRate)
  33. {
  34. jassert (newSampleRate > 0);
  35. const ScopedLock sl (midiCallbackLock);
  36. sampleRate = newSampleRate;
  37. incomingMessages.clear();
  38. lastCallbackTime = Time::getMillisecondCounterHiRes();
  39. }
  40. void MidiMessageCollector::addMessageToQueue (const MidiMessage& message)
  41. {
  42. // you need to call reset() to set the correct sample rate before using this object
  43. jassert (sampleRate != 44100.0001);
  44. // the messages that come in here need to be time-stamped correctly - see MidiInput
  45. // for details of what the number should be.
  46. jassert (message.getTimeStamp() != 0);
  47. const ScopedLock sl (midiCallbackLock);
  48. const int sampleNumber
  49. = (int) ((message.getTimeStamp() - 0.001 * lastCallbackTime) * sampleRate);
  50. incomingMessages.addEvent (message, sampleNumber);
  51. // if the messages don't get used for over a second, we'd better
  52. // get rid of any old ones to avoid the queue getting too big
  53. if (sampleNumber > sampleRate)
  54. incomingMessages.clear (0, sampleNumber - (int) sampleRate);
  55. }
  56. void MidiMessageCollector::removeNextBlockOfMessages (MidiBuffer& destBuffer,
  57. const int numSamples)
  58. {
  59. // you need to call reset() to set the correct sample rate before using this object
  60. jassert (sampleRate != 44100.0001);
  61. jassert (numSamples > 0);
  62. const double timeNow = Time::getMillisecondCounterHiRes();
  63. const double msElapsed = timeNow - lastCallbackTime;
  64. const ScopedLock sl (midiCallbackLock);
  65. lastCallbackTime = timeNow;
  66. if (! incomingMessages.isEmpty())
  67. {
  68. int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));
  69. int startSample = 0;
  70. int scale = 1 << 16;
  71. const uint8* midiData;
  72. int numBytes, samplePosition;
  73. MidiBuffer::Iterator iter (incomingMessages);
  74. if (numSourceSamples > numSamples)
  75. {
  76. // if our list of events is longer than the buffer we're being
  77. // asked for, scale them down to squeeze them all in..
  78. const int maxBlockLengthToUse = numSamples << 5;
  79. if (numSourceSamples > maxBlockLengthToUse)
  80. {
  81. startSample = numSourceSamples - maxBlockLengthToUse;
  82. numSourceSamples = maxBlockLengthToUse;
  83. iter.setNextSamplePosition (startSample);
  84. }
  85. scale = (numSamples << 10) / numSourceSamples;
  86. while (iter.getNextEvent (midiData, numBytes, samplePosition))
  87. {
  88. samplePosition = ((samplePosition - startSample) * scale) >> 10;
  89. destBuffer.addEvent (midiData, numBytes,
  90. jlimit (0, numSamples - 1, samplePosition));
  91. }
  92. }
  93. else
  94. {
  95. // if our event list is shorter than the number we need, put them
  96. // towards the end of the buffer
  97. startSample = numSamples - numSourceSamples;
  98. while (iter.getNextEvent (midiData, numBytes, samplePosition))
  99. {
  100. destBuffer.addEvent (midiData, numBytes,
  101. jlimit (0, numSamples - 1, samplePosition + startSample));
  102. }
  103. }
  104. incomingMessages.clear();
  105. }
  106. }
  107. //==============================================================================
  108. void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
  109. {
  110. MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
  111. m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
  112. addMessageToQueue (m);
  113. }
  114. void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
  115. {
  116. MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
  117. m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
  118. addMessageToQueue (m);
  119. }
  120. void MidiMessageCollector::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
  121. {
  122. addMessageToQueue (message);
  123. }