OpenShot Audio Library | OpenShotAudio: juce_MidiMessageCollector.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.3.0

Loading...
Searching...
No Matches
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
23namespace juce
24{
25
26MidiMessageCollector::MidiMessageCollector()
27{
28}
29
30MidiMessageCollector::~MidiMessageCollector()
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
49 void MidiMessageCollector::addMessageToQueue (const MidiMessage& message)
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
71 void MidiMessageCollector::removeNextBlockOfMessages (MidiBuffer& destBuffer,
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));
142 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
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));
150 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
151
152 addMessageToQueue (m);
153}
154
155 void MidiMessageCollector::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
156{
157 addMessageToQueue (message);
158}
159
160} // namespace juce
juce::GenericScopedLock
Definition: juce_ScopedLock.h:55
juce::MidiBuffer::Iterator
Definition: juce_MidiBuffer.h:171
juce::MidiBuffer::Iterator::setNextSamplePosition
void setNextSamplePosition(int samplePosition) noexcept
Definition: juce_MidiBuffer.cpp:196
juce::MidiBuffer::Iterator::getNextEvent
bool getNextEvent(MidiMessage &result, int &samplePosition) noexcept
Definition: juce_MidiBuffer.cpp:219
juce::MidiBuffer
Definition: juce_MidiBuffer.h:43
juce::MidiBuffer::addEvent
void addEvent(const MidiMessage &midiMessage, int sampleNumber)
Definition: juce_MidiBuffer.cpp:113
juce::MidiBuffer::isEmpty
bool isEmpty() const noexcept
Definition: juce_MidiBuffer.cpp:103
juce::MidiBuffer::clear
void clear() noexcept
Definition: juce_MidiBuffer.cpp:101
juce::MidiInput
Definition: juce_MidiDevices.h:82
juce::MidiKeyboardState
Definition: juce_MidiKeyboardState.h:87
juce::MidiMessageCollector::handleIncomingMidiMessage
void handleIncomingMidiMessage(MidiInput *, const MidiMessage &) override
Definition: juce_MidiMessageCollector.cpp:155
juce::MidiMessageCollector::handleNoteOn
void handleNoteOn(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
Definition: juce_MidiMessageCollector.cpp:139
juce::MidiMessageCollector::removeNextBlockOfMessages
void removeNextBlockOfMessages(MidiBuffer &destBuffer, int numSamples)
Definition: juce_MidiMessageCollector.cpp:71
juce::MidiMessageCollector::handleNoteOff
void handleNoteOff(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
Definition: juce_MidiMessageCollector.cpp:147
juce::MidiMessageCollector::MidiMessageCollector
MidiMessageCollector()
Definition: juce_MidiMessageCollector.cpp:26
juce::MidiMessageCollector::~MidiMessageCollector
~MidiMessageCollector() override
Definition: juce_MidiMessageCollector.cpp:30
juce::MidiMessageCollector::reset
void reset(double sampleRate)
Definition: juce_MidiMessageCollector.cpp:35
juce::MidiMessageCollector::addMessageToQueue
void addMessageToQueue(const MidiMessage &message)
Definition: juce_MidiMessageCollector.cpp:49
juce::MidiMessage
Definition: juce_MidiMessage.h:35
juce::MidiMessage::noteOn
static MidiMessage noteOn(int channel, int noteNumber, float velocity) noexcept
Definition: juce_MidiMessage.cpp:584
juce::MidiMessage::getTimeStamp
double getTimeStamp() const noexcept
Definition: juce_MidiMessage.h:163
juce::MidiMessage::noteOff
static MidiMessage noteOff(int channel, int noteNumber, float velocity) noexcept
Definition: juce_MidiMessage.cpp:598
juce::MidiMessage::setTimeStamp
void setTimeStamp(double newTimestamp) noexcept
Definition: juce_MidiMessage.h:169
juce::Time::getMillisecondCounterHiRes
static double getMillisecondCounterHiRes() noexcept