/* * Carla State utils * Copyright (C) 2012-2018 Filipe Coelho * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or any later version. * * This program 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. * * For a full copy of the GNU General Public License see the doc/GPL.txt file. */ #include "CarlaStateUtils.hpp" #include "CarlaBackendUtils.hpp" #include "CarlaMathUtils.hpp" #include "CarlaMIDI.h" #include "water/streams/MemoryOutputStream.h" #include "water/xml/XmlElement.h" #include using water::MemoryOutputStream; using water::String; using water::XmlElement; CARLA_BACKEND_START_NAMESPACE // ----------------------------------------------------------------------- // getNewLineSplittedString static void getNewLineSplittedString(MemoryOutputStream& stream, const String& string) { static const int kLineWidth = 120; int i = 0; const int length = string.length(); const char* const raw = string.toUTF8(); stream.preallocate(static_cast(length + length/kLineWidth + 3)); for (; i+kLineWidth < length; i += kLineWidth) { stream.write(raw+i, kLineWidth); stream.writeByte('\n'); } stream << (raw+i); } // ----------------------------------------------------------------------- // xmlSafeStringFast /* Based on some code by James Kanze from stackoverflow * https://stackoverflow.com/questions/7724011/in-c-whats-the-fastest-way-to-replace-all-occurrences-of-a-substring-within */ static std::string replaceStdString(const std::string& original, const std::string& before, const std::string& after) { std::string::const_iterator current = original.begin(), end = original.end(), next; std::string retval; for (; (next = std::search(current, end, before.begin(), before.end())) != end;) { retval.append(current, next); retval.append(after); current = next + static_cast(before.size()); } retval.append(current, next); return retval; } static std::string xmlSafeStringFast(const char* const cstring, const bool toXml) { std::string string(cstring); if (toXml) { string = replaceStdString(string, "&","&"); string = replaceStdString(string, "<","<"); string = replaceStdString(string, ">",">"); string = replaceStdString(string, "'","'"); string = replaceStdString(string, "\"","""); } else { string = replaceStdString(string, "<","<"); string = replaceStdString(string, ">",">"); string = replaceStdString(string, "'","'"); string = replaceStdString(string, ""","\""); string = replaceStdString(string, "&","&"); } return string; } // ----------------------------------------------------------------------- // xmlSafeStringCharDup /* static const char* xmlSafeStringCharDup(const char* const cstring, const bool toXml) { return carla_strdup(xmlSafeString(cstring, toXml).toRawUTF8()); } */ static const char* xmlSafeStringCharDup(const String& string, const bool toXml) { return carla_strdup(xmlSafeString(string, toXml).toRawUTF8()); } // ----------------------------------------------------------------------- // StateParameter CarlaStateSave::Parameter::Parameter() noexcept : dummy(true), index(-1), name(nullptr), symbol(nullptr), #ifndef BUILD_BRIDGE value(0.0f), midiChannel(0), midiCC(-1) {} #else value(0.0f) {} #endif CarlaStateSave::Parameter::~Parameter() noexcept { if (name != nullptr) { delete[] name; name = nullptr; } if (symbol != nullptr) { delete[] symbol; symbol = nullptr; } } // ----------------------------------------------------------------------- // StateCustomData CarlaStateSave::CustomData::CustomData() noexcept : type(nullptr), key(nullptr), value(nullptr) {} CarlaStateSave::CustomData::~CustomData() noexcept { if (type != nullptr) { delete[] type; type = nullptr; } if (key != nullptr) { delete[] key; key = nullptr; } if (value != nullptr) { delete[] value; value = nullptr; } } bool CarlaStateSave::CustomData::isValid() const noexcept { if (type == nullptr || type[0] == '\0') return false; if (key == nullptr || key [0] == '\0') return false; if (value == nullptr) return false; return true; } // ----------------------------------------------------------------------- // StateSave CarlaStateSave::CarlaStateSave() noexcept : type(nullptr), name(nullptr), label(nullptr), binary(nullptr), uniqueId(0), options(0x0), #ifndef BUILD_BRIDGE active(false), dryWet(1.0f), volume(1.0f), balanceLeft(-1.0f), balanceRight(1.0f), panning(0.0f), ctrlChannel(-1), #endif currentProgramIndex(-1), currentProgramName(nullptr), currentMidiBank(-1), currentMidiProgram(-1), chunk(nullptr), parameters(), customData() {} CarlaStateSave::~CarlaStateSave() noexcept { clear(); } void CarlaStateSave::clear() noexcept { if (type != nullptr) { delete[] type; type = nullptr; } if (name != nullptr) { delete[] name; name = nullptr; } if (label != nullptr) { delete[] label; label = nullptr; } if (binary != nullptr) { delete[] binary; binary = nullptr; } if (currentProgramName != nullptr) { delete[] currentProgramName; currentProgramName = nullptr; } if (chunk != nullptr) { delete[] chunk; chunk = nullptr; } uniqueId = 0; options = 0x0; #ifndef BUILD_BRIDGE active = false; dryWet = 1.0f; volume = 1.0f; balanceLeft = -1.0f; balanceRight = 1.0f; panning = 0.0f; ctrlChannel = -1; #endif currentProgramIndex = -1; currentMidiBank = -1; currentMidiProgram = -1; for (ParameterItenerator it = parameters.begin2(); it.valid(); it.next()) { Parameter* const stateParameter(it.getValue(nullptr)); delete stateParameter; } for (CustomDataItenerator it = customData.begin2(); it.valid(); it.next()) { CustomData* const stateCustomData(it.getValue(nullptr)); delete stateCustomData; } parameters.clear(); customData.clear(); } // ----------------------------------------------------------------------- // fillFromXmlElement bool CarlaStateSave::fillFromXmlElement(const XmlElement* const xmlElement) { CARLA_SAFE_ASSERT_RETURN(xmlElement != nullptr, false); clear(); for (XmlElement* elem = xmlElement->getFirstChildElement(); elem != nullptr; elem = elem->getNextElement()) { const String& tagName(elem->getTagName()); // --------------------------------------------------------------- // Info if (tagName == "Info") { for (XmlElement* xmlInfo = elem->getFirstChildElement(); xmlInfo != nullptr; xmlInfo = xmlInfo->getNextElement()) { const String& tag(xmlInfo->getTagName()); const String text(xmlInfo->getAllSubText().trim()); /**/ if (tag == "Type") type = xmlSafeStringCharDup(text, false); else if (tag == "Name") name = xmlSafeStringCharDup(text, false); else if (tag == "Label" || tag == "URI" || tag == "Setup") label = xmlSafeStringCharDup(text, false); else if (tag == "Binary" || tag == "Filename") binary = xmlSafeStringCharDup(text, false); else if (tag == "UniqueID") uniqueId = text.getLargeIntValue(); } } // --------------------------------------------------------------- // Data else if (tagName == "Data") { for (XmlElement* xmlData = elem->getFirstChildElement(); xmlData != nullptr; xmlData = xmlData->getNextElement()) { const String& tag(xmlData->getTagName()); const String text(xmlData->getAllSubText().trim()); #ifndef BUILD_BRIDGE // ------------------------------------------------------- // Internal Data /**/ if (tag == "Active") { active = (text == "Yes"); } else if (tag == "DryWet") { dryWet = carla_fixedValue(0.0f, 1.0f, text.getFloatValue()); } else if (tag == "Volume") { volume = carla_fixedValue(0.0f, 1.27f, text.getFloatValue()); } else if (tag == "Balance-Left") { balanceLeft = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue()); } else if (tag == "Balance-Right") { balanceRight = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue()); } else if (tag == "Panning") { panning = carla_fixedValue(-1.0f, 1.0f, text.getFloatValue()); } else if (tag == "ControlChannel") { if (! text.startsWithIgnoreCase("n")) { const int value(text.getIntValue()); if (value >= 1 && value <= MAX_MIDI_CHANNELS) ctrlChannel = static_cast(value-1); } } else if (tag == "Options") { const int value(text.getHexValue32()); if (value > 0) options = static_cast(value); } #else if (false) {} #endif // ------------------------------------------------------- // Program (current) else if (tag == "CurrentProgramIndex") { const int value(text.getIntValue()); if (value >= 1) currentProgramIndex = value-1; } else if (tag == "CurrentProgramName") { currentProgramName = xmlSafeStringCharDup(text, false); } // ------------------------------------------------------- // Midi Program (current) else if (tag == "CurrentMidiBank") { const int value(text.getIntValue()); if (value >= 1) currentMidiBank = value-1; } else if (tag == "CurrentMidiProgram") { const int value(text.getIntValue()); if (value >= 1) currentMidiProgram = value-1; } // ------------------------------------------------------- // Parameters else if (tag == "Parameter") { Parameter* const stateParameter(new Parameter()); for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement()) { const String& pTag(xmlSubData->getTagName()); const String pText(xmlSubData->getAllSubText().trim()); /**/ if (pTag == "Index") { const int index(pText.getIntValue()); if (index >= 0) stateParameter->index = index; } else if (pTag == "Name") { stateParameter->name = xmlSafeStringCharDup(pText, false); } else if (pTag == "Symbol") { stateParameter->symbol = xmlSafeStringCharDup(pText, false); } else if (pTag == "Value") { stateParameter->dummy = false; stateParameter->value = pText.getFloatValue(); } #ifndef BUILD_BRIDGE else if (pTag == "MidiChannel") { const int channel(pText.getIntValue()); if (channel >= 1 && channel <= MAX_MIDI_CHANNELS) stateParameter->midiChannel = static_cast(channel-1); } else if (pTag == "MidiCC") { const int cc(pText.getIntValue()); if (cc >= -1 && cc < MAX_MIDI_CONTROL) stateParameter->midiCC = static_cast(cc); } #endif } parameters.append(stateParameter); } // ------------------------------------------------------- // Custom Data else if (tag == "CustomData") { CustomData* const stateCustomData(new CustomData()); for (XmlElement* xmlSubData = xmlData->getFirstChildElement(); xmlSubData != nullptr; xmlSubData = xmlSubData->getNextElement()) { const String& cTag(xmlSubData->getTagName()); const String cText(xmlSubData->getAllSubText().trim()); /**/ if (cTag == "Type") stateCustomData->type = xmlSafeStringCharDup(cText, false); else if (cTag == "Key") stateCustomData->key = xmlSafeStringCharDup(cText, false); else if (cTag == "Value") stateCustomData->value = carla_strdup(cText.toRawUTF8()); //xmlSafeStringCharDup(cText, false); } if (stateCustomData->isValid()) customData.append(stateCustomData); else carla_stderr("Reading CustomData property failed, missing data"); } // ------------------------------------------------------- // Chunk else if (tag == "Chunk") { chunk = carla_strdup(text.toRawUTF8()); } } } } return true; } // ----------------------------------------------------------------------- // fillXmlStringFromStateSave void CarlaStateSave::dumpToMemoryStream(MemoryOutputStream& content) const { { MemoryOutputStream infoXml; infoXml << " \n"; infoXml << " " << String(type != nullptr ? type : "") << "\n"; infoXml << " " << xmlSafeString(name, true) << "\n"; switch (getPluginTypeFromString(type)) { case PLUGIN_NONE: break; case PLUGIN_INTERNAL: infoXml << " \n"; break; case PLUGIN_LADSPA: infoXml << " " << xmlSafeString(binary, true) << "\n"; infoXml << " \n"; infoXml << " " << water::int64(uniqueId) << "\n"; break; case PLUGIN_DSSI: infoXml << " " << xmlSafeString(binary, true) << "\n"; infoXml << " \n"; break; case PLUGIN_LV2: infoXml << " " << xmlSafeString(label, true) << "\n"; break; case PLUGIN_VST2: infoXml << " " << xmlSafeString(binary, true) << "\n"; infoXml << " " << water::int64(uniqueId) << "\n"; break; case PLUGIN_SF2: infoXml << " " << xmlSafeString(binary, true) << "\n"; infoXml << " \n"; break; case PLUGIN_SFZ: infoXml << " " << xmlSafeString(binary, true) << "\n"; break; case PLUGIN_JACK: infoXml << " " << xmlSafeString(binary, true) << "\n"; infoXml << " " << xmlSafeString(label, true) << "\n"; break; } infoXml << " \n\n"; content << infoXml; } content << " \n"; #ifndef BUILD_BRIDGE { MemoryOutputStream dataXml; dataXml << " " << (active ? "Yes" : "No") << "\n"; if (carla_isNotEqual(dryWet, 1.0f)) dataXml << " " << String(dryWet, 7) << "\n"; if (carla_isNotEqual(volume, 1.0f)) dataXml << " " << String(volume, 7) << "\n"; if (carla_isNotEqual(balanceLeft, -1.0f)) dataXml << " " << String(balanceLeft, 7) << "\n"; if (carla_isNotEqual(balanceRight, 1.0f)) dataXml << " " << String(balanceRight, 7) << "\n"; if (carla_isNotEqual(panning, 0.0f)) dataXml << " " << String(panning, 7) << "\n"; if (ctrlChannel < 0) dataXml << " N\n"; else dataXml << " " << int(ctrlChannel+1) << "\n"; dataXml << " 0x" << String::toHexString(static_cast(options)) << "\n"; content << dataXml; } #endif for (ParameterItenerator it = parameters.begin2(); it.valid(); it.next()) { Parameter* const stateParameter(it.getValue(nullptr)); CARLA_SAFE_ASSERT_CONTINUE(stateParameter != nullptr); MemoryOutputStream parameterXml; parameterXml << "\n"; parameterXml << " \n"; parameterXml << " " << String(stateParameter->index) << "\n"; parameterXml << " " << xmlSafeString(stateParameter->name, true) << "\n"; if (stateParameter->symbol != nullptr && stateParameter->symbol[0] != '\0') parameterXml << " " << xmlSafeString(stateParameter->symbol, true) << "\n"; #ifndef BUILD_BRIDGE if (stateParameter->midiCC > 0) { parameterXml << " " << stateParameter->midiCC << "\n"; parameterXml << " " << stateParameter->midiChannel+1 << "\n"; } #endif if (! stateParameter->dummy) parameterXml << " " << String(stateParameter->value, 15) << "\n"; parameterXml << " \n"; content << parameterXml; } if (currentProgramIndex >= 0 && currentProgramName != nullptr && currentProgramName[0] != '\0') { // ignore 'default' program if (currentProgramIndex > 0 || ! String(currentProgramName).equalsIgnoreCase("default")) { MemoryOutputStream programXml; programXml << "\n"; programXml << " " << currentProgramIndex+1 << "\n"; programXml << " " << xmlSafeString(currentProgramName, true) << "\n"; content << programXml; } } if (currentMidiBank >= 0 && currentMidiProgram >= 0) { MemoryOutputStream midiProgramXml; midiProgramXml << "\n"; midiProgramXml << " " << currentMidiBank+1 << "\n"; midiProgramXml << " " << currentMidiProgram+1 << "\n"; content << midiProgramXml; } for (CustomDataItenerator it = customData.begin2(); it.valid(); it.next()) { CustomData* const stateCustomData(it.getValue(nullptr)); CARLA_SAFE_ASSERT_CONTINUE(stateCustomData != nullptr); CARLA_SAFE_ASSERT_CONTINUE(stateCustomData->isValid()); MemoryOutputStream customDataXml; customDataXml << "\n"; customDataXml << " \n"; customDataXml << " " << xmlSafeString(stateCustomData->type, true) << "\n"; customDataXml << " " << xmlSafeString(stateCustomData->key, true) << "\n"; if (std::strcmp(stateCustomData->type, CUSTOM_DATA_TYPE_CHUNK) == 0 || std::strlen(stateCustomData->value) >= 128) { customDataXml << " \n"; customDataXml << xmlSafeStringFast(stateCustomData->value, true); customDataXml << "\n \n"; } else { customDataXml << " "; customDataXml << xmlSafeStringFast(stateCustomData->value, true); customDataXml << "\n"; } customDataXml << " \n"; content << customDataXml; } if (chunk != nullptr && chunk[0] != '\0') { MemoryOutputStream chunkXml, chunkSplt; getNewLineSplittedString(chunkSplt, chunk); chunkXml << "\n \n"; chunkXml << chunkSplt; chunkXml << "\n \n"; content << chunkXml; } content << " \n"; } // ----------------------------------------------------------------------- CARLA_BACKEND_END_NAMESPACE