|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590 |
- /*
- * Carla Native Plugins
- * Copyright (C) 2013-2014 Filipe Coelho <falktx@falktx.com>
- *
- * 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.
- */
-
- #define CARLA_NATIVE_PLUGIN_LV2
- #include "carla-base.cpp"
-
- #include "juce_core.h"
-
- #include "lv2/atom.h"
- #include "lv2/buf-size.h"
- #include "lv2/instance-access.h"
- #include "lv2/midi.h"
- #include "lv2/options.h"
- #include "lv2/port-props.h"
- #include "lv2/state.h"
- #include "lv2/time.h"
- #include "lv2/ui.h"
- #include "lv2/units.h"
- #include "lv2/urid.h"
- #include "lv2/lv2_external_ui.h"
- #include "lv2/lv2_programs.h"
-
- #include <fstream>
-
- #if defined(CARLA_OS_WIN)
- # define PLUGIN_EXT ".dll"
- #elif defined(JUCE_MAC)
- # define PLUGIN_EXT ".dylib"
- #else
- # define PLUGIN_EXT ".so"
- #endif
-
- using juce::String;
- using juce::StringArray;
- using juce::juce_wchar;
-
- // -----------------------------------------------------------------------
- // Converts a parameter name to an LV2 compatible symbol
-
- static StringArray gUsedSymbols;
-
- static const String nameToSymbol(const String& name, const uint32_t portIndex)
- {
- String symbol, trimmedName = name.trim().toLowerCase();
-
- if (trimmedName.isEmpty())
- {
- symbol += "lv2_port_";
- symbol += String(portIndex+1);
- }
- else
- {
- for (int i=0; i < trimmedName.length(); ++i)
- {
- const juce_wchar c = trimmedName[i];
-
- if (i == 0 && std::isdigit(c))
- symbol += "_";
- else if (std::isalpha(c) || std::isdigit(c))
- symbol += c;
- else
- symbol += "_";
- }
- }
-
- // Do not allow identical symbols
- if (gUsedSymbols.contains(symbol))
- {
- int offset = 2;
- String offsetStr = "_2";
- symbol += offsetStr;
-
- while (gUsedSymbols.contains(symbol))
- {
- offset += 1;
- String newOffsetStr = "_" + String(offset);
- symbol = symbol.replace(offsetStr, newOffsetStr);
- offsetStr = newOffsetStr;
- }
- }
-
- gUsedSymbols.add(symbol);
-
- return symbol;
- }
-
- // -----------------------------------------------------------------------
-
- static void writeManifestFile(PluginListManager& plm)
- {
- String text;
-
- // -------------------------------------------------------------------
- // Header
-
- text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
- text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
- text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
- text += "\n";
-
- // -------------------------------------------------------------------
- // Plugins
-
- for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
- {
- const NativePluginDescriptor* const& pluginDesc(it.getValue());
- const String label(pluginDesc->label);
-
- text += "<http://kxstudio.sf.net/carla/plugins/" + label + ">\n";
- text += " a lv2:Plugin ;\n";
- text += " lv2:binary <carla" PLUGIN_EXT "> ;\n";
- text += " rdfs:seeAlso <" + label + ".ttl> .\n";
- text += "\n";
- }
-
- // -------------------------------------------------------------------
- // UI
-
- #ifdef CARLA_OS_LINUX
- text += "<http://kxstudio.sf.net/carla/ui-embed>\n";
- text += " a <" LV2_UI__X11UI "> ;\n";
- text += " ui:binary <carla" PLUGIN_EXT "> ;\n";
- text += " lv2:extensionData <" LV2_PROGRAMS__UIInterface "> ;\n";
- text += " lv2:optionalFeature <" LV2_UI__fixedSize "> ,\n";
- text += " <" LV2_UI__noUserResize "> ;\n";
- text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> ,\n";
- text += " <" LV2_UI__resize "> .\n";
- text += "\n";
- #endif
-
- text += "<http://kxstudio.sf.net/carla/ui-ext>\n";
- text += " a <" LV2_EXTERNAL_UI__Widget "> ;\n";
- text += " ui:binary <carla" PLUGIN_EXT "> ;\n";
- text += " lv2:extensionData <" LV2_UI__idleInterface "> ,\n";
- text += " <" LV2_UI__showInterface "> ,\n";
- text += " <" LV2_PROGRAMS__UIInterface "> ;\n";
- text += " lv2:requiredFeature <" LV2_INSTANCE_ACCESS_URI "> .\n";
-
- // -------------------------------------------------------------------
- // Write file now
-
- std::fstream manifest("carla.lv2/manifest.ttl", std::ios::out);
- manifest << text.toRawUTF8();
- manifest.close();
- }
-
- // -----------------------------------------------------------------------
-
- static uint32_t host_getBufferSize(NativeHostHandle) { return 512; }
- static double host_getSampleRate(NativeHostHandle) { return 44100.0; }
- static bool host_isOffline(NativeHostHandle) { return true; }
- static intptr_t host_dispatcher(NativeHostHandle, NativeHostDispatcherOpcode, int32_t, intptr_t, void*, float) { return 0; }
-
- static void writePluginFile(const NativePluginDescriptor* const pluginDesc)
- {
- const String pluginLabel(pluginDesc->label);
- const String pluginFile("carla.lv2/" + pluginLabel + ".ttl");
-
- uint32_t portIndex = 0;
- String text;
-
- gUsedSymbols.clear();
-
- carla_stdout("Generating data for %s...", pluginDesc->name);
-
- // -------------------------------------------------------------------
- // Init plugin
-
- NativeHostDescriptor hostDesc;
- hostDesc.handle = nullptr;
- hostDesc.resourceDir = "";
- hostDesc.uiName = "";
- hostDesc.get_buffer_size = host_getBufferSize;
- hostDesc.get_sample_rate = host_getSampleRate;
- hostDesc.is_offline = host_isOffline;
- hostDesc.get_time_info = nullptr;
- hostDesc.write_midi_event = nullptr;
- hostDesc.ui_parameter_changed = nullptr;
- hostDesc.ui_midi_program_changed = nullptr;
- hostDesc.ui_custom_data_changed = nullptr;
- hostDesc.ui_closed = nullptr;
- hostDesc.ui_open_file = nullptr;
- hostDesc.ui_save_file = nullptr;
- hostDesc.dispatcher = host_dispatcher;
-
- NativePluginHandle pluginHandle = nullptr;
-
- if (! pluginLabel.startsWithIgnoreCase("carla"))
- {
- pluginHandle = pluginDesc->instantiate(&hostDesc);
- CARLA_SAFE_ASSERT_RETURN(pluginHandle != nullptr,)
- }
-
- // -------------------------------------------------------------------
- // Header
-
- text += "@prefix atom: <" LV2_ATOM_PREFIX "> .\n";
- text += "@prefix doap: <http://usefulinc.com/ns/doap#> .\n";
- text += "@prefix foaf: <http://xmlns.com/foaf/0.1/> .\n";
- text += "@prefix lv2: <" LV2_CORE_PREFIX "> .\n";
- text += "@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .\n";
- text += "@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .\n";
- text += "@prefix ui: <" LV2_UI_PREFIX "> .\n";
- text += "@prefix unit: <" LV2_UNITS_PREFIX "> .\n";
- text += "\n";
-
- // -------------------------------------------------------------------
- // Plugin URI
-
- text += "<http://kxstudio.sf.net/carla/plugins/" + pluginLabel + ">\n";
-
- // -------------------------------------------------------------------
- // Category
-
- switch (pluginDesc->category)
- {
- case NATIVE_PLUGIN_CATEGORY_SYNTH:
- text += " a lv2:InstrumentPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_DELAY:
- text += " a lv2:DelayPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_EQ:
- text += " a lv2:EQPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_FILTER:
- text += " a lv2:FilterPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_DYNAMICS:
- text += " a lv2:DynamicsPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_MODULATOR:
- text += " a lv2:ModulatorPlugin, lv2:Plugin ;\n";
- break;
- case NATIVE_PLUGIN_CATEGORY_UTILITY:
- text += " a lv2:UtilityPlugin, lv2:Plugin ;\n";
- break;
- default:
- text += " a lv2:Plugin ;\n";
- break;
- }
-
- text += "\n";
-
- // -------------------------------------------------------------------
- // Features
-
- // optional
- if (pluginDesc->hints & NATIVE_PLUGIN_IS_RTSAFE)
- text += " lv2:optionalFeature <" LV2_CORE__hardRTCapable "> ;\n\n";
-
- // required
- text += " lv2:requiredFeature <" LV2_BUF_SIZE__boundedBlockLength "> ,\n";
-
- if (pluginDesc->hints & NATIVE_PLUGIN_NEEDS_FIXED_BUFFERS)
- text += " <" LV2_BUF_SIZE__fixedBlockLength "> ,\n";
-
- text += " <" LV2_OPTIONS__options "> ,\n";
- text += " <" LV2_URID__map "> ;\n";
- text += "\n";
-
- // -------------------------------------------------------------------
- // Extensions
-
- text += " lv2:extensionData <" LV2_OPTIONS__interface "> ;\n";
-
- if (pluginDesc->hints & NATIVE_PLUGIN_USES_STATE)
- text += " lv2:extensionData <" LV2_STATE__interface "> ;\n";
-
- if (pluginDesc->category != NATIVE_PLUGIN_CATEGORY_SYNTH)
- text += " lv2:extensionData <" LV2_PROGRAMS__Interface "> ;\n";
-
- text += "\n";
-
- // -------------------------------------------------------------------
- // UIs
-
- if (pluginDesc->hints & NATIVE_PLUGIN_HAS_UI)
- {
- #ifdef CARLA_OS_LINUX
- if (std::strncmp(pluginDesc->label, "carla", 5) == 0)
- {
- text += " ui:ui <http://kxstudio.sf.net/carla/ui-embed> ,\n";
- text += " <http://kxstudio.sf.net/carla/ui-ext> ;\n";
- }
- else
- #endif
- {
- text += " ui:ui <http://kxstudio.sf.net/carla/ui-ext> ;\n";
- }
-
- text += "\n";
- }
-
- // -------------------------------------------------------------------
- // First MIDI/Time port
-
- if (pluginDesc->midiIns > 0 || (pluginDesc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
- {
- text += " lv2:port [\n";
- text += " a lv2:InputPort, atom:AtomPort ;\n";
- text += " atom:bufferType atom:Sequence ;\n";
-
- if (pluginDesc->midiIns > 0 && (pluginDesc->hints & NATIVE_PLUGIN_USES_TIME) != 0)
- {
- text += " atom:supports <" LV2_MIDI__MidiEvent "> ,\n";
- text += " <" LV2_TIME__Position "> ;\n";
- }
- else if (pluginDesc->midiIns > 0)
- text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
- else
- text += " atom:supports <" LV2_TIME__Position "> ;\n";
-
- text += " lv2:designation lv2:control ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
-
- if (pluginDesc->hints & NATIVE_PLUGIN_USES_TIME)
- {
- if (pluginDesc->midiIns > 1)
- {
- text += " lv2:symbol \"lv2_events_in_1\" ;\n";
- text += " lv2:name \"Events Input #1\" ;\n";
- }
- else
- {
- text += " lv2:symbol \"lv2_events_in\" ;\n";
- text += " lv2:name \"Events Input\" ;\n";
- }
- }
- else
- {
- if (pluginDesc->midiIns > 1)
- {
- text += " lv2:symbol \"lv2_midi_in_1\" ;\n";
- text += " lv2:name \"MIDI Input #1\" ;\n";
- }
- else
- {
- text += " lv2:symbol \"lv2_midi_in\" ;\n";
- text += " lv2:name \"MIDI Input\" ;\n";
- }
- }
-
- text += " ] ;\n\n";
- }
-
- // -------------------------------------------------------------------
- // MIDI inputs
-
- for (uint32_t i=1; i < pluginDesc->midiIns; ++i)
- {
- if (i == 1)
- text += " lv2:port [\n";
-
- text += " a lv2:InputPort, atom:AtomPort ;\n";
- text += " atom:bufferType atom:Sequence ;\n";
- text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
- text += " lv2:symbol \"lv2_midi_in_" + String(i+1) + "\" ;\n";
- text += " lv2:name \"MIDI Input #" + String(i+1) + "\" ;\n";
-
- if (i+1 == pluginDesc->midiIns)
- text += " ] ;\n\n";
- else
- text += " ] , [\n";
- }
-
- // -------------------------------------------------------------------
- // MIDI outputs
-
- for (uint32_t i=0; i < pluginDesc->midiOuts; ++i)
- {
- if (i == 0)
- text += " lv2:port [\n";
-
- text += " a lv2:OutputPort, atom:AtomPort ;\n";
- text += " atom:bufferType atom:Sequence ;\n";
- text += " atom:supports <" LV2_MIDI__MidiEvent "> ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
-
- if (pluginDesc->midiOuts > 1)
- {
- text += " lv2:symbol \"lv2_midi_out_" + String(i+1) + "\" ;\n";
- text += " lv2:name \"MIDI Output #" + String(i+1) + "\" ;\n";
- }
- else
- {
- text += " lv2:symbol \"lv2_midi_out\" ;\n";
- text += " lv2:name \"MIDI Output\" ;\n";
- }
-
- if (i+1 == pluginDesc->midiOuts)
- text += " ] ;\n\n";
- else
- text += " ] , [\n";
- }
-
- // -------------------------------------------------------------------
- // Freewheel port
-
- text += " lv2:port [\n";
- text += " a lv2:InputPort, lv2:ControlPort ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
- text += " lv2:symbol \"lv2_freewheel\" ;\n";
- text += " lv2:name \"Freewheel\" ;\n";
- text += " lv2:default 0.0 ;\n";
- text += " lv2:minimum 0.0 ;\n";
- text += " lv2:maximum 1.0 ;\n";
- text += " lv2:designation <" LV2_CORE__freeWheeling "> ;\n";
- text += " lv2:portProperty lv2:toggled, <" LV2_PORT_PROPS__notOnGUI "> ;\n";
- text += " ] ;\n";
- text += "\n";
-
- // -------------------------------------------------------------------
- // Audio inputs
-
- for (uint32_t i=0; i < pluginDesc->audioIns; ++i)
- {
- if (i == 0)
- text += " lv2:port [\n";
-
- text += " a lv2:InputPort, lv2:AudioPort ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
- text += " lv2:symbol \"lv2_audio_in_" + String(i+1) + "\" ;\n";
- text += " lv2:name \"Audio Input " + String(i+1) + "\" ;\n";
-
- if (i+1 == pluginDesc->audioIns)
- text += " ] ;\n\n";
- else
- text += " ] , [\n";
- }
-
- // -------------------------------------------------------------------
- // Audio outputs
-
- for (uint32_t i=0; i < pluginDesc->audioOuts; ++i)
- {
- if (i == 0)
- text += " lv2:port [\n";
-
- text += " a lv2:OutputPort, lv2:AudioPort ;\n";
- text += " lv2:index " + String(portIndex++) + " ;\n";
- text += " lv2:symbol \"lv2_audio_out_" + String(i+1) + "\" ;\n";
- text += " lv2:name \"Audio Output " + String(i+1) + "\" ;\n";
-
- if (i+1 == pluginDesc->audioOuts)
- text += " ] ;\n\n";
- else
- text += " ] , [\n";
- }
-
- // -------------------------------------------------------------------
- // Parameters
-
- const uint32_t paramCount((pluginHandle != nullptr && pluginDesc->get_parameter_count != nullptr) ? pluginDesc->get_parameter_count(pluginHandle) : 0);
-
- if (paramCount > 0)
- {
- CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_info != nullptr,)
- CARLA_SAFE_ASSERT_RETURN(pluginDesc->get_parameter_value != nullptr,)
- }
-
- for (uint32_t i=0; i < paramCount; ++i)
- {
- const NativeParameter* paramInfo(pluginDesc->get_parameter_info(pluginHandle, i));
- const String paramName(paramInfo->name != nullptr ? paramInfo->name : "");
- const String paramUnit(paramInfo->unit != nullptr ? paramInfo->unit : "");
-
- CARLA_SAFE_ASSERT_RETURN(paramInfo != nullptr,)
-
- if (i == 0)
- text += " lv2:port [\n";
-
- if (paramInfo->hints & NATIVE_PARAMETER_IS_OUTPUT)
- text += " a lv2:OutputPort, lv2:ControlPort ;\n";
- else
- text += " a lv2:InputPort, lv2:ControlPort ;\n";
-
- text += " lv2:index " + String(portIndex++) + " ;\n";
- text += " lv2:symbol \"" + nameToSymbol(paramName, i) + "\" ;\n";
-
- if (paramName.isNotEmpty())
- text += " lv2:name \"" + paramName + "\" ;\n";
- else
- text += " lv2:name \"Port " + String(i+1) + "\" ;\n";
-
- text += " lv2:default " + String::formatted("%f", paramInfo->ranges.def) + " ;\n";
- text += " lv2:minimum " + String::formatted("%f", paramInfo->ranges.min) + " ;\n";
- text += " lv2:maximum " + String::formatted("%f", paramInfo->ranges.max) + " ;\n";
-
- if ((paramInfo->hints & NATIVE_PARAMETER_IS_AUTOMABLE) == 0)
- text += " lv2:portProperty <" LV2_PORT_PROPS__expensive "> ;\n";
- if (paramInfo->hints & NATIVE_PARAMETER_IS_BOOLEAN)
- text += " lv2:portProperty lv2:toggled ;\n";
- if (paramInfo->hints & NATIVE_PARAMETER_IS_INTEGER)
- text += " lv2:portProperty lv2:integer ;\n";
- if (paramInfo->hints & NATIVE_PARAMETER_IS_LOGARITHMIC)
- text += " lv2:portProperty <" LV2_PORT_PROPS__logarithmic "> ;\n";
- if (paramInfo->hints & NATIVE_PARAMETER_USES_SAMPLE_RATE)
- text += " lv2:portProperty lv2:toggled ;\n";
- if (paramInfo->hints & NATIVE_PARAMETER_USES_SCALEPOINTS)
- text += " lv2:portProperty lv2:enumeration ;\n";
- if ((paramInfo->hints & NATIVE_PARAMETER_IS_ENABLED) == 0)
- text += " lv2:portProperty <" LV2_PORT_PROPS__notOnGUI "> ;\n";
-
- for (uint32_t j=0; j < paramInfo->scalePointCount; ++j)
- {
- const NativeParameterScalePoint* const scalePoint(¶mInfo->scalePoints[j]);
-
- if (j == 0)
- text += " lv2:scalePoint [ ";
- else
- text += " [ ";
-
- text += "rdfs:label \"" + String(scalePoint->label) + "\" ;\n";
- text += " rdf:value " + String::formatted("%f", scalePoint->value) + " ";
-
- if (j+1 == paramInfo->scalePointCount)
- text += "] ;\n";
- else
- text += "] ,\n";
- }
-
- if (paramUnit.isNotEmpty())
- {
- text += " unit:unit [\n";
- text += " a unit:Unit ;\n";
- text += " rdfs:label \"" + paramUnit + "\" ;\n";
- text += " unit:symbol \"" + paramUnit + "\" ;\n";
- text += " unit:render \"%f " + paramUnit + "\" ;\n";
- text += " ] ;\n";
- }
-
- if (i+1 == paramCount)
- text += " ] ;\n\n";
- else
- text += " ] , [\n";
- }
-
- text += " doap:name \"" + String(pluginDesc->name) + "\" ;\n";
- text += " doap:maintainer [ foaf:name \"" + String(pluginDesc->maker) + "\" ] .\n";
-
- // -------------------------------------------------------------------
- // Write file now
-
- std::fstream pluginStream(pluginFile.toRawUTF8(), std::ios::out);
- pluginStream << text.toRawUTF8();
- pluginStream.close();
-
- // -------------------------------------------------------------------
- // Cleanup plugin
-
- if (pluginHandle != nullptr && pluginDesc->cleanup != nullptr)
- pluginDesc->cleanup(pluginHandle);
- }
-
- // -----------------------------------------------------------------------
-
- int main()
- {
- PluginListManager& plm(PluginListManager::getInstance());
-
- writeManifestFile(plm);
-
- for (LinkedList<const NativePluginDescriptor*>::Itenerator it = plm.descs.begin2(); it.valid(); it.next())
- {
- const NativePluginDescriptor* const& pluginDesc(it.getValue());
- writePluginFile(pluginDesc);
- }
-
- carla_stdout("Done.");
-
- return 0;
- }
-
- // -----------------------------------------------------------------------
|