|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2017 - ROLI Ltd.
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- The code included in this file is provided under the terms of the ISC license
- http://www.isc.org/downloads/software-support-policy/isc-license. Permission
- To use, copy, modify, and/or distribute this software for any purpose with or
- without fee is hereby granted provided that the above copyright notice and
- this permission notice appear in all copies.
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- MPEZoneLayout::MPEZoneLayout() noexcept
- {
- }
-
- MPEZoneLayout::MPEZoneLayout (const MPEZoneLayout& other)
- : zones (other.zones)
- {
- }
-
- MPEZoneLayout& MPEZoneLayout::operator= (const MPEZoneLayout& other)
- {
- zones = other.zones;
- listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
- return *this;
- }
-
- //==============================================================================
- bool MPEZoneLayout::addZone (MPEZone newZone)
- {
- bool noOtherZonesModified = true;
-
- for (int i = zones.size(); --i >= 0;)
- {
- MPEZone& zone = zones.getReference (i);
-
- if (zone.overlapsWith (newZone))
- {
- if (! zone.truncateToFit (newZone))
- zones.removeRange (i, 1);
- // can't use zones.remove (i) because that requires a default c'tor :-(
-
- noOtherZonesModified = false;
- }
- }
-
- zones.add (newZone);
- listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
- return noOtherZonesModified;
- }
-
- //==============================================================================
- int MPEZoneLayout::getNumZones() const noexcept
- {
- return zones.size();
- }
-
- MPEZone* MPEZoneLayout::getZoneByIndex (int index) const noexcept
- {
- if (zones.size() < index)
- return nullptr;
-
- return &(zones.getReference (index));
- }
-
- void MPEZoneLayout::clearAllZones()
- {
- zones.clear();
- listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
- }
-
- //==============================================================================
- void MPEZoneLayout::processNextMidiEvent (const MidiMessage& message)
- {
- if (! message.isController())
- return;
-
- MidiRPNMessage rpn;
-
- if (rpnDetector.parseControllerMessage (message.getChannel(),
- message.getControllerNumber(),
- message.getControllerValue(),
- rpn))
- {
- processRpnMessage (rpn);
- }
- }
-
- void MPEZoneLayout::processRpnMessage (MidiRPNMessage rpn)
- {
- if (rpn.parameterNumber == MPEMessages::zoneLayoutMessagesRpnNumber)
- processZoneLayoutRpnMessage (rpn);
- else if (rpn.parameterNumber == 0)
- processPitchbendRangeRpnMessage (rpn);
- }
-
- void MPEZoneLayout::processZoneLayoutRpnMessage (MidiRPNMessage rpn)
- {
- if (rpn.value < 16)
- addZone (MPEZone (rpn.channel - 1, rpn.value));
- else
- clearAllZones();
- }
-
- //==============================================================================
- void MPEZoneLayout::processPitchbendRangeRpnMessage (MidiRPNMessage rpn)
- {
- if (MPEZone* zone = getZoneByFirstNoteChannel (rpn.channel))
- {
- if (zone->getPerNotePitchbendRange() != rpn.value)
- {
- zone->setPerNotePitchbendRange (rpn.value);
- listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
- return;
- }
- }
-
- if (MPEZone* zone = getZoneByMasterChannel (rpn.channel))
- {
- if (zone->getMasterPitchbendRange() != rpn.value)
- {
- zone->setMasterPitchbendRange (rpn.value);
- listeners.call (&MPEZoneLayout::Listener::zoneLayoutChanged, *this);
- return;
- }
- }
- }
-
- //==============================================================================
- void MPEZoneLayout::processNextMidiBuffer (const MidiBuffer& buffer)
- {
- MidiBuffer::Iterator iter (buffer);
- MidiMessage message;
- int samplePosition; // not actually used, so no need to initialise.
-
- while (iter.getNextEvent (message, samplePosition))
- processNextMidiEvent (message);
- }
-
- //==============================================================================
- MPEZone* MPEZoneLayout::getZoneByChannel (int channel) const noexcept
- {
- for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
- if (zone->isUsingChannel (channel))
- return zone;
-
- return nullptr;
- }
-
- MPEZone* MPEZoneLayout::getZoneByMasterChannel (int channel) const noexcept
- {
- for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
- if (zone->getMasterChannel() == channel)
- return zone;
-
- return nullptr;
- }
-
- MPEZone* MPEZoneLayout::getZoneByFirstNoteChannel (int channel) const noexcept
- {
- for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
- if (zone->getFirstNoteChannel() == channel)
- return zone;
-
- return nullptr;
- }
-
- MPEZone* MPEZoneLayout::getZoneByNoteChannel (int channel) const noexcept
- {
- for (MPEZone* zone = zones.begin(); zone != zones.end(); ++zone)
- if (zone->isUsingChannelAsNoteChannel (channel))
- return zone;
-
- return nullptr;
- }
-
- //==============================================================================
- void MPEZoneLayout::addListener (Listener* const listenerToAdd) noexcept
- {
- listeners.add (listenerToAdd);
- }
-
- void MPEZoneLayout::removeListener (Listener* const listenerToRemove) noexcept
- {
- listeners.remove (listenerToRemove);
- }
-
- //==============================================================================
- //==============================================================================
- #if JUCE_UNIT_TESTS
-
-
- class MPEZoneLayoutTests : public UnitTest
- {
- public:
- MPEZoneLayoutTests() : UnitTest ("MPEZoneLayout class", "MIDI/MPE") {}
-
- void runTest() override
- {
- beginTest ("initialisation");
- {
- MPEZoneLayout layout;
- expectEquals (layout.getNumZones(), 0);
- }
-
- beginTest ("adding zones");
- {
- MPEZoneLayout layout;
-
- expect (layout.addZone (MPEZone (1, 7)));
-
- expectEquals (layout.getNumZones(), 1);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
-
- expect (layout.addZone (MPEZone (9, 7)));
-
- expectEquals (layout.getNumZones(), 2);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
- expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
- expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
-
- expect (! layout.addZone (MPEZone (5, 3)));
-
- expectEquals (layout.getNumZones(), 3);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
- expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
- expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
- expectEquals (layout.getZoneByIndex (2)->getMasterChannel(), 5);
- expectEquals (layout.getZoneByIndex (2)->getNumNoteChannels(), 3);
-
- expect (! layout.addZone (MPEZone (5, 4)));
-
- expectEquals (layout.getNumZones(), 2);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
- expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 5);
- expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
-
- expect (! layout.addZone (MPEZone (6, 4)));
-
- expectEquals (layout.getNumZones(), 2);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
- expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 6);
- expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 4);
- }
-
- beginTest ("querying zones");
- {
- MPEZoneLayout layout;
-
- layout.addZone (MPEZone (2, 5));
- layout.addZone (MPEZone (9, 4));
-
- expect (layout.getZoneByMasterChannel (1) == nullptr);
- expect (layout.getZoneByMasterChannel (2) != nullptr);
- expect (layout.getZoneByMasterChannel (3) == nullptr);
- expect (layout.getZoneByMasterChannel (8) == nullptr);
- expect (layout.getZoneByMasterChannel (9) != nullptr);
- expect (layout.getZoneByMasterChannel (10) == nullptr);
-
- expectEquals (layout.getZoneByMasterChannel (2)->getNumNoteChannels(), 5);
- expectEquals (layout.getZoneByMasterChannel (9)->getNumNoteChannels(), 4);
-
- expect (layout.getZoneByFirstNoteChannel (2) == nullptr);
- expect (layout.getZoneByFirstNoteChannel (3) != nullptr);
- expect (layout.getZoneByFirstNoteChannel (4) == nullptr);
- expect (layout.getZoneByFirstNoteChannel (9) == nullptr);
- expect (layout.getZoneByFirstNoteChannel (10) != nullptr);
- expect (layout.getZoneByFirstNoteChannel (11) == nullptr);
-
- expectEquals (layout.getZoneByFirstNoteChannel (3)->getNumNoteChannels(), 5);
- expectEquals (layout.getZoneByFirstNoteChannel (10)->getNumNoteChannels(), 4);
-
- expect (layout.getZoneByNoteChannel (2) == nullptr);
- expect (layout.getZoneByNoteChannel (3) != nullptr);
- expect (layout.getZoneByNoteChannel (4) != nullptr);
- expect (layout.getZoneByNoteChannel (6) != nullptr);
- expect (layout.getZoneByNoteChannel (7) != nullptr);
- expect (layout.getZoneByNoteChannel (8) == nullptr);
- expect (layout.getZoneByNoteChannel (9) == nullptr);
- expect (layout.getZoneByNoteChannel (10) != nullptr);
- expect (layout.getZoneByNoteChannel (11) != nullptr);
- expect (layout.getZoneByNoteChannel (12) != nullptr);
- expect (layout.getZoneByNoteChannel (13) != nullptr);
- expect (layout.getZoneByNoteChannel (14) == nullptr);
-
- expectEquals (layout.getZoneByNoteChannel (5)->getNumNoteChannels(), 5);
- expectEquals (layout.getZoneByNoteChannel (13)->getNumNoteChannels(), 4);
- }
-
- beginTest ("clear all zones");
- {
- MPEZoneLayout layout;
-
- expect (layout.addZone (MPEZone (1, 7)));
- expect (layout.addZone (MPEZone (10, 2)));
- layout.clearAllZones();
-
- expectEquals (layout.getNumZones(), 0);
- }
-
- beginTest ("process MIDI buffers");
- {
- MPEZoneLayout layout;
- MidiBuffer buffer;
-
- buffer = MPEMessages::addZone (MPEZone (1, 7));
- layout.processNextMidiBuffer (buffer);
-
- expectEquals (layout.getNumZones(), 1);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
-
- buffer = MPEMessages::addZone (MPEZone (9, 7));
- layout.processNextMidiBuffer (buffer);
-
- expectEquals (layout.getNumZones(), 2);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 7);
- expectEquals (layout.getZoneByIndex (1)->getMasterChannel(), 9);
- expectEquals (layout.getZoneByIndex (1)->getNumNoteChannels(), 7);
-
- MPEZone zone (1, 10);
-
- buffer = MPEMessages::addZone (zone);
- layout.processNextMidiBuffer (buffer);
-
- expectEquals (layout.getNumZones(), 1);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 10);
-
- zone.setPerNotePitchbendRange (33);
- zone.setMasterPitchbendRange (44);
-
- buffer = MPEMessages::masterPitchbendRange (zone);
- buffer.addEvents (MPEMessages::perNotePitchbendRange (zone), 0, -1, 0);
-
- layout.processNextMidiBuffer (buffer);
-
- expectEquals (layout.getZoneByIndex (0)->getPerNotePitchbendRange(), 33);
- expectEquals (layout.getZoneByIndex (0)->getMasterPitchbendRange(), 44);
- }
-
- beginTest ("process individual MIDI messages");
- {
- MPEZoneLayout layout;
-
- layout.processNextMidiEvent (MidiMessage (0x80, 0x59, 0xd0)); // unrelated note-off msg
- layout.processNextMidiEvent (MidiMessage (0xb1, 0x64, 0x06)); // RPN part 1
- layout.processNextMidiEvent (MidiMessage (0xb1, 0x65, 0x00)); // RPN part 2
- layout.processNextMidiEvent (MidiMessage (0xb8, 0x0b, 0x66)); // unrelated CC msg
- layout.processNextMidiEvent (MidiMessage (0xb1, 0x06, 0x03)); // RPN part 3
- layout.processNextMidiEvent (MidiMessage (0x90, 0x60, 0x00)); // unrelated note-on msg
-
- expectEquals (layout.getNumZones(), 1);
- expectEquals (layout.getZoneByIndex (0)->getMasterChannel(), 1);
- expectEquals (layout.getZoneByIndex (0)->getNumNoteChannels(), 3);
- }
- }
- };
-
- static MPEZoneLayoutTests MPEZoneLayoutUnitTests;
-
-
- #endif // JUCE_UNIT_TESTS
-
- } // namespace juce
|