Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions include/element/session.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,4 @@ class Session : public Model,
typedef juce::ReferenceCountedObjectPtr<Session> SessionPtr;
typedef SessionPtr SessionRef;

/** Convert legacy <controllers> + <maps> data into flat <midiMappings>.
Operates directly on a session value tree so it can be unit tested.
Appends one MidiMapping per resolvable ControllerMap; legacy trees are
left untouched. Safe to call repeatedly only on freshly loaded data. */
void migrateControllerMaps (juce::ValueTree session);

} // namespace element
91 changes: 49 additions & 42 deletions src/session.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,51 @@ using namespace juce;

namespace element {

namespace detail {

void migrateControllerMaps (ValueTree session)
{
if (! session.isValid())
return;

auto controllers = session.getChildWithName (tags::controllers);
auto maps = session.getChildWithName (tags::maps);
auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr);

for (int i = 0; i < maps.getNumChildren(); ++i)
{
const auto map = maps.getChild (i);
if (! map.hasType (types::ControllerMap))
continue;

const auto controllerId = map.getProperty (tags::controller).toString();
const auto controlId = map.getProperty (tags::control).toString();

auto controller = controllers.getChildWithProperty (tags::uuid, controllerId);
if (! controller.isValid())
continue;
auto control = controller.getChildWithProperty (tags::uuid, controlId);
if (! control.isValid())
continue;

MidiMapping mapping (String {});
mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString());
mapping.setProperty (tags::name, control.getProperty (tags::name).toString());
mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString());
mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0));
mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0));
mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false));
mapping.setProperty (tags::targetType, "parameter");
mapping.setProperty (tags::node, map.getProperty (tags::node).toString());
mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1));

mappings.addChild (mapping.data(), -1, nullptr);
}

session.removeChild (controllers, nullptr);
session.removeChild (maps, nullptr);
}

static Node findNodeRecursive (const Node& start, const Uuid& uuid)
{
if (! uuid.isNull() && uuid == start.getUuid())
Expand All @@ -28,6 +73,8 @@ static Node findNodeRecursive (const Node& start, const Uuid& uuid)
return Node();
}

} // namespace detail

class Session::Impl
{
public:
Expand Down Expand Up @@ -98,7 +145,7 @@ bool Session::loadData (const ValueTree& data)
objectData.removeListener (this);
objectData = data;
setMissingProperties();
migrateControllerMaps (objectData);
detail::migrateControllerMaps (objectData);
cleanOrphanMidiMappings();
objectData.addListener (this);
return true;
Expand Down Expand Up @@ -183,7 +230,7 @@ Node Session::findNodeById (const Uuid& uuid)

for (int i = getNumGraphs(); --i >= 0;)
{
node = findNodeRecursive (getGraph (i), uuid);
node = detail::findNodeRecursive (getGraph (i), uuid);
if (node.isValid())
break;
}
Expand Down Expand Up @@ -260,46 +307,6 @@ void Session::forEach (const ValueTree tree, ValueTreeFunction handler) const
forEach (tree.getChild (i), handler);
}

void migrateControllerMaps (ValueTree session)
{
if (! session.isValid())
return;

auto controllers = session.getChildWithName (tags::controllers);
auto maps = session.getChildWithName (tags::maps);
auto mappings = session.getOrCreateChildWithName (tags::midiMappings, nullptr);

for (int i = 0; i < maps.getNumChildren(); ++i)
{
const auto map = maps.getChild (i);
if (! map.hasType (types::ControllerMap))
continue;

const auto controllerId = map.getProperty (tags::controller).toString();
const auto controlId = map.getProperty (tags::control).toString();

auto controller = controllers.getChildWithProperty (tags::uuid, controllerId);
if (! controller.isValid())
continue;
auto control = controller.getChildWithProperty (tags::uuid, controlId);
if (! control.isValid())
continue;

MidiMapping mapping (String {});
mapping.setProperty (tags::device, controller.getProperty (tags::inputDevice).toString());
mapping.setProperty (tags::name, control.getProperty (tags::name).toString());
mapping.setProperty (tags::eventType, control.getProperty ("eventType").toString());
mapping.setProperty (tags::eventId, (int) control.getProperty ("eventId", 0));
mapping.setProperty (tags::midiChannel, (int) control.getProperty (tags::midiChannel, 0));
mapping.setProperty (tags::toggle, ! (bool) control.getProperty ("momentary", false));
mapping.setProperty (tags::targetType, "parameter");
mapping.setProperty (tags::node, map.getProperty (tags::node).toString());
mapping.setProperty (tags::parameter, (int) map.getProperty (tags::parameter, -1));

mappings.addChild (mapping.data(), -1, nullptr);
}
}

void Session::setActiveGraph (int index)
{
if (! isPositiveAndBelow (index, getNumGraphs()))
Expand Down
9 changes: 8 additions & 1 deletion test/MidiMappingSessionTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
using namespace element;
using namespace juce;

namespace element::detail {
extern void migrateControllerMaps (ValueTree session);
}

BOOST_AUTO_TEST_SUITE (MidiMappingSessionTests)

BOOST_AUTO_TEST_CASE (AddRemoveFind)
Expand Down Expand Up @@ -79,8 +83,11 @@ BOOST_AUTO_TEST_CASE (MigrateLegacyControllerMaps)
map.setProperty (tags::parameter, 5, nullptr);
maps.addChild (map, -1, nullptr);

migrateControllerMaps (session);
element::detail::migrateControllerMaps (session);

BOOST_REQUIRE(! session.getChildWithName(tags::controllers).isValid());
BOOST_REQUIRE(! session.getChildWithName(tags::maps).isValid());

auto mappings = session.getChildWithName (tags::midiMappings);
BOOST_REQUIRE (mappings.isValid());
BOOST_REQUIRE_EQUAL (mappings.getNumChildren(), 1);
Expand Down
Loading