OpenShot Library | libopenshot-audio 0.2.0
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
29
33
34//==============================================================================
36{
37 jassert (newSampleRate > 0);
38
39 const ScopedLock sl (midiCallbackLock);
40 #if JUCE_DEBUG
41 hasCalledReset = true;
42 #endif
43 sampleRate = newSampleRate;
44 incomingMessages.clear();
45 lastCallbackTime = Time::getMillisecondCounterHiRes();
46}
47
49{
50 #if JUCE_DEBUG
51 jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
52 #endif
53
54 // the messages that come in here need to be time-stamped correctly - see MidiInput
55 // for details of what the number should be.
56 jassert (message.getTimeStamp() != 0);
57
58 const ScopedLock sl (midiCallbackLock);
59
60 auto sampleNumber = (int) ((message.getTimeStamp() - 0.001 * lastCallbackTime) * sampleRate);
61
62 incomingMessages.addEvent (message, sampleNumber);
63
64 // if the messages don't get used for over a second, we'd better
65 // get rid of any old ones to avoid the queue getting too big
66 if (sampleNumber > sampleRate)
67 incomingMessages.clear (0, sampleNumber - (int) sampleRate);
68}
69
71 const int numSamples)
72{
73 #if JUCE_DEBUG
74 jassert (hasCalledReset); // you need to call reset() to set the correct sample rate before using this object
75 #endif
76
77 jassert (numSamples > 0);
78
80 auto msElapsed = timeNow - lastCallbackTime;
81
82 const ScopedLock sl (midiCallbackLock);
83 lastCallbackTime = timeNow;
84
85 if (! incomingMessages.isEmpty())
86 {
87 int numSourceSamples = jmax (1, roundToInt (msElapsed * 0.001 * sampleRate));
88 int startSample = 0;
89 int scale = 1 << 16;
90
91 const uint8* midiData;
92 int numBytes, samplePosition;
93
94 MidiBuffer::Iterator iter (incomingMessages);
95
96 if (numSourceSamples > numSamples)
97 {
98 // if our list of events is longer than the buffer we're being
99 // asked for, scale them down to squeeze them all in..
100 const int maxBlockLengthToUse = numSamples << 5;
101
103 {
106 iter.setNextSamplePosition (startSample);
107 }
108
109 scale = (numSamples << 10) / numSourceSamples;
110
111 while (iter.getNextEvent (midiData, numBytes, samplePosition))
112 {
113 samplePosition = ((samplePosition - startSample) * scale) >> 10;
114
115 destBuffer.addEvent (midiData, numBytes,
116 jlimit (0, numSamples - 1, samplePosition));
117 }
118 }
119 else
120 {
121 // if our event list is shorter than the number we need, put them
122 // towards the end of the buffer
123 startSample = numSamples - numSourceSamples;
124
125 while (iter.getNextEvent (midiData, numBytes, samplePosition))
126 {
127 destBuffer.addEvent (midiData, numBytes,
128 jlimit (0, numSamples - 1, samplePosition + startSample));
129 }
130 }
131
132 incomingMessages.clear();
133 }
134}
135
136//==============================================================================
138{
140 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
141
143}
144
146{
148 m.setTimeStamp (Time::getMillisecondCounterHiRes() * 0.001);
149
151}
152
157
158} // namespace juce
Holds a resizable array of primitive or copy-by-value objects.
Definition juce_Array.h:60
Used to iterate through the events in a MidiBuffer.
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.
static double getMillisecondCounterHiRes() noexcept
Returns the number of millisecs since a fixed event (usually system startup).