Allow to dynamically un/load Midnam Patches
authorRobin Gareus <robin@gareus.org>
Sat, 29 Oct 2016 17:01:16 +0000 (19:01 +0200)
committerRobin Gareus <robin@gareus.org>
Sat, 29 Oct 2016 17:57:43 +0000 (19:57 +0200)
libs/ardour/ardour/midi_patch_manager.h
libs/ardour/midi_patch_manager.cc
libs/midi++2/midi++/midnam_patch.h

index 3c10a3a149047e2200a1e846ac31af044953f8d2..aa4fda108e071b5fe94a0fc0810c3cb730381d4b 100644 (file)
@@ -57,12 +57,16 @@ public:
                return *_manager;
        }
 
+       PBD::Signal0<void> PatchesChanged;
+
+       bool add_custom_midnam (const std::string& id, const std::string& midnam);
+       bool remove_custom_midnam (const std::string& id);
+
        void add_search_path (const PBD::Searchpath& search_path);
 
        void remove_search_path (const PBD::Searchpath& search_path);
 
-       boost::shared_ptr<MIDINameDocument> document_by_model(std::string model_name)
-               { return _documents[model_name]; }
+       boost::shared_ptr<MIDINameDocument> document_by_model(std::string model_name) const;
 
        boost::shared_ptr<MasterDeviceNames> master_device_by_model(std::string model_name)
                { return _master_devices_by_model[model_name]; }
@@ -139,7 +143,8 @@ public:
        const DeviceNamesByMaker& devices_by_manufacturer() const { return _devices_by_manufacturer; }
 
 private:
-       bool add_midi_name_document(const std::string& file_path);
+       bool load_midi_name_document(const std::string& file_path);
+       bool add_midi_name_document(boost::shared_ptr<MIDINameDocument>);
        bool remove_midi_name_document(const std::string& file_path);
 
        void add_midnam_files_from_directory(const std::string& directory_path);
index 8a6d8dfd100a5a88be14a8bfc47b6cb076b5e250..e3bf899bf5de0c5e87ec9cb63051a80cd1f15993 100644 (file)
@@ -68,6 +68,28 @@ MidiPatchManager::add_search_path (const Searchpath& search_path)
        }
 }
 
+bool
+MidiPatchManager::add_custom_midnam (const std::string& id, const std::string& midnam)
+{
+       boost::shared_ptr<MIDINameDocument> document;
+       document = boost::shared_ptr<MIDINameDocument>(new MIDINameDocument());
+       XMLTree mxml;
+       if (mxml.read_buffer (midnam, true)) {
+               if (0 == document->set_state (mxml, *mxml.root())) {
+                       document->set_file_path ("custom:" + id);
+                       add_midi_name_document (document);
+                       return true;
+               }
+       }
+       return false;
+}
+
+bool
+MidiPatchManager::remove_custom_midnam (const std::string& id)
+{
+       return remove_midi_name_document ("custom:" + id);
+}
+
 void
 MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_path)
 {
@@ -80,7 +102,7 @@ MidiPatchManager::add_midnam_files_from_directory(const std::string& directory_p
             << endmsg;
 
        for (vector<std::string>::const_iterator i = result.begin(); i != result.end(); ++i) {
-               add_midi_name_document (*i);
+               load_midi_name_document (*i);
        }
 }
 
@@ -116,7 +138,7 @@ MidiPatchManager::remove_midnam_files_from_directory(const std::string& director
 }
 
 bool
-MidiPatchManager::add_midi_name_document (const std::string& file_path)
+MidiPatchManager::load_midi_name_document (const std::string& file_path)
 {
        boost::shared_ptr<MIDINameDocument> document;
        try {
@@ -127,6 +149,22 @@ MidiPatchManager::add_midi_name_document (const std::string& file_path)
                      << endmsg;
                return false;
        }
+       return add_midi_name_document (document);
+}
+
+boost::shared_ptr<MIDINameDocument>
+MidiPatchManager::document_by_model(std::string model_name) const
+{
+       MidiNameDocuments::const_iterator i = _documents.find (model_name);
+       if (i != _documents.end ()) {
+               return i->second;
+       }
+       return boost::shared_ptr<MIDINameDocument> ();
+}
+
+bool
+MidiPatchManager::add_midi_name_document (boost::shared_ptr<MIDINameDocument> document)
+{
        for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
                 document->master_device_names_by_model().begin();
             device != document->master_device_names_by_model().end();
@@ -134,7 +172,7 @@ MidiPatchManager::add_midi_name_document (const std::string& file_path)
                if (_documents.find(device->first) != _documents.end()) {
                        warning << string_compose(_("Duplicate MIDI device `%1' in `%2' ignored"),
                                                  device->first,
-                                                 file_path) << endmsg;
+                                                 document->file_path()) << endmsg;
                        continue;
                }
 
@@ -155,6 +193,8 @@ MidiPatchManager::add_midi_name_document (const std::string& file_path)
                assert(_documents.count(device->first) == 1);
                assert(_master_devices_by_model.count(device->first) == 1);
        }
+
+       PatchesChanged(); /* EMIT SIGNAL */
        return true;
 }
 
@@ -167,9 +207,10 @@ MidiPatchManager::remove_midi_name_document (const std::string& file_path)
 
                        boost::shared_ptr<MIDINameDocument> document = i->second;
 
+                       cout << string_compose(_("Removing MIDI patch file %1"), file_path) << "\n";
                        info << string_compose(_("Removing MIDI patch file %1"), file_path) << endmsg;
 
-                       _documents.erase(i++);
+                       i = _documents.erase(i);
 
                        for (MIDINameDocument::MasterDeviceNamesList::const_iterator device =
                                 document->master_device_names_by_model().begin();
@@ -189,5 +230,8 @@ MidiPatchManager::remove_midi_name_document (const std::string& file_path)
                        ++i;
                }
        }
+       if (removed) {
+               PatchesChanged(); /* EMIT SIGNAL */
+       }
        return removed;
 }
index 683f52f84d9f9c6b4ec9edda8a1aedd130423712..f6f5f12edf81a77e2167211b9d42fc58388de1c6 100644 (file)
@@ -469,9 +469,10 @@ public:
        virtual ~MIDINameDocument() {};
 
        const std::string& file_path () const { return _file_path; }
-
        const std::string& author() const { return _author; }
+
        void set_author(const std::string& author) { _author = author; }
+       void set_file_path(const std::string& file_path) { _file_path = file_path; }
 
        boost::shared_ptr<MasterDeviceNames> master_device_names(const std::string& model);
 
@@ -483,7 +484,7 @@ public:
        int      set_state (const XMLTree&, const XMLNode&);
 
 private:
-       const std::string             _file_path;
+       std::string                   _file_path;
        std::string                   _author;
        MasterDeviceNamesList         _master_device_names_list;
        MasterDeviceNames::Models     _all_models;