gearmulator

Emulation of classic VA synths of the late 90s/2000s that are based on Motorola 56300 family DSPs
Log | Files | Refs | Submodules | README | LICENSE

patchmodifications.cpp (2448B)


      1 #include "patchmodifications.h"
      2 
      3 #include "patch.h"
      4 
      5 #include "juce_core/juce_core.h"
      6 
      7 #include "baseLib/binarystream.h"
      8 
      9 namespace pluginLib::patchDB
     10 {
     11 	bool PatchModifications::modifyTags(const TypedTags& _tags)
     12 	{
     13 		bool res = false;
     14 
     15 		for (const auto& it : _tags.get())
     16 		{
     17 			const auto type = it.first;
     18 			const auto& t = it.second;
     19 
     20 			const auto p = patch.lock();
     21 
     22 			for (const auto& tag : t.getAdded())
     23 			{
     24 				if (!p->tags.containsAdded(type, tag))
     25 					res |= tags.add(type, tag);
     26 				else if(tags.containsRemoved(type, tag))
     27 					res |= tags.erase(type, tag);
     28 			}
     29 
     30 			for (const auto& tag : t.getRemoved())
     31 			{
     32 				if (p->tags.containsAdded(type, tag))
     33 					res |= tags.addRemoved(type, tag);
     34 				else if (tags.containsAdded(type, tag))
     35 					res |= tags.erase(type, tag);
     36 			}
     37 		}
     38 
     39 		if (!res)
     40 			return false;
     41 
     42 		updateCache();
     43 		return true;
     44 	}
     45 
     46 	void PatchModifications::updateCache()
     47 	{
     48 		const auto p = patch.lock();
     49 		if (!p)
     50 		{
     51 			mergedTags = tags;
     52 			return;
     53 		}
     54 
     55 		mergedTags = p->tags;
     56 
     57 		for (const auto& it : tags.get())
     58 		{
     59 			const auto& type = it.first;
     60 			const auto & t = it.second;
     61 
     62 			for (const auto& tag: t.getAdded())
     63 				mergedTags.add(type, tag);
     64 
     65 			for (const auto& tag : t.getRemoved())
     66 				mergedTags.addRemoved(type, tag);
     67 		}
     68 	}
     69 
     70 	juce::DynamicObject* PatchModifications::serialize() const
     71 	{
     72 		auto* o = new juce::DynamicObject();
     73 
     74 		auto* doTags = tags.serialize();
     75 
     76 		o->setProperty("tags", doTags);
     77 
     78 		if(!name.empty())
     79 		{
     80 			const auto p = patch.lock();
     81 			if (!p || name != p->name)
     82 				o->setProperty("name", juce::String(name));
     83 		}
     84 
     85 		return o;
     86 	}
     87 
     88 	bool PatchModifications::deserialize(const juce::var& _var)
     89 	{
     90 		name.clear();
     91 		tags.clear();
     92 
     93 		const auto n = _var["name"].toString();
     94 		if (!n.isEmpty())
     95 			name = n.toStdString();
     96 
     97 		auto* t = _var["tags"].getDynamicObject();
     98 
     99 		if(t)
    100 		{
    101 			tags.deserialize(t);
    102 		}
    103 
    104 		return true;
    105 	}
    106 
    107 	bool PatchModifications::empty() const
    108 	{
    109 		return name.empty() && tags.empty();
    110 	}
    111 
    112 	void PatchModifications::write(baseLib::BinaryStream& _outStream) const
    113 	{
    114 		baseLib::ChunkWriter cw(_outStream, chunks::g_patchModification, 1);
    115 		_outStream.write(name);
    116 		tags.write(_outStream);
    117 	}
    118 
    119 	bool PatchModifications::read(baseLib::BinaryStream& _binaryStream)
    120 	{
    121 		auto in = _binaryStream.tryReadChunk(chunks::g_patchModification, 1);
    122 		if(!in)
    123 			return false;
    124 
    125 		name = in.readString();
    126 
    127 		if(!tags.read(in))
    128 			return false;
    129 
    130 		return true;
    131 	}
    132 }