diff options
Diffstat (limited to 'src')
53 files changed, 728 insertions, 661 deletions
diff --git a/src/client/ClientModel.cpp b/src/client/ClientModel.cpp index c945de7..1cc4d3e 100644 --- a/src/client/ClientModel.cpp +++ b/src/client/ClientModel.cpp @@ -20,20 +20,20 @@ namespace Machina { namespace Client { -SharedPtr<ClientObject> +SPtr<ClientObject> ClientModel::find(uint64_t id) { - SharedPtr<ClientObject> key(new ClientObjectKey(id)); - Objects::iterator i = _objects.find(key); + SPtr<ClientObject> key(new ClientObjectKey(id)); + Objects::iterator i = _objects.find(key); if (i != _objects.end()) { return *i; } else { - return SharedPtr<ClientObject>(); + return SPtr<ClientObject>(); } } void -ClientModel::new_object(SharedPtr<ClientObject> object) +ClientModel::new_object(SPtr<ClientObject> object) { _objects.insert(object); signal_new_object.emit(object); @@ -42,21 +42,21 @@ ClientModel::new_object(SharedPtr<ClientObject> object) void ClientModel::erase_object(uint64_t id) { - SharedPtr<ClientObject> key(new ClientObjectKey(id)); - Objects::iterator i = _objects.find(key); + SPtr<ClientObject> key(new ClientObjectKey(id)); + Objects::iterator i = _objects.find(key); if (i == _objects.end()) { return; } signal_erase_object.emit(*i); - (*i)->set_view(SharedPtr<ClientObject::View>()); + (*i)->set_view(SPtr<ClientObject::View>()); _objects.erase(i); } void ClientModel::property(uint64_t id, URIInt key, const Raul::Atom& value) { - SharedPtr<ClientObject> object = find(id); + SPtr<ClientObject> object = find(id); if (object) { object->set(key, value); } diff --git a/src/client/ClientModel.hpp b/src/client/ClientModel.hpp index 07500db..b629a37 100644 --- a/src/client/ClientModel.hpp +++ b/src/client/ClientModel.hpp @@ -34,25 +34,25 @@ namespace Client { class ClientModel { public: - void new_object(SharedPtr<ClientObject> object); + void new_object(SPtr<ClientObject> object); void erase_object(uint64_t id); void property(uint64_t id, URIInt key, const Raul::Atom& value); - SharedPtr<ClientObject> find(uint64_t id); + SPtr<ClientObject> find(uint64_t id); - sigc::signal< void, SharedPtr<ClientObject> > signal_new_object; - sigc::signal< void, SharedPtr<ClientObject> > signal_erase_object; + sigc::signal< void, SPtr<ClientObject> > signal_new_object; + sigc::signal< void, SPtr<ClientObject> > signal_erase_object; private: struct ClientObjectComparator { - inline bool operator()(SharedPtr<ClientObject> lhs, - SharedPtr<ClientObject> rhs) const { + inline bool operator()(SPtr<ClientObject> lhs, + SPtr<ClientObject> rhs) const { return lhs->id() < rhs->id(); } }; - typedef std::set<SharedPtr<ClientObject>, ClientObjectComparator> Objects; + typedef std::set<SPtr<ClientObject>, ClientObjectComparator> Objects; Objects _objects; }; diff --git a/src/client/ClientObject.hpp b/src/client/ClientObject.hpp index 994a5ba..df8a1dd 100644 --- a/src/client/ClientObject.hpp +++ b/src/client/ClientObject.hpp @@ -23,7 +23,6 @@ #include <sigc++/sigc++.h> #include "raul/Atom.hpp" -#include "raul/SharedPtr.hpp" #include "machina/types.hpp" @@ -52,12 +51,12 @@ public: typedef std::map<URIInt, Raul::Atom> Properties; const Properties& properties() { return _properties; } - SharedPtr<View> view() const { return _view; } - void set_view(SharedPtr<View> view) { _view = view; } + SPtr<View> view() const { return _view; } + void set_view(SPtr<View> view) { _view = view; } private: - uint64_t _id; - SharedPtr<View> _view; + uint64_t _id; + SPtr<View> _view; Properties _properties; }; 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 diff --git a/src/gui/EdgeView.cpp b/src/gui/EdgeView.cpp index afa8cfd..dd28a41 100644 --- a/src/gui/EdgeView.cpp +++ b/src/gui/EdgeView.cpp @@ -25,7 +25,8 @@ #include "MachinaGUI.hpp" #include "NodeView.hpp" -using Machina::URIs; +namespace Machina { +namespace GUI { /* probability colour stuff */ @@ -63,10 +64,10 @@ inline static uint32_t edge_color(float prob) using namespace Ganv; -EdgeView::EdgeView(Canvas& canvas, - SharedPtr<NodeView> src, - SharedPtr<NodeView> dst, - SharedPtr<Machina::Client::ClientObject> edge) +EdgeView::EdgeView(Canvas& canvas, + SPtr<NodeView> src, + SPtr<NodeView> dst, + SPtr<Machina::Client::ClientObject> edge) : Ganv::Edge(canvas, src.get(), dst.get(), 0x9FA0A0F4, true, false) , _edge(edge) { @@ -132,3 +133,6 @@ EdgeView::on_property(Machina::URIInt key, const Raul::Atom& value) set_color(edge_color(value.get_float())); } } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/EdgeView.hpp b/src/gui/EdgeView.hpp index b532071..bafcb6b 100644 --- a/src/gui/EdgeView.hpp +++ b/src/gui/EdgeView.hpp @@ -24,6 +24,9 @@ #include "machina/types.hpp" +namespace Machina { +namespace GUI { + class NodeView; class EdgeView @@ -31,10 +34,10 @@ class EdgeView , public Machina::Client::ClientObject::View { public: - EdgeView(Ganv::Canvas& canvas, - SharedPtr<NodeView> src, - SharedPtr<NodeView> dst, - SharedPtr<Machina::Client::ClientObject> edge); + EdgeView(Ganv::Canvas& canvas, + SPtr<NodeView> src, + SPtr<NodeView> dst, + SPtr<Machina::Client::ClientObject> edge); void show_label(bool show); @@ -46,7 +49,10 @@ private: float probability() const; - SharedPtr<Machina::Client::ClientObject> _edge; + SPtr<Machina::Client::ClientObject> _edge; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_EDGEVIEW_HPP diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp index 2501cec..95a3f14 100644 --- a/src/gui/MachinaCanvas.cpp +++ b/src/gui/MachinaCanvas.cpp @@ -17,13 +17,13 @@ #include <map> -#include "raul/SharedPtr.hpp" #include "raul/TimeStamp.hpp" -#include "machina/Engine.hpp" -#include "machina/Controller.hpp" -#include "client/ClientObject.hpp" #include "client/ClientModel.hpp" +#include "client/ClientObject.hpp" +#include "machina/Controller.hpp" +#include "machina/Engine.hpp" +#include "machina/types.hpp" #include "EdgeView.hpp" #include "MachinaCanvas.hpp" @@ -32,7 +32,9 @@ using namespace Raul; using namespace Ganv; -using namespace Machina; + +namespace Machina { +namespace GUI { MachinaCanvas::MachinaCanvas(MachinaGUI* app, int width, int height) : Canvas(width, height) @@ -45,9 +47,9 @@ MachinaCanvas::MachinaCanvas(MachinaGUI* app, int width, int height) } bool -MachinaCanvas::node_clicked(WeakPtr<NodeView> item, GdkEventButton* event) +MachinaCanvas::node_clicked(WPtr<NodeView> item, GdkEventButton* event) { - SharedPtr<NodeView> node = PtrCast<NodeView>(item.lock()); + SPtr<NodeView> node = dynamic_ptr_cast<NodeView>(item.lock()); if (!node) { return false; } @@ -63,7 +65,7 @@ MachinaCanvas::node_clicked(WeakPtr<NodeView> item, GdkEventButton* event) } else if (event->button == 1) { // Left click: connect/disconnect - SharedPtr<NodeView> last = _last_clicked.lock(); + SPtr<NodeView> last = _last_clicked.lock(); if (last) { if (node != last) { @@ -101,7 +103,7 @@ MachinaCanvas::on_event(GdkEvent* event) } void -MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object) +MachinaCanvas::on_new_object(SPtr<Client::ClientObject> object) { const Machina::URIs& uris = URIs::instance(); const Raul::Atom& type = object->get(uris.rdf_type); @@ -110,7 +112,7 @@ MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object) } if (type.get<URIInt>() == uris.machina_Node) { - SharedPtr<NodeView> view( + SPtr<NodeView> view( new NodeView(_app->window(), *this, object, object->get(uris.machina_canvas_x).get_float(), object->get(uris.machina_canvas_y).get_float())); @@ -120,28 +122,25 @@ MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object) view->signal_clicked().connect( sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked), - WeakPtr<NodeView>(view))); + WPtr<NodeView>(view))); object->set_view(view); } else if (type.get<URIInt>() == uris.machina_Edge) { - SharedPtr<Machina::Client::ClientObject> tail - = _app->client_model()->find( - object->get(uris.machina_tail_id).get_int32()); - SharedPtr<Machina::Client::ClientObject> head - = _app->client_model()->find( - object->get(uris.machina_head_id).get_int32()); + SPtr<Machina::Client::ClientObject> tail = _app->client_model()->find( + object->get(uris.machina_tail_id).get_int32()); + SPtr<Machina::Client::ClientObject> head = _app->client_model()->find( + object->get(uris.machina_head_id).get_int32()); if (!tail || !head) { std::cerr << "Invalid arc" << std::endl; return; } - SharedPtr<NodeView> tail_view = PtrCast<NodeView>(tail->view()); - SharedPtr<NodeView> head_view = PtrCast<NodeView>(head->view()); + SPtr<NodeView> tail_view = dynamic_ptr_cast<NodeView>(tail->view()); + SPtr<NodeView> head_view = dynamic_ptr_cast<NodeView>(head->view()); - SharedPtr<EdgeView> view( - new EdgeView(*this, tail_view, head_view, object)); + SPtr<EdgeView> view(new EdgeView(*this, tail_view, head_view, object)); object->set_view(view); @@ -151,13 +150,13 @@ MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object) } void -MachinaCanvas::on_erase_object(SharedPtr<Client::ClientObject> object) +MachinaCanvas::on_erase_object(SPtr<Client::ClientObject> object) { const Raul::Atom& type = object->get(URIs::instance().rdf_type); if (type.get<URIInt>() == URIs::instance().machina_Node) { // Destruction of the view will remove from the canvas } else if (type.get<URIInt>() == URIs::instance().machina_Edge) { - object->set_view(SharedPtr<Client::ClientObject::View>()); + object->set_view(SPtr<Client::ClientObject::View>()); } else { std::cerr << "Unknown object type" << std::endl; } @@ -176,15 +175,18 @@ MachinaCanvas::action_create_node(double x, double y) } void -MachinaCanvas::action_connect(boost::shared_ptr<NodeView> src, - boost::shared_ptr<NodeView> head) +MachinaCanvas::action_connect(SPtr<NodeView> src, + SPtr<NodeView> head) { _app->controller()->connect(src->node()->id(), head->node()->id()); } void -MachinaCanvas::action_disconnect(boost::shared_ptr<NodeView> src, - boost::shared_ptr<NodeView> head) +MachinaCanvas::action_disconnect(SPtr<NodeView> src, + SPtr<NodeView> head) { _app->controller()->disconnect(src->node()->id(), head->node()->id()); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/MachinaCanvas.hpp b/src/gui/MachinaCanvas.hpp index 4f190fd..a9ca5b9 100644 --- a/src/gui/MachinaCanvas.hpp +++ b/src/gui/MachinaCanvas.hpp @@ -19,53 +19,56 @@ #define MACHINA_CANVAS_HPP_HPP #include <string> -#include "raul/SharedPtr.hpp" -#include "raul/WeakPtr.hpp" + #include "ganv/Canvas.hpp" +#include "machina/types.hpp" using namespace Ganv; +namespace Machina { + +namespace Client { class ClientObject; } + +namespace GUI { + class MachinaGUI; class NodeView; -namespace Machina { -namespace Client { -class ClientObject; -} -} - class MachinaCanvas : public Canvas { public: MachinaCanvas(MachinaGUI* app, int width, int height); - //void build(SharedPtr<const Machina::Machine> machine, bool show_labels); + //void build(SPtr<const Machina::Machine> machine, bool show_labels); //void update_edges(); - void on_new_object(SharedPtr<Machina::Client::ClientObject> object); - void on_erase_object(SharedPtr<Machina::Client::ClientObject> object); + void on_new_object(SPtr<Machina::Client::ClientObject> object); + void on_erase_object(SPtr<Machina::Client::ClientObject> object); MachinaGUI* app() { return _app; } protected: bool on_event(GdkEvent* event); - bool node_clicked(WeakPtr<NodeView> item, GdkEventButton* ev); + bool node_clicked(WPtr<NodeView> item, GdkEventButton* ev); private: - //SharedPtr<NodeView> create_node_view(SharedPtr<Machina::Node> node); + //SPtr<NodeView> create_node_view(SPtr<Machina::Node> node); void action_create_node(double x, double y); - void action_connect(SharedPtr<NodeView> port1, - SharedPtr<NodeView> port2); + void action_connect(SPtr<NodeView> port1, + SPtr<NodeView> port2); - void action_disconnect(SharedPtr<NodeView> port1, - SharedPtr<NodeView> port2); + void action_disconnect(SPtr<NodeView> port1, + SPtr<NodeView> port2); - MachinaGUI* _app; - WeakPtr<NodeView> _last_clicked; + MachinaGUI* _app; + WPtr<NodeView> _last_clicked; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_CANVAS_HPP_HPP diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp index 6e3b188..f162ebd 100644 --- a/src/gui/MachinaGUI.cpp +++ b/src/gui/MachinaGUI.cpp @@ -38,9 +38,10 @@ #include "machina/Evolver.hpp" #endif -using namespace Machina; +namespace Machina { +namespace GUI { -MachinaGUI::MachinaGUI(SharedPtr<Machina::Engine> engine) +MachinaGUI::MachinaGUI(SPtr<Machina::Engine> engine) : _refresh(false) , _evolve(false) , _unit(TimeUnit::BEATS, 19200) @@ -49,8 +50,7 @@ MachinaGUI::MachinaGUI(SharedPtr<Machina::Engine> engine) , _controller(new Machina::Controller(_engine, *_client_model.get())) , _maid(new Raul::Maid()) { - _canvas = boost::shared_ptr<MachinaCanvas>( - new MachinaCanvas(this, 1600*2, 1200*2)); + _canvas = SPtr<MachinaCanvas>(new MachinaCanvas(this, 1600*2, 1200*2)); Glib::RefPtr<Gtk::Builder> xml = WidgetFactory::create(); @@ -146,23 +146,30 @@ MachinaGUI::MachinaGUI(SharedPtr<Machina::Engine> engine) _quantize_spinbutton->signal_changed().connect( sigc::mem_fun(this, &MachinaGUI::quantize_changed)); - _mutate_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::random_mutation), - SharedPtr<Machine>())); - _compress_button->signal_clicked().connect(sigc::hide_return(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 0))); - _add_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 1)); - _remove_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 2)); - _adjust_node_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 3)); - _add_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 4)); - _remove_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 5)); - _adjust_edge_button->signal_clicked().connect(sigc::bind( - sigc::mem_fun(this, &MachinaGUI::mutate), SharedPtr<Machine>(), 6)); + _mutate_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::random_mutation), + SPtr<Machine>())); + _compress_button->signal_clicked().connect( + sigc::hide_return(sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 0))); + _add_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 1)); + _remove_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 2)); + _adjust_node_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 3)); + _add_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 4)); + _remove_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 5)); + _adjust_edge_button->signal_clicked().connect( + sigc::bind(sigc::mem_fun(this, &MachinaGUI::mutate), + SPtr<Machine>(), 6)); connect_widgets(); @@ -177,9 +184,8 @@ MachinaGUI::MachinaGUI(SharedPtr<Machina::Engine> engine) // Idle callback to drive the maid (collect garbage) Glib::signal_timeout().connect( - sigc::bind_return( - sigc::mem_fun(_maid.get(), &Raul::Maid::cleanup), - true), + sigc::bind_return(sigc::mem_fun(_maid.get(), &Raul::Maid::cleanup), + true), 1000); // Idle callback to update node states @@ -289,16 +295,16 @@ void MachinaGUI::evolve_toggled() { if (_evolve_button->get_active()) { - _evolver = SharedPtr<Evolver>( + _evolver = SPtr<Evolver>( new Evolver(_unit, _target_filename, _engine->machine())); _evolve = true; stop_clicked(); - _engine->driver()->set_machine(SharedPtr<Machine>()); + _engine->driver()->set_machine(SPtr<Machine>()); _evolver->start(); } else { _evolver->stop(); _evolve = false; - SharedPtr<Machine> new_machine = SharedPtr<Machine>( + SPtr<Machine> new_machine = SPtr<Machine>( new Machine(*_evolver->best())); _engine->driver()->set_machine(new_machine); _canvas->build(new_machine, _menu_view_labels->get_active()); @@ -309,7 +315,7 @@ MachinaGUI::evolve_toggled() #endif void -MachinaGUI::random_mutation(SharedPtr<Machine> machine) +MachinaGUI::random_mutation(SPtr<Machine> machine) { if (!machine) machine = _engine->machine(); @@ -318,7 +324,7 @@ MachinaGUI::random_mutation(SharedPtr<Machine> machine) } void -MachinaGUI::mutate(SharedPtr<Machine> machine, unsigned mutation) +MachinaGUI::mutate(SPtr<Machine> machine, unsigned mutation) { #if 0 if (!machine) @@ -423,7 +429,7 @@ MachinaGUI::menu_file_open() const int result = dialog.run(); if (result == Gtk::RESPONSE_OK) { - SharedPtr<Machina::Machine> new_machine = _engine->load_machine(dialog.get_uri()); + SPtr<Machina::Machine> new_machine = _engine->load_machine(dialog.get_uri()); if (new_machine) { _canvas->destroy(); _controller->announce(new_machine); @@ -530,7 +536,7 @@ MachinaGUI::menu_import_midi() const double length_dbl = length_sb->get_value_as_int(); const Raul::TimeStamp length(_unit, length_dbl); - SharedPtr<Machina::Machine> machine = _engine->load_machine_midi( + SPtr<Machina::Machine> machine = _engine->load_machine_midi( dialog.get_filename(), 0.0, length); if (machine) { @@ -689,13 +695,16 @@ MachinaGUI::play_toggled() } void -MachinaGUI::on_new_object(SharedPtr<Client::ClientObject> object) +MachinaGUI::on_new_object(SPtr<Client::ClientObject> object) { _canvas->on_new_object(object); } void -MachinaGUI::on_erase_object(SharedPtr<Client::ClientObject> object) +MachinaGUI::on_erase_object(SPtr<Client::ClientObject> object) { _canvas->on_erase_object(object); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/MachinaGUI.hpp b/src/gui/MachinaGUI.hpp index c48ac93..f9b0857 100644 --- a/src/gui/MachinaGUI.hpp +++ b/src/gui/MachinaGUI.hpp @@ -22,52 +22,55 @@ #include <gtkmm.h> -#include "raul/Maid.hpp" -#include "raul/SharedPtr.hpp" #include "raul/TimeStamp.hpp" +#include "machina/types.hpp" #include "machina_config.h" using namespace std; namespace Machina { + class Machine; class Engine; class Evolver; class Controller; + namespace Client { -class ClientModel; class ClientObject; -} +class ClientModel; +class ClientObject; } +namespace GUI { + class MachinaCanvas; class MachinaGUI { public: - MachinaGUI(SharedPtr<Machina::Engine> engine); + MachinaGUI(SPtr<Machina::Engine> engine); ~MachinaGUI(); - boost::shared_ptr<MachinaCanvas> canvas() { return _canvas; } - boost::shared_ptr<Machina::Engine> engine() { return _engine; } + SPtr<MachinaCanvas> canvas() { return _canvas; } + SPtr<Machina::Engine> engine() { return _engine; } Raul::Forge& forge() { return _forge; } - SharedPtr<Raul::Maid> maid() { return _maid; } + SPtr<Raul::Maid> maid() { return _maid; } Gtk::Window* window() { return _main_window; } void attach(); void quit() { _main_window->hide(); } - SharedPtr<Machina::Controller> controller() { return _controller; } + SPtr<Machina::Controller> controller() { return _controller; } inline void queue_refresh() { _refresh = true; } - void on_new_object(SharedPtr<Machina::Client::ClientObject> object); - void on_erase_object(SharedPtr<Machina::Client::ClientObject> object); + void on_new_object(SPtr<Machina::Client::ClientObject> object); + void on_erase_object(SPtr<Machina::Client::ClientObject> object); - SharedPtr<Machina::Client::ClientModel> client_model() { + SPtr<Machina::Client::ClientModel> client_model() { return _client_model; } @@ -88,8 +91,8 @@ protected: void arrange(); void load_target_clicked(); - void random_mutation(SharedPtr<Machina::Machine> machine); - void mutate(SharedPtr<Machina::Machine> machine, unsigned mutation); + void random_mutation(SPtr<Machina::Machine> machine); + void mutate(SPtr<Machina::Machine> machine, unsigned mutation); void zoom(double z); void update_toolbar(); @@ -116,13 +119,13 @@ protected: Raul::TimeUnit _unit; - SharedPtr<MachinaCanvas> _canvas; - SharedPtr<Machina::Engine> _engine; - SharedPtr<Machina::Client::ClientModel> _client_model; - SharedPtr<Machina::Controller> _controller; + SPtr<MachinaCanvas> _canvas; + SPtr<Machina::Engine> _engine; + SPtr<Machina::Client::ClientModel> _client_model; + SPtr<Machina::Controller> _controller; - SharedPtr<Raul::Maid> _maid; - SharedPtr<Machina::Evolver> _evolver; + SPtr<Raul::Maid> _maid; + SPtr<Machina::Evolver> _evolver; Raul::Forge _forge; @@ -171,4 +174,7 @@ protected: Gtk::ToolButton* _adjust_edge_button; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_GUI_HPP diff --git a/src/gui/NodePropertiesWindow.cpp b/src/gui/NodePropertiesWindow.cpp index 9a76420..a58d649 100644 --- a/src/gui/NodePropertiesWindow.cpp +++ b/src/gui/NodePropertiesWindow.cpp @@ -20,7 +20,9 @@ #include "WidgetFactory.hpp" using namespace std; -using namespace Machina; + +namespace Machina { +namespace GUI { NodePropertiesWindow* NodePropertiesWindow::_instance = NULL; @@ -57,9 +59,9 @@ NodePropertiesWindow::apply_clicked() _node->set_enter_action(ActionFactory::note_on(note)); _node->set_exit_action(ActionFactory::note_off(note)); } else { - SharedPtr<MidiAction> action = PtrCast<MidiAction>(_node->enter_action()); + SPtr<MidiAction> action = dynamic_ptr_cast<MidiAction>(_node->enter_action()); action->event()[1] = note; - action = PtrCast<MidiAction>(_node->exit_action()); + action = dynamic_ptr_cast<MidiAction>(_node->exit_action()); action->event()[1] = note; } @@ -86,11 +88,11 @@ NodePropertiesWindow::ok_clicked() } void -NodePropertiesWindow::set_node(SharedPtr<Machina::Client::ClientObject> node) +NodePropertiesWindow::set_node(SPtr<Machina::Client::ClientObject> node) { _node = node; #if 0 - SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(node->enter_action()); + SPtr<MidiAction> enter_action = dynamic_ptr_cast<MidiAction>(node->enter_action()); if (enter_action && ( enter_action->event_size() > 1) && ( (enter_action->event()[0] & 0xF0) == 0x90) ) { _note_spinbutton->set_value(enter_action->event()[1]); @@ -106,8 +108,8 @@ NodePropertiesWindow::set_node(SharedPtr<Machina::Client::ClientObject> node) } void -NodePropertiesWindow::present(Gtk::Window* parent, - SharedPtr<Machina::Client::ClientObject> node) +NodePropertiesWindow::present(Gtk::Window* parent, + SPtr<Machina::Client::ClientObject> node) { if (!_instance) { Glib::RefPtr<Gtk::Builder> xml = WidgetFactory::create(); @@ -122,3 +124,6 @@ NodePropertiesWindow::present(Gtk::Window* parent, _instance->set_node(node); _instance->show(); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/NodePropertiesWindow.hpp b/src/gui/NodePropertiesWindow.hpp index 7d395c9..78a5285 100644 --- a/src/gui/NodePropertiesWindow.hpp +++ b/src/gui/NodePropertiesWindow.hpp @@ -20,13 +20,13 @@ #include <gtkmm.h> -#include "raul/SharedPtr.hpp" +#include "machina/types.hpp" namespace Machina { -namespace Client { -class ClientObject; -} -} + +namespace Client { class ClientObject; } + +namespace GUI { class NodePropertiesWindow : public Gtk::Dialog @@ -37,11 +37,11 @@ public: ~NodePropertiesWindow(); - static void present(Gtk::Window* parent, - SharedPtr<Machina::Client::ClientObject> node); + static void present(Gtk::Window* parent, + SPtr<Machina::Client::ClientObject> node); private: - void set_node(SharedPtr<Machina::Client::ClientObject> node); + void set_node(SPtr<Machina::Client::ClientObject> node); void apply_clicked(); void cancel_clicked(); @@ -49,7 +49,7 @@ private: static NodePropertiesWindow* _instance; - SharedPtr<Machina::Client::ClientObject> _node; + SPtr<Machina::Client::ClientObject> _node; Gtk::SpinButton* _note_spinbutton; Gtk::SpinButton* _duration_spinbutton; @@ -58,4 +58,7 @@ private: Gtk::Button* _ok_button; }; +} // namespace Machina +} // namespace GUI + #endif // NODEPROPERTIESWINDOW_HPP diff --git a/src/gui/NodeView.cpp b/src/gui/NodeView.cpp index fc5377e..79258cd 100644 --- a/src/gui/NodeView.cpp +++ b/src/gui/NodeView.cpp @@ -27,13 +27,15 @@ #include "NodeView.hpp" using namespace std; -using Machina::URIs; -NodeView::NodeView(Gtk::Window* window, - Ganv::Canvas& canvas, - SharedPtr<Machina::Client::ClientObject> node, - double x, - double y) +namespace Machina { +namespace GUI { + +NodeView::NodeView(Gtk::Window* window, + Ganv::Canvas& canvas, + SPtr<Machina::Client::ClientObject> node, + double x, + double y) : Ganv::Circle(canvas, "", x, y, 20, false) , _window(window) , _node(node) @@ -172,3 +174,6 @@ NodeView::set_default_colors() set_fill_color(_default_fill_color); set_border_color(_default_border_color); } + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/NodeView.hpp b/src/gui/NodeView.hpp index b0c0da4..5507a75 100644 --- a/src/gui/NodeView.hpp +++ b/src/gui/NodeView.hpp @@ -24,18 +24,21 @@ #include "machina/types.hpp" +namespace Machina { +namespace GUI { + class NodeView : public Ganv::Circle , public Machina::Client::ClientObject::View { public: - NodeView(Gtk::Window* window, - Canvas& canvas, - SharedPtr<Machina::Client::ClientObject> node, - double x, - double y); + NodeView(Gtk::Window* window, + Canvas& canvas, + SPtr<Machina::Client::ClientObject> node, + double x, + double y); - SharedPtr<Machina::Client::ClientObject> node() { return _node; } + SPtr<Machina::Client::ClientObject> node() { return _node; } void show_label(bool show); @@ -55,15 +58,18 @@ private: bool node_is(Raul::Forge& forge, Machina::URIInt key); - Gtk::Window* _window; - SharedPtr<Machina::Client::ClientObject> _node; - uint32_t _default_border_color; - uint32_t _default_fill_color; + Gtk::Window* _window; + SPtr<Machina::Client::ClientObject> _node; + uint32_t _default_border_color; + uint32_t _default_fill_color; - SharedPtr<Machina::Client::ClientObject> _enter_action; - sigc::connection _enter_action_connection; + SPtr<Machina::Client::ClientObject> _enter_action; + sigc::connection _enter_action_connection; sigc::signal<bool, GdkEventButton*> _signal_clicked; }; +} // namespace Machina +} // namespace GUI + #endif // MACHINA_NODEVIEW_HPP diff --git a/src/gui/WidgetFactory.hpp b/src/gui/WidgetFactory.hpp index 41e8200..4f54998 100644 --- a/src/gui/WidgetFactory.hpp +++ b/src/gui/WidgetFactory.hpp @@ -23,6 +23,9 @@ #include "machina_config.h" +namespace Machina { +namespace GUI { + class WidgetFactory { public: @@ -58,3 +61,6 @@ public: } }; + +} // namespace Machina +} // namespace GUI diff --git a/src/gui/main.cpp b/src/gui/main.cpp index e87e29d..25b1254 100644 --- a/src/gui/main.cpp +++ b/src/gui/main.cpp @@ -40,14 +40,14 @@ main(int argc, char** argv) Machina::URIs::init(); - SharedPtr<Machina::Machine> machine; + SPtr<Machina::Machine> machine; // Load machine, if given #if 0 if (argc >= 2) { const string filename = argv[1]; cout << "Building machine from MIDI file " << filename << endl; - SharedPtr<Machina::SMFDriver> file_driver(new Machina::SMFDriver()); + SPtr<Machina::SMFDriver> file_driver(new Machina::SMFDriver()); if (argc >= 3) { float q = strtof(argv[2], NULL); @@ -68,8 +68,7 @@ main(int argc, char** argv) Raul::Forge forge; if (!machine) { - machine = SharedPtr<Machine>( - new Machine(TimeUnit(TimeUnit::BEATS, 19200))); + machine = SPtr<Machine>(new Machine(TimeUnit(TimeUnit::BEATS, 19200))); } std::string driver_name = "smf"; @@ -78,13 +77,13 @@ main(int argc, char** argv) #endif // Build engine - SharedPtr<Driver> driver(Engine::new_driver(forge, driver_name, machine)); - SharedPtr<Engine> engine(new Engine(forge, driver, rdf_world)); + SPtr<Driver> driver(Engine::new_driver(forge, driver_name, machine)); + SPtr<Engine> engine(new Engine(forge, driver, rdf_world)); Gtk::Main app(argc, argv); driver->activate(); - MachinaGUI gui(engine); + GUI::MachinaGUI gui(engine); app.run(*gui.window()); diff --git a/src/machina.cpp b/src/machina.cpp index 4dc9ada..7772da5 100644 --- a/src/machina.cpp +++ b/src/machina.cpp @@ -58,7 +58,7 @@ main(int argc, char** argv) Glib::thread_init(); } - SharedPtr<JackDriver> driver(new JackDriver()); + SPtr<JackDriver> driver(new JackDriver()); Sord::World rdf_world; Engine engine(driver, rdf_world); diff --git a/src/midi2machina.cpp b/src/midi2machina.cpp index 3a7f055..7cd6242 100644 --- a/src/midi2machina.cpp +++ b/src/midi2machina.cpp @@ -51,9 +51,9 @@ main(int argc, char** argv) return -1; } - SharedPtr<SMFDriver> driver(new SMFDriver()); + SPtr<SMFDriver> driver(new SMFDriver()); - SharedPtr<Machine> machine = driver->learn(argv[2], strtof(argv[1], NULL)); + SPtr<Machine> machine = driver->learn(argv[2], strtof(argv[1], NULL)); if (!machine) { cout << "Failed to load MIDI file." << endl; |