diff options
Diffstat (limited to 'src/engine')
-rw-r--r-- | src/engine/Action.cpp | 3 | ||||
-rw-r--r-- | src/engine/JackDriver.cpp | 10 | ||||
-rw-r--r-- | src/engine/Node.cpp | 25 | ||||
-rw-r--r-- | src/engine/SMFDriver.cpp | 60 | ||||
-rw-r--r-- | src/engine/machina/MidiAction.hpp | 9 | ||||
-rw-r--r-- | src/engine/machina/Node.hpp | 5 |
6 files changed, 97 insertions, 15 deletions
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<Raul::MIDISink> 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<Raul::MIDISink> 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 <list> #include <iostream> #include <glibmm/convert.h> +#include <raul/midi_events.h> #include <raul/SMFWriter.h> +#include <raul/SMFReader.h> #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<Machine> m(new Machine()); + list<SharedPtr<Node> > active_nodes; + SharedPtr<Node> 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> node(new Node()); + node->add_enter_action(SharedPtr<Action>(new MidiAction(ev_size, buf))); + connect_node->add_outgoing_edge(SharedPtr<Edge>(new Edge(connect_node, node))); + node->enter(SharedPtr<Raul::MIDISink>(), t); + active_nodes.push_back(node); + } else if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_OFF) { + cerr << "NOTE OFF: " << (int)buf[1] << endl; + for (list<SharedPtr<Node> >::iterator i = active_nodes.begin(); + i != active_nodes.end(); ++i) { + SharedPtr<MidiAction> action = PtrCast<MidiAction>((*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<Action>(new MidiAction(ev_size, buf))); + (*i)->set_duration(t - (*i)->enter_time()); + (*i)->exit(SharedPtr<Raul::MIDISink>(), 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<MidiAction> create(SharedPtr<Raul::Maid> 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<Raul::MIDISink> 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> action); void remove_enter_action(SharedPtr<Action> action); - + void add_exit_action(SharedPtr<Action> action); void remove_exit_action(SharedPtr<Action> action); + SharedPtr<Action> enter_action() { return _enter_action; } + SharedPtr<Action> exit_action() { return _exit_action; } + void enter(SharedPtr<Raul::MIDISink> driver, BeatTime time); void exit(SharedPtr<Raul::MIDISink> driver, BeatTime time); |