From aa11564fef8ed9330d42081288e8f310072c67d6 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Fri, 7 Dec 2007 17:27:06 +0000 Subject: Fix loading selector nodes. Choosing evolution target MIDI from GUI. git-svn-id: http://svn.drobilla.net/lad/machina@963 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Loader.cpp | 20 ++++++++++++++++++ src/engine/MachineBuilder.cpp | 6 +++--- src/engine/Mutation.cpp | 6 ++++-- src/engine/Problem.cpp | 4 ++-- src/engine/machina/Node.hpp | 2 +- src/engine/machina/Problem.hpp | 2 +- src/gui/MachinaGUI.cpp | 40 ++++++++++++++++++++++++++++++++++- src/gui/MachinaGUI.hpp | 3 +++ src/gui/machina.glade | 48 ++++++++++++++++++++++++++++++------------ 9 files changed, 107 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp index 3b60223..cf41bd1 100644 --- a/src/engine/Loader.cpp +++ b/src/engine/Loader.cpp @@ -119,6 +119,26 @@ Loader::load(const Glib::ustring& uri) } + /* Find out which nodes are selectors */ + + query = Query(_rdf_world, ustring( + "SELECT DISTINCT ?node WHERE {\n") + + machine_uri + " :node ?node .\n" + "?node a :SelectorNode .\n" + "}\n"); + + results = query.run(_rdf_world, model); + + for (Query::Results::iterator i = results.begin(); i != results.end(); ++i) { + const char* node_id = (*i)["node"]; + Created::iterator n = created.find(node_id); + if (n != created.end()) + n->second->set_selector(true); + else + cerr << "WARNING: Selector node " << node_id << " not found" << endl; + } + + /* Get note actions */ query = Query(_rdf_world, ustring( diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index 7f0da03..34e45b8 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -44,7 +44,7 @@ MachineBuilder::MachineBuilder(SharedPtr machine, Raul::BeatTime q) void MachineBuilder::reset() { - _initial_node = SharedPtr(new Node()); + _initial_node = SharedPtr(new Node(0.0)); _initial_node->set_initial(true); _connect_node = _initial_node; _connect_node_end_time = 0; @@ -123,7 +123,7 @@ MachineBuilder::event(Raul::BeatTime time_offset, if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) { - SharedPtr node(new Node()); + SharedPtr node(new Node(0.0)); node->set_enter_action(SharedPtr(new MidiAction(ev_size, buf))); SharedPtr this_connect_node; @@ -187,7 +187,7 @@ MachineBuilder::event(Raul::BeatTime time_offset, // Finish a polyphonic section if (_poly_nodes.size() > 0) { - _connect_node = SharedPtr(new Node()); + _connect_node = SharedPtr(new Node(0.0)); _machine->add_node(_connect_node); connect_nodes(_machine, resolved, t, _connect_node, t); diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp index d60ef34..a307ce5 100644 --- a/src/engine/Mutation.cpp +++ b/src/engine/Mutation.cpp @@ -52,10 +52,12 @@ AddNode::mutate(Machine& machine) //cout << "ADD NODE" << endl; // Create random node - SharedPtr node(new Node(1.0)); + SharedPtr node(new Node()); node->set_selector(true); SharedPtr note_node = machine.random_node(); + if (!note_node) + return; uint8_t note = PtrCast(note_node->enter_action())->event()[1]; node->set_enter_action(ActionFactory::note_on(note)); @@ -154,7 +156,7 @@ RemoveEdge::mutate(Machine& machine) //cout << "REMOVE EDGE" << endl; SharedPtr tail = machine.random_node(); - if (tail) + if (tail && !(tail->is_initial() && tail->edges().size() == 1)) tail->remove_edge(tail->random_edge()); } diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index 3fa793f..0db588c 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -42,7 +42,7 @@ Problem::Problem(const std::string& target_midi, SharedPtr seed) const bool opened = smf.open(target_midi); assert(opened); - smf.seek_to_track(2); // FIXME: kluge + smf.seek_to_track(2); // FIXME: ? uint8_t buf[4]; uint32_t ev_size; @@ -221,7 +221,7 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const for (uint8_t i=0; i < 128; ++i) { if (_target._counts[i] > 0) { //cout << "Initial note: " << (int)i << endl; - SharedPtr node(new Node(1.0)); + SharedPtr node(new Node(1/4.0)); node->set_enter_action(ActionFactory::note_on(i)); node->set_exit_action(ActionFactory::note_off(i)); node->set_selector(true); diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp index 87e14af..04e7c07 100644 --- a/src/engine/machina/Node.hpp +++ b/src/engine/machina/Node.hpp @@ -44,7 +44,7 @@ class Node : public Raul::Stateful { public: typedef std::string ID; - Node(BeatCount duration=1.0, bool initial=false); + Node(BeatCount duration=1/4.0, bool initial=false); Node(const Node& copy); void set_enter_action(SharedPtr action); diff --git a/src/engine/machina/Problem.hpp b/src/engine/machina/Problem.hpp index 7605e88..067be51 100644 --- a/src/engine/machina/Problem.hpp +++ b/src/engine/machina/Problem.hpp @@ -81,7 +81,7 @@ private: };*/ struct Evaluator : public Raul::MIDISink { - Evaluator(const Problem& problem) : _problem(problem), _order(8), _n_notes(0), _first_note(0) { + Evaluator(const Problem& problem) : _problem(problem), _order(4), _n_notes(0), _first_note(0) { for (uint8_t i=0; i < 128; ++i) _counts[i] = 0; } diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp index 0e872fd..4fdc6fa 100644 --- a/src/gui/MachinaGUI.cpp +++ b/src/gui/MachinaGUI.cpp @@ -78,6 +78,7 @@ 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("load_target_but", _load_target_button); xml->get_widget("evolve_but", _evolve_button); xml->get_widget("mutate_but", _mutate_button); xml->get_widget("compress_but", _compress_button); @@ -174,6 +175,7 @@ MachinaGUI::MachinaGUI(SharedPtr engine) Glib::signal_timeout().connect(sigc::mem_fun(this, &MachinaGUI::idle_callback), 100); #ifdef HAVE_EUGENE + _load_target_button->signal_clicked().connect(sigc::mem_fun(this, &MachinaGUI::load_target_clicked)); _evolve_button->signal_clicked().connect(sigc::mem_fun(this, &MachinaGUI::evolve_toggled)); Glib::signal_timeout().connect(sigc::mem_fun(this, &MachinaGUI::evolve_callback), 1000); #else @@ -251,11 +253,32 @@ MachinaGUI::arrange() } +void +MachinaGUI::load_target_clicked() +{ + Gtk::FileChooserDialog dialog(*_main_window, + "Load MIDI file for evolution", Gtk::FILE_CHOOSER_ACTION_OPEN); + dialog.set_local_only(false); + dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); + dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + Gtk::FileFilter filt; + filt.add_pattern("*.mid"); + filt.set_name("MIDI Files"); + dialog.set_filter(filt); + + const int result = dialog.run(); + + if (result == Gtk::RESPONSE_OK) + _target_filename = dialog.get_filename(); +} + + void MachinaGUI::evolve_toggled() { if (_evolve_button->get_active()) { - _evolver = SharedPtr(new Evolver("target.mid", _engine->machine())); + _evolver = SharedPtr(new Evolver(_target_filename, _engine->machine())); _evolve = true; stop_clicked(); _engine->driver()->set_machine(SharedPtr()); @@ -384,6 +407,11 @@ MachinaGUI::menu_file_open() dialog.set_local_only(false); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + + Gtk::FileFilter filt; + filt.add_pattern("*.machina.ttl"); + filt.set_name("Machina Machines (Turtle/RDF)"); + dialog.set_filter(filt); const int result = dialog.run(); @@ -477,6 +505,11 @@ MachinaGUI::menu_import_midi() dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK); + Gtk::FileFilter filt; + filt.add_pattern("*.mid"); + filt.set_name("MIDI Files"); + dialog.set_filter(filt); + Gtk::HBox* extra_widget = Gtk::manage(new Gtk::HBox()); Gtk::SpinButton* length_sb = Gtk::manage(new Gtk::SpinButton()); length_sb->set_increments(1, 10); @@ -520,6 +553,11 @@ MachinaGUI::menu_export_midi() Gtk::FILE_CHOOSER_ACTION_SAVE); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); dialog.add_button(Gtk::Stock::SAVE, Gtk::RESPONSE_OK); + + Gtk::FileFilter filt; + filt.add_pattern("*.mid"); + filt.set_name("MIDI Files"); + dialog.set_filter(filt); const int result = dialog.run(); diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp index 3b6c340..26cfad4 100644 --- a/src/gui/MachinaGUI.hpp +++ b/src/gui/MachinaGUI.hpp @@ -63,6 +63,7 @@ protected: void menu_help_about(); void menu_help_help(); void arrange(); + void load_target_clicked(); void evolve_toggled(); void random_mutation(SharedPtr machine); void mutate(SharedPtr machine, unsigned mutation); @@ -83,6 +84,7 @@ protected: bool _evolve; string _save_uri; + string _target_filename; boost::shared_ptr _canvas; boost::shared_ptr _engine; @@ -121,6 +123,7 @@ protected: Gtk::ToolButton* _zoom_normal_button; Gtk::ToolButton* _zoom_full_button; Gtk::ToolButton* _arrange_button; + Gtk::ToolButton* _load_target_button; Gtk::ToggleToolButton* _evolve_button; Gtk::ToolButton* _mutate_button; Gtk::ToolButton* _compress_button; diff --git a/src/gui/machina.glade b/src/gui/machina.glade index e91553d..f991a08 100644 --- a/src/gui/machina.glade +++ b/src/gui/machina.glade @@ -403,6 +403,26 @@ 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 + + + + + True + GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK + Load MIDI file as evolutionary goal + gtk-open + + + False + + True @@ -704,58 +724,58 @@ Selector nodes are shown in green. 4 8 - + True True - 60 0 127 1 10 10 + True + 1 0 999999 1 10 10 1 + 2 True 1 2 + 1 + 2 - + True 0 - Note: + Duration: + 1 + 2 GTK_FILL - + True 0 - Duration: + Note: - 1 - 2 GTK_FILL - + True True - True - 1 0 999999 1 10 10 + 60 0 127 1 10 10 1 - 2 True 1 2 - 1 - 2 -- cgit v1.2.1