diff options
author | David Robillard <d@drobilla.net> | 2013-01-12 23:38:03 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2013-01-12 23:38:03 +0000 |
commit | 1dad5b5aaa139993fe19e266d08dfc55844e6804 (patch) | |
tree | fd2bed5971853b429f1b74369a778a4d608e6925 /src/engine | |
parent | 8f048287d06afd7d3c2e90f4a503d7666a9cb6fa (diff) | |
download | machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.tar.gz machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.tar.bz2 machina-1dad5b5aaa139993fe19e266d08dfc55844e6804.zip |
Remove Raul::SharedPtr and switch to std::shared_ptr.
Use project local short type aliases for shared_ptr and friends.
Move Raul::Disposable and Raul::Manageable into Raul::Maid.
Use sets to store machina nodes and edges to avoid O(n) searches.
git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4939 a436a847-0d15-0410-975c-d299462d15a1
Diffstat (limited to 'src/engine')
36 files changed, 501 insertions, 487 deletions
diff --git a/src/engine/Action.hpp b/src/engine/Action.hpp index f954a39..7397eaa 100644 --- a/src/engine/Action.hpp +++ b/src/engine/Action.hpp @@ -21,9 +21,8 @@ #include <string> #include <iostream> -#include "raul/SharedPtr.hpp" +#include "raul/Maid.hpp" #include "raul/TimeSlice.hpp" -#include "raul/Manageable.hpp" #include "machina/types.hpp" @@ -35,15 +34,14 @@ namespace Machina { /** An Action, executed on entering or exiting of a state. */ struct Action - : public Raul::Manageable - , public Stateful { + : public Raul::Maid::Manageable + , public Stateful { virtual void execute(MIDISink* sink, Raul::TimeStamp time) = 0; virtual void write_state(Sord::Model& model); }; -class PrintAction - : public Action +class PrintAction : public Action { public: PrintAction(const std::string& msg) diff --git a/src/engine/ActionFactory.cpp b/src/engine/ActionFactory.cpp index 92bc114..afcc178 100644 --- a/src/engine/ActionFactory.cpp +++ b/src/engine/ActionFactory.cpp @@ -20,18 +20,18 @@ namespace Machina { -SharedPtr<Action> -ActionFactory::copy(SharedPtr<Action> copy) +SPtr<Action> +ActionFactory::copy(SPtr<Action> copy) { - SharedPtr<MidiAction> ma = PtrCast<MidiAction>(copy); + SPtr<MidiAction> ma = dynamic_ptr_cast<MidiAction>(copy); if (ma) { - return SharedPtr<Action>(new MidiAction(ma->event_size(), ma->event())); + return SPtr<Action>(new MidiAction(ma->event_size(), ma->event())); } else { - return SharedPtr<Action>(); + return SPtr<Action>(); } } -SharedPtr<Action> +SPtr<Action> ActionFactory::note_on(unsigned char note) { unsigned char buf[3]; @@ -39,10 +39,10 @@ ActionFactory::note_on(unsigned char note) buf[1] = note; buf[2] = 0x40; - return SharedPtr<Action>(new MidiAction(3, buf)); + return SPtr<Action>(new MidiAction(3, buf)); } -SharedPtr<Action> +SPtr<Action> ActionFactory::note_off(unsigned char note) { unsigned char buf[3]; @@ -50,7 +50,7 @@ ActionFactory::note_off(unsigned char note) buf[1] = note; buf[2] = 0x40; - return SharedPtr<Action>(new MidiAction(3, buf)); + return SPtr<Action>(new MidiAction(3, buf)); } } // namespace Machine diff --git a/src/engine/ActionFactory.hpp b/src/engine/ActionFactory.hpp index 1ae47c4..d273274 100644 --- a/src/engine/ActionFactory.hpp +++ b/src/engine/ActionFactory.hpp @@ -18,16 +18,16 @@ #ifndef MACHINA_ACTIONFACTORY_HPP #define MACHINA_ACTIONFACTORY_HPP -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" namespace Machina { struct Action; namespace ActionFactory { -SharedPtr<Action> copy(SharedPtr<Action> copy); -SharedPtr<Action> note_on(unsigned char note); -SharedPtr<Action> note_off(unsigned char note); +SPtr<Action> copy(SPtr<Action> copy); +SPtr<Action> note_on(unsigned char note); +SPtr<Action> note_off(unsigned char note); } } // namespace Machina diff --git a/src/engine/Controller.cpp b/src/engine/Controller.cpp index f29d10e..df60c97 100644 --- a/src/engine/Controller.cpp +++ b/src/engine/Controller.cpp @@ -27,7 +27,7 @@ namespace Machina { -Controller::Controller(SharedPtr<Engine> engine, +Controller::Controller(SPtr<Engine> engine, Client::ClientModel& client_model) : _engine(engine) , _client_model(client_model) @@ -41,8 +41,8 @@ Controller::create(const Client::ClientObject& properties) { TimeDuration dur(_engine->machine()->time().unit(), properties.get(URIs::instance().machina_duration).get_float()); - SharedPtr<Machina::Node> node(new Machina::Node(dur)); - SharedPtr<Client::ClientObject> obj( + SPtr<Machina::Node> node(new Machina::Node(dur)); + SPtr<Client::ClientObject> obj( new Client::ClientObject(properties, node->id())); _objects.insert(node); _client_model.new_object(obj); @@ -51,13 +51,13 @@ Controller::create(const Client::ClientObject& properties) } void -Controller::announce(SharedPtr<Machine> machine) +Controller::announce(SPtr<Machine> machine) { Raul::Forge& forge = _engine->forge(); for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); n != machine->nodes().end(); ++n) { - SharedPtr<Machina::Client::ClientObject> obj( + SPtr<Machina::Client::ClientObject> obj( new Machina::Client::ClientObject((*n)->id())); obj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Node)); @@ -75,7 +75,7 @@ Controller::announce(SharedPtr<Machine> machine) for (Machina::Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { _objects.insert(*e); - SharedPtr<Client::ClientObject> eobj( + SPtr<Client::ClientObject> eobj( new Client::ClientObject((*e)->id())); eobj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Edge)); @@ -91,21 +91,21 @@ Controller::announce(SharedPtr<Machine> machine) } } -SharedPtr<Stateful> +SPtr<Stateful> Controller::find(uint64_t id) { - SharedPtr<StatefulKey> key(new StatefulKey(id)); - Objects::iterator i = _objects.find(key); + SPtr<StatefulKey> key(new StatefulKey(id)); + Objects::iterator i = _objects.find(key); if (i != _objects.end()) { return *i; } - return SharedPtr<Stateful>(); + return SPtr<Stateful>(); } void -Controller::learn(SharedPtr<Raul::Maid> maid, uint64_t node_id) +Controller::learn(SPtr<Raul::Maid> maid, uint64_t node_id) { - SharedPtr<Machina::Node> node = PtrCast<Machina::Node>(find(node_id)); + SPtr<Machina::Node> node = dynamic_ptr_cast<Machina::Node>(find(node_id)); if (node) { _engine->machine()->learn(maid, node); } else { @@ -119,7 +119,7 @@ Controller::set_property(uint64_t object_id, URIInt key, const Raul::Atom& value) { - SharedPtr<Stateful> object = find(object_id); + SPtr<Stateful> object = find(object_id); if (object) { object->set(key, value); _client_model.property(object_id, key, value); @@ -129,17 +129,16 @@ Controller::set_property(uint64_t object_id, uint64_t Controller::connect(uint64_t tail_id, uint64_t head_id) { - SharedPtr<Machina::Node> tail = PtrCast<Machina::Node>(find(tail_id)); - SharedPtr<Machina::Node> head = PtrCast<Machina::Node>(find(head_id)); + SPtr<Machina::Node> tail = dynamic_ptr_cast<Machina::Node>(find(tail_id)); + SPtr<Machina::Node> head = dynamic_ptr_cast<Machina::Node>(find(head_id)); - SharedPtr<Machina::Edge> edge(new Machina::Edge(tail, head)); + SPtr<Machina::Edge> edge(new Machina::Edge(tail, head)); tail->add_edge(edge); _objects.insert(edge); Raul::Forge& forge = _engine->forge(); - SharedPtr<Client::ClientObject> obj( - new Client::ClientObject(edge->id())); + SPtr<Client::ClientObject> obj(new Client::ClientObject(edge->id())); obj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().machina_Edge)); obj->set(URIs::instance().machina_probability, forge.make(1.0f)); @@ -154,10 +153,10 @@ Controller::connect(uint64_t tail_id, uint64_t head_id) void Controller::disconnect(uint64_t tail_id, uint64_t head_id) { - SharedPtr<Machina::Node> tail = PtrCast<Machina::Node>(find(tail_id)); - SharedPtr<Machina::Node> head = PtrCast<Machina::Node>(find(head_id)); + SPtr<Machina::Node> tail = dynamic_ptr_cast<Machina::Node>(find(tail_id)); + SPtr<Machina::Node> head = dynamic_ptr_cast<Machina::Node>(find(head_id)); - SharedPtr<Edge> edge = tail->remove_edge_to(head); + SPtr<Edge> edge = tail->remove_edge_to(head); if (edge) { _client_model.erase_object(edge->id()); } else { @@ -168,13 +167,13 @@ Controller::disconnect(uint64_t tail_id, uint64_t head_id) void Controller::erase(uint64_t id) { - SharedPtr<StatefulKey> key(new StatefulKey(id)); + SPtr<StatefulKey> key(new StatefulKey(id)); Objects::iterator i = _objects.find(key); if (i == _objects.end()) { return; } - SharedPtr<Node> node = PtrCast<Node>(*i); + SPtr<Node> node = dynamic_ptr_cast<Node>(*i); if (node) { _engine->machine()->remove_node(node); } @@ -193,13 +192,11 @@ Controller::process_updates() Raul::Atom value; for (uint32_t i = 0; i < read_space; ) { i += read_set(_updates, &subject, &key, &value); - SharedPtr<Machina::Client::ClientObject> obj = _client_model.find( - subject); + SPtr<Machina::Client::ClientObject> obj = _client_model.find(subject); if (obj) { obj->set(key, value); } else { - SharedPtr<Client::ClientObject> obj( - new Client::ClientObject(subject)); + SPtr<Client::ClientObject> obj(new Client::ClientObject(subject)); obj->set(key, value); _client_model.new_object(obj); } diff --git a/src/engine/Edge.cpp b/src/engine/Edge.cpp index 9aee198..e43efa1 100644 --- a/src/engine/Edge.cpp +++ b/src/engine/Edge.cpp @@ -42,8 +42,8 @@ Edge::write_state(Sord::Model& model) Sord::Curie(model.world(), "rdf:type"), Sord::Curie(model.world(), "machina:Edge")); - SharedPtr<Node> tail = _tail.lock(); - SharedPtr<Node> head = _head; + SPtr<Node> tail = _tail.lock(); + SPtr<Node> head = _head; if (!tail || !head) return; diff --git a/src/engine/Edge.hpp b/src/engine/Edge.hpp index 10c7ab8..6d916c1 100644 --- a/src/engine/Edge.hpp +++ b/src/engine/Edge.hpp @@ -22,8 +22,6 @@ #include <boost/utility.hpp> #include "raul/DoubleBuffer.hpp" -#include "raul/SharedPtr.hpp" -#include "raul/WeakPtr.hpp" #include "machina/types.hpp" @@ -38,7 +36,7 @@ class Edge : public Stateful { public: - Edge(WeakPtr<Node> tail, SharedPtr<Node> head) + Edge(WPtr<Node> tail, SPtr<Node> head) : _probability(1.0f) , _tail(tail) , _head(head) @@ -47,11 +45,11 @@ public: void set(URIInt key, const Raul::Atom& value); void write_state(Sord::Model& model); - WeakPtr<Node> tail() { return _tail; } - SharedPtr<Node> head() { return _head; } + WPtr<Node> tail() { return _tail; } + SPtr<Node> head() { return _head; } - void set_tail(WeakPtr<Node> tail) { _tail = tail; } - void set_head(SharedPtr<Node> head) { _head = head; } + void set_tail(WPtr<Node> tail) { _tail = tail; } + void set_head(SPtr<Node> head) { _head = head; } inline float probability() { return _probability.get(); } inline void set_probability(float p) { _probability.set(p); } @@ -59,8 +57,8 @@ public: private: Raul::DoubleBuffer<float> _probability; - WeakPtr<Node> _tail; - SharedPtr<Node> _head; + WPtr<Node> _tail; + SPtr<Node> _head; }; } // namespace Machina diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index b103310..e151cae 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -28,43 +28,43 @@ namespace Machina { -Engine::Engine(Raul::Forge& forge, - SharedPtr<Driver> driver, - Sord::World& rdf_world) +Engine::Engine(Raul::Forge& forge, + SPtr<Driver> driver, + Sord::World& rdf_world) : _driver(driver) , _rdf_world(rdf_world) , _loader(_forge, _rdf_world) , _forge(forge) {} -SharedPtr<Driver> +SPtr<Driver> Engine::new_driver(Raul::Forge& forge, const std::string& name, - SharedPtr<Machine> machine) + SPtr<Machine> machine) { #ifdef HAVE_JACK if (name == "jack") { JackDriver* driver = new JackDriver(forge, machine); driver->attach("machina"); - return SharedPtr<Driver>(driver); + return SPtr<Driver>(driver); } #endif if (name == "smf") { - return SharedPtr<Driver>(new SMFDriver(forge, machine->time().unit())); + return SPtr<Driver>(new SMFDriver(forge, machine->time().unit())); } std::cerr << "Error: Unknown driver type `" << name << "'" << std::endl; - return SharedPtr<Driver>(); + return SPtr<Driver>(); } /** Load the machine at @a uri, and run it (replacing current machine). * Safe to call while engine is processing. */ -SharedPtr<Machine> +SPtr<Machine> Engine::load_machine(const Glib::ustring& uri) { - SharedPtr<Machine> machine = _loader.load(uri); - SharedPtr<Machine> old_machine; + SPtr<Machine> machine = _loader.load(uri); + SPtr<Machine> old_machine; if (machine) { old_machine = _driver->machine(); // Keep a reference to old machine... machine->activate(); @@ -79,13 +79,14 @@ Engine::load_machine(const Glib::ustring& uri) /** Build a machine from the MIDI at @a uri, and run it (replacing current machine). * Safe to call while engine is processing. */ -SharedPtr<Machine> -Engine::load_machine_midi(const Glib::ustring& uri, double q, - Raul::TimeDuration dur) +SPtr<Machine> +Engine::load_machine_midi(const Glib::ustring& uri, + double q, + Raul::TimeDuration dur) { - SharedPtr<SMFDriver> file_driver(new SMFDriver(_forge, dur.unit())); - SharedPtr<Machine> machine = file_driver->learn(uri, q, dur); - SharedPtr<Machine> old_machine; + SPtr<SMFDriver> file_driver(new SMFDriver(_forge, dur.unit())); + SPtr<Machine> machine = file_driver->learn(uri, q, dur); + SPtr<Machine> old_machine; if (machine) { old_machine = _driver->machine(); // Keep a reference to old machine... machine->activate(); @@ -98,11 +99,10 @@ Engine::load_machine_midi(const Glib::ustring& uri, double q, } void -Engine::import_machine(SharedPtr<Machine> machine) +Engine::import_machine(SPtr<Machine> machine) { machine->activate(); - _driver->machine()->nodes().insert(_driver->machine()->nodes().end(), - machine->nodes().begin(), + _driver->machine()->nodes().insert(machine->nodes().begin(), machine->nodes().end()); // FIXME: thread safe? // FIXME: announce @@ -111,8 +111,8 @@ Engine::import_machine(SharedPtr<Machine> machine) void Engine::export_midi(const Glib::ustring& filename, Raul::TimeDuration dur) { - SharedPtr<Machine> machine = _driver->machine(); - SharedPtr<Machina::SMFDriver> file_driver( + SPtr<Machine> machine = _driver->machine(); + SPtr<Machina::SMFDriver> file_driver( new Machina::SMFDriver(_forge, dur.unit())); const bool activated = _driver->is_activated(); diff --git a/src/engine/Evolver.cpp b/src/engine/Evolver.cpp index a71d64b..56b3b45 100644 --- a/src/engine/Evolver.cpp +++ b/src/engine/Evolver.cpp @@ -32,12 +32,13 @@ using namespace boost; namespace Machina { -Evolver::Evolver(TimeUnit unit, const string& target_midi, - SharedPtr<Machine> seed) +Evolver::Evolver(TimeUnit unit, + const string& target_midi, + SPtr<Machine> seed) : _problem(new Problem(unit, target_midi, seed)) , _seed_fitness(-FLT_MAX) { - SharedPtr<Eugene::HybridMutation<Machine> > m(new HybridMutation<Machine>()); + SPtr<Eugene::HybridMutation<Machine> > m(new HybridMutation<Machine>()); m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( new Mutation::Compress())); @@ -59,14 +60,14 @@ Evolver::Evolver(TimeUnit unit, const string& target_midi, boost::shared_ptr< Selection<Machine> > s(new TournamentSelection<Machine>( _problem, 3, 0.8)); boost::shared_ptr< Crossover<Machine> > crossover; - _ga = SharedPtr<MachinaGA>(new MachinaGA(_problem, s, crossover, m, - 20, 20, 2, 1.0, 0.0)); + _ga = SPtr<MachinaGA>( + new MachinaGA(_problem, s, crossover, m, 20, 20, 2, 1.0, 0.0)); } void -Evolver::seed(SharedPtr<Machine> parent) +Evolver::seed(SPtr<Machine> parent) { - /*_best = SharedPtr<Machine>(new Machine(*parent.get())); + /*_best = SPtr<Machine>(new Machine(*parent.get())); _best_fitness = _problem->fitness(*_best.get());*/ _problem->seed(parent); _seed_fitness = _problem->fitness(*parent.get()); diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index d13bbdd..d8442b1 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -32,7 +32,7 @@ using namespace std; namespace Machina { -JackDriver::JackDriver(Raul::Forge& forge, SharedPtr<Machine> machine) +JackDriver::JackDriver(Raul::Forge& forge, SPtr<Machine> machine) : Driver(forge, machine) , _client(NULL) , _machine_changed(0) @@ -107,9 +107,9 @@ JackDriver::attach(const std::string& client_name) } if (!_machine) { - _machine = SharedPtr<Machine>( - new Machine(TimeUnit::frames(jack_get_sample_rate( - jack_client())))); + _machine = SPtr<Machine>( + new Machine(TimeUnit::frames( + jack_get_sample_rate(jack_client())))); } _machine->activate(); @@ -160,13 +160,13 @@ JackDriver::deactivate() } void -JackDriver::set_machine(SharedPtr<Machine> machine) +JackDriver::set_machine(SPtr<Machine> machine) { if (machine == _machine) { return; } - SharedPtr<Machine> last_machine = _last_machine; // Keep a reference + SPtr<Machine> last_machine = _last_machine; // Keep a reference _machine_changed.reset(0); assert(!last_machine.unique()); _machine = machine; @@ -178,7 +178,7 @@ JackDriver::set_machine(SharedPtr<Machine> machine) } void -JackDriver::read_input_recording(SharedPtr<Machine> machine, +JackDriver::read_input_recording(SPtr<Machine> machine, const Raul::TimeSlice& time) { const jack_nframes_t nframes = time.length_ticks().ticks(); @@ -203,7 +203,7 @@ JackDriver::read_input_recording(SharedPtr<Machine> machine, } void -JackDriver::read_input_playing(SharedPtr<Machine> machine, +JackDriver::read_input_playing(SPtr<Machine> machine, const Raul::TimeSlice& time) { const jack_nframes_t nframes = time.length_ticks().ticks(); @@ -215,7 +215,7 @@ JackDriver::read_input_playing(SharedPtr<Machine> machine, jack_midi_event_get(&ev, buf, i); if (ev.buffer[0] == 0x90) { - const SharedPtr<LearnRequest> learn = machine->pending_learn(); + const SPtr<LearnRequest> learn = machine->pending_learn(); if (learn) { learn->enter_action()->set_event(ev.size, ev.buffer); learn->start(_quantization.get(), @@ -225,7 +225,7 @@ JackDriver::read_input_playing(SharedPtr<Machine> machine, } } else if (ev.buffer[0] == 0x80) { - const SharedPtr<LearnRequest> learn = machine->pending_learn(); + const SPtr<LearnRequest> learn = machine->pending_learn(); if (learn && learn->started()) { learn->exit_action()->set_event(ev.size, ev.buffer); learn->finish( @@ -323,7 +323,7 @@ JackDriver::on_process(jack_nframes_t nframes) /* Take a reference to machine here and use only it during the process * cycle so _machine can be switched with set_machine during a cycle. */ - SharedPtr<Machine> machine = _machine; + SPtr<Machine> machine = _machine; // Machine was switched since last cycle, finalize old machine. if (machine != _last_machine) { @@ -411,9 +411,8 @@ void JackDriver::start_record(bool step) { // FIXME: Choose an appropriate maximum ringbuffer size - _recorder - = SharedPtr<Recorder>(new Recorder(_forge, 1024, _beats_unit, - _quantization.get(), step)); + _recorder = SPtr<Recorder>( + new Recorder(_forge, 1024, _beats_unit, _quantization.get(), step)); _recorder->start(); _record_dur = 0; _recording = true; @@ -423,12 +422,10 @@ void JackDriver::finish_record() { _recording = false; - SharedPtr<Machine> machine = _recorder->finish(); + SPtr<Machine> machine = _recorder->finish(); _recorder.reset(); machine->activate(); - _machine->nodes().insert(_machine->nodes().end(), - machine->nodes().begin(), - machine->nodes().end()); + _machine->nodes().insert(machine->nodes().begin(), machine->nodes().end()); } int diff --git a/src/engine/JackDriver.hpp b/src/engine/JackDriver.hpp index c0f94ff..06ffabe 100644 --- a/src/engine/JackDriver.hpp +++ b/src/engine/JackDriver.hpp @@ -25,11 +25,11 @@ #include "raul/DoubleBuffer.hpp" #include "raul/Semaphore.hpp" -#include "raul/SharedPtr.hpp" #include "machina/Context.hpp" #include "machina/Driver.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Recorder.hpp" @@ -47,8 +47,8 @@ class JackDriver : public Machina::Driver { public: - JackDriver(Raul::Forge& forge, - SharedPtr<Machine> machine = SharedPtr<Machine>()); + JackDriver(Raul::Forge& forge, + SPtr<Machine> machine = SPtr<Machine>()); ~JackDriver(); void attach(const std::string& client_name); @@ -57,7 +57,7 @@ public: void activate(); void deactivate(); - void set_machine(SharedPtr<Machine> machine); + void set_machine(SPtr<Machine> machine); void write_event(Raul::TimeStamp time, size_t size, @@ -90,10 +90,10 @@ public: jack_client_t* jack_client() const { return _client; } private: - void read_input_recording(SharedPtr<Machine> machine, + void read_input_recording(SPtr<Machine> machine, const Raul::TimeSlice& time); - void read_input_playing(SharedPtr<Machine> machine, + void read_input_playing(SPtr<Machine> machine, const Raul::TimeSlice& time); static void jack_error_cb(const char* msg); @@ -104,8 +104,8 @@ private: jack_client_t* _client; - Raul::Semaphore _machine_changed; - SharedPtr<Machine> _last_machine; + Raul::Semaphore _machine_changed; + SPtr<Machine> _last_machine; jack_port_t* _input_port; jack_port_t* _output_port; @@ -121,10 +121,10 @@ private: Raul::Semaphore _stop; bool _stop_flag; - Raul::TimeDuration _record_dur; - std::atomic<bool> _recording; - SharedPtr<Recorder> _recorder; - bool _is_activated; + Raul::TimeDuration _record_dur; + std::atomic<bool> _recording; + SPtr<Recorder> _recorder; + bool _is_activated; }; } // namespace Machina diff --git a/src/engine/LearnRequest.cpp b/src/engine/LearnRequest.cpp index 81ef0e3..acf5bf0 100644 --- a/src/engine/LearnRequest.cpp +++ b/src/engine/LearnRequest.cpp @@ -20,7 +20,7 @@ namespace Machina { -LearnRequest::LearnRequest(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node) +LearnRequest::LearnRequest(SPtr<Raul::Maid> maid, SPtr<Node> node) : _started(false) , _start_time(TimeUnit(TimeUnit::BEATS, 19200), 0, 0) // irrelevant , _quantization(0) // irrelevant @@ -32,10 +32,10 @@ LearnRequest::LearnRequest(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node) maid->manage(_exit_action); } -SharedPtr<LearnRequest> -LearnRequest::create(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node) +SPtr<LearnRequest> +LearnRequest::create(SPtr<Raul::Maid> maid, SPtr<Node> node) { - SharedPtr<LearnRequest> ret(new LearnRequest(maid, node)); + SPtr<LearnRequest> ret(new LearnRequest(maid, node)); maid->manage(ret); return ret; } diff --git a/src/engine/LearnRequest.hpp b/src/engine/LearnRequest.hpp index af28017..4093d24 100644 --- a/src/engine/LearnRequest.hpp +++ b/src/engine/LearnRequest.hpp @@ -19,8 +19,6 @@ #define MACHINA_LEARNREQUEST_HPP #include "raul/Maid.hpp" -#include "raul/SharedPtr.hpp" -#include "raul/Manageable.hpp" #include "machina/types.hpp" @@ -34,31 +32,30 @@ class MidiAction; /** A request to MIDI learn a certain node. */ -class LearnRequest - : public Raul::Manageable +class LearnRequest : public Raul::Maid::Manageable { public: - static SharedPtr<LearnRequest>create(SharedPtr<Raul::Maid> maid, - SharedPtr<Node> node); + static SPtr<LearnRequest>create(SPtr<Raul::Maid> maid, + SPtr<Node> node); void start(double q, Raul::TimeStamp time); void finish(TimeStamp time); inline bool started() const { return _started; } - const SharedPtr<Node>& node() { return _node; } - const SharedPtr<MidiAction>& enter_action() { return _enter_action; } - const SharedPtr<MidiAction>& exit_action() { return _exit_action; } + const SPtr<Node>& node() { return _node; } + const SPtr<MidiAction>& enter_action() { return _enter_action; } + const SPtr<MidiAction>& exit_action() { return _exit_action; } private: - LearnRequest(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node); + LearnRequest(SPtr<Raul::Maid> maid, SPtr<Node> node); bool _started; TimeStamp _start_time; double _quantization; - SharedPtr<Node> _node; - SharedPtr<MidiAction> _enter_action; - SharedPtr<MidiAction> _exit_action; + SPtr<Node> _node; + SPtr<MidiAction> _enter_action; + SPtr<MidiAction> _exit_action; }; } // namespace Machina diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp index 32e8d19..c206c4c 100644 --- a/src/engine/Loader.cpp +++ b/src/engine/Loader.cpp @@ -50,12 +50,12 @@ Loader::Loader(Raul::Forge& forge, Sord::World& rdf_world) * @param uri URI of machine (resolvable URI to an RDF document). * @return Loaded Machine. */ -SharedPtr<Machine> +SPtr<Machine> Loader::load(const Glib::ustring& uri) { using Glib::ustring; - SharedPtr<Machine> machine; + SPtr<Machine> machine; ustring document_uri = uri; @@ -68,10 +68,10 @@ Loader::load(const Glib::ustring& uri) cout << "[Loader] Loading " << document_uri << endl; - machine = SharedPtr<Machine>( + machine = SPtr<Machine>( new Machine(TimeUnit::beats(MACHINA_PPQN))); - typedef std::map<Sord::Node, SharedPtr<Node> > Created; + typedef std::map<Sord::Node, SPtr<Node> > Created; Created created; Sord::Model model(_rdf_world, document_uri); @@ -98,11 +98,10 @@ Loader::load(const Glib::ustring& uri) !i.end(); ++i) { const Sord::Node& node_id = i.get_object(); Sord::Iter d = model.find(node_id, machina_duration, nil); - SharedPtr<Node> node( - new Node( - TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), - d.get_object().to_float()), - true)); + SPtr<Node> node( + new Node(TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), + d.get_object().to_float()), + true)); Sord::Iter s = model.find(node_id, rdf_type, machina_SelectorNode); if (!s.end()) { @@ -119,10 +118,10 @@ Loader::load(const Glib::ustring& uri) const Sord::Node& node_id = i.get_object(); Sord::Iter d = model.find(node_id, machina_duration, nil); if (created.find(node_id) == created.end()) { - SharedPtr<Node> node(new Node( - TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), - d.get_object().to_float()), - false)); + SPtr<Node> node( + new Node(TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), + d.get_object().to_float()), + false)); machine->add_node(node); created[node_id] = node; } @@ -143,8 +142,8 @@ Loader::load(const Glib::ustring& uri) Created::iterator node_i = created.find((const char*)results->get("node")); if (node_i != created.end()) { - SharedPtr<Node> node = node_i->second; - const int note_num = results->get("note").to_int(); + SPtr<Node> node = node_i->second; + const int note_num = results->get("note").to_int(); if (note_num >= 0 && note_num <= 127) { node->set_enter_action( ActionFactory::note_on((uint8_t)note_num)); @@ -179,10 +178,10 @@ Loader::load(const Glib::ustring& uri) Created::iterator head_i = created.find(head); if (tail_i != created.end() && head_i != created.end()) { - const SharedPtr<Node> tail = tail_i->second; - const SharedPtr<Node> head = head_i->second; + const SPtr<Node> tail = tail_i->second; + const SPtr<Node> head = head_i->second; - SharedPtr<Edge> edge(new Edge(tail, head)); + SPtr<Edge> edge(new Edge(tail, head)); edge->set_probability(prob); tail->add_edge(edge); @@ -197,7 +196,7 @@ Loader::load(const Glib::ustring& uri) machine->reset(NULL, machine->time()); return machine; } else { - return SharedPtr<Machine>(); + return SPtr<Machine>(); } } diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index 4b548a7..73eba15 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -19,14 +19,14 @@ #include <map> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "sord/sordmm.hpp" +#include "machina/Context.hpp" #include "machina/Machine.hpp" -#include "machina/Updates.hpp" #include "machina/URIs.hpp" -#include "machina/Context.hpp" +#include "machina/Updates.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "Node.hpp" @@ -39,7 +39,7 @@ using namespace Raul; namespace Machina { Machine::Machine(TimeUnit unit) - : _active_nodes(MAX_ACTIVE_NODES, SharedPtr<Node>()) + : _active_nodes(MAX_ACTIVE_NODES, SPtr<Node>()) , _time(unit, 0, 0) , _is_activated(false) , _is_finished(false) @@ -52,17 +52,17 @@ Machine::Machine(TimeUnit unit) */ Machine::Machine(const Machine& copy) : Stateful() // don't copy RDF ID - , _active_nodes(MAX_ACTIVE_NODES, SharedPtr<Node>()) + , _active_nodes(MAX_ACTIVE_NODES, SPtr<Node>()) , _time(copy.time()) , _is_activated(false) , _is_finished(false) { - std::map< SharedPtr<Node>, SharedPtr<Node> > replacements; + std::map< SPtr<Node>, SPtr<Node> > replacements; for (Nodes::const_iterator n = copy._nodes.begin(); n != copy._nodes.end(); ++n) { - SharedPtr<Machina::Node> node(new Machina::Node(*n->get())); - _nodes.push_back(node); + SPtr<Machina::Node> node(new Machina::Node(*n->get())); + _nodes.insert(node); replacements[*n] = node; } @@ -79,20 +79,20 @@ Machine::Machine(const Machine& copy) Machine& Machine::operator=(const Machine& other) { - _active_nodes = std::vector< SharedPtr<Node> >(MAX_ACTIVE_NODES, - SharedPtr<Node>()); + _active_nodes = std::vector< SPtr<Node> >(MAX_ACTIVE_NODES, + SPtr<Node>()); _is_activated = false; _is_finished = false; _time = other._time; - _pending_learn = SharedPtr<LearnRequest>(); + _pending_learn = SPtr<LearnRequest>(); _nodes.clear(); - map< SharedPtr<Node>, SharedPtr<Node> > replacements; + map< SPtr<Node>, SPtr<Node> > replacements; for (Nodes::const_iterator n = other._nodes.begin(); n != other._nodes.end(); ++n) { - SharedPtr<Machina::Node> node(new Machina::Node(*n->get())); - _nodes.push_back(node); + SPtr<Machina::Node> node(new Machina::Node(*n->get())); + _nodes.insert(node); replacements[*n] = node; } @@ -109,11 +109,11 @@ Machine::operator=(const Machine& other) } /** Always returns a node, unless there are none */ -SharedPtr<Node> +SPtr<Node> Machine::random_node() { if (_nodes.empty()) { - return SharedPtr<Node>(); + return SPtr<Node>(); } size_t i = rand() % _nodes.size(); @@ -126,33 +126,32 @@ Machine::random_node() } } - return SharedPtr<Node>(); + return SPtr<Node>(); } /** May return NULL even if edges exist (with low probability) */ -SharedPtr<Edge> +SPtr<Edge> Machine::random_edge() { - SharedPtr<Node> tail = random_node(); + SPtr<Node> tail = random_node(); for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) { tail = random_node(); } - return tail ? tail->random_edge() : SharedPtr<Edge>(); + return tail ? tail->random_edge() : SPtr<Edge>(); } void -Machine::add_node(SharedPtr<Node> node) +Machine::add_node(SPtr<Node> node) { - assert(std::find(_nodes.begin(), _nodes.end(), node) == _nodes.end()); - _nodes.push_back(node); + _nodes.insert(node); } void -Machine::remove_node(SharedPtr<Node> node) +Machine::remove_node(SPtr<Node> node) { - _nodes.erase(std::find(_nodes.begin(), _nodes.end(), node)); + _nodes.erase(node); for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { (*n)->remove_edge_to(node); @@ -165,9 +164,8 @@ void Machine::reset(MIDISink* sink, Raul::TimeStamp time) { if (!_is_finished) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); - ++n) { - SharedPtr<Node> node = (*n); + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + SPtr<Node> node = (*n); if (node->is_active()) { node->exit(sink, time); @@ -187,13 +185,13 @@ Machine::reset(MIDISink* sink, Raul::TimeStamp time) /** Return the active Node with the earliest exit time. */ -SharedPtr<Node> +SPtr<Node> Machine::earliest_node() const { - SharedPtr<Node> earliest; + SPtr<Node> earliest; for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { - SharedPtr<Node> node = _active_nodes.at(i); + SPtr<Node> node = _active_nodes.at(i); if (node) { if (!node->is_active()) { @@ -214,9 +212,9 @@ Machine::earliest_node() const * Returns true if node was entered, or false if the maximum active nodes has been reached. */ bool -Machine::enter_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates) +Machine::enter_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates) { assert(!node->is_active()); @@ -245,9 +243,9 @@ Machine::enter_node(Context& context, /** Exit an active node at the current _time. */ void -Machine::exit_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates) +Machine::exit_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates) { node->exit(context.sink(), _time); write_set(updates, @@ -294,7 +292,7 @@ Machine::exit_node(Context& context, const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] if (rand_normal <= (*e)->probability()) { - SharedPtr<Node> head = (*e)->head(); + SPtr<Node> head = (*e)->head(); if (!head->is_active()) { enter_node(context, head, updates); @@ -315,7 +313,7 @@ Machine::exit_node(Context& context, * with sample accuracy if necessary). */ uint32_t -Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) +Machine::run(Context& context, SPtr<UpdateBuffer> updates) { if (_is_finished) { return 0; @@ -357,7 +355,7 @@ Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) while (true) { - SharedPtr<Node> earliest = earliest_node(); + SPtr<Node> earliest = earliest_node(); if (!earliest) { // No more active states, machine is finished @@ -393,7 +391,7 @@ Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) * NOT realtime (actions are allocated here). */ void -Machine::learn(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node) +Machine::learn(SPtr<Raul::Maid> maid, SPtr<Node> node) { _pending_learn = LearnRequest::create(maid, node); } diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index 0a6dbfc..57d6c2f 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -19,9 +19,8 @@ #include "lv2/lv2plug.in/ns/ext/midi/midi.h" -#include "raul/SharedPtr.hpp" - #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "MachineBuilder.hpp" @@ -34,7 +33,7 @@ using namespace Raul; namespace Machina { -MachineBuilder::MachineBuilder(SharedPtr<Machine> machine, double q, bool step) +MachineBuilder::MachineBuilder(SPtr<Machine> machine, double q, bool step) : _quantization(q) , _time(machine->time().unit()) // = 0 , _machine(machine) @@ -48,13 +47,13 @@ void MachineBuilder::reset() { _time = TimeStamp(_machine->time().unit()); // = 0 - _initial_node = SharedPtr<Node>(new Node(_time, true)); // duration 0 + _initial_node = SPtr<Node>(new Node(_time, true)); // duration 0 _connect_node = _initial_node; _connect_node_end_time = _time; // = 0 } bool -MachineBuilder::is_delay_node(SharedPtr<Node> node) const +MachineBuilder::is_delay_node(SPtr<Node> node) const { return !node->enter_action() && !node->exit_action(); } @@ -62,7 +61,7 @@ MachineBuilder::is_delay_node(SharedPtr<Node> node) const /** Set the duration of a node, with quantization. */ void -MachineBuilder::set_node_duration(SharedPtr<Node> node, +MachineBuilder::set_node_duration(SPtr<Node> node, Raul::TimeDuration d) const { if (_step) { @@ -83,31 +82,31 @@ MachineBuilder::set_node_duration(SharedPtr<Node> node, * * If a delay node is added to the machine, it is returned. */ -SharedPtr<Node> -MachineBuilder::connect_nodes(SharedPtr<Machine> m, - SharedPtr<Node> tail, - Raul::TimeStamp tail_end_time, - SharedPtr<Node> head, - Raul::TimeStamp head_start_time) +SPtr<Node> +MachineBuilder::connect_nodes(SPtr<Machine> m, + SPtr<Node> tail, + Raul::TimeStamp tail_end_time, + SPtr<Node> head, + Raul::TimeStamp head_start_time) { assert(tail != head); assert(head_start_time >= tail_end_time); - SharedPtr<Node> delay_node; + SPtr<Node> delay_node; 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_edge(SharedPtr<Edge>(new Edge(tail, head))); + tail->add_edge(SPtr<Edge>(new Edge(tail, head))); } else if (_step || (head_start_time == tail_end_time)) { // Connect directly - tail->add_edge(SharedPtr<Edge>(new Edge(tail, head))); + tail->add_edge(SPtr<Edge>(new Edge(tail, head))); } else { // Need to actually create a delay node - delay_node = SharedPtr<Node>(new Node(head_start_time - tail_end_time)); - tail->add_edge(SharedPtr<Edge>(new Edge(tail, delay_node))); - delay_node->add_edge(SharedPtr<Edge>(new Edge(delay_node, head))); + delay_node = SPtr<Node>(new Node(head_start_time - tail_end_time)); + tail->add_edge(SPtr<Edge>(new Edge(tail, delay_node))); + delay_node->add_edge(SPtr<Edge>(new Edge(delay_node, head))); m->add_node(delay_node); } @@ -117,10 +116,10 @@ MachineBuilder::connect_nodes(SharedPtr<Machine> m, void MachineBuilder::note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf) { - SharedPtr<Node> node(new Node(TimeStamp(t.unit()))); - node->set_enter_action(SharedPtr<Action>(new MidiAction(ev_size, buf))); + SPtr<Node> node(new Node(TimeStamp(t.unit()))); + node->set_enter_action(SPtr<Action>(new MidiAction(ev_size, buf))); - SharedPtr<Node> this_connect_node; + SPtr<Node> this_connect_node; Raul::TimeStamp this_connect_node_end_time(t.unit()); /* If currently polyphonic, use a poly node with no successors as connect @@ -143,7 +142,7 @@ MachineBuilder::note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf) this_connect_node_end_time = _connect_node_end_time; } - SharedPtr<Node> delay_node = connect_nodes( + SPtr<Node> delay_node = connect_nodes( _machine, this_connect_node, this_connect_node_end_time, node, t); if (delay_node) { @@ -159,10 +158,10 @@ void MachineBuilder::resolve_note(Raul::TimeStamp t, size_t ev_size, uint8_t* buf, - SharedPtr<Node> resolved) + SPtr<Node> resolved) { resolved->set_exit_action( - SharedPtr<Action>(new MidiAction(ev_size, buf))); + SPtr<Action>(new MidiAction(ev_size, buf))); set_node_duration(resolved, t - resolved->enter_time()); if (_active_nodes.size() == 1) { @@ -171,7 +170,7 @@ MachineBuilder::resolve_note(Raul::TimeStamp t, if (!_poly_nodes.empty()) { // Finish a polyphonic section - _connect_node = SharedPtr<Node>(new Node(TimeStamp(t.unit()))); + _connect_node = SPtr<Node>(new Node(TimeStamp(t.unit()))); _machine->add_node(_connect_node); connect_nodes(_machine, resolved, t, _connect_node, t); @@ -201,8 +200,8 @@ MachineBuilder::resolve_note(Raul::TimeStamp t, assert(_connect_node->edges().empty()); _connect_node->set_enter_action(resolved->enter_action()); _connect_node->set_exit_action(resolved->exit_action()); - resolved->set_enter_action(SharedPtr<Action>()); - resolved->set_exit_action(SharedPtr<Action>()); + resolved->set_enter_action(SPtr<Action>()); + resolved->set_exit_action(SPtr<Action>()); set_node_duration(_connect_node, resolved->duration()); resolved = _connect_node; if (std::find(_machine->nodes().begin(), @@ -246,8 +245,7 @@ MachineBuilder::event(Raul::TimeStamp time_offset, } else if ((buf[0] & 0xF0) == LV2_MIDI_MSG_NOTE_OFF) { for (ActiveList::iterator i = _active_nodes.begin(); i != _active_nodes.end(); ++i) { - SharedPtr<MidiAction> action = PtrCast<MidiAction>( - (*i)->enter_action()); + SPtr<MidiAction> action = dynamic_ptr_cast<MidiAction>((*i)->enter_action()); if (!action) { continue; } @@ -275,10 +273,10 @@ MachineBuilder::resolve() { // Resolve stuck notes if (!_active_nodes.empty()) { - for (list<SharedPtr<Node> >::iterator i = _active_nodes.begin(); + for (list<SPtr<Node> >::iterator i = _active_nodes.begin(); i != _active_nodes.end(); ++i) { cerr << "WARNING: Resolving stuck note." << endl; - SharedPtr<MidiAction> action = PtrCast<MidiAction>( + SPtr<MidiAction> action = dynamic_ptr_cast<MidiAction>( (*i)->enter_action()); if (!action) { continue; @@ -293,7 +291,7 @@ MachineBuilder::resolve() 0x40 }; (*i)->set_exit_action( - SharedPtr<Action>(new MidiAction(3, note_off))); + SPtr<Action>(new MidiAction(3, note_off))); set_node_duration((*i), _time - (*i)->enter_time()); (*i)->exit(NULL, _time); _machine->add_node((*i)); @@ -312,7 +310,7 @@ MachineBuilder::resolve() } -SharedPtr<Machine> +SPtr<Machine> MachineBuilder::finish() { resolve(); diff --git a/src/engine/MachineBuilder.hpp b/src/engine/MachineBuilder.hpp index cb3ccea..4a915aa 100644 --- a/src/engine/MachineBuilder.hpp +++ b/src/engine/MachineBuilder.hpp @@ -22,7 +22,7 @@ #include <list> -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "raul/TimeStamp.hpp" namespace Machina { @@ -33,9 +33,9 @@ class Node; class MachineBuilder { public: - MachineBuilder(SharedPtr<Machine> machine, - double quantization, - bool step); + MachineBuilder(SPtr<Machine> machine, + double quantization, + bool step); void set_time(Raul::TimeStamp time) { _time = time; } @@ -44,38 +44,38 @@ public: void reset(); void resolve(); - SharedPtr<Machine> finish(); + SPtr<Machine> finish(); private: - bool is_delay_node(SharedPtr<Node> node) const; - void set_node_duration(SharedPtr<Node> node, Raul::TimeDuration d) const; + bool is_delay_node(SPtr<Node> node) const; + void set_node_duration(SPtr<Node> node, Raul::TimeDuration d) const; void note_on(Raul::TimeStamp t, size_t ev_size, uint8_t* buf); void resolve_note(Raul::TimeStamp t, size_t ev_size, uint8_t* buf, - SharedPtr<Node> resolved); + SPtr<Node> resolved); - SharedPtr<Node>connect_nodes(SharedPtr<Machine> m, - SharedPtr<Node> tail, - Raul::TimeStamp tail_end_time, - SharedPtr<Node> head, - Raul::TimeStamp head_start_time); + SPtr<Node>connect_nodes(SPtr<Machine> m, + SPtr<Node> tail, + Raul::TimeStamp tail_end_time, + SPtr<Node> head, + Raul::TimeStamp head_start_time); - typedef std::list<SharedPtr<Node> > ActiveList; + typedef std::list<SPtr<Node> > ActiveList; ActiveList _active_nodes; - typedef std::list<std::pair<Raul::TimeStamp, SharedPtr<Node> > > PolyList; + typedef std::list<std::pair<Raul::TimeStamp, SPtr<Node> > > PolyList; PolyList _poly_nodes; double _quantization; Raul::TimeStamp _time; - SharedPtr<Machine> _machine; - SharedPtr<Node> _initial_node; - SharedPtr<Node> _connect_node; - Raul::TimeStamp _connect_node_end_time; + SPtr<Machine> _machine; + SPtr<Node> _initial_node; + SPtr<Node> _connect_node; + Raul::TimeStamp _connect_node_end_time; bool _step; }; diff --git a/src/engine/MidiAction.cpp b/src/engine/MidiAction.cpp index 268279f..cac01a3 100644 --- a/src/engine/MidiAction.cpp +++ b/src/engine/MidiAction.cpp @@ -17,7 +17,6 @@ #include <iostream> -#include "raul/SharedPtr.hpp" #include "raul/Atom.hpp" #include "machina/types.hpp" diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp index e25cdbb..fde1ba6 100644 --- a/src/engine/Mutation.cpp +++ b/src/engine/Mutation.cpp @@ -55,17 +55,17 @@ AddNode::mutate(Machine& machine) //cout << "ADD NODE" << endl; // Create random node - SharedPtr<Node> node(new Node(machine.time().unit())); + SPtr<Node> node(new Node(machine.time().unit())); node->set_selector(true); - SharedPtr<Node> note_node = machine.random_node(); + SPtr<Node> note_node = machine.random_node(); if (!note_node) { return; } uint8_t note = rand() % 128; - SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>( + SPtr<MidiAction> enter_action = dynamic_ptr_cast<MidiAction>( note_node->enter_action()); if (enter_action) { note = enter_action->event()[1]; @@ -76,15 +76,15 @@ AddNode::mutate(Machine& machine) machine.add_node(node); // Insert after some node - SharedPtr<Node> tail = machine.random_node(); + SPtr<Node> tail = machine.random_node(); if (tail && (tail != node) /* && !node->connected_to(tail)*/) { - tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, node))); + tail->add_edge(SPtr<Edge>(new Edge(tail, node))); } // Insert before some other node - SharedPtr<Node> head = machine.random_node(); + SPtr<Node> head = machine.random_node(); if (head && (head != node) /* && !head->connected_to(node)*/) { - node->add_edge(boost::shared_ptr<Edge>(new Edge(node, head))); + node->add_edge(SPtr<Edge>(new Edge(node, head))); } } @@ -93,7 +93,7 @@ RemoveNode::mutate(Machine& machine) { //cout << "REMOVE NODE" << endl; - SharedPtr<Node> node = machine.random_node(); + SPtr<Node> node = machine.random_node(); if (node && !node->is_initial()) { machine.remove_node(node); } @@ -104,11 +104,11 @@ AdjustNode::mutate(Machine& machine) { //cout << "ADJUST NODE" << endl; - SharedPtr<Node> node = machine.random_node(); + SPtr<Node> node = machine.random_node(); if (node) { - SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>( + SPtr<MidiAction> enter_action = dynamic_ptr_cast<MidiAction>( node->enter_action()); - SharedPtr<MidiAction> exit_action = PtrCast<MidiAction>( + SPtr<MidiAction> exit_action = dynamic_ptr_cast<MidiAction>( node->exit_action()); if (enter_action && exit_action) { const uint8_t note = rand() % 128; @@ -128,16 +128,16 @@ SwapNodes::mutate(Machine& machine) return; } - SharedPtr<Node> a = machine.random_node(); - SharedPtr<Node> b = machine.random_node(); + SPtr<Node> a = machine.random_node(); + SPtr<Node> b = machine.random_node(); while (b == a) { b = machine.random_node(); } - SharedPtr<MidiAction> a_enter = PtrCast<MidiAction>(a->enter_action()); - SharedPtr<MidiAction> a_exit = PtrCast<MidiAction>(a->exit_action()); - SharedPtr<MidiAction> b_enter = PtrCast<MidiAction>(b->enter_action()); - SharedPtr<MidiAction> b_exit = PtrCast<MidiAction>(b->exit_action()); + SPtr<MidiAction> a_enter = dynamic_ptr_cast<MidiAction>(a->enter_action()); + SPtr<MidiAction> a_exit = dynamic_ptr_cast<MidiAction>(a->exit_action()); + SPtr<MidiAction> b_enter = dynamic_ptr_cast<MidiAction>(b->enter_action()); + SPtr<MidiAction> b_exit = dynamic_ptr_cast<MidiAction>(b->exit_action()); uint8_t note_a = a_enter->event()[1]; uint8_t note_b = b_enter->event()[1]; @@ -153,14 +153,14 @@ AddEdge::mutate(Machine& machine) { //cout << "ADJUST EDGE" << endl; - SharedPtr<Node> tail = machine.random_node(); - SharedPtr<Node> head = machine.random_node(); + SPtr<Node> tail = machine.random_node(); + SPtr<Node> head = machine.random_node(); if (tail && head && tail != head) { // && !tail->connected_to(head) && !head->connected_to(tail) - SharedPtr<Edge> edge(new Edge(tail, head)); + SPtr<Edge> edge(new Edge(tail, head)); edge->set_probability(rand() / (float)RAND_MAX); - tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, head))); + tail->add_edge(SPtr<Edge>(new Edge(tail, head))); } } @@ -169,7 +169,7 @@ RemoveEdge::mutate(Machine& machine) { //cout << "REMOVE EDGE" << endl; - SharedPtr<Node> tail = machine.random_node(); + SPtr<Node> tail = machine.random_node(); if (tail && !(tail->is_initial() && tail->edges().size() == 1)) { tail->remove_edge(tail->random_edge()); } @@ -180,7 +180,7 @@ AdjustEdge::mutate(Machine& machine) { //cout << "ADJUST EDGE" << endl; - SharedPtr<Edge> edge = machine.random_edge(); + SPtr<Edge> edge = machine.random_edge(); if (edge) { edge->set_probability(rand() / (float)RAND_MAX); edge->tail().lock()->edges_changed(); diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp index 4de00d2..d8d89b9 100644 --- a/src/engine/Node.cpp +++ b/src/engine/Node.cpp @@ -53,16 +53,16 @@ Node::Node(const Node& copy) { for (Edges::const_iterator i = copy._edges.begin(); i != copy._edges.end(); ++i) { - SharedPtr<Edge> edge(new Edge(*i->get())); - _edges.push_back(edge); + SPtr<Edge> edge(new Edge(*i->get())); + _edges.insert(edge); } } /** Always returns an edge, unless there are none */ -SharedPtr<Edge> +SPtr<Edge> Node::random_edge() { - SharedPtr<Edge> ret; + SPtr<Edge> ret; if (_edges.empty()) { return ret; } @@ -115,14 +115,14 @@ Node::set_selector(bool yn) } void -Node::set_enter_action(SharedPtr<Action> action) +Node::set_enter_action(SPtr<Action> action) { _enter_action = action; _changed = true; } void -Node::set_exit_action(SharedPtr<Action> action) +Node::set_exit_action(SPtr<Action> action) { _exit_action = action; _changed = true; @@ -156,52 +156,52 @@ Node::exit(MIDISink* sink, TimeStamp time) _enter_time = 0; } -void -Node::add_edge(SharedPtr<Edge> edge) +SPtr<Edge> +Node::edge_to(SPtr<Node> head) const { - assert(edge->tail().lock().get() == this); + // TODO: Make logarithmic for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == edge->head()) { - return; + if ((*i)->head() == head) { + return *i; } } + return SPtr<Edge>(); +} + +void +Node::add_edge(SPtr<Edge> edge) +{ + assert(edge->tail().lock().get() == this); + if (edge_to(edge->head())) { + return; + } - _edges.push_back(edge); + _edges.insert(edge); edges_changed(); } void -Node::remove_edge(SharedPtr<Edge> edge) +Node::remove_edge(SPtr<Edge> edge) { - _edges.erase(std::find(_edges.begin(), _edges.end(), edge)); + _edges.erase(edge); edges_changed(); } bool -Node::connected_to(SharedPtr<Node> node) +Node::connected_to(SPtr<Node> node) { - for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == node) { - return true; - } - } - - return false; + return bool(edge_to(node)); } -SharedPtr<Edge> -Node::remove_edge_to(SharedPtr<Node> node) +SPtr<Edge> +Node::remove_edge_to(SPtr<Node> node) { - for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) { - if ((*i)->head() == node) { - SharedPtr<Edge> edge(*i); - _edges.erase(i); - edges_changed(); - return edge; - } + SPtr<Edge> edge = edge_to(node); + if (edge) { + _edges.erase(edge); // TODO: avoid double search + edges_changed(); } - - return SharedPtr<Edge>(); + return edge; } void diff --git a/src/engine/Node.hpp b/src/engine/Node.hpp index cf41b26..953f652 100644 --- a/src/engine/Node.hpp +++ b/src/engine/Node.hpp @@ -18,9 +18,9 @@ #ifndef MACHINA_NODE_HPP #define MACHINA_NODE_HPP -#include <list> +#include <set> -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "Action.hpp" #include "Schrodinbit.hpp" @@ -36,34 +36,33 @@ using Raul::TimeUnit; /** A node is a state (as in a FSM diagram), or "note". * - * It contains a action, as well as a duration and pointers to it's + * It contains a action, as well as a duration and pointers to its * successors (states/nodes that (may) follow it). * * Initial nodes do not have enter actions (since they are entered at * an undefined point in time <= 0). */ -class Node - : public Stateful +class Node : public Stateful { public: Node(TimeDuration duration, bool initial = false); Node(const Node& copy); - void set_enter_action(SharedPtr<Action> action); - void set_exit_action(SharedPtr<Action> action); + void set_enter_action(SPtr<Action> action); + void set_exit_action(SPtr<Action> action); - SharedPtr<Action> enter_action() { return _enter_action; } - SharedPtr<Action> exit_action() { return _exit_action; } + SPtr<Action> enter_action() { return _enter_action; } + SPtr<Action> exit_action() { return _exit_action; } void enter(MIDISink* sink, TimeStamp time); void exit(MIDISink* sink, TimeStamp time); void edges_changed(); - void add_edge(SharedPtr<Edge> edge); - void remove_edge(SharedPtr<Edge> edge); - SharedPtr<Edge> remove_edge_to(SharedPtr<Node> node); - bool connected_to(SharedPtr<Node> node); + void add_edge(SPtr<Edge> edge); + void remove_edge(SPtr<Edge> edge); + SPtr<Edge> remove_edge_to(SPtr<Node> node); + bool connected_to(SPtr<Node> node); void set(URIInt key, const Raul::Atom& value); void write_state(Sord::Model& model); @@ -81,23 +80,32 @@ public: inline bool changed() { return _changed; } inline void set_changed() { _changed = true; } - typedef std::list< SharedPtr<Edge> > Edges; + struct EdgeHeadOrder { + inline bool operator()(const SPtr<Edge>& a, const SPtr<Edge>& b) { + return a < b; + } + }; + + SPtr<Edge> edge_to(SPtr<Node> head) const; + + typedef std::set<SPtr<Edge>, EdgeHeadOrder> Edges; + Edges& edges() { return _edges; } - SharedPtr<Edge> random_edge(); + SPtr<Edge> random_edge(); private: Node& operator=(const Node& other); // undefined - TimeStamp _enter_time; ///< valid iff _is_active - TimeDuration _duration; - SharedPtr<Action> _enter_action; - SharedPtr<Action> _exit_action; - Edges _edges; - Schrodinbit _changed; - bool _is_initial; - bool _is_selector; - bool _is_active; + TimeStamp _enter_time; ///< valid iff _is_active + TimeDuration _duration; + SPtr<Action> _enter_action; + SPtr<Action> _exit_action; + Edges _edges; + Schrodinbit _changed; + bool _is_initial; + bool _is_selector; + bool _is_active; }; } // namespace Machina diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index 2db4f45..3540b2b 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -38,8 +38,9 @@ using namespace std; namespace Machina { -Problem::Problem(TimeUnit unit, const std::string& target_midi, - SharedPtr<Machine> seed) +Problem::Problem(TimeUnit unit, + const std::string& target_midi, + SPtr<Machine> seed) : _unit(unit) , _target(*this) , _seed(new Machine(*seed.get())) @@ -86,7 +87,7 @@ Problem::fitness(const Machine& const_machine) const return cached->second; } - SharedPtr<Evaluator> eval(new Evaluator(*this)); + SPtr<Evaluator> eval(new Evaluator(*this)); //machine.reset(); machine.deactivate(); @@ -230,11 +231,11 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const // FIXME: ignores _seed and builds based on MIDI // evolution of the visible machine would be nice.. - SharedPtr<Machine> base = SharedPtr<Machine>(new Machine(_unit)); + SPtr<Machine> base = SPtr<Machine>(new Machine(_unit)); for (uint8_t i = 0; i < 128; ++i) { if (_target._counts[i] > 0) { //cout << "Initial note: " << (int)i << endl; - SharedPtr<Node> node(new Node(TimeDuration(_unit, 1 / 2.0))); + SPtr<Node> node(new Node(TimeDuration(_unit, 1 / 2.0))); node->set_enter_action(ActionFactory::note_on(i)); node->set_exit_action(ActionFactory::note_off(i)); node->set_selector(true); @@ -246,12 +247,12 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const // FIXME: double copy Machine m(*base.get()); - set< SharedPtr<Node> > unreachable; - SharedPtr<Node> initial; + set< SPtr<Node> > unreachable; + SPtr<Node> initial; for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); ++i) { - if (PtrCast<MidiAction>((*i)->enter_action())->event()[1] == + if (dynamic_ptr_cast<MidiAction>((*i)->enter_action())->event()[1] == _target.first_note()) { (*i)->set_initial(true); initial = *i; @@ -260,9 +261,9 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const } } - SharedPtr<Node> cur = initial; + SPtr<Node> cur = initial; unreachable.erase(cur); - SharedPtr<Node> head; + SPtr<Node> head; while (!unreachable.empty()) { if (rand() % 2) { @@ -272,7 +273,7 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const } if (!head->connected_to(head) ) { - cur->add_edge(SharedPtr<Edge>(new Edge(cur, head))); + cur->add_edge(SPtr<Edge>(new Edge(cur, head))); unreachable.erase(head); cur = head; } diff --git a/src/engine/Problem.hpp b/src/engine/Problem.hpp index a5fe853..7d96250 100644 --- a/src/engine/Problem.hpp +++ b/src/engine/Problem.hpp @@ -29,10 +29,12 @@ namespace Machina { class Problem : public Eugene::Problem<Machine> { public: - Problem(TimeUnit unit, const std::string& target_midi, SharedPtr<Machine> seed = SharedPtr<Machine>()); + Problem(TimeUnit unit, + const std::string& target_midi, + SPtr<Machine> seed = SPtr<Machine>()); virtual ~Problem() {} - void seed(SharedPtr<Machine> parent) { _seed = parent; } + void seed(SPtr<Machine> parent) { _seed = parent; } float fitness(const Machine& machine) const; @@ -109,8 +111,8 @@ private: TimeUnit _unit; - Evaluator _target; - SharedPtr<Machine> _seed; + Evaluator _target; + SPtr<Machine> _seed; /// for levenshtein distance mutable std::vector< std::vector<uint16_t> > _matrix; diff --git a/src/engine/Recorder.cpp b/src/engine/Recorder.cpp index 8bc9a4a..df9a200 100644 --- a/src/engine/Recorder.cpp +++ b/src/engine/Recorder.cpp @@ -34,8 +34,7 @@ Recorder::Recorder(Raul::Forge& forge, : _forge(forge) , _unit(unit) , _record_buffer(buffer_size) - , _builder(new MachineBuilder( - SharedPtr<Machine>(new Machine(unit)), q, step)) + , _builder(new MachineBuilder(SPtr<Machine>(new Machine(unit)), q, step)) {} void @@ -62,10 +61,10 @@ Recorder::_whipped() } } -SharedPtr<Machine> +SPtr<Machine> Recorder::finish() { - SharedPtr<Machine> machine = _builder->finish(); + SPtr<Machine> machine = _builder->finish(); _builder.reset(); return machine; } diff --git a/src/engine/Recorder.hpp b/src/engine/Recorder.hpp index ce9cad8..62295cf 100644 --- a/src/engine/Recorder.hpp +++ b/src/engine/Recorder.hpp @@ -18,10 +18,10 @@ #ifndef MACHINA_RECORDER_HPP #define MACHINA_RECORDER_HPP -#include "raul/SharedPtr.hpp" #include "raul/RingBuffer.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Slave.hpp" @@ -52,15 +52,15 @@ public: } } - SharedPtr<Machine> finish(); + SPtr<Machine> finish(); private: virtual void _whipped(); - Raul::Forge& _forge; - TimeUnit _unit; - Raul::RingBuffer _record_buffer; - SharedPtr<MachineBuilder> _builder; + Raul::Forge& _forge; + TimeUnit _unit; + Raul::RingBuffer _record_buffer; + SPtr<MachineBuilder> _builder; }; } // namespace Machina diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp index da7fecb..7401755 100644 --- a/src/engine/SMFDriver.cpp +++ b/src/engine/SMFDriver.cpp @@ -20,10 +20,9 @@ #include <glibmm/convert.h> -#include "raul/SharedPtr.hpp" - #include "machina/Context.hpp" #include "machina/Machine.hpp" +#include "machina/types.hpp" #include "Edge.hpp" #include "SMFDriver.hpp" @@ -36,9 +35,9 @@ using namespace std; namespace Machina { SMFDriver::SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit) - : Driver(forge, SharedPtr<Machine>()) + : Driver(forge, SPtr<Machine>()) { - _writer = SharedPtr<SMFWriter>(new SMFWriter(unit)); + _writer = SPtr<SMFWriter>(new SMFWriter(unit)); } /** Learn a single track from the MIDI file at @a uri @@ -48,23 +47,25 @@ SMFDriver::SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit) * Currently only file:// URIs are supported. * @return the resulting machine. */ -SharedPtr<Machine> -SMFDriver::learn(const string& filename, unsigned track, double q, +SPtr<Machine> +SMFDriver::learn(const string& filename, + unsigned track, + double q, Raul::TimeDuration max_duration) { //assert(q.unit() == max_duration.unit()); - SharedPtr<Machine> m(new Machine(max_duration.unit())); - SharedPtr<MachineBuilder> builder = SharedPtr<MachineBuilder>( + SPtr<Machine> m(new Machine(max_duration.unit())); + SPtr<MachineBuilder> builder = SPtr<MachineBuilder>( new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { cerr << "Unable to open MIDI file " << filename << endl; - return SharedPtr<Machine>(); + return SPtr<Machine>(); } if (track > reader.num_tracks()) { - return SharedPtr<Machine>(); + return SPtr<Machine>(); } else { learn_track(builder, reader, track, q, max_duration); } @@ -74,7 +75,7 @@ SMFDriver::learn(const string& filename, unsigned track, double q, if (m->nodes().size() > 1) { return m; } else { - return SharedPtr<Machine>(); + return SPtr<Machine>(); } } @@ -82,17 +83,17 @@ SMFDriver::learn(const string& filename, unsigned track, double q, * * This will result in one disjoint subgraph in the machine for each track. */ -SharedPtr<Machine> +SPtr<Machine> SMFDriver::learn(const string& filename, double q, Raul::TimeStamp max_duration) { - SharedPtr<Machine> m(new Machine(max_duration.unit())); - SharedPtr<MachineBuilder> builder = SharedPtr<MachineBuilder>( + SPtr<Machine> m(new Machine(max_duration.unit())); + SPtr<MachineBuilder> builder = SPtr<MachineBuilder>( new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { cerr << "Unable to open MIDI file " << filename << endl; - return SharedPtr<Machine>(); + return SPtr<Machine>(); } for (unsigned t = 1; t <= reader.num_tracks(); ++t) { @@ -105,16 +106,16 @@ SMFDriver::learn(const string& filename, double q, Raul::TimeStamp max_duration) if (m->nodes().size() > 1) { return m; } else { - return SharedPtr<Machine>(); + return SPtr<Machine>(); } } void -SMFDriver::learn_track(SharedPtr<MachineBuilder> builder, - SMFReader& reader, - unsigned track, - double q, - Raul::TimeDuration max_duration) +SMFDriver::learn_track(SPtr<MachineBuilder> builder, + SMFReader& reader, + unsigned track, + double q, + Raul::TimeDuration max_duration) { const bool found_track = reader.seek_to_track(track); if (!found_track) { @@ -153,7 +154,7 @@ SMFDriver::learn_track(SharedPtr<MachineBuilder> builder, } void -SMFDriver::run(SharedPtr<Machine> machine, Raul::TimeStamp max_time) +SMFDriver::run(SPtr<Machine> machine, Raul::TimeStamp max_time) { // FIXME: unit kludge (tempo only) Context context(_forge, machine->time().unit().ppt(), @@ -161,7 +162,7 @@ SMFDriver::run(SharedPtr<Machine> machine, Raul::TimeStamp max_time) context.set_sink(this); context.time().set_slice(TimeStamp(max_time.unit(), 0, 0), context.time().beats_to_ticks(max_time)); - machine->run(context, SharedPtr<UpdateBuffer>()); + machine->run(context, SPtr<UpdateBuffer>()); } } // namespace Machina diff --git a/src/engine/SMFDriver.hpp b/src/engine/SMFDriver.hpp index abed40d..ee3c220 100644 --- a/src/engine/SMFDriver.hpp +++ b/src/engine/SMFDriver.hpp @@ -20,8 +20,6 @@ #include <glibmm/ustring.h> -#include "raul/SharedPtr.hpp" - #include "machina/Driver.hpp" #include "machina/types.hpp" @@ -40,16 +38,16 @@ class SMFDriver public: SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit); - SharedPtr<Machine> learn(const std::string& filename, - double q, - Raul::TimeDuration max_duration); + SPtr<Machine> learn(const std::string& filename, + double q, + Raul::TimeDuration max_duration); - SharedPtr<Machine> learn(const std::string& filename, - unsigned track, - double q, - Raul::TimeDuration max_duration); + SPtr<Machine> learn(const std::string& filename, + unsigned track, + double q, + Raul::TimeDuration max_duration); - void run(SharedPtr<Machine> machine, Raul::TimeStamp max_time); + void run(SPtr<Machine> machine, Raul::TimeStamp max_time); void write_event(Raul::TimeStamp time, size_t ev_size, @@ -59,16 +57,16 @@ public: void set_bpm(double /*bpm*/) {} void set_quantization(double /*quantization*/) {} - SharedPtr<SMFWriter> writer() { return _writer; } + SPtr<SMFWriter> writer() { return _writer; } private: - SharedPtr<SMFWriter> _writer; + SPtr<SMFWriter> _writer; - void learn_track(SharedPtr<MachineBuilder> builder, - SMFReader& reader, - unsigned track, - double q, - Raul::TimeDuration max_duration); + void learn_track(SPtr<MachineBuilder> builder, + SMFReader& reader, + unsigned track, + double q, + Raul::TimeDuration max_duration); }; } // namespace Machina diff --git a/src/engine/Updates.cpp b/src/engine/Updates.cpp index 16717e4..275ca2b 100644 --- a/src/engine/Updates.cpp +++ b/src/engine/Updates.cpp @@ -18,7 +18,6 @@ #include "lv2/lv2plug.in/ns/ext/atom/atom.h" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" #include "machina/Updates.hpp" @@ -26,10 +25,10 @@ namespace Machina { void -write_set(SharedPtr<UpdateBuffer> buf, - uint64_t subject, - URIInt key, - const Raul::Atom& value) +write_set(SPtr<UpdateBuffer> buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value) { const uint32_t update_type = UPDATE_SET; buf->write(sizeof(update_type), &update_type); @@ -42,10 +41,10 @@ write_set(SharedPtr<UpdateBuffer> buf, } uint32_t -read_set(SharedPtr<UpdateBuffer> buf, - uint64_t* subject, - URIInt* key, - Raul::Atom* value) +read_set(SPtr<UpdateBuffer> buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value) { uint32_t update_type; buf->read(sizeof(update_type), &update_type); diff --git a/src/engine/machina/Context.hpp b/src/engine/machina/Context.hpp index 8f86602..8195a0e 100644 --- a/src/engine/machina/Context.hpp +++ b/src/engine/machina/Context.hpp @@ -18,8 +18,8 @@ #ifndef MACHINA_CONTEXT_HPP #define MACHINA_CONTEXT_HPP +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeSlice.hpp" namespace Machina { diff --git a/src/engine/machina/Controller.hpp b/src/engine/machina/Controller.hpp index ddd1d4c..eb04c59 100644 --- a/src/engine/machina/Controller.hpp +++ b/src/engine/machina/Controller.hpp @@ -22,8 +22,8 @@ #include <set> -#include "raul/SharedPtr.hpp" #include "raul/RingBuffer.hpp" +#include "raul/Maid.hpp" #include "machina/types.hpp" #include "machina/URIs.hpp" @@ -31,7 +31,7 @@ #include "Stateful.hpp" namespace Raul { -class Atom; class Maid; +class Atom; } namespace Machina { @@ -47,7 +47,7 @@ class ClientModel; class ClientObject; class Controller { public: - Controller(SharedPtr<Engine> engine, Client::ClientModel& client_model); + Controller(SPtr<Engine> engine, Client::ClientModel& client_model); uint64_t create(const Client::ClientObject& obj); uint64_t connect(uint64_t tail_id, uint64_t head_id); @@ -56,32 +56,32 @@ public: URIInt key, const Raul::Atom& value); - void learn(SharedPtr<Raul::Maid> maid, uint64_t node_id); + void learn(SPtr<Raul::Maid> maid, uint64_t node_id); void disconnect(uint64_t tail_id, uint64_t head_id); void erase(uint64_t id); - void announce(SharedPtr<Machine> machine); + void announce(SPtr<Machine> machine); void process_updates(); private: - SharedPtr<Stateful> find(uint64_t id); + SPtr<Stateful> find(uint64_t id); struct StatefulComparator { - inline bool operator()(SharedPtr<Stateful> lhs, - SharedPtr<Stateful> rhs) const { + inline bool operator()(SPtr<Stateful> lhs, + SPtr<Stateful> rhs) const { return lhs->id() < rhs->id(); } }; - typedef std::set<SharedPtr<Stateful>, StatefulComparator> Objects; + typedef std::set<SPtr<Stateful>, StatefulComparator> Objects; Objects _objects; - SharedPtr<Engine> _engine; + SPtr<Engine> _engine; Client::ClientModel& _client_model; - SharedPtr<UpdateBuffer> _updates; + SPtr<UpdateBuffer> _updates; }; } diff --git a/src/engine/machina/Driver.hpp b/src/engine/machina/Driver.hpp index 778fd22..ea0bf8f 100644 --- a/src/engine/machina/Driver.hpp +++ b/src/engine/machina/Driver.hpp @@ -32,22 +32,22 @@ class Driver : public MIDISink { public: - Driver(Raul::Forge& forge, SharedPtr<Machine> machine) + Driver(Raul::Forge& forge, SPtr<Machine> machine) : _forge(forge) , _machine(machine) {} virtual ~Driver() {} - SharedPtr<Machine> machine() { return _machine; } + SPtr<Machine> machine() { return _machine; } - virtual void set_machine(SharedPtr<Machine> machine) { + virtual void set_machine(SPtr<Machine> machine) { _machine = machine; } - SharedPtr<UpdateBuffer> update_sink() { return _updates; } + SPtr<UpdateBuffer> update_sink() { return _updates; } - void set_update_sink(SharedPtr<UpdateBuffer> b) { + void set_update_sink(SPtr<UpdateBuffer> b) { _updates = b; } @@ -65,9 +65,9 @@ public: virtual void finish_record() {} protected: - Raul::Forge& _forge; - SharedPtr<Machine> _machine; - SharedPtr<UpdateBuffer> _updates; + Raul::Forge& _forge; + SPtr<Machine> _machine; + SPtr<UpdateBuffer> _updates; }; } // namespace Machina diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index 7040082..1c8d6ae 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -23,8 +23,8 @@ #include <glibmm/ustring.h> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "machina/Driver.hpp" #include "machina/Loader.hpp" @@ -35,26 +35,26 @@ class Machine; class Engine { public: - Engine(Raul::Forge& forge, - SharedPtr<Driver> driver, - Sord::World& rdf_world); + Engine(Raul::Forge& forge, + SPtr<Driver> driver, + Sord::World& rdf_world); Sord::World& rdf_world() { return _rdf_world; } - static SharedPtr<Driver> new_driver(Raul::Forge& forge, - const std::string& name, - SharedPtr<Machine> machine); + static SPtr<Driver> new_driver(Raul::Forge& forge, + const std::string& name, + SPtr<Machine> machine); - SharedPtr<Driver> driver() { return _driver; } - SharedPtr<Machine> machine() { return _driver->machine(); } - Raul::Forge& forge() { return _forge; } + SPtr<Driver> driver() { return _driver; } + SPtr<Machine> machine() { return _driver->machine(); } + Raul::Forge& forge() { return _forge; } - SharedPtr<Machine> load_machine(const Glib::ustring& uri); - SharedPtr<Machine> load_machine_midi(const Glib::ustring& uri, - double q, - Raul::TimeDuration dur); + SPtr<Machine> load_machine(const Glib::ustring& uri); + SPtr<Machine> load_machine_midi(const Glib::ustring& uri, + double q, + Raul::TimeDuration dur); - void import_machine(SharedPtr<Machine> machine); + void import_machine(SPtr<Machine> machine); void export_midi(const Glib::ustring& filename, Raul::TimeDuration dur); @@ -63,10 +63,10 @@ public: void set_quantization(double beat_fraction); private: - SharedPtr<Driver> _driver; - Sord::World& _rdf_world; - Loader _loader; - Raul::Forge _forge; + SPtr<Driver> _driver; + Sord::World& _rdf_world; + Loader _loader; + Raul::Forge _forge; }; } // namespace Machina diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp index 1aec3e8..7c5504e 100644 --- a/src/engine/machina/Evolver.hpp +++ b/src/engine/machina/Evolver.hpp @@ -19,7 +19,7 @@ #define MACHINA_EVOLVER_HPP #include "eugene/GAImpl.hpp" -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" #include "raul/Thread.hpp" #include "raul/TimeStamp.hpp" @@ -39,23 +39,24 @@ class Evolver : public Raul::Thread { public: - Evolver(Raul::TimeUnit unit, const string& target_midi, - SharedPtr<Machine> seed); + Evolver(Raul::TimeUnit unit, + const string& target_midi, + SPtr<Machine> seed); - void seed(SharedPtr<Machine> parent); + void seed(SPtr<Machine> parent); bool improvement() { return _improvement; } - SharedPtr<const Machine> best() { return _ga->best(); } + SPtr<const Machine> best() { return _ga->best(); } typedef Eugene::GAImpl<Machine> MachinaGA; private: void _run(); - SharedPtr<MachinaGA> _ga; - SharedPtr<Problem> _problem; - float _seed_fitness; - Schrodinbit _improvement; + SPtr<MachinaGA> _ga; + SPtr<Problem> _problem; + float _seed_fitness; + Schrodinbit _improvement; }; } // namespace Machina diff --git a/src/engine/machina/Loader.hpp b/src/engine/machina/Loader.hpp index f97618a..665f7b7 100644 --- a/src/engine/machina/Loader.hpp +++ b/src/engine/machina/Loader.hpp @@ -20,9 +20,8 @@ #include <glibmm/ustring.h> +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" - #include "sord/sordmm.hpp" using Sord::Namespaces; @@ -36,7 +35,7 @@ class Loader public: Loader(Raul::Forge& forge, Sord::World& rdf_world); - SharedPtr<Machine> load(const Glib::ustring& filename); + SPtr<Machine> load(const Glib::ustring& filename); private: Raul::Forge& _forge; diff --git a/src/engine/machina/Machine.hpp b/src/engine/machina/Machine.hpp index acfc599..1a8f267 100644 --- a/src/engine/machina/Machine.hpp +++ b/src/engine/machina/Machine.hpp @@ -19,15 +19,13 @@ #define MACHINA_MACHINE_HPP #include <vector> +#include <set> #include <boost/utility.hpp> +#include "machina/types.hpp" #include "raul/Atom.hpp" -#include "raul/Maid.hpp" #include "raul/RingBuffer.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeSlice.hpp" -#include "raul/WeakPtr.hpp" - #include "sord/sordmm.hpp" #include "types.hpp" @@ -61,49 +59,50 @@ public: bool is_finished() { return _is_finished; } bool is_activated() { return _is_activated; } - void add_node(SharedPtr<Node> node); - void remove_node(SharedPtr<Node> node); - void learn(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node); + void add_node(SPtr<Node> node); + void remove_node(SPtr<Node> node); + void learn(SPtr<Raul::Maid> maid, SPtr<Node> node); void write_state(Sord::Model& model); // Audio context void reset(MIDISink* sink, Raul::TimeStamp time); - uint32_t run(Context& context, SharedPtr<UpdateBuffer> updates); + uint32_t run(Context& context, SPtr<UpdateBuffer> updates); // Any context inline Raul::TimeStamp time() const { return _time; } - SharedPtr<LearnRequest> pending_learn() { return _pending_learn; } - void clear_pending_learn() { _pending_learn.reset(); } + SPtr<LearnRequest> pending_learn() { return _pending_learn; } + void clear_pending_learn() { _pending_learn.reset(); } - typedef std::list< SharedPtr<Node> > Nodes; + typedef std::set< SPtr<Node> > Nodes; Nodes& nodes() { return _nodes; } const Nodes& nodes() const { return _nodes; } - SharedPtr<Node> random_node(); - SharedPtr<Edge> random_edge(); + SPtr<Node> random_node(); + SPtr<Edge> random_edge(); private: // Audio context - SharedPtr<Node> earliest_node() const; + SPtr<Node> earliest_node() const; + + bool enter_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates); - bool enter_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates); + void exit_node(Context& context, + SPtr<Node> node, + SPtr<UpdateBuffer> updates); - void exit_node(Context& context, - SharedPtr<Node> node, - SharedPtr<UpdateBuffer> updates); + static const size_t MAX_ACTIVE_NODES = 128; - static const size_t MAX_ACTIVE_NODES = 128; - std::vector< SharedPtr<Node> > _active_nodes; + std::vector< SPtr<Node> > _active_nodes; - SharedPtr<LearnRequest> _pending_learn; - Nodes _nodes; - Raul::TimeStamp _time; - bool _is_activated; - bool _is_finished; + SPtr<LearnRequest> _pending_learn; + Nodes _nodes; + Raul::TimeStamp _time; + bool _is_activated; + bool _is_finished; }; } // namespace Machina diff --git a/src/engine/machina/Updates.hpp b/src/engine/machina/Updates.hpp index d2947e7..cf29886 100644 --- a/src/engine/machina/Updates.hpp +++ b/src/engine/machina/Updates.hpp @@ -21,7 +21,6 @@ #include <stdint.h> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" @@ -32,16 +31,16 @@ enum UpdateType { }; void -write_set(SharedPtr<UpdateBuffer> buf, - uint64_t subject, - URIInt key, - const Raul::Atom& value); +write_set(SPtr<UpdateBuffer> buf, + uint64_t subject, + URIInt key, + const Raul::Atom& value); uint32_t -read_set(SharedPtr<UpdateBuffer> buf, - uint64_t* subject, - URIInt* key, - Raul::Atom* value); +read_set(SPtr<UpdateBuffer> buf, + uint64_t* subject, + URIInt* key, + Raul::Atom* value); } // namespace Machina diff --git a/src/engine/machina/types.hpp b/src/engine/machina/types.hpp index 7603d3c..d71e144 100644 --- a/src/engine/machina/types.hpp +++ b/src/engine/machina/types.hpp @@ -18,6 +18,8 @@ #ifndef MACHINA_TYPES_HPP #define MACHINA_TYPES_HPP +#include <memory> + #include "raul/RingBuffer.hpp" namespace Machina { @@ -28,6 +30,30 @@ typedef Raul::RingBuffer UpdateBuffer; typedef uint32_t URIInt; +template <class T> +using SPtr = std::shared_ptr<T>; + +template <class T> +using WPtr = std::weak_ptr<T>; + +template <class T> +void NullDeleter(T* ptr) {} + +template<class T, class U> +SPtr<T> static_ptr_cast(const SPtr<U>& r) { + return std::static_pointer_cast<T>(r); +} + +template<class T, class U> +SPtr<T> dynamic_ptr_cast(const SPtr<U>& r) { + return std::dynamic_pointer_cast<T>(r); +} + +template<class T, class U> +SPtr<T> const_ptr_cast(const SPtr<U>& r) { + return std::const_pointer_cast<T>(r); +} + } // namespace Machina #endif // MACHINA_TYPES_HPP |