VST3: fetch midi mappings all at once, use it for note/sound-off
[carla.git] / source / modules / juce_audio_basics / midi / juce_MidiKeyboardState.cpp
blob1cf11feb08e5b7a3394cb1b10e6ff70c69c56ff4
1 /*
2 ==============================================================================
4 This file is part of the JUCE library.
5 Copyright (c) 2022 - Raw Material Software Limited
7 JUCE is an open source library subject to commercial or open-source
8 licensing.
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.
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.
20 ==============================================================================
23 namespace juce
26 MidiKeyboardState::MidiKeyboardState()
28 zerostruct (noteStates);
31 //==============================================================================
32 void MidiKeyboardState::reset()
34 const ScopedLock sl (lock);
35 zerostruct (noteStates);
36 eventsToAdd.clear();
39 bool MidiKeyboardState::isNoteOn (const int midiChannel, const int n) const noexcept
41 jassert (midiChannel > 0 && midiChannel <= 16);
43 return isPositiveAndBelow (n, 128)
44 && (noteStates[n] & (1 << (midiChannel - 1))) != 0;
47 bool MidiKeyboardState::isNoteOnForChannels (const int midiChannelMask, const int n) const noexcept
49 return isPositiveAndBelow (n, 128)
50 && (noteStates[n] & midiChannelMask) != 0;
53 void MidiKeyboardState::noteOn (const int midiChannel, const int midiNoteNumber, const float velocity)
55 jassert (midiChannel > 0 && midiChannel <= 16);
56 jassert (isPositiveAndBelow (midiNoteNumber, 128));
58 const ScopedLock sl (lock);
60 if (isPositiveAndBelow (midiNoteNumber, 128))
62 const int timeNow = (int) Time::getMillisecondCounter();
63 eventsToAdd.addEvent (MidiMessage::noteOn (midiChannel, midiNoteNumber, velocity), timeNow);
64 eventsToAdd.clear (0, timeNow - 500);
66 noteOnInternal (midiChannel, midiNoteNumber, velocity);
70 void MidiKeyboardState::noteOnInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
72 if (isPositiveAndBelow (midiNoteNumber, 128))
74 noteStates[midiNoteNumber] = static_cast<uint16> (noteStates[midiNoteNumber] | (1 << (midiChannel - 1)));
75 listeners.call ([&] (Listener& l) { l.handleNoteOn (this, midiChannel, midiNoteNumber, velocity); });
79 void MidiKeyboardState::noteOff (const int midiChannel, const int midiNoteNumber, const float velocity)
81 const ScopedLock sl (lock);
83 if (isNoteOn (midiChannel, midiNoteNumber))
85 const int timeNow = (int) Time::getMillisecondCounter();
86 eventsToAdd.addEvent (MidiMessage::noteOff (midiChannel, midiNoteNumber), timeNow);
87 eventsToAdd.clear (0, timeNow - 500);
89 noteOffInternal (midiChannel, midiNoteNumber, velocity);
93 void MidiKeyboardState::noteOffInternal (const int midiChannel, const int midiNoteNumber, const float velocity)
95 if (isNoteOn (midiChannel, midiNoteNumber))
97 noteStates[midiNoteNumber] = static_cast<uint16> (noteStates[midiNoteNumber] & ~(1 << (midiChannel - 1)));
98 listeners.call ([&] (Listener& l) { l.handleNoteOff (this, midiChannel, midiNoteNumber, velocity); });
102 void MidiKeyboardState::allNotesOff (const int midiChannel)
104 const ScopedLock sl (lock);
106 if (midiChannel <= 0)
108 for (int i = 1; i <= 16; ++i)
109 allNotesOff (i);
111 else
113 for (int i = 0; i < 128; ++i)
114 noteOff (midiChannel, i, 0.0f);
118 void MidiKeyboardState::processNextMidiEvent (const MidiMessage& message)
120 if (message.isNoteOn())
122 noteOnInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
124 else if (message.isNoteOff())
126 noteOffInternal (message.getChannel(), message.getNoteNumber(), message.getFloatVelocity());
128 else if (message.isAllNotesOff())
130 for (int i = 0; i < 128; ++i)
131 noteOffInternal (message.getChannel(), i, 0.0f);
135 void MidiKeyboardState::processNextMidiBuffer (MidiBuffer& buffer,
136 const int startSample,
137 const int numSamples,
138 const bool injectIndirectEvents)
140 const ScopedLock sl (lock);
142 for (const auto metadata : buffer)
143 processNextMidiEvent (metadata.getMessage());
145 if (injectIndirectEvents)
147 const int firstEventToAdd = eventsToAdd.getFirstEventTime();
148 const double scaleFactor = numSamples / (double) (eventsToAdd.getLastEventTime() + 1 - firstEventToAdd);
150 for (const auto metadata : eventsToAdd)
152 const auto pos = jlimit (0, numSamples - 1, roundToInt ((metadata.samplePosition - firstEventToAdd) * scaleFactor));
153 buffer.addEvent (metadata.getMessage(), startSample + pos);
157 eventsToAdd.clear();
160 //==============================================================================
161 void MidiKeyboardState::addListener (Listener* listener)
163 const ScopedLock sl (lock);
164 listeners.add (listener);
167 void MidiKeyboardState::removeListener (Listener* listener)
169 const ScopedLock sl (lock);
170 listeners.remove (listener);
173 } // namespace juce