JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
juce_MidiMessageCollector.cpp
Go to the documentation of this file.
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
29
33
34//==============================================================================
36{
37 const ScopedLock sl (midiCallbackLock);
38
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
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
102 {
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//==============================================================================
137void MidiMessageCollector::handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
138{
139 MidiMessage m (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity));
141
143}
144
145void MidiMessageCollector::handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity)
146{
147 MidiMessage m (MidiMessage::noteOff (midiChannel, midiNoteNumber, velocity));
149
151}
152
157
158} // namespace juce
Automatically locks and unlocks a mutex object.
Holds a sequence of time-stamped midi events.
void ensureSize(size_t minimumNumBytes)
Preallocates some memory for the buffer to use.
MidiBufferIterator cbegin() const noexcept
Get a read-only iterator pointing to the beginning of this buffer.
MidiBufferIterator findNextSamplePosition(int samplePosition) const noexcept
Get an iterator pointing to the first event with a timestamp greater-than or equal-to samplePosition.
MidiBufferIterator cend() const noexcept
Get a read-only iterator pointing one past the end of this buffer.
bool isEmpty() const noexcept
Returns true if the buffer is empty.
bool addEvent(const MidiMessage &midiMessage, int sampleNumber)
Adds an event to the buffer.
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
Receives an incoming message.
void handleNoteOn(MidiKeyboardState *, int midiChannel, int midiNoteNumber, float velocity) override
Called when one of the MidiKeyboardState's keys is pressed.
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
Called when one of the MidiKeyboardState's keys is released.
MidiMessageCollector()
Creates a MidiMessageCollector.
void ensureStorageAllocated(size_t bytes)
Preallocates storage for collected messages.
~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).
T for_each(T... args)
#define jassert(expression)
Platform-independent assertion macro.
typedef int
JUCE Namespace.
constexpr bool approximatelyEqual(Type a, Type b, Tolerance< Type > tolerance=Tolerance< Type >{} .withAbsolute(std::numeric_limits< Type >::min()) .withRelative(std::numeric_limits< Type >::epsilon()))
Returns true if the two floating-point numbers are approximately equal.
constexpr Type jmax(Type a, Type b)
Returns the larger of two values.
Type jlimit(Type lowerLimit, Type upperLimit, Type valueToConstrain) noexcept
Constrains a value to keep it within a given range.
Type unalignedPointerCast(void *ptr) noexcept
Casts a pointer to another type via void*, which suppresses the cast-align warning which sometimes ar...
Definition juce_Memory.h:88
int roundToInt(const FloatType value) noexcept
Fast floating-point-to-integer conversion.
A view of MIDI message data stored in a contiguous buffer.