OpenShot Library | OpenShotAudio  0.2.2
juce_MidiMessageCollector.cpp
1 /*
2  ==============================================================================
3 
4  This file is part of the JUCE library.
5  Copyright (c) 2017 - ROLI Ltd.
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 (message.getTimeStamp() != 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  const uint8* midiData;
94  int numBytes, samplePosition;
95 
96  MidiBuffer::Iterator iter (incomingMessages);
97 
98  if (numSourceSamples > numSamples)
99  {
100  // if our list of events is longer than the buffer we're being
101  // asked for, scale them down to squeeze them all in..
102  const int maxBlockLengthToUse = numSamples << 5;
103 
104  if (numSourceSamples > maxBlockLengthToUse)
105  {
106  startSample = numSourceSamples - maxBlockLengthToUse;
107  numSourceSamples = maxBlockLengthToUse;
108  iter.setNextSamplePosition (startSample);
109  }
110 
111  scale = (numSamples << 10) / numSourceSamples;
112 
113  while (iter.getNextEvent (midiData, numBytes, samplePosition))
114  {
115  samplePosition = ((samplePosition - startSample) * scale) >> 10;
116 
117  destBuffer.addEvent (midiData, numBytes,
118  jlimit (0, numSamples - 1, samplePosition));
119  }
120  }
121  else
122  {
123  // if our event list is shorter than the number we need, put them
124  // towards the end of the buffer
125  startSample = numSamples - numSourceSamples;
126 
127  while (iter.getNextEvent (midiData, numBytes, samplePosition))
128  {
129  destBuffer.addEvent (midiData, numBytes,
130  jlimit (0, numSamples - 1, samplePosition + startSample));
131  }
132  }
133 
134  incomingMessages.clear();
135  }
136 }
137 
138 //==============================================================================
139 void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
140 {
141  MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
143 
144  addMessageToQueue (m);
145 }
146 
147 void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
148 {
149  MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
151 
152  addMessageToQueue (m);
153 }
154 
156 {
157  addMessageToQueue (message);
158 }
159 
160 } // namespace juce
Automatically locks and unlocks a mutex object.
Used to iterate through the events in a MidiBuffer.
void setNextSamplePosition(int samplePosition) noexcept
Repositions the iterator so that the next event retrieved will be the first one whose sample position...
bool getNextEvent(MidiMessage &result, int &samplePosition) noexcept
Retrieves a copy of the next event from the buffer.
Holds a sequence of time-stamped midi events.
void addEvent(const MidiMessage &midiMessage, int sampleNumber)
Adds an event to the buffer.
bool isEmpty() const noexcept
Returns true if the buffer is empty.
void clear() noexcept
Removes all events from the buffer.
Represents a midi input device.
Represents a piano keyboard, keeping track of which keys are currently pressed.
void handleIncomingMidiMessage(MidiInput *, const MidiMessage &) override
void handleNoteOn(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
void removeNextBlockOfMessages(MidiBuffer &destBuffer, int numSamples)
Removes all the pending messages from the queue as a buffer.
void handleNoteOff(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
MidiMessageCollector()
Creates a MidiMessageCollector.
~MidiMessageCollector() override
Destructor.
void reset(double sampleRate)
Clears any messages from the queue.
void addMessageToQueue(const MidiMessage &message)
Takes an incoming real-time message and adds it to the queue.
Encapsulates a MIDI message.
static MidiMessage noteOn(int channel, int noteNumber, float velocity) noexcept
Creates a key-down message (using a floating-point velocity).
double getTimeStamp() const noexcept
Returns the timestamp associated with this message.
static MidiMessage noteOff(int channel, int noteNumber, float velocity) noexcept
Creates a key-up message.
void setTimeStamp(double newTimestamp) noexcept
Changes the message's associated timestamp.
static double getMillisecondCounterHiRes() noexcept
Returns the number of millisecs since a fixed event (usually system startup).