From f3ba4fc81cecf8ba61c2508ed9a624c794ab88b5 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 11 Mar 2007 17:33:20 +0000 Subject: MIDI file reading. git-svn-id: http://svn.drobilla.net/lad/machina@352 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Action.cpp | 3 ++ src/engine/JackDriver.cpp | 10 ++++--- src/engine/Node.cpp | 25 ++++++++++------ src/engine/SMFDriver.cpp | 60 +++++++++++++++++++++++++++++++++++++++ src/engine/machina/MidiAction.hpp | 9 ++++-- src/engine/machina/Node.hpp | 5 +++- src/gui/MachinaGUI.cpp | 12 +++++--- src/gui/MachinaGUI.hpp | 4 +-- src/gui/NodeView.cpp | 4 ++- src/gui/machina.glade | 59 +++++++++++++++----------------------- 10 files changed, 133 insertions(+), 58 deletions(-) (limited to 'src') diff --git a/src/engine/Action.cpp b/src/engine/Action.cpp index a893d74..471dfa8 100644 --- a/src/engine/Action.cpp +++ b/src/engine/Action.cpp @@ -24,6 +24,9 @@ void Action::write_state(Raul::RDFWriter& writer) { using Raul::RdfId; + + if (!_id) + set_id(writer.blank_id()); writer.write(_id, RdfId(RdfId::RESOURCE, "rdf:type"), diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index b22ad85..0261dd5 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -193,10 +193,12 @@ JackDriver::on_process(jack_nframes_t nframes) machine->set_sink(shared_from_this()); // Machine was switched since last cycle, finalize old machine. - if (_last_machine && machine != _last_machine) { - _last_machine->reset(); // Exit all active states - assert(_last_machine.use_count() > 1); // Realtime, can't delete - _last_machine.reset(); // Cut our reference + if (machine != _last_machine) { + if (_last_machine) { + _last_machine->reset(); // Exit all active states + assert(_last_machine.use_count() > 1); // Realtime, can't delete + _last_machine.reset(); // Cut our reference + } _machine_changed.post(); // Signal we're done with it } diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp index fbf02fe..4a1ded0 100644 --- a/src/engine/Node.cpp +++ b/src/engine/Node.cpp @@ -68,7 +68,7 @@ Node::enter(SharedPtr sink, BeatTime time) //cerr << "ENTER " << time << endl; _is_active = true; _enter_time = time; - if (_enter_action) + if (sink && _enter_action) _enter_action->execute(sink, time); } @@ -77,7 +77,7 @@ void Node::exit(SharedPtr sink, BeatTime time) { //cerr << "EXIT " << time << endl; - if (_exit_action) + if (sink && _exit_action) _exit_action->execute(sink, time); _is_active = false; _enter_time = 0; @@ -131,13 +131,22 @@ Node::write_state(Raul::RDFWriter& writer) RdfId(RdfId::RESOURCE, "machina:duration"), Raul::Atom((float)_duration)); - writer.write(_id, - RdfId(RdfId::RESOURCE, "machina:enterAction"), - _enter_action->id()); - _enter_action->write_state(writer); + if (_enter_action) { + _enter_action->write_state(writer); + + writer.write(_id, + RdfId(RdfId::RESOURCE, "machina:enterAction"), + _enter_action->id()); + } + + if (_exit_action) { + _exit_action->write_state(writer); + + writer.write(_id, + RdfId(RdfId::RESOURCE, "machina:exitAction"), + _exit_action->id()); + } - _exit_action->write_state(writer); - /*for (Node::Edges::const_iterator e = _outgoing_edges.begin(); e != _outgoing_edges.end(); ++e) (*e)->write_state(writer);*/ diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp index dd60903..181da52 100644 --- a/src/engine/SMFDriver.cpp +++ b/src/engine/SMFDriver.cpp @@ -15,10 +15,14 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ +#include #include #include +#include #include +#include #include "machina/Machine.hpp" +#include "machina/Edge.hpp" #include "machina/SMFDriver.hpp" using namespace std; @@ -37,8 +41,64 @@ SMFDriver::learn(const Glib::ustring& uri) const string filename = Glib::filename_from_uri(uri); std::cerr << "Learn MIDI: " << filename << std::endl; + SharedPtr m(new Machine()); + list > active_nodes; + SharedPtr connect_node(new Node()); + connect_node->set_initial(true); + m->add_node(connect_node); + + Raul::SMFReader reader; + reader.open(filename); + Raul::BeatTime t = 0; + unsigned char buf[4]; + uint32_t ev_size; + uint32_t ev_time; + while (reader.read_event(4, buf, &ev_size, &ev_time) >= 0) { + t += ev_time / (double)reader.ppqn(); + cerr << "t = " << t << endl; + if (ev_size > 0) { + if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) { + cerr << "NOTE ON: " << (int)buf[1] << endl; + SharedPtr node(new Node()); + node->add_enter_action(SharedPtr(new MidiAction(ev_size, buf))); + connect_node->add_outgoing_edge(SharedPtr(new Edge(connect_node, node))); + node->enter(SharedPtr(), t); + active_nodes.push_back(node); + } else if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_OFF) { + cerr << "NOTE OFF: " << (int)buf[1] << endl; + for (list >::iterator i = active_nodes.begin(); + i != active_nodes.end(); ++i) { + SharedPtr action = PtrCast((*i)->enter_action()); + if (!action) + continue; + + const size_t ev_size = action->event_size(); + const unsigned char* ev = action->event(); + if (ev_size == 3 && (ev[0] & 0xF0) == MIDI_CMD_NOTE_ON + && ev[1] == buf[1]) { + cerr << "FOUND MATCHING NOTE ON!\n"; + (*i)->add_exit_action(SharedPtr(new MidiAction(ev_size, buf))); + (*i)->set_duration(t - (*i)->enter_time()); + (*i)->exit(SharedPtr(), t); + m->add_node((*i)); + if (active_nodes.size() == 1) + connect_node = (*i); + active_nodes.erase(i); + break; + } + } + } + } + /*std::cerr << "Event, size = " << ev_size << ", time = " << ev_time << std::endl; + cerr.flags(ios::hex); + for (uint32_t i=0; i < ev_size; ++i) { + cerr << "0x" << (int)buf[i] << " "; + } + cerr.flags(ios::dec); + cerr << endl;*/ + } return m; } diff --git a/src/engine/machina/MidiAction.hpp b/src/engine/machina/MidiAction.hpp index 2a9f91d..60d9189 100644 --- a/src/engine/machina/MidiAction.hpp +++ b/src/engine/machina/MidiAction.hpp @@ -34,6 +34,9 @@ class MidiAction : public Action { public: ~MidiAction(); + MidiAction(size_t size, + const unsigned char* event); + static SharedPtr create(SharedPtr maid, size_t size, const unsigned char* event) @@ -43,6 +46,9 @@ public: return ret; } + size_t event_size() { return _size; } + byte* event() { return _event.get(); } + bool set_event(size_t size, const byte* event); void execute(SharedPtr driver, Raul::BeatTime time); @@ -50,8 +56,7 @@ public: virtual void write_state(Raul::RDFWriter& writer); private: - MidiAction(size_t size, - const unsigned char* event); + size_t _size; const size_t _max_size; diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp index bd1a66e..2f9c2e4 100644 --- a/src/engine/machina/Node.hpp +++ b/src/engine/machina/Node.hpp @@ -48,10 +48,13 @@ public: void add_enter_action(SharedPtr action); void remove_enter_action(SharedPtr action); - + void add_exit_action(SharedPtr action); void remove_exit_action(SharedPtr action); + SharedPtr enter_action() { return _enter_action; } + SharedPtr exit_action() { return _exit_action; } + void enter(SharedPtr driver, BeatTime time); void exit(SharedPtr driver, BeatTime time); diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp index 65ecb13..121e25a 100644 --- a/src/gui/MachinaGUI.cpp +++ b/src/gui/MachinaGUI.cpp @@ -115,7 +115,7 @@ MachinaGUI::MachinaGUI(SharedPtr engine) xml->get_widget("save_menuitem", _menu_file_save); xml->get_widget("save_as_menuitem", _menu_file_save_as); xml->get_widget("quit_menuitem", _menu_file_quit); - xml->get_widget("learn_midi_menuitem", _menu_learn_midi); + xml->get_widget("import_midi_menuitem", _menu_import_midi); xml->get_widget("export_midi_menuitem", _menu_export_midi); xml->get_widget("view_toolbar_menuitem", _menu_view_toolbar); //xml->get_widget("view_refresh_menuitem", _menu_view_refresh); @@ -154,8 +154,8 @@ MachinaGUI::MachinaGUI(SharedPtr engine) sigc::mem_fun(this, &MachinaGUI::menu_file_save_as)); _menu_file_quit->signal_activate().connect( sigc::mem_fun(this, &MachinaGUI::menu_file_quit)); - _menu_learn_midi->signal_activate().connect( - sigc::mem_fun(this, &MachinaGUI::menu_learn_midi)); + _menu_import_midi->signal_activate().connect( + sigc::mem_fun(this, &MachinaGUI::menu_import_midi)); _menu_export_midi->signal_activate().connect( sigc::mem_fun(this, &MachinaGUI::menu_export_midi)); //_menu_view_refresh->signal_activate().connect( @@ -437,7 +437,7 @@ MachinaGUI::menu_file_save_as() void -MachinaGUI::menu_learn_midi() +MachinaGUI::menu_import_midi() { Gtk::FileChooserDialog dialog(*_main_window, "Learn from MIDI file", Gtk::FILE_CHOOSER_ACTION_OPEN); dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL); @@ -448,6 +448,10 @@ MachinaGUI::menu_learn_midi() if (result == Gtk::RESPONSE_OK) { SharedPtr file_driver(new Machina::SMFDriver()); SharedPtr machine = file_driver->learn(dialog.get_uri()); + machine->activate(); + machine->reset(); + _engine->driver()->set_machine(machine); + _canvas->build(machine); } } diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp index 962a6f9..0e507fc 100644 --- a/src/gui/MachinaGUI.hpp +++ b/src/gui/MachinaGUI.hpp @@ -59,7 +59,7 @@ protected: void menu_file_open(); void menu_file_save(); void menu_file_save_as(); - void menu_learn_midi(); + void menu_import_midi(); void menu_export_midi(); //void show_messages_toggled(); void show_toolbar_toggled(); @@ -100,7 +100,7 @@ protected: Gtk::MenuItem* _menu_file_save; Gtk::MenuItem* _menu_file_save_as; Gtk::MenuItem* _menu_file_quit; - Gtk::MenuItem* _menu_learn_midi; + Gtk::MenuItem* _menu_import_midi; Gtk::MenuItem* _menu_export_midi; Gtk::MenuItem* _menu_help_about; Gtk::CheckMenuItem* _menu_view_toolbar; diff --git a/src/gui/NodeView.cpp b/src/gui/NodeView.cpp index 62be95b..f34c658 100644 --- a/src/gui/NodeView.cpp +++ b/src/gui/NodeView.cpp @@ -34,10 +34,11 @@ void NodeView::on_double_click(GdkEventButton*) { bool is_initial = _node->is_initial(); - std::cerr << "INITIAL " << is_initial; + std::cerr << "Initial: " << is_initial << std::endl; _node->set_initial( ! is_initial ); } + void NodeView::update_state() { @@ -52,3 +53,4 @@ NodeView::update_state() set_base_color(_old_color); } } + diff --git a/src/gui/machina.glade b/src/gui/machina.glade index d82f528..c0d1b87 100644 --- a/src/gui/machina.glade +++ b/src/gui/machina.glade @@ -76,6 +76,28 @@ + + + True + _Import MIDI... + True + + + + + + True + gtk-media-record + 1 + 0.5 + 0.5 + 0 + 0 + + + + + True @@ -85,7 +107,7 @@ - + True gtk-convert 1 @@ -117,41 +139,6 @@ - - - True - _Machine - True - - - - - - - True - Learn MIDI... - True - - - - - - True - gtk-media-record - 1 - 0.5 - 0.5 - 0 - 0 - - - - - - - - - True -- cgit v1.2.1