From a96b70b1c92b7f2622ebeff9d34c92ba089997f1 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Wed, 5 Dec 2007 23:45:51 +0000 Subject: Working mutation operators. git-svn-id: http://svn.drobilla.net/lad/machina@952 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Loader.cpp | 2 +- src/engine/Machine.cpp | 5 +- src/engine/MachineBuilder.cpp | 8 +- src/engine/MachineMutation.cpp | 69 ++++++++++- src/engine/Node.cpp | 15 ++- src/engine/machina/MachineMutation.hpp | 4 + src/engine/machina/Node.hpp | 5 +- src/gui/MachinaCanvas.cpp | 2 +- src/gui/MachinaCanvas.hpp | 6 +- src/gui/MachinaGUI.cpp | 77 ++++++++++-- src/gui/MachinaGUI.hpp | 13 +- src/gui/machina.glade | 218 +++++++++++++++++++++------------ 12 files changed, 322 insertions(+), 102 deletions(-) (limited to 'src') diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp index 27b95fd..3b60223 100644 --- a/src/engine/Loader.cpp +++ b/src/engine/Loader.cpp @@ -171,7 +171,7 @@ Loader::load(const Glib::ustring& uri) SharedPtr edge(new Edge(src, dst)); edge->set_probability(prob); - src->add_outgoing_edge(edge); + src->add_edge(edge); } else { cerr << "[Loader] WARNING: Ignored edge between unknown nodes " diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index 7a99b58..44e8446 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -59,6 +59,9 @@ Machine::set_sink(SharedPtr sink) SharedPtr Machine::random_node() { + if (_nodes.empty()) + return SharedPtr(); + size_t i = rand() % _nodes.size(); // FIXME: O(n) worst case :( @@ -79,7 +82,7 @@ Machine::random_edge() for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) tail = random_node(); - return tail->random_edge(); + return tail ? tail->random_edge() : SharedPtr(); } diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index fd53487..7f0da03 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -94,16 +94,16 @@ MachineBuilder::connect_nodes(SharedPtr m, if (is_delay_node(tail) && tail->edges().size() == 0) { // Tail is a delay node, just accumulate the time difference into it set_node_duration(tail, tail->duration() + head_start_time - tail_end_time); - tail->add_outgoing_edge(SharedPtr(new Edge(tail, head))); + tail->add_edge(SharedPtr(new Edge(tail, head))); } else if (head_start_time == tail_end_time) { // Connect directly - tail->add_outgoing_edge(SharedPtr(new Edge(tail, head))); + tail->add_edge(SharedPtr(new Edge(tail, head))); } else { // Need to actually create a delay node delay_node = SharedPtr(new Node()); set_node_duration(delay_node, head_start_time - tail_end_time); - tail->add_outgoing_edge(SharedPtr(new Edge(tail, delay_node))); - delay_node->add_outgoing_edge(SharedPtr(new Edge(delay_node, head))); + tail->add_edge(SharedPtr(new Edge(tail, delay_node))); + delay_node->add_edge(SharedPtr(new Edge(delay_node, head))); m->add_node(delay_node); } diff --git a/src/engine/MachineMutation.cpp b/src/engine/MachineMutation.cpp index 6e98ee0..3c899bc 100644 --- a/src/engine/MachineMutation.cpp +++ b/src/engine/MachineMutation.cpp @@ -20,6 +20,7 @@ #include "machina/Edge.hpp" #include "machina/Machine.hpp" #include "machina/MachineMutation.hpp" +#include "machina/ActionFactory.hpp" using namespace std; @@ -27,17 +28,81 @@ namespace Machina { namespace Mutation { +void +Compress::mutate(Machine& machine) +{ + // Trim disconnected nodes + for (Machine::Nodes::iterator i = machine.nodes().begin(); i != machine.nodes().end() ;) { + Machine::Nodes::iterator next = i; + ++next; + + if ((*i)->edges().empty()) + machine.remove_node(*i); + + i = next; + } +} + + +void +AddNode::mutate(Machine& machine) +{ + // Create random node + SharedPtr node(new Node(1.0)); + uint8_t note = rand() % 128; + node->set_enter_action(ActionFactory::note_on(note)); + node->set_exit_action(ActionFactory::note_off(note)); + machine.add_node(node); + + // Add as a successor to some other random node + SharedPtr tail = machine.random_node(); + if (tail && tail != node) + tail->add_edge(boost::shared_ptr(new Edge(tail, node))); +} + + +void +RemoveNode::mutate(Machine& machine) +{ + SharedPtr node = machine.random_node(); + machine.remove_node(node); +} + + +void +AdjustNode::mutate(Machine& machine) +{ + SharedPtr node = machine.random_node(); + if (node) { + SharedPtr enter_action = PtrCast(node->enter_action()); + SharedPtr exit_action = PtrCast(node->exit_action()); + if (enter_action && exit_action) { + const uint8_t note = rand() % 128; + enter_action->event()[1] = note; + exit_action->event()[1] = note; + } + node->set_changed(); + } +} + + void AddEdge::mutate(Machine& machine) { - cout << "ADD" << endl; + SharedPtr tail = machine.random_node(); + SharedPtr head = machine.random_node(); + + if (tail && head && tail != head && !tail->connected_to(head)) + tail->add_edge(boost::shared_ptr(new Edge(tail, head))); } void RemoveEdge::mutate(Machine& machine) { - cout << "REMOVE" << endl; + SharedPtr tail = machine.random_node(); + if (tail) + tail->remove_edge(tail->random_edge()); } diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp index fd784e5..0eca0e6 100644 --- a/src/engine/Node.cpp +++ b/src/engine/Node.cpp @@ -119,7 +119,7 @@ Node::exit(SharedPtr sink, BeatTime time) void -Node::add_outgoing_edge(SharedPtr edge) +Node::add_edge(SharedPtr edge) { assert(edge->tail().lock().get() == this); @@ -128,11 +128,22 @@ Node::add_outgoing_edge(SharedPtr edge) void -Node::remove_outgoing_edge(SharedPtr edge) +Node::remove_edge(SharedPtr edge) { _edges.erase(_edges.find(edge)); } + +bool +Node::connected_to(SharedPtr node) +{ + for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) + if ((*i)->head() == node) + return true; + + return false; +} + void Node::remove_edges_to(SharedPtr node) diff --git a/src/engine/machina/MachineMutation.hpp b/src/engine/machina/MachineMutation.hpp index 34eda30..0a9731c 100644 --- a/src/engine/machina/MachineMutation.hpp +++ b/src/engine/machina/MachineMutation.hpp @@ -26,6 +26,10 @@ namespace Mutation { struct Mutation { virtual void mutate(Machine& machine) = 0; }; +struct Compress { static void mutate(Machine& machine); }; +struct AddNode { static void mutate(Machine& machine); }; +struct RemoveNode { static void mutate(Machine& machine); }; +struct AdjustNode { static void mutate(Machine& machine); }; struct AddEdge { static void mutate(Machine& machine); }; struct RemoveEdge { static void mutate(Machine& machine); }; struct AdjustEdge { static void mutate(Machine& machine); }; diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp index a916eb6..5926a50 100644 --- a/src/engine/machina/Node.hpp +++ b/src/engine/machina/Node.hpp @@ -56,9 +56,10 @@ public: void enter(SharedPtr driver, BeatTime time); void exit(SharedPtr driver, BeatTime time); - void add_outgoing_edge(SharedPtr edge); - void remove_outgoing_edge(SharedPtr edge); + void add_edge(SharedPtr edge); + void remove_edge(SharedPtr edge); void remove_edges_to(SharedPtr node); + bool connected_to(SharedPtr node); void write_state(Redland::Model& model); diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp index 8ed9e40..fb6ae94 100644 --- a/src/gui/MachinaCanvas.cpp +++ b/src/gui/MachinaCanvas.cpp @@ -117,7 +117,7 @@ MachinaCanvas::connect_node(boost::shared_ptr src, boost::shared_ptr head) { SharedPtr edge(new Machina::Edge(src->node(), head->node())); - src->node()->add_outgoing_edge(edge); + src->node()->add_edge(edge); boost::shared_ptr c(new EdgeView(shared_from_this(), src, head, edge)); diff --git a/src/gui/MachinaCanvas.hpp b/src/gui/MachinaCanvas.hpp index 631957e..211ebd7 100644 --- a/src/gui/MachinaCanvas.hpp +++ b/src/gui/MachinaCanvas.hpp @@ -15,8 +15,8 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ -#ifndef PATCHAGEPATCHBAYAREA_H -#define PATCHAGEPATCHBAYAREA_H +#ifndef MACHINA_CANVAS_HPP_H +#define MACHINA_CANVAS_HPP_H #include #include @@ -59,4 +59,4 @@ private: }; -#endif // PATCHAGEPATCHBAYAREA_H +#endif // MACHINA_CANVAS_HPP_H diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp index 6c94211..3749d3a 100644 --- a/src/gui/MachinaGUI.cpp +++ b/src/gui/MachinaGUI.cpp @@ -61,8 +61,7 @@ MachinaGUI::MachinaGUI(SharedPtr engine) xml->get_widget("help_about_menuitem", _menu_help_about); xml->get_widget("help_help_menuitem", _menu_help_help); xml->get_widget("canvas_scrolledwindow", _canvas_scrolledwindow); - xml->get_widget("slave_radiobutton", _slave_radiobutton); - xml->get_widget("bpm_radiobutton", _bpm_radiobutton); + xml->get_widget("clock_checkbutton", _clock_checkbutton); xml->get_widget("bpm_spinbutton", _bpm_spinbutton); xml->get_widget("quantize_checkbutton", _quantize_checkbutton); xml->get_widget("quantize_spinbutton", _quantize_spinbutton); @@ -72,6 +71,11 @@ MachinaGUI::MachinaGUI(SharedPtr engine) xml->get_widget("zoom_normal_but", _zoom_normal_button); xml->get_widget("zoom_full_but", _zoom_full_button); xml->get_widget("arrange_but", _arrange_button); + xml->get_widget("mutate_but", _mutate_button); + xml->get_widget("compress_but", _compress_button); + xml->get_widget("add_node_but", _add_node_button); + xml->get_widget("remove_node_but", _remove_node_button); + xml->get_widget("adjust_node_but", _adjust_node_button); xml->get_widget("add_edge_but", _add_edge_button); xml->get_widget("remove_edge_but", _remove_edge_button); xml->get_widget("adjust_edge_but", _adjust_edge_button); @@ -117,17 +121,23 @@ MachinaGUI::MachinaGUI(SharedPtr engine) sigc::mem_fun(this, &MachinaGUI::menu_help_about)); _menu_help_help->signal_activate().connect( sigc::mem_fun(this, &MachinaGUI::menu_help_help)); - _slave_radiobutton->signal_toggled().connect( - sigc::mem_fun(this, &MachinaGUI::tempo_changed)); - _bpm_radiobutton->signal_toggled().connect( - sigc::mem_fun(this, &MachinaGUI::tempo_changed)); - _bpm_spinbutton->signal_changed().connect( + _clock_checkbutton->signal_toggled().connect( sigc::mem_fun(this, &MachinaGUI::tempo_changed)); _quantize_checkbutton->signal_toggled().connect( sigc::mem_fun(this, &MachinaGUI::quantize_changed)); _quantize_spinbutton->signal_changed().connect( sigc::mem_fun(this, &MachinaGUI::quantize_changed)); + _mutate_button->signal_clicked().connect( + sigc::mem_fun(this, &MachinaGUI::mutate)); + _compress_button->signal_clicked().connect( + sigc::mem_fun(this, &MachinaGUI::compress)); + _add_node_button->signal_clicked().connect( + sigc::mem_fun(this, &MachinaGUI::add_node)); + _remove_node_button->signal_clicked().connect( + sigc::mem_fun(this, &MachinaGUI::remove_node)); + _adjust_node_button->signal_clicked().connect( + sigc::mem_fun(this, &MachinaGUI::adjust_node)); _add_edge_button->signal_clicked().connect( sigc::mem_fun(this, &MachinaGUI::add_edge)); _remove_edge_button->signal_clicked().connect( @@ -141,7 +151,7 @@ MachinaGUI::MachinaGUI(SharedPtr engine) _main_window->present(); - _bpm_radiobutton->set_active(true); + _clock_checkbutton->set_active(true); _quantize_checkbutton->set_active(false); update_toolbar(); @@ -211,11 +221,59 @@ MachinaGUI::arrange() _canvas->arrange(_menu_view_time_edges->get_active()); } + +void +MachinaGUI::mutate() +{ + switch (rand() % 7) { + case 0: compress(); break; + case 1: add_node(); break; + case 2: remove_node(); break; + case 3: adjust_node(); break; + case 4: add_edge(); break; + case 5: remove_edge(); break; + case 6: adjust_edge(); default: break; + } +} + + +void +MachinaGUI::compress() +{ + Mutation::Compress::mutate(*_engine->machine().get()); + _canvas->build(_engine->machine()); +} + + +void +MachinaGUI::add_node() +{ + Mutation::AddNode::mutate(*_engine->machine().get()); + _canvas->build(_engine->machine()); +} + + +void +MachinaGUI::remove_node() +{ + Mutation::RemoveNode::mutate(*_engine->machine().get()); + _canvas->build(_engine->machine()); +} + + +void +MachinaGUI::adjust_node() +{ + Mutation::AdjustNode::mutate(*_engine->machine().get()); + idle_callback(); // update nodes +} + void MachinaGUI::add_edge() { Mutation::AddEdge::mutate(*_engine->machine().get()); + _canvas->build(_engine->machine()); } @@ -223,6 +281,7 @@ void MachinaGUI::remove_edge() { Mutation::RemoveEdge::mutate(*_engine->machine().get()); + _canvas->build(_engine->machine()); } @@ -239,7 +298,7 @@ MachinaGUI::update_toolbar() { _record_button->set_active(_engine->driver()->recording()); _play_button->set_active(_engine->machine()->is_activated()); - _bpm_spinbutton->set_sensitive(_bpm_radiobutton->get_active()); + _bpm_spinbutton->set_sensitive(_clock_checkbutton->get_active()); _quantize_spinbutton->set_sensitive(_quantize_checkbutton->get_active()); } diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp index cc1eee3..c82cd01 100644 --- a/src/gui/MachinaGUI.hpp +++ b/src/gui/MachinaGUI.hpp @@ -63,6 +63,11 @@ protected: void menu_help_about(); void menu_help_help(); void arrange(); + void mutate(); + void compress(); + void add_node(); + void remove_node(); + void adjust_node(); void add_edge(); void remove_edge(); void adjust_edge(); @@ -108,8 +113,7 @@ protected: Gtk::ScrolledWindow* _canvas_scrolledwindow; Gtk::TextView* _status_text; Gtk::Expander* _messages_expander; - Gtk::RadioButton* _slave_radiobutton; - Gtk::RadioButton* _bpm_radiobutton; + Gtk::CheckButton* _clock_checkbutton; Gtk::SpinButton* _bpm_spinbutton; Gtk::CheckButton* _quantize_checkbutton; Gtk::SpinButton* _quantize_spinbutton; @@ -119,6 +123,11 @@ protected: Gtk::ToolButton* _zoom_normal_button; Gtk::ToolButton* _zoom_full_button; Gtk::ToolButton* _arrange_button; + Gtk::ToolButton* _mutate_button; + Gtk::ToolButton* _compress_button; + Gtk::ToolButton* _add_node_button; + Gtk::ToolButton* _remove_node_button; + Gtk::ToolButton* _adjust_node_button; Gtk::ToolButton* _add_edge_button; Gtk::ToolButton* _remove_edge_button; Gtk::ToolButton* _adjust_edge_button; diff --git a/src/gui/machina.glade b/src/gui/machina.glade index 11f3e7c..2153908 100644 --- a/src/gui/machina.glade +++ b/src/gui/machina.glade @@ -213,7 +213,7 @@ True - GTK_TOOLBAR_BOTH_HORIZ + GTK_TOOLBAR_ICONS False @@ -259,71 +259,40 @@ True - + True - 4 - 6 - + True - False True - Slave to JACK transport - Slave - True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Internal clock 0 + True True + + + + True + True + Set internal tempo + 120 1 640 1 10 10 + 1 + - False - False + 1 - + True - - - True - True - Use internal tempo - True - 0 - True - True - slave_radiobutton - - - False - False - - - - - True - True - Set internal tempo - 120 1 640 1 10 10 - 1 - - - 1 - - - - - True - BPM - - - False - False - 2 - - + BPM - 1 + False + False + 2 @@ -354,8 +323,8 @@ True True - Quantize recording - Quantize: 1/ + Quantize recording (beats) + 1/ True 0 True @@ -430,16 +399,115 @@ True GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + GTK_TOOLBAR_ICONS False GTK_ICON_SIZE_SMALL_TOOLBAR True + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + + + False + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Random mutation + gtk-dialog-warning + + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Compress (merge identical nodes) + gtk-convert + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + + + False + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Create node + Add Node + gtk-new + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Delete node + Delete Node + gtk-delete + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Adjust node + Adjust Node + gtk-edit + + + False + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + + + False + False + + True GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK - Add random edge + Add edge Add Edge - gtk-add + gtk-connect False @@ -449,9 +517,9 @@ True GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK - Remove random edge + Remove edge Remove Edge - gtk-remove + gtk-disconnect False @@ -461,9 +529,9 @@ True GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK - Adjust random edge probability - Mutate Edge Probabililty - gtk-edit + Adjust edge + Adjust Edge + gtk-select-color False @@ -614,58 +682,58 @@ Selector nodes are shown in green. 4 8 - + True True - True - 1 0 999999 1 10 10 + 60 0 127 1 10 10 1 - 2 True 1 2 - 1 - 2 - + True 0 - Duration: + Note: - 1 - 2 GTK_FILL - + True 0 - Note: + Duration: + 1 + 2 GTK_FILL - + True True - 60 0 127 1 10 10 + True + 1 0 999999 1 10 10 1 + 2 True 1 2 + 1 + 2 -- cgit v1.2.1