OpenShot Audio Library | OpenShotAudio: juce_MidiMessageCollector.cpp Source File

OpenShot Audio Library | OpenShotAudio 0.4.0

Loading...
Searching...
No Matches
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
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 (! 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
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 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
131 void MidiMessageCollector::ensureStorageAllocated (size_t bytes)
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));
140 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
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));
148 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
149
150 addMessageToQueue (m);
151}
152
153 void MidiMessageCollector::handleIncomingMidiMessage (MidiInput*, const MidiMessage& message)
154{
155 addMessageToQueue (message);
156}
157
158} // namespace juce
juce::GenericScopedLock
Definition juce_ScopedLock.h:55
juce::MidiBuffer
Definition juce_MidiBuffer.h:145
juce::MidiBuffer::ensureSize
void ensureSize(size_t minimumNumBytes)
Definition juce_MidiBuffer.cpp:111
juce::MidiBuffer::cbegin
MidiBufferIterator cbegin() const noexcept
Definition juce_MidiBuffer.h:265
juce::MidiBuffer::findNextSamplePosition
MidiBufferIterator findNextSamplePosition(int samplePosition) const noexcept
Definition juce_MidiBuffer.cpp:203
juce::MidiBuffer::cend
MidiBufferIterator cend() const noexcept
Definition juce_MidiBuffer.h:268
juce::MidiBuffer::isEmpty
bool isEmpty() const noexcept
Definition juce_MidiBuffer.cpp:112
juce::MidiBuffer::addEvent
bool addEvent(const MidiMessage &midiMessage, int sampleNumber)
Definition juce_MidiBuffer.cpp:122
juce::MidiBuffer::clear
void clear() noexcept
Definition juce_MidiBuffer.cpp:110
juce::MidiInput
Definition juce_MidiDevices.h:164
juce::MidiKeyboardState
Definition juce_MidiKeyboardState.h:42
juce::MidiMessageCollector::handleIncomingMidiMessage
void handleIncomingMidiMessage(MidiInput *, const MidiMessage &) override
Definition juce_MidiMessageCollector.cpp:153
juce::MidiMessageCollector::handleNoteOn
void handleNoteOn(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
Definition juce_MidiMessageCollector.cpp:137
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:145
juce::MidiMessageCollector::MidiMessageCollector
MidiMessageCollector()
Definition juce_MidiMessageCollector.cpp:26
juce::MidiMessageCollector::ensureStorageAllocated
void ensureStorageAllocated(size_t bytes)
Definition juce_MidiMessageCollector.cpp:131
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:604
juce::MidiMessage::getTimeStamp
double getTimeStamp() const noexcept
Definition juce_MidiMessage.h:164
juce::MidiMessage::noteOff
static MidiMessage noteOff(int channel, int noteNumber, float velocity) noexcept
Definition juce_MidiMessage.cpp:618
juce::MidiMessage::setTimeStamp
void setTimeStamp(double newTimestamp) noexcept
Definition juce_MidiMessage.h:170
juce::Time::getMillisecondCounterHiRes
static double getMillisecondCounterHiRes() noexcept
juce::MidiMessageMetadata
Definition juce_MidiBuffer.h:37