OpenShot Audio Library | OpenShotAudio  0.6.0
juce_MidiMessageCollector.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2022 - Raw Material Software Limited
6 
7  JUCE is an open source library subject to commercial or open-source
8  licensing.
9 
10  The code included in this file is provided under the terms of the ISC license
11  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
12  To use, copy, modify, and/or distribute this software for any purpose with or
13  without fee is hereby granted provided that the above copyright notice and
14  this permission notice appear in all copies.
15 
16  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
17  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
18  DISCLAIMED.
19 
20  ==============================================================================
21 */
22 
23 namespace juce
24 {
25 
27 {
28 }
29 
31 {
32 }
33 
34 //==============================================================================
35 void MidiMessageCollector::reset (const double newSampleRate)
36 {
37  const ScopedLock sl (midiCallbackLock);
38 
39  jassert (newSampleRate > 0);
40 
41  #if JUCE_DEBUG
42  hasCalledReset = true;
43  #endif
44  sampleRate = newSampleRate;
45  incomingMessages.clear();
46  lastCallbackTime = Time::getMillisecondCounterHiRes();
47 }
48 
50 {
51  const ScopedLock sl (midiCallbackLock);
52 
53  #if JUCE_DEBUG
54  jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
55  #endif
56 
57  // the messages that come in here need to be time-stamped correctly - see MidiInput
58  // for details of what the number should be.
59  jassert (! approximatelyEqual (message.getTimeStamp(), 0.0));
60 
61  auto sampleNumber = (int) ((message.getTimeStamp() - 0.001 * lastCallbackTime) * sampleRate);
62 
63  incomingMessages.addEvent (message, sampleNumber);
64 
65  // if the messages don't get used for over a second, we'd better
66  // get rid of any old ones to avoid the queue getting too big
67  if (sampleNumber > sampleRate)
68  incomingMessages.clear (0, sampleNumber - (int) sampleRate);
69 }
70 
72  const int numSamples)
73 {
74  const ScopedLock sl (midiCallbackLock);
75 
76  #if JUCE_DEBUG
77  jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
78  #endif
79 
80  jassert (numSamples > 0);
81 
82  auto timeNow = Time::getMillisecondCounterHiRes();
83  auto msElapsed = timeNow - lastCallbackTime;
84 
85  lastCallbackTime = timeNow;
86 
87  if (! incomingMessages.isEmpty())
88  {
89  int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));
90  int startSample = 0;
91  int scale = 1 << 16;
92 
93  if (numSourceSamples > numSamples)
94  {
95  // if our list of events is longer than the buffer we're being
96  // asked for, scale them down to squeeze them all in..
97  const int maxBlockLengthToUse = numSamples << 5;
98 
99  auto iter = incomingMessages.cbegin();
100 
101  if (numSourceSamples > maxBlockLengthToUse)
102  {
103  startSample = numSourceSamples - maxBlockLengthToUse;
104  numSourceSamples = maxBlockLengthToUse;
105  iter = incomingMessages.findNextSamplePosition (startSample);
106  }
107 
108  scale = (numSamples << 10) / numSourceSamples;
109 
110  std::for_each (iter, incomingMessages.cend(), [&] (const MidiMessageMetadata& meta)
111  {
112  const auto pos = ((meta.samplePosition - startSample) * scale) >> 10;
113  destBuffer.addEvent (meta.data, meta.numBytes, jlimit (0, numSamples - 1, pos));
114  });
115  }
116  else
117  {
118  // if our event list is shorter than the number we need, put them
119  // towards the end of the buffer
120  startSample = numSamples - numSourceSamples;
121 
122  for (const auto metadata : incomingMessages)
123  destBuffer.addEvent (metadata.data, metadata.numBytes,
124  jlimit (0, numSamples - 1, metadata.samplePosition + startSample));
125  }
126 
127  incomingMessages.clear();
128  }
129 }
130 
132 {
133  incomingMessages.ensureSize (bytes);
134 }
135 
136 //==============================================================================
137 void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
138 {
139  MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
141 
142  addMessageToQueue (m);
143 }
144 
145 void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
146 {
147  MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
149 
150  addMessageToQueue (m);
151 }
152 
154 {
155  addMessageToQueue (message);
156 }
157 
158 } // namespace juce
void ensureSize(size_t minimumNumBytes)
MidiBufferIterator cbegin() const noexcept
MidiBufferIterator findNextSamplePosition(int samplePosition) const noexcept
MidiBufferIterator cend() const noexcept
bool isEmpty() const noexcept
bool addEvent(const MidiMessage &midiMessage, int sampleNumber)
void clear() noexcept
void handleIncomingMidiMessage(MidiInput *, const MidiMessage &) override
void handleNoteOn(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
void removeNextBlockOfMessages(MidiBuffer &destBuffer, int numSamples)
void handleNoteOff(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
void addMessageToQueue(const MidiMessage &message)
static MidiMessage noteOn(int channel, int noteNumber, float velocity) noexcept
double getTimeStamp() const noexcept
static MidiMessage noteOff(int channel, int noteNumber, float velocity) noexcept
void setTimeStamp(double newTimestamp) noexcept
static double getMillisecondCounterHiRes() noexcept