/* ============================================================================== This file is part of the JUCE library. Copyright (c) 2013 - Raw Material Software Ltd. Permission is granted to use this software under the terms of either: a) the GPL v2 (or any later version) b) the Affero GPL v3 Details of these licenses can be found at: www.gnu.org/licenses JUCE is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. ------------------------------------------------------------------------------ To release a closed-source product which uses JUCE, commercial licenses are available: visit www.juce.com for more information. ============================================================================== */ #ifndef JUCE_MIDIMESSAGECOLLECTOR_H_INCLUDED #define JUCE_MIDIMESSAGECOLLECTOR_H_INCLUDED //============================================================================== /** Collects incoming realtime MIDI messages and turns them into blocks suitable for processing by a block-based audio callback. The class can also be used as either a MidiKeyboardStateListener or a MidiInputCallback so it can easily use a midi input or keyboard component as its source. @see MidiMessage, MidiInput */ class JUCE_API MidiMessageCollector : public MidiKeyboardStateListener, public MidiInputCallback { public: //============================================================================== /** Creates a MidiMessageCollector. */ MidiMessageCollector(); /** Destructor. */ ~MidiMessageCollector(); //============================================================================== /** Clears any messages from the queue. You need to call this method before starting to use the collector, so that it knows the correct sample rate to use. */ void reset (double sampleRate); /** Takes an incoming real-time message and adds it to the queue. The message's timestamp is taken, and it will be ready for retrieval as part of the block returned by the next call to removeNextBlockOfMessages(). This method is fully thread-safe when overlapping calls are made with removeNextBlockOfMessages(). */ void addMessageToQueue (const MidiMessage& message); /** Removes all the pending messages from the queue as a buffer. This will also correct the messages' timestamps to make sure they're in the range 0 to numSamples - 1. This call should be made regularly by something like an audio processing callback, because the time that it happens is used in calculating the midi event positions. This method is fully thread-safe when overlapping calls are made with addMessageToQueue(). Precondition: numSamples must be greater than 0. */ void removeNextBlockOfMessages (MidiBuffer& destBuffer, int numSamples); //============================================================================== /** @internal */ void handleNoteOn (MidiKeyboardState*, int midiChannel, int midiNoteNumber, float velocity) override; /** @internal */ void handleNoteOff (MidiKeyboardState*, int midiChannel, int midiNoteNumber) override; /** @internal */ void handleIncomingMidiMessage (MidiInput*, const MidiMessage&) override; private: //============================================================================== double lastCallbackTime; CriticalSection midiCallbackLock; MidiBuffer incomingMessages; double sampleRate; JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (MidiMessageCollector) }; #endif // JUCE_MIDIMESSAGECOLLECTOR_H_INCLUDED