diff options
Diffstat (limited to 'src/engine')
50 files changed, 889 insertions, 665 deletions
diff --git a/src/engine/Action.cpp b/src/engine/Action.cpp index 59dd8c7..e49e69b 100644 --- a/src/engine/Action.cpp +++ b/src/engine/Action.cpp @@ -27,9 +27,8 @@ Action::write_state(Sord::Model& model) using namespace Raul; model.add_statement(rdf_id(model.world()), - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:Action")); + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:Action")); } } // namespace Machina - diff --git a/src/engine/Action.hpp b/src/engine/Action.hpp index 76834d9..f954a39 100644 --- a/src/engine/Action.hpp +++ b/src/engine/Action.hpp @@ -34,15 +34,20 @@ namespace Machina { /** An Action, executed on entering or exiting of a state. */ -struct Action : public Raul::Manageable, public Stateful { +struct Action + : public Raul::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) : _msg(msg) {} + PrintAction(const std::string& msg) + : _msg(msg) {} void execute(MIDISink* sink, Raul::TimeStamp time) { std::cout << "t=" << time << ": " << _msg << std::endl; } diff --git a/src/engine/ActionFactory.cpp b/src/engine/ActionFactory.cpp index b6a1cc1..92bc114 100644 --- a/src/engine/ActionFactory.cpp +++ b/src/engine/ActionFactory.cpp @@ -24,10 +24,11 @@ SharedPtr<Action> ActionFactory::copy(SharedPtr<Action> copy) { SharedPtr<MidiAction> ma = PtrCast<MidiAction>(copy); - if (ma) + if (ma) { return SharedPtr<Action>(new MidiAction(ma->event_size(), ma->event())); - else + } else { return SharedPtr<Action>(); + } } SharedPtr<Action> @@ -53,4 +54,3 @@ ActionFactory::note_off(unsigned char note) } } // namespace Machine - diff --git a/src/engine/ActionFactory.hpp b/src/engine/ActionFactory.hpp index 0d9ce3f..1ae47c4 100644 --- a/src/engine/ActionFactory.hpp +++ b/src/engine/ActionFactory.hpp @@ -25,9 +25,9 @@ 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); +SharedPtr<Action> copy(SharedPtr<Action> copy); +SharedPtr<Action> note_on(unsigned char note); +SharedPtr<Action> note_off(unsigned char note); } } // namespace Machina diff --git a/src/engine/Controller.cpp b/src/engine/Controller.cpp index 7533a77..f209018 100644 --- a/src/engine/Controller.cpp +++ b/src/engine/Controller.cpp @@ -27,7 +27,8 @@ namespace Machina { -Controller::Controller(SharedPtr<Engine> engine, Client::ClientModel& client_model) +Controller::Controller(SharedPtr<Engine> engine, + Client::ClientModel& client_model) : _engine(engine) , _client_model(client_model) , _updates(new UpdateBuffer(4096)) @@ -39,9 +40,10 @@ uint64_t Controller::create(const Client::ClientObject& properties) { TimeDuration dur(_engine->machine()->time().unit(), - properties.get(URIs::instance().machina_duration).get_float()); + properties.get(URIs::instance().machina_duration).get_float()); SharedPtr<Machina::Node> node(new Machina::Node(dur)); - SharedPtr<Client::ClientObject> obj(new Client::ClientObject(properties, node->id())); + SharedPtr<Client::ClientObject> obj( + new Client::ClientObject(properties, node->id())); _objects.insert(node); _client_model.new_object(obj); _engine->machine()->add_node(node); @@ -55,16 +57,17 @@ Controller::announce(SharedPtr<Machine> machine) for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); n != machine->nodes().end(); ++n) { - SharedPtr<Machina::Client::ClientObject> obj(new Machina::Client::ClientObject((*n)->id())); - obj->set(URIs::instance().rdf_type, - forge.make_urid(URIs::instance().machina_Node)); - obj->set(URIs::instance().machina_duration, - forge.make(float((*n)->duration().to_double()))); - obj->set(URIs::instance().machina_canvas_x, forge.make(0.0f)); - obj->set(URIs::instance().machina_canvas_y, forge.make(0.0f)); - - _objects.insert(*n); - _client_model.new_object(obj); + SharedPtr<Machina::Client::ClientObject> obj( + new Machina::Client::ClientObject((*n)->id())); + obj->set(URIs::instance().rdf_type, + forge.make_urid(URIs::instance().machina_Node)); + obj->set(URIs::instance().machina_duration, + forge.make(float((*n)->duration().to_double()))); + obj->set(URIs::instance().machina_canvas_x, forge.make(0.0f)); + obj->set(URIs::instance().machina_canvas_y, forge.make(0.0f)); + + _objects.insert(*n); + _client_model.new_object(obj); } for (Machina::Machine::Nodes::const_iterator n = machine->nodes().begin(); @@ -72,7 +75,8 @@ 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(new Client::ClientObject((*e)->id())); + SharedPtr<Client::ClientObject> eobj( + new Client::ClientObject((*e)->id())); eobj->set(URIs::instance().rdf_type, forge.make_urid(URIs::instance().rdf_type)); eobj->set(URIs::instance().machina_probability, @@ -105,12 +109,15 @@ Controller::learn(SharedPtr<Raul::Maid> maid, uint64_t node_id) if (node) { _engine->machine()->learn(maid, node); } else { - std::cerr << "Failed to find node " << node_id << " for learn" << std::endl; + std::cerr << "Failed to find node " << node_id << " for learn" + << std::endl; } } void -Controller::set_property(uint64_t object_id, URIInt key, const Raul::Atom& value) +Controller::set_property(uint64_t object_id, + URIInt key, + const Raul::Atom& value) { SharedPtr<Stateful> object = find(object_id); if (object) { @@ -131,7 +138,8 @@ Controller::connect(uint64_t tail_id, uint64_t head_id) Raul::Forge& forge = _engine->forge(); - SharedPtr<Client::ClientObject> obj(new Client::ClientObject(/**this,*/ edge->id())); + SharedPtr<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)); @@ -185,11 +193,13 @@ 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); + SharedPtr<Machina::Client::ClientObject> obj = _client_model.find( + subject); if (obj) { obj->set(key, value); } else { - SharedPtr<Client::ClientObject> obj(new Client::ClientObject(subject)); + SharedPtr<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 c79d1cc..9aee198 100644 --- a/src/engine/Edge.cpp +++ b/src/engine/Edge.cpp @@ -38,9 +38,9 @@ Edge::write_state(Sord::Model& model) const Sord::Node& rdf_id = this->rdf_id(model.world()); model.add_statement( - rdf_id, - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:Edge")); + rdf_id, + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:Edge")); SharedPtr<Node> tail = _tail.lock(); SharedPtr<Node> head = _head; @@ -61,11 +61,11 @@ Edge::write_state(Sord::Model& model) std::cerr << "FIXME: save atom" << std::endl; /* - model.add_statement( - rdf_id, - Sord::Curie(model.world(), "machina:probability"), - AtomRDF::atom_to_node(model, Atom(_probability.get()))); - */ + model.add_statement( + rdf_id, + Sord::Curie(model.world(), "machina:probability"), + AtomRDF::atom_to_node(model, Atom(_probability.get()))); + */ } } // namespace Machina diff --git a/src/engine/Edge.hpp b/src/engine/Edge.hpp index 17afac3..10c7ab8 100644 --- a/src/engine/Edge.hpp +++ b/src/engine/Edge.hpp @@ -34,7 +34,9 @@ namespace Machina { class Node; -class Edge : public Stateful { +class Edge + : public Stateful +{ public: Edge(WeakPtr<Node> tail, SharedPtr<Node> head) : _probability(1.0f) diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index 1a8c867..b103310 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -35,23 +35,23 @@ Engine::Engine(Raul::Forge& forge, , _rdf_world(rdf_world) , _loader(_forge, _rdf_world) , _forge(forge) -{ -} +{} SharedPtr<Driver> Engine::new_driver(Raul::Forge& forge, const std::string& name, SharedPtr<Machine> machine) { - #ifdef HAVE_JACK +#ifdef HAVE_JACK if (name == "jack") { JackDriver* driver = new JackDriver(forge, machine); driver->attach("machina"); return SharedPtr<Driver>(driver); } - #endif - if (name == "smf") +#endif + if (name == "smf") { return SharedPtr<Driver>(new SMFDriver(forge, machine->time().unit())); + } std::cerr << "Error: Unknown driver type `" << name << "'" << std::endl; return SharedPtr<Driver>(); @@ -80,7 +80,8 @@ Engine::load_machine(const Glib::ustring& uri) * Safe to call while engine is processing. */ SharedPtr<Machine> -Engine::load_machine_midi(const Glib::ustring& uri, double q, Raul::TimeDuration dur) +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); @@ -111,19 +112,22 @@ void Engine::export_midi(const Glib::ustring& filename, Raul::TimeDuration dur) { SharedPtr<Machine> machine = _driver->machine(); - SharedPtr<Machina::SMFDriver> file_driver(new Machina::SMFDriver(_forge, dur.unit())); + SharedPtr<Machina::SMFDriver> file_driver( + new Machina::SMFDriver(_forge, dur.unit())); const bool activated = _driver->is_activated(); - if (activated) + if (activated) { _driver->deactivate(); // FIXME: disable instead + } file_driver->writer()->start(filename, TimeStamp(dur.unit(), 0.0)); file_driver->run(machine, dur); machine->reset(NULL, machine->time()); file_driver->writer()->finish(); - if (activated) + if (activated) { _driver->activate(); + } } void @@ -139,4 +143,3 @@ Engine::set_quantization(double q) } } // namespace Machina - diff --git a/src/engine/Evolver.cpp b/src/engine/Evolver.cpp index 83b0d75..a71d64b 100644 --- a/src/engine/Evolver.cpp +++ b/src/engine/Evolver.cpp @@ -32,40 +32,42 @@ using namespace boost; namespace Machina { -Evolver::Evolver(TimeUnit unit, const string& target_midi, SharedPtr<Machine> seed) +Evolver::Evolver(TimeUnit unit, const string& target_midi, + SharedPtr<Machine> seed) : _problem(new Problem(unit, target_midi, seed)) , _seed_fitness(-FLT_MAX) { SharedPtr<Eugene::HybridMutation<Machine> > m(new HybridMutation<Machine>()); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::Compress())); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::AddNode())); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::Compress())); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::AddNode())); //m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( // new Mutation::RemoveNode())); //m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( // new Mutation::AdjustNode())); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::SwapNodes())); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::AddEdge())); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::RemoveEdge())); - m->append_mutation(1/6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( - new Mutation::AdjustEdge())); - - boost::shared_ptr< Selection<Machine> > s(new TournamentSelection<Machine>(_problem, 3, 0.8)); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::SwapNodes())); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::AddEdge())); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::RemoveEdge())); + m->append_mutation(1 / 6.0f, boost::shared_ptr< Eugene::Mutation<Machine> >( + new Mutation::AdjustEdge())); + + 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)); + 20, 20, 2, 1.0, 0.0)); } void Evolver::seed(SharedPtr<Machine> parent) { /*_best = SharedPtr<Machine>(new Machine(*parent.get())); - _best_fitness = _problem->fitness(*_best.get());*/ + _best_fitness = _problem->fitness(*_best.get());*/ _problem->seed(parent); _seed_fitness = _problem->fitness(*parent.get()); } @@ -87,15 +89,15 @@ Evolver::_run() float new_best = _ga->best_fitness(); /*cout << _problem->fitness_less(old_best, *_ga->best().get()) << endl; - cout << "best: " << _ga->best().get() << endl; - cout << "best fitness: " << _problem->fitness(*_ga->best().get()) << endl; - cout << "old best: " << old_best << endl; - cout << "new best: " << new_best << endl;*/ + cout << "best: " << _ga->best().get() << endl; + cout << "best fitness: " << _problem->fitness(*_ga->best().get()) << endl; + cout << "old best: " << old_best << endl; + cout << "new best: " << new_best << endl;*/ cout << "generation best: " << new_best << endl; if (_problem->fitness_less_than(old_best, new_best)) { _improvement = true; - old_best = new_best; + old_best = new_best; cout << "*** NEW BEST: " << new_best << endl; } @@ -104,4 +106,3 @@ Evolver::_run() } } // namespace Machina - diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index f863bae..5319d53 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -26,7 +26,6 @@ #include "JackDriver.hpp" #include "LearnRequest.hpp" #include "MidiAction.hpp" -#include "jack_compat.h" using namespace Machina; using namespace std; @@ -46,8 +45,9 @@ JackDriver::JackDriver(Raul::Forge& forge, SharedPtr<Machine> machine) , _quantization(0.0f) , _stop(0) , _stop_flag(false) - , _record_dur(_frames_unit) // = 0 - , _recording(false) + , _record_dur(_frames_unit) + , // = 0 + _recording(false) , _is_activated(false) { _context.set_sink(this); @@ -62,8 +62,9 @@ void JackDriver::attach(const std::string& client_name) { // Already connected - if (_client) + if (_client) { return; + } jack_set_error_function(jack_error_cb); @@ -81,25 +82,35 @@ JackDriver::attach(const std::string& client_name) _context.time().set_tick_rate(sample_rate()); - _input_port = jack_port_register(jack_client(), - "in", - JACK_DEFAULT_MIDI_TYPE, JackPortIsInput, - 0); + _input_port = jack_port_register( + jack_client(), + "in", + JACK_DEFAULT_MIDI_TYPE, + JackPortIsInput, + 0); - if (!_input_port) - std:: cerr << "WARNING: Failed to create MIDI input port." << std::endl; + if (!_input_port) { + std::cerr << "WARNING: Failed to create MIDI input port." + << std::endl; + } - _output_port = jack_port_register(jack_client(), - "out", - JACK_DEFAULT_MIDI_TYPE, JackPortIsOutput, - 0); + _output_port = jack_port_register( + jack_client(), + "out", + JACK_DEFAULT_MIDI_TYPE, + JackPortIsOutput, + 0); - if (!_output_port) - std::cerr << "WARNING: Failed to create MIDI output port." << std::endl; + if (!_output_port) { + std::cerr << "WARNING: Failed to create MIDI output port." + << std::endl; + } - if (!_machine) + if (!_machine) { _machine = SharedPtr<Machine>( - new Machine(TimeUnit::frames(jack_get_sample_rate(jack_client())))); + new Machine(TimeUnit::frames(jack_get_sample_rate( + jack_client())))); + } _machine->activate(); } @@ -123,7 +134,7 @@ JackDriver::detach() if (_client) { deactivate(); jack_client_close(_client); - _client = NULL; + _client = NULL; _is_activated = false; } } @@ -141,8 +152,9 @@ JackDriver::activate() void JackDriver::deactivate() { - if (_client) + if (_client) { jack_deactivate(_client); + } _is_activated = false; } @@ -150,21 +162,24 @@ JackDriver::deactivate() void JackDriver::set_machine(SharedPtr<Machine> machine) { - if (machine == _machine) + if (machine == _machine) { return; + } SharedPtr<Machine> last_machine = _last_machine; // Keep a reference _machine_changed.reset(0); assert(!last_machine.unique()); _machine = machine; - if (is_activated()) + if (is_activated()) { _machine_changed.wait(); + } assert(_machine == machine); last_machine.reset(); } void -JackDriver::process_input(SharedPtr<Machine> machine, const Raul::TimeSlice& time) +JackDriver::process_input(SharedPtr<Machine> machine, + const Raul::TimeSlice& time) { // We only actually read Jack input at the beginning of a cycle //assert(time.offset_ticks().is_zero()); @@ -173,30 +188,37 @@ JackDriver::process_input(SharedPtr<Machine> machine, const Raul::TimeSlice& tim if (_recording) { const jack_nframes_t nframes = time.length_ticks().ticks(); - void* jack_buffer = jack_port_get_buffer(_input_port, nframes); - const jack_nframes_t event_count = jack_midi_get_event_count(jack_buffer); + void* jack_buffer = jack_port_get_buffer(_input_port, + nframes); + const jack_nframes_t event_count = jack_midi_get_event_count( + jack_buffer); - for (jack_nframes_t i=0; i < event_count; ++i) { + for (jack_nframes_t i = 0; i < event_count; ++i) { jack_midi_event_t ev; jack_midi_event_get(&ev, jack_buffer, i); const TimeStamp rel_time_frames = TimeStamp(_frames_unit, ev.time); const TimeStamp time_frames = _record_dur + rel_time_frames; - _recorder->write(time.ticks_to_beats(time_frames), ev.size, ev.buffer); + _recorder->write(time.ticks_to_beats( + time_frames), ev.size, ev.buffer); } - if (event_count > 0) + if (event_count > 0) { _recorder->whip(); + } _record_dur += time.length_ticks(); } else { - const jack_nframes_t nframes = time.length_ticks().ticks(); - void* jack_buffer = jack_port_get_buffer(_input_port, nframes); - const jack_nframes_t event_count = jack_midi_get_event_count(jack_buffer); + const jack_nframes_t nframes = time.length_ticks().ticks(); + + void* jack_buffer = jack_port_get_buffer(_input_port,nframes); - for (jack_nframes_t i=0; i < event_count; ++i) { + const jack_nframes_t event_count = jack_midi_get_event_count( + jack_buffer); + + for (jack_nframes_t i = 0; i < event_count; ++i) { jack_midi_event_t ev; jack_midi_event_get(&ev, jack_buffer, i); @@ -207,7 +229,8 @@ JackDriver::process_input(SharedPtr<Machine> machine, const Raul::TimeSlice& tim learn->enter_action()->set_event(ev.size, ev.buffer); learn->start(_quantization.get(), TimeStamp(TimeUnit::frames(sample_rate()), - jack_last_frame_time(_client) + ev.time, 0)); + jack_last_frame_time(_client) + + ev.time, 0)); } } else if (ev.buffer[0] == 0x80) { @@ -218,13 +241,15 @@ JackDriver::process_input(SharedPtr<Machine> machine, const Raul::TimeSlice& tim if (learn->started()) { learn->exit_action()->set_event(ev.size, ev.buffer); learn->finish( - TimeStamp(TimeUnit::frames(sample_rate()), - jack_last_frame_time(_client) + ev.time, 0)); + TimeStamp(TimeUnit::frames(sample_rate()), + jack_last_frame_time(_client) + ev.time, + 0)); const uint64_t id = Stateful::next_id(); write_set(_updates, id, URIs::instance().rdf_type, - _forge.make_urid(URIs::instance().machina_MidiAction)); + _forge.make_urid(URIs::instance(). + machina_MidiAction)); write_set(_updates, learn->node()->id(), URIs::instance().machina_enter_action, _forge.make((int32_t)id)); @@ -246,44 +271,49 @@ JackDriver::write_event(Raul::TimeStamp time, size_t size, const byte* event) { - if (!_output_port) + if (!_output_port) { return; + } const Raul::TimeSlice& slice = _context.time(); - if (slice.beats_to_ticks(time) + slice.offset_ticks() < slice.start_ticks()) { + if (slice.beats_to_ticks(time) + slice.offset_ticks() < + slice.start_ticks()) { std::cerr << "ERROR: Missed event by " << slice.start_ticks() - - (slice.beats_to_ticks(time) + slice.offset_ticks()) + - (slice.beats_to_ticks(time) + slice.offset_ticks()) << " ticks" << "\n\tbpm: " << slice.bpm() << "\n\tev time: " << slice.beats_to_ticks(time) << "\n\tcycle_start: " << slice.start_ticks() - << "\n\tcycle_end: " << slice.start_ticks() + slice.length_ticks() - << "\n\tcycle_length: " << slice.length_ticks() << std::endl << std::endl; + << "\n\tcycle_end: " << slice.start_ticks() + + slice.length_ticks() + << "\n\tcycle_length: " << slice.length_ticks() + << std::endl << std::endl; return; } const TimeDuration nframes = slice.length_ticks(); const TimeStamp offset = slice.beats_to_ticks(time) - + slice.offset_ticks() - slice.start_ticks(); + + slice.offset_ticks() - slice.start_ticks(); - if ( ! (offset < slice.offset_ticks() + nframes)) { + if (!(offset < slice.offset_ticks() + nframes)) { std::cerr << "ERROR: Event offset " << offset << " outside cycle " << "\n\tbpm: " << slice.bpm() << "\n\tev time: " << slice.beats_to_ticks(time) << "\n\tcycle_start: " << slice.start_ticks() - << "\n\tcycle_end: " << slice.start_ticks() + slice.length_ticks() + << "\n\tcycle_end: " << slice.start_ticks() + + slice.length_ticks() << "\n\tcycle_length: " << slice.length_ticks() << std::endl; } else { #ifdef JACK_MIDI_NEEDS_NFRAMES jack_midi_event_write( - jack_port_get_buffer(_output_port, nframes), offset, - event, size, nframes); + jack_port_get_buffer(_output_port, nframes), offset, + event, size, nframes); #else jack_midi_event_write( - jack_port_get_buffer(_output_port, nframes.ticks()), offset.ticks(), - event, size); + jack_port_get_buffer(_output_port, nframes.ticks()), offset.ticks(), + event, size); #endif } } @@ -313,9 +343,9 @@ JackDriver::on_process(jack_nframes_t nframes) // Machine was switched since last cycle, finalize old machine. if (machine != _last_machine) { if (_last_machine) { - assert(!_last_machine.unique()); // Realtime, can't delete + assert(!_last_machine.unique()); // Realtime, can't delete _last_machine->reset(_context.sink(), _last_machine->time()); // Exit all active states - _last_machine.reset(); // Cut our reference + _last_machine.reset(); // Cut our reference } _machine_changed.post(); // Signal we're done with it } @@ -325,13 +355,15 @@ JackDriver::on_process(jack_nframes_t nframes) return; } - if (_stop_flag) + if (_stop_flag) { machine->reset(_context.sink(), _context.time().start_beats()); + } process_input(machine, _context.time()); - if (machine->is_empty() || !machine->is_activated()) + if (machine->is_empty() || !machine->is_activated()) { goto end; + } while (true) { const uint32_t run_dur_frames = machine->run(_context, _updates); @@ -348,14 +380,16 @@ JackDriver::on_process(jack_nframes_t nframes) machine->reset(_context.sink(), machine->time()); _context.time().set_slice(TimeStamp(_frames_unit, 0, 0), - TimeStamp(_frames_unit, nframes - run_dur_frames, 0)); - _context.time().set_offset(TimeStamp(_frames_unit, run_dur_frames, 0)); + TimeStamp(_frames_unit, nframes + - run_dur_frames, 0)); + _context.time().set_offset(TimeStamp(_frames_unit, run_dur_frames, + 0)); } else { // Machine ran for entire cycle _context.time().set_slice( - _context.time().start_ticks() + _context.time().length_ticks(), - TimeStamp(_frames_unit, 0, 0)); + _context.time().start_ticks() + _context.time().length_ticks(), + TimeStamp(_frames_unit, 0, 0)); break; } } @@ -375,10 +409,11 @@ end: void JackDriver::stop() { - if (recording()) + if (recording()) { finish_record(); + } - _stop_flag= true; + _stop_flag = true; _stop.wait(); _machine->deactivate(); } @@ -387,10 +422,12 @@ 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 + = SharedPtr<Recorder>(new Recorder(_forge, 1024, _beats_unit, + _quantization.get(), step)); _recorder->start(); _record_dur = 0; - _recording = true; + _recording = true; } void diff --git a/src/engine/JackDriver.hpp b/src/engine/JackDriver.hpp index 9f47363..8b62424 100644 --- a/src/engine/JackDriver.hpp +++ b/src/engine/JackDriver.hpp @@ -43,7 +43,9 @@ class Node; * "Ticks" are individual frames when running under this driver, and all code * in the processing context must be realtime safe (non-blocking). */ -class JackDriver : public Machina::Driver { +class JackDriver + : public Machina::Driver +{ public: JackDriver(Raul::Forge& forge, SharedPtr<Machine> machine = SharedPtr<Machine>()); @@ -81,7 +83,7 @@ public: } bool is_activated() const { return _is_activated; } - bool is_attached() const { return (_client != NULL); } + bool is_attached() const { return _client != NULL; } bool is_realtime() const { return _client && jack_is_realtime(_client); } jack_nframes_t sample_rate() const { return jack_get_sample_rate(_client); } @@ -107,8 +109,8 @@ private: Context _context; - Raul::TimeUnit _frames_unit; - Raul::TimeUnit _beats_unit; + Raul::TimeUnit _frames_unit; + Raul::TimeUnit _beats_unit; Raul::DoubleBuffer<double> _bpm; Raul::DoubleBuffer<double> _quantization; diff --git a/src/engine/LearnRequest.cpp b/src/engine/LearnRequest.cpp index ae77e7b..eabf568 100644 --- a/src/engine/LearnRequest.cpp +++ b/src/engine/LearnRequest.cpp @@ -43,8 +43,8 @@ LearnRequest::create(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node) void LearnRequest::start(double q, Raul::TimeStamp time) { - _started = true; - _start_time = time; + _started = true; + _start_time = time; _quantization = q; } diff --git a/src/engine/LearnRequest.hpp b/src/engine/LearnRequest.hpp index 8a5f1e0..af28017 100644 --- a/src/engine/LearnRequest.hpp +++ b/src/engine/LearnRequest.hpp @@ -34,10 +34,12 @@ class MidiAction; /** A request to MIDI learn a certain node. */ -class LearnRequest : public Raul::Manageable { +class LearnRequest + : public Raul::Manageable +{ public: - static SharedPtr<LearnRequest> - create(SharedPtr<Raul::Maid> maid, SharedPtr<Node> node); + static SharedPtr<LearnRequest>create(SharedPtr<Raul::Maid> maid, + SharedPtr<Node> node); void start(double q, Raul::TimeStamp time); void finish(TimeStamp time); diff --git a/src/engine/Loader.cpp b/src/engine/Loader.cpp index 1ce8fae..1a85e29 100644 --- a/src/engine/Loader.cpp +++ b/src/engine/Loader.cpp @@ -69,27 +69,27 @@ Loader::load(const Glib::ustring& uri) cout << "[Loader] Loading " << document_uri << endl; machine = SharedPtr<Machine>( - new Machine(TimeUnit::beats(MACHINA_PPQN))); + new Machine(TimeUnit::beats(MACHINA_PPQN))); typedef std::map<Sord::Node, SharedPtr<Node> > Created; Created created; Sord::Model model(_rdf_world, document_uri); - SerdEnv* env = serd_env_new(NULL); + SerdEnv* env = serd_env_new(NULL); model.load_file(env, SERD_TURTLE, document_uri); serd_env_free(env); Sord::Node nil; - Sord::URI rdf_type(_rdf_world, NS_RDF "type"); + Sord::URI rdf_type(_rdf_world, NS_RDF "type"); Sord::URI machina_SelectorNode(_rdf_world, NS_MACHINA "initialNode"); - Sord::URI machina_node(_rdf_world, NS_MACHINA "node"); - Sord::URI machina_initialNode(_rdf_world, NS_MACHINA "initialNode"); - Sord::URI machina_duration(_rdf_world, NS_MACHINA "duration"); - Sord::URI machina_edge(_rdf_world, NS_MACHINA "edge"); - Sord::URI machina_tail(_rdf_world, NS_MACHINA "tail"); - Sord::URI machina_head(_rdf_world, NS_MACHINA "head"); - Sord::URI machina_probability(_rdf_world, NS_MACHINA "probability"); + Sord::URI machina_node(_rdf_world, NS_MACHINA "node"); + Sord::URI machina_initialNode(_rdf_world, NS_MACHINA "initialNode"); + Sord::URI machina_duration(_rdf_world, NS_MACHINA "duration"); + Sord::URI machina_edge(_rdf_world, NS_MACHINA "edge"); + Sord::URI machina_tail(_rdf_world, NS_MACHINA "tail"); + Sord::URI machina_head(_rdf_world, NS_MACHINA "head"); + Sord::URI machina_probability(_rdf_world, NS_MACHINA "probability"); Sord::Node machine_node = Sord::URI(_rdf_world, "."); @@ -97,12 +97,12 @@ Loader::load(const Glib::ustring& uri) for (Sord::Iter i = model.find(machine_node, machina_initialNode, nil); !i.end(); ++i) { const Sord::Node& node_id = i.get_object(); - Sord::Iter d = model.find(node_id, machina_duration, nil); - SharedPtr<Node> node( + 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)); + 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()) { @@ -117,12 +117,12 @@ Loader::load(const Glib::ustring& uri) for (Sord::Iter i = model.find(machine_node, machina_node, nil); !i.end(); ++i) { const Sord::Node& node_id = i.get_object(); - Sord::Iter d = model.find(node_id, machina_duration, nil); + 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)); + TimeStamp(TimeUnit(TimeUnit::BEATS, MACHINA_PPQN), + d.get_object().to_float()), + false)); machine->add_node(node); created[node_id] = node; } @@ -133,25 +133,30 @@ Loader::load(const Glib::ustring& uri) /* Get note actions */ query = Query(_rdf_world, ustring( - "SELECT DISTINCT ?node ?note WHERE {\n" - " ?node :enterAction [ a :MidiAction; :midiNote ?note ] ;\n" - " :exitAction [ a :MidiAction; :midiNote ?note ] .\n" - "}\n")); + "SELECT DISTINCT ?node ?note WHERE {\n" + " ?node :enterAction [ a :MidiAction; :midiNote ?note ] ;\n" + " :exitAction [ a :MidiAction; :midiNote ?note ] .\n" + "}\n")); results = query.run(_rdf_world, model); for (; !results->finished(); results->next()) { - Created::iterator node_i = created.find((const char*)results->get("node")); + 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(); + SharedPtr<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((unsigned char)note_num)); - node->set_exit_action(ActionFactory::note_off((unsigned char)note_num)); + node->set_enter_action( + ActionFactory::note_on((uint8_t)note_num)); + node->set_exit_action( + ActionFactory::note_off((uint8_t)note_num)); } else { - cerr << "WARNING: MIDI note number out of range, ignoring." << endl; + cerr << "WARNING: MIDI note number out of range, ignoring." + << endl; } } else { - cerr << "WARNING: Found note for unknown states. Ignoring." << endl; + cerr << "WARNING: Found note for unknown states. Ignoring." + << endl; } } #endif @@ -173,7 +178,7 @@ Loader::load(const Glib::ustring& uri) Created::iterator tail_i = created.find(tail); Created::iterator head_i = created.find(head); - if (tail_i != created.end() && head_i != created.end()) { + if (( tail_i != created.end()) && ( head_i != created.end()) ) { const SharedPtr<Node> tail = tail_i->second; const SharedPtr<Node> head = head_i->second; @@ -183,12 +188,12 @@ Loader::load(const Glib::ustring& uri) } else { cerr << "[Loader] WARNING: Ignored edge between unknown nodes " - << tail << " -> " << head << endl; + << tail << " -> " << head << endl; } } - if (machine && machine->nodes().size() > 0) { + if (machine && ( machine->nodes().size() > 0) ) { machine->reset(NULL, machine->time()); return machine; } else { @@ -197,4 +202,3 @@ Loader::load(const Glib::ustring& uri) } } // namespace Machina - diff --git a/src/engine/MIDISink.hpp b/src/engine/MIDISink.hpp index 47aa234..1bf53dc 100644 --- a/src/engine/MIDISink.hpp +++ b/src/engine/MIDISink.hpp @@ -24,7 +24,9 @@ namespace Machina { /** Pure virtual base for anything you can write MIDI to. */ -class MIDISink : public Raul::Deletable { +class MIDISink + : public Raul::Deletable +{ public: virtual void write_event(Raul::TimeStamp time, size_t ev_size, @@ -34,4 +36,3 @@ public: } // namespace Machina #endif // MACHINA_MIDI_SINK_HPP - diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index 8a7f0b4..92a0d7e 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -43,8 +43,7 @@ Machine::Machine(TimeUnit unit) , _time(unit, 0, 0) , _is_activated(false) , _is_finished(false) -{ -} +{} /** Copy a Machine. * @@ -60,14 +59,16 @@ Machine::Machine(const Machine& copy) { std::map< SharedPtr<Node>, SharedPtr<Node> > replacements; - for (Nodes::const_iterator n = copy._nodes.begin(); n != copy._nodes.end(); ++n) { + 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); replacements[*n] = node; } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { - for (Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { + for (Node::Edges::const_iterator e = (*n)->edges().begin(); + e != (*n)->edges().end(); ++e) { (*e)->set_tail(*n); (*e)->set_head(replacements[(*e)->head()]); assert((*e)->head()); @@ -78,23 +79,26 @@ Machine::Machine(const Machine& copy) Machine& Machine::operator=(const Machine& other) { - _active_nodes = std::vector< SharedPtr<Node> >(MAX_ACTIVE_NODES, SharedPtr<Node>()); - _is_activated = false; - _is_finished = false; - _time = other._time; + _active_nodes = std::vector< SharedPtr<Node> >(MAX_ACTIVE_NODES, + SharedPtr<Node>()); + _is_activated = false; + _is_finished = false; + _time = other._time; _pending_learn = SharedPtr<LearnRequest>(); _nodes.clear(); map< SharedPtr<Node>, SharedPtr<Node> > replacements; - for (Nodes::const_iterator n = other._nodes.begin(); n != other._nodes.end(); ++n) { + 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); replacements[*n] = node; } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { - for (Node::Edges::const_iterator e = (*n)->edges().begin(); e != (*n)->edges().end(); ++e) { + for (Node::Edges::const_iterator e = (*n)->edges().begin(); + e != (*n)->edges().end(); ++e) { (*e)->set_tail(*n); (*e)->set_head(replacements[(*e)->head()]); assert((*e)->head()); @@ -108,15 +112,19 @@ Machine::operator=(const Machine& other) SharedPtr<Node> Machine::random_node() { - if (_nodes.empty()) + if (_nodes.empty()) { return SharedPtr<Node>(); + } size_t i = rand() % _nodes.size(); // FIXME: O(n) worst case :( - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n, --i) - if (i == 0) + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n, + --i) { + if (i == 0) { return *n; + } + } return SharedPtr<Node>(); } @@ -127,8 +135,9 @@ Machine::random_edge() { SharedPtr<Node> tail = random_node(); - for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) + for (size_t i = 0; i < _nodes.size() && tail->edges().empty(); ++i) { tail = random_node(); + } return tail ? tail->random_edge() : SharedPtr<Edge>(); } @@ -145,8 +154,9 @@ Machine::remove_node(SharedPtr<Node> node) { _nodes.erase(std::find(_nodes.begin(), _nodes.end(), node)); - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { (*n)->remove_edge_to(node); + } } /** Exit all active states and reset time to 0. @@ -155,20 +165,23 @@ void Machine::reset(MIDISink* sink, Raul::TimeStamp time) { if (!_is_finished) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { SharedPtr<Node> node = (*n); - if (sink && node->is_active()) + if (sink && node->is_active()) { node->exit(sink, time); + } assert(!node->is_active()); } - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { _active_nodes.at(i).reset(); + } } - _time = TimeStamp(_time.unit(), 0, 0); + _time = TimeStamp(_time.unit(), 0, 0); _is_finished = false; } @@ -179,12 +192,12 @@ Machine::earliest_node() const { SharedPtr<Node> earliest; - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) { + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { SharedPtr<Node> node = _active_nodes.at(i); if (node) { assert(node->is_active()); - if (!earliest || node->exit_time() < earliest->exit_time()) { + if (!earliest || ( node->exit_time() < earliest->exit_time()) ) { earliest = node; } } @@ -198,14 +211,16 @@ 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, + SharedPtr<Node> node, + SharedPtr<UpdateBuffer> updates) { assert(!node->is_active()); /* FIXME: Would be best to use the MIDI note here as a hash key, at least * while all actions are still MIDI notes... */ size_t index = (rand() % MAX_ACTIVE_NODES); - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) { + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { if (_active_nodes.at(index) == NULL) { node->enter(context.sink(), _time); assert(node->is_active()); @@ -227,7 +242,9 @@ Machine::enter_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuff /** 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, + SharedPtr<Node> node, + SharedPtr<UpdateBuffer> updates) { node->exit(context.sink(), _time); write_set(updates, @@ -237,9 +254,11 @@ Machine::exit_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffe assert(!node->is_active()); - for (size_t i=0; i < MAX_ACTIVE_NODES; ++i) - if (_active_nodes.at(i) == node) + for (size_t i = 0; i < MAX_ACTIVE_NODES; ++i) { + if (_active_nodes.at(i) == node) { _active_nodes.at(i).reset(); + } + } // Activate successors to this node // (that aren't aready active right now) @@ -247,14 +266,14 @@ Machine::exit_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffe if (node->is_selector()) { const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] - double range_min = 0; + double range_min = 0; for (Node::Edges::const_iterator s = node->edges().begin(); - s != node->edges().end(); ++s) { + s != node->edges().end(); ++s) { if (!(*s)->head()->is_active() - && rand_normal > range_min - && rand_normal < range_min + (*s)->probability()) { + && ( rand_normal > range_min) + && ( rand_normal < range_min + (*s)->probability()) ) { enter_node(context, (*s)->head(), updates); break; @@ -267,15 +286,16 @@ Machine::exit_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffe } else { for (Node::Edges::const_iterator e = node->edges().begin(); - e != node->edges().end(); ++e) { + e != node->edges().end(); ++e) { const double rand_normal = rand() / (double)RAND_MAX; // [0, 1] if (rand_normal <= (*e)->probability()) { SharedPtr<Node> head = (*e)->head(); - if ( ! head->is_active()) + if (!head->is_active()) { enter_node(context, head, updates); + } } } @@ -294,19 +314,23 @@ Machine::exit_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffe uint32_t Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) { - if (_is_finished) + if (_is_finished) { return 0; + } - const TimeStamp cycle_end_frames = context.time().start_ticks() + context.time().length_ticks(); - const TimeStamp cycle_end = context.time().ticks_to_beats(cycle_end_frames); + const TimeStamp cycle_end_frames = context.time().start_ticks() + + context.time().length_ticks(); + const TimeStamp cycle_end = context.time().ticks_to_beats( + cycle_end_frames); assert(_is_activated); // Initial run, enter all initial states if (_time.is_zero()) { bool entered = false; - if ( ! _nodes.empty()) { - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + if (!_nodes.empty()) { + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { if ((*n)->is_active()) { (*n)->exit(context.sink(), _time); write_set(updates, @@ -316,8 +340,9 @@ Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) } if ((*n)->is_initial()) { - if (enter_node(context, (*n), updates)) + if (enter_node(context, (*n), updates)) { entered = true; + } } } } @@ -334,13 +359,16 @@ Machine::run(Context& context, SharedPtr<UpdateBuffer> updates) if (!earliest) { // No more active states, machine is finished #ifndef NDEBUG - for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) - assert( ! (*n)->is_active()); + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); + ++n) { + assert(!(*n)->is_active()); + } #endif _is_finished = true; break; - } else if (context.time().beats_to_ticks(earliest->exit_time()) < cycle_end_frames) { + } else if (context.time().beats_to_ticks(earliest->exit_time()) < + cycle_end_frames) { // Earliest active state ends this cycle _time = earliest->exit_time(); exit_node(context, earliest, updates); @@ -375,8 +403,8 @@ Machine::write_state(Sord::Model& model) model.world().add_prefix("machina", "http://drobilla.net/ns/machina#"); model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:Machine")); + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:Machine")); for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { @@ -384,25 +412,25 @@ Machine::write_state(Sord::Model& model) if ((*n)->is_initial()) { model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:initialNode"), - (*n)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:initialNode"), + (*n)->rdf_id(model.world())); } else { model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:node"), - (*n)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:node"), + (*n)->rdf_id(model.world())); } } for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { for (Node::Edges::const_iterator e = (*n)->edges().begin(); - e != (*n)->edges().end(); ++e) { + e != (*n)->edges().end(); ++e) { (*e)->write_state(model); model.add_statement(model.base_uri(), - Sord::Curie(model.world(), "machina:edge"), - (*e)->rdf_id(model.world())); + Sord::Curie(model.world(), "machina:edge"), + (*e)->rdf_id(model.world())); } } diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index 1eaf22e..818c893 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -42,15 +42,14 @@ MachineBuilder::MachineBuilder(SharedPtr<Machine> machine, double q, bool step) , _connect_node(_initial_node) , _connect_node_end_time(_time) // = 0 , _step(step) -{ -} +{} void MachineBuilder::reset() { - _time = TimeStamp(_machine->time().unit()); // = 0 - _initial_node = SharedPtr<Node>(new Node(_time, true)); // duration 0 - _connect_node = _initial_node; + _time = TimeStamp(_machine->time().unit()); // = 0 + _initial_node = SharedPtr<Node>(new Node(_time, true)); // duration 0 + _connect_node = _initial_node; _connect_node_end_time = _time; // = 0 } @@ -63,7 +62,8 @@ MachineBuilder::is_delay_node(SharedPtr<Node> node) const /** Set the duration of a node, with quantization. */ void -MachineBuilder::set_node_duration(SharedPtr<Node> node, Raul::TimeDuration d) const +MachineBuilder::set_node_duration(SharedPtr<Node> node, + Raul::TimeDuration d) const { if (_step) { node->set_duration(TimeStamp(d.unit(), 1, 0)); @@ -73,9 +73,9 @@ MachineBuilder::set_node_duration(SharedPtr<Node> node, Raul::TimeDuration d) co Raul::TimeStamp q_dur = quantize(TimeStamp(d.unit(), _quantization), d); // Never quantize a note to duration 0 - if (q_dur.is_zero() && ( node->enter_action() || node->exit_action() )) + if (q_dur.is_zero() && ( node->enter_action() || node->exit_action() )) { q_dur = _quantization; // Round up - + } node->set_duration(q_dur); } @@ -85,8 +85,10 @@ MachineBuilder::set_node_duration(SharedPtr<Node> node, Raul::TimeDuration d) co */ SharedPtr<Node> MachineBuilder::connect_nodes(SharedPtr<Machine> m, - SharedPtr<Node> tail, Raul::TimeStamp tail_end_time, - SharedPtr<Node> head, Raul::TimeStamp head_start_time) + SharedPtr<Node> tail, + Raul::TimeStamp tail_end_time, + SharedPtr<Node> head, + Raul::TimeStamp head_start_time) { assert(tail != head); assert(head_start_time >= tail_end_time); @@ -95,7 +97,8 @@ MachineBuilder::connect_nodes(SharedPtr<Machine> m, 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); + set_node_duration(tail, + tail->duration() + head_start_time - tail_end_time); tail->add_edge(SharedPtr<Edge>(new Edge(tail, head))); } else if (_step || (head_start_time == tail_end_time)) { // Connect directly @@ -117,10 +120,11 @@ MachineBuilder::event(Raul::TimeStamp time_offset, unsigned char* buf) { const Raul::TimeUnit unit = time_offset.unit(); - Raul::TimeStamp t = _time + time_offset; + Raul::TimeStamp t = _time + time_offset; - if (ev_size == 0) + if (ev_size == 0) { return; + } if ((buf[0] & 0xF0) == LV2_MIDI_MSG_NOTE_ON) { @@ -132,10 +136,11 @@ MachineBuilder::event(Raul::TimeStamp time_offset, // If currently polyphonic, use a poly node with no successors as connect node // Results in patterns closest to what a human would choose - if ( ! _poly_nodes.empty()) { - for (PolyList::iterator j = _poly_nodes.begin(); j != _poly_nodes.end(); ++j) { + if (!_poly_nodes.empty()) { + for (PolyList::iterator j = _poly_nodes.begin(); + j != _poly_nodes.end(); ++j) { if (j->second->edges().empty()) { - this_connect_node = j->second; + this_connect_node = j->second; this_connect_node_end_time = j->first + j->second->duration(); break; } @@ -144,17 +149,20 @@ MachineBuilder::event(Raul::TimeStamp time_offset, // Currently monophonic, or didn't find a poly node, so use _connect_node // which is maintained below on note off events. - if ( ! this_connect_node) { + if (!this_connect_node) { this_connect_node = _connect_node; this_connect_node_end_time = _connect_node_end_time; } SharedPtr<Node> delay_node = connect_nodes(_machine, - this_connect_node, this_connect_node_end_time, node, t); + this_connect_node, + this_connect_node_end_time, + node, + t); if (delay_node) { - _connect_node = delay_node; - _connect_node_end_time = t; + _connect_node = delay_node; + _connect_node_end_time = t; } node->enter(NULL, t); @@ -162,21 +170,25 @@ 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()); - if (!action) + for (ActiveList::iterator i = _active_nodes.begin(); + i != _active_nodes.end(); ++i) { + SharedPtr<MidiAction> action = PtrCast<MidiAction>( + (*i)->enter_action()); + if (!action) { continue; + } const size_t ev_size = action->event_size(); const unsigned char* ev = action->event(); - if (ev_size == 3 && (ev[0] & 0xF0) == LV2_MIDI_MSG_NOTE_ON - && (ev[0] & 0x0F) == (buf[0] & 0x0F) // same channel - && ev[1] == buf[1]) // same note - { + if ((ev_size == 3) && ((ev[0] & 0xF0) == LV2_MIDI_MSG_NOTE_ON) + && ((ev[0] & 0x0F) == (buf[0] & 0x0F)) // same channel + && (ev[1] == buf[1])) { + // same note SharedPtr<Node> resolved = *i; - resolved->set_exit_action(SharedPtr<Action>(new MidiAction(ev_size, buf))); + resolved->set_exit_action( + SharedPtr<Action>(new MidiAction(ev_size, buf))); set_node_duration(resolved, t - resolved->enter_time()); // Last active note @@ -192,35 +204,45 @@ MachineBuilder::event(Raul::TimeStamp time_offset, connect_nodes(_machine, resolved, t, _connect_node, t); - for (PolyList::iterator j = _poly_nodes.begin(); j != _poly_nodes.end(); ++j) { + for (PolyList::iterator j = _poly_nodes.begin(); + j != _poly_nodes.end(); ++j) { _machine->add_node(j->second); - if (j->second->edges().size() == 0) - connect_nodes(_machine, j->second, j->first + j->second->duration(), - _connect_node, t); + if (j->second->edges().size() == 0) { + connect_nodes(_machine, j->second, + j->first + j->second->duration(), + _connect_node, t); + } } _poly_nodes.clear(); _machine->add_node(resolved); - // Just monophonic + // Just monophonic } else { // Trim useless delay node if possible (these appear after poly sections) - if (is_delay_node(_connect_node) && _connect_node->duration().is_zero() - && _connect_node->edges().size() == 1 - && (*_connect_node->edges().begin())->head() == resolved) { + if (is_delay_node(_connect_node) + && _connect_node->duration().is_zero() + && ( _connect_node->edges().size() == 1) + && ( (*_connect_node->edges().begin())->head() == + resolved) ) { _connect_node->edges().clear(); assert(_connect_node->edges().empty()); - _connect_node->set_enter_action(resolved->enter_action()); - _connect_node->set_exit_action(resolved->exit_action()); + _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>()); set_node_duration(_connect_node, resolved->duration()); resolved = _connect_node; - if (std::find(_machine->nodes().begin(), _machine->nodes().end(), _connect_node) - == _machine->nodes().end()) + if (std::find(_machine->nodes().begin(), + _machine->nodes().end(), + _connect_node) + == _machine->nodes().end()) { _machine->add_node(_connect_node); + } } else { _connect_node = resolved; @@ -228,15 +250,17 @@ MachineBuilder::event(Raul::TimeStamp time_offset, } } - // Polyphonic, add this state to poly list + // Polyphonic, add this state to poly list } else { - _poly_nodes.push_back(make_pair(resolved->enter_time(), resolved)); - _connect_node = resolved; + _poly_nodes.push_back(make_pair(resolved->enter_time(), + resolved)); + _connect_node = resolved; _connect_node_end_time = t; } - if (resolved->is_active()) + if (resolved->is_active()) { resolved->exit(NULL, t); + } _active_nodes.erase(i); @@ -254,18 +278,26 @@ void MachineBuilder::resolve() { // Resolve stuck notes - if ( ! _active_nodes.empty()) { - for (list<SharedPtr<Node> >::iterator i = _active_nodes.begin(); i != _active_nodes.end(); ++i) { + if (!_active_nodes.empty()) { + for (list<SharedPtr<Node> >::iterator i = _active_nodes.begin(); + i != _active_nodes.end(); ++i) { cerr << "WARNING: Resolving stuck note." << endl; - SharedPtr<MidiAction> action = PtrCast<MidiAction>((*i)->enter_action()); - if (!action) + SharedPtr<MidiAction> action = PtrCast<MidiAction>( + (*i)->enter_action()); + if (!action) { continue; + } const size_t ev_size = action->event_size(); const unsigned char* ev = action->event(); - if (ev_size == 3 && (ev[0] & 0xF0) == LV2_MIDI_MSG_NOTE_ON) { - unsigned char note_off[3] = { ((LV2_MIDI_MSG_NOTE_OFF & 0xF0) | (ev[0] & 0x0F)), ev[1], 0x40 }; - (*i)->set_exit_action(SharedPtr<Action>(new MidiAction(3, note_off))); + if (( ev_size == 3) + && ( (ev[0] & 0xF0) == LV2_MIDI_MSG_NOTE_ON) ) { + unsigned char note_off[3] + = { ((LV2_MIDI_MSG_NOTE_OFF & 0xF0) | (ev[0] & 0x0F)), + ev[1], + 0x40 }; + (*i)->set_exit_action( + SharedPtr<Action>(new MidiAction(3, note_off))); set_node_duration((*i), _time - (*i)->enter_time()); (*i)->exit(NULL, _time); _machine->add_node((*i)); @@ -275,10 +307,12 @@ MachineBuilder::resolve() } // Add initial note if necessary - if (_machine->nodes().size() > 0 - && (std::find(_machine->nodes().begin(), _machine->nodes().end(), _initial_node) - == _machine->nodes().end())) + if (( _machine->nodes().size() > 0) + && (std::find(_machine->nodes().begin(), _machine->nodes().end(), + _initial_node) + == _machine->nodes().end())) { _machine->add_node(_initial_node); + } } diff --git a/src/engine/MachineBuilder.hpp b/src/engine/MachineBuilder.hpp index c3d4c52..7bd865d 100644 --- a/src/engine/MachineBuilder.hpp +++ b/src/engine/MachineBuilder.hpp @@ -28,7 +28,8 @@ namespace Machina { class Machine; class Node; -class MachineBuilder { +class MachineBuilder +{ public: MachineBuilder(SharedPtr<Machine> machine, double quantization, @@ -47,10 +48,11 @@ private: bool is_delay_node(SharedPtr<Node> node) const; void set_node_duration(SharedPtr<Node> node, Raul::TimeDuration d) const; - SharedPtr<Node> - connect_nodes(SharedPtr<Machine> m, - SharedPtr<Node> tail, Raul::TimeStamp tail_end_time, - SharedPtr<Node> head, Raul::TimeStamp head_start_time); + SharedPtr<Node>connect_nodes(SharedPtr<Machine> m, + SharedPtr<Node> tail, + Raul::TimeStamp tail_end_time, + SharedPtr<Node> head, + Raul::TimeStamp head_start_time); typedef std::list<SharedPtr<Node> > ActiveList; ActiveList _active_nodes; @@ -58,8 +60,8 @@ private: typedef std::list<std::pair<Raul::TimeStamp, SharedPtr<Node> > > PolyList; PolyList _poly_nodes; - double _quantization; - Raul::TimeStamp _time; + double _quantization; + Raul::TimeStamp _time; SharedPtr<Machine> _machine; SharedPtr<Node> _initial_node; diff --git a/src/engine/MidiAction.cpp b/src/engine/MidiAction.cpp index c61ce68..a646f67 100644 --- a/src/engine/MidiAction.cpp +++ b/src/engine/MidiAction.cpp @@ -62,9 +62,10 @@ MidiAction::set_event(size_t size, const byte* new_event) byte* const event = _event.load(); if (size <= _max_size) { _event = NULL; - if (size > 0 && new_event) + if (( size > 0) && new_event) { memcpy(event, new_event, size); - _size = size; + } + _size = size; _event = event; return true; } else { @@ -82,8 +83,9 @@ MidiAction::execute(MIDISink* sink, Raul::TimeStamp time) const byte* const event = _event.load(); if (event) { - if (sink) + if (sink) { sink->write_event(time, _size, event); + } } else { std::cerr << "NULL MIDI ACTION"; } @@ -103,11 +105,10 @@ MidiAction::write_state(Sord::Model& model) // FIXME: Assumes note on/note off std::cerr << "FIXME: AtomRDF" << std::endl; /* - model.add_statement(rdf_id(model.world()), - Sord::Curie(model.world(), "machina:midiNote"), - AtomRDF::atom_to_node(model, Atom((int)(_event.get()[1])))); - */ + model.add_statement(rdf_id(model.world()), + Sord::Curie(model.world(), "machina:midiNote"), + AtomRDF::atom_to_node(model, Atom((int)(_event.get()[1])))); + */ } } // namespace Machina - diff --git a/src/engine/MidiAction.hpp b/src/engine/MidiAction.hpp index 11863c6..96d104c 100644 --- a/src/engine/MidiAction.hpp +++ b/src/engine/MidiAction.hpp @@ -30,7 +30,9 @@ namespace Machina { class MIDISink; -class MidiAction : public Action { +class MidiAction + : public Action +{ public: ~MidiAction(); diff --git a/src/engine/Mutation.cpp b/src/engine/Mutation.cpp index 73fc0e9..e25cdbb 100644 --- a/src/engine/Mutation.cpp +++ b/src/engine/Mutation.cpp @@ -36,12 +36,14 @@ Compress::mutate(Machine& machine) //cout << "COMPRESS" << endl; // Trim disconnected nodes - for (Machine::Nodes::iterator i = machine.nodes().begin(); i != machine.nodes().end() ;) { + for (Machine::Nodes::iterator i = machine.nodes().begin(); + i != machine.nodes().end(); ) { Machine::Nodes::iterator next = i; ++next; - if ((*i)->edges().empty()) + if ((*i)->edges().empty()) { machine.remove_node(*i); + } i = next; } @@ -57,14 +59,17 @@ AddNode::mutate(Machine& machine) node->set_selector(true); SharedPtr<Node> note_node = machine.random_node(); - if (!note_node) + if (!note_node) { return; + } uint8_t note = rand() % 128; - SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(note_node->enter_action()); - if (enter_action) + SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>( + note_node->enter_action()); + if (enter_action) { note = enter_action->event()[1]; + } node->set_enter_action(ActionFactory::note_on(note)); node->set_exit_action(ActionFactory::note_off(note)); @@ -72,13 +77,15 @@ AddNode::mutate(Machine& machine) // Insert after some node SharedPtr<Node> tail = machine.random_node(); - if (tail && tail != node/* && !node->connected_to(tail)*/) + if (tail && (tail != node) /* && !node->connected_to(tail)*/) { tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, node))); + } // Insert before some other node SharedPtr<Node> head = machine.random_node(); - if (head && head != node/* && !head->connected_to(node)*/) + if (head && (head != node) /* && !head->connected_to(node)*/) { node->add_edge(boost::shared_ptr<Edge>(new Edge(node, head))); + } } void @@ -87,8 +94,9 @@ RemoveNode::mutate(Machine& machine) //cout << "REMOVE NODE" << endl; SharedPtr<Node> node = machine.random_node(); - if (node && !node->is_initial()) + if (node && !node->is_initial()) { machine.remove_node(node); + } } void @@ -98,12 +106,14 @@ AdjustNode::mutate(Machine& machine) SharedPtr<Node> node = machine.random_node(); if (node) { - SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>(node->enter_action()); - SharedPtr<MidiAction> exit_action = PtrCast<MidiAction>(node->exit_action()); + SharedPtr<MidiAction> enter_action = PtrCast<MidiAction>( + node->enter_action()); + SharedPtr<MidiAction> exit_action = PtrCast<MidiAction>( + node->exit_action()); if (enter_action && exit_action) { const uint8_t note = rand() % 128; enter_action->event()[1] = note; - exit_action->event()[1] = note; + exit_action->event()[1] = note; } node->set_changed(); } @@ -114,13 +124,15 @@ SwapNodes::mutate(Machine& machine) { //cout << "SWAP NODE" << endl; - if (machine.nodes().size() <= 1) + if (machine.nodes().size() <= 1) { return; + } SharedPtr<Node> a = machine.random_node(); SharedPtr<Node> b = machine.random_node(); - while (b == a) + 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()); @@ -131,9 +143,9 @@ SwapNodes::mutate(Machine& machine) uint8_t note_b = b_enter->event()[1]; a_enter->event()[1] = note_b; - a_exit->event()[1] = note_b; + a_exit->event()[1] = note_b; b_enter->event()[1] = note_a; - b_exit->event()[1] = note_a; + b_exit->event()[1] = note_a; } void @@ -144,7 +156,8 @@ AddEdge::mutate(Machine& machine) SharedPtr<Node> tail = machine.random_node(); SharedPtr<Node> head = machine.random_node(); - if (tail && head && tail != head/* && !tail->connected_to(head) && !head->connected_to(tail)*/) { + if (tail && head && tail != head) { + // && !tail->connected_to(head) && !head->connected_to(tail) SharedPtr<Edge> edge(new Edge(tail, head)); edge->set_probability(rand() / (float)RAND_MAX); tail->add_edge(boost::shared_ptr<Edge>(new Edge(tail, head))); @@ -157,8 +170,9 @@ RemoveEdge::mutate(Machine& machine) //cout << "REMOVE EDGE" << endl; SharedPtr<Node> tail = machine.random_node(); - if (tail && !(tail->is_initial() && tail->edges().size() == 1)) + if (tail && !(tail->is_initial() && tail->edges().size() == 1)) { tail->remove_edge(tail->random_edge()); + } } void @@ -175,4 +189,3 @@ AdjustEdge::mutate(Machine& machine) } // namespace Mutation } // namespace Machina - diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp index 744d7ac..c0e4654 100644 --- a/src/engine/Node.cpp +++ b/src/engine/Node.cpp @@ -39,12 +39,12 @@ Node::Node(TimeDuration duration, bool initial) , _is_initial(initial) , _is_selector(false) , _is_active(false) -{ -} +{} Node::Node(const Node& copy) - : Stateful() // don't copy RDF ID - , _enter_time(copy._enter_time) + : Stateful() + , // don't copy RDF ID + _enter_time(copy._enter_time) , _duration(copy._duration) , _enter_action(ActionFactory::copy(copy._enter_action)) , _exit_action(ActionFactory::copy(copy._exit_action)) @@ -52,7 +52,8 @@ Node::Node(const Node& copy) , _is_selector(copy._is_selector) , _is_active(false) { - for (Edges::const_iterator i = copy._edges.begin(); i != copy._edges.end(); ++i) { + for (Edges::const_iterator i = copy._edges.begin(); i != copy._edges.end(); + ++i) { SharedPtr<Edge> edge(new Edge(*i->get())); _edges.push_back(edge); } @@ -63,13 +64,15 @@ SharedPtr<Edge> Node::random_edge() { SharedPtr<Edge> ret; - if (_edges.empty()) + if (_edges.empty()) { return ret; + } size_t i = rand() % _edges.size(); // FIXME: O(n) worst case :( - for (Edges::const_iterator e = _edges.begin(); e != _edges.end(); ++e, --i) { + for (Edges::const_iterator e = _edges.begin(); e != _edges.end(); ++e, + --i) { if (i == 0) { ret = *e; break; @@ -82,17 +85,20 @@ Node::random_edge() void Node::edges_changed() { - if ( ! _is_selector) + if (!_is_selector) { return; + } // Normalize edge probabilities if we're a selector double prob_sum = 0; - for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) + for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) { prob_sum += (*i)->probability(); + } - for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) + for (Edges::iterator i = _edges.begin(); i != _edges.end(); ++i) { (*i)->set_probability((*i)->probability() / prob_sum); + } _changed = true; } @@ -102,8 +108,9 @@ Node::set_selector(bool yn) { _is_selector = yn; - if (yn) + if (yn) { edges_changed(); + } _changed = true; } @@ -112,14 +119,14 @@ void Node::set_enter_action(SharedPtr<Action> action) { _enter_action = action; - _changed = true; + _changed = true; } void Node::set_exit_action(SharedPtr<Action> action) { _exit_action = action; - _changed = true; + _changed = true; } void @@ -127,12 +134,13 @@ Node::enter(MIDISink* sink, TimeStamp time) { assert(!_is_active); - _changed = true; - _is_active = true; + _changed = true; + _is_active = true; _enter_time = time; - if (sink && _enter_action) + if (sink && _enter_action) { _enter_action->execute(sink, time); + } } void @@ -140,11 +148,12 @@ Node::exit(MIDISink* sink, TimeStamp time) { assert(_is_active); - if (sink && _exit_action) + if (sink && _exit_action) { _exit_action->execute(sink, time); + } - _changed = true; - _is_active = false; + _changed = true; + _is_active = false; _enter_time = 0; } @@ -152,9 +161,11 @@ void Node::add_edge(SharedPtr<Edge> edge) { assert(edge->tail().lock().get() == this); - for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) - if ((*i)->head() == edge->head()) + for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { + if ((*i)->head() == edge->head()) { return; + } + } _edges.push_back(edge); edges_changed(); @@ -170,9 +181,11 @@ Node::remove_edge(SharedPtr<Edge> edge) bool Node::connected_to(SharedPtr<Node> node) { - for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) - if ((*i)->head() == node) + for (Edges::const_iterator i = _edges.begin(); i != _edges.end(); ++i) { + if ((*i)->head() == node) { return true; + } + } return false; } @@ -211,29 +224,29 @@ Node::write_state(Sord::Model& model) if (_is_selector) model.add_statement( - rdf_id, - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:SelectorNode")); + rdf_id, + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:SelectorNode")); else model.add_statement( - rdf_id, - Sord::Curie(model.world(), "rdf:type"), - Sord::Curie(model.world(), "machina:Node")); + rdf_id, + Sord::Curie(model.world(), "rdf:type"), + Sord::Curie(model.world(), "machina:Node")); std::cerr << "ATOMRDF" << std::endl; /* - model.add_statement( - rdf_id, - Sord::Curie(model.world(), "machina:duration"), - AtomRDF::atom_to_node(model, Atom((float)_duration.to_double()))); - */ + model.add_statement( + rdf_id, + Sord::Curie(model.world(), "machina:duration"), + AtomRDF::atom_to_node(model, Atom((float)_duration.to_double()))); + */ if (_enter_action) { - _enter_action->write_state(model); + _enter_action->write_state(model); - model.add_statement(rdf_id, - Sord::Curie(model.world(), "machina:enterAction"), - _enter_action->rdf_id(model.world())); + model.add_statement(rdf_id, + Sord::Curie(model.world(), "machina:enterAction"), + _enter_action->rdf_id(model.world())); } if (_exit_action) { @@ -246,4 +259,3 @@ Node::write_state(Sord::Model& model) } } // namespace Machina - diff --git a/src/engine/Node.hpp b/src/engine/Node.hpp index 76995b5..cf41b26 100644 --- a/src/engine/Node.hpp +++ b/src/engine/Node.hpp @@ -42,9 +42,11 @@ using Raul::TimeUnit; * 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(TimeDuration duration, bool initial = false); Node(const Node& copy); void set_enter_action(SharedPtr<Action> action); @@ -58,10 +60,10 @@ public: void edges_changed(); - void add_edge(SharedPtr<Edge> edge); - void remove_edge(SharedPtr<Edge> edge); + 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); + bool connected_to(SharedPtr<Node> node); void set(URIInt key, const Raul::Atom& value); void write_state(Sord::Model& model); @@ -69,10 +71,10 @@ public: bool is_initial() const { return _is_initial; } void set_initial(bool i) { _is_initial = i; } bool is_active() const { return _is_active; } - TimeStamp enter_time() const { assert(_is_active); return _enter_time; } - TimeStamp exit_time() const { assert(_is_active); return _enter_time + _duration; } + TimeStamp enter_time() const { return _enter_time; } + TimeStamp exit_time() const { return _enter_time + _duration; } TimeDuration duration() const { return _duration; } - void set_duration(TimeDuration d) { _duration = d; } + void set_duration(TimeDuration d) { _duration = d; } bool is_selector() const { return _is_selector; } void set_selector(bool i); diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index 7607829..5c5992d 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -38,12 +38,13 @@ 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, + SharedPtr<Machine> seed) : _unit(unit) , _target(*this) , _seed(new Machine(*seed.get())) { - SMFReader smf; + SMFReader smf; const bool opened = smf.open(target_midi); assert(opened); @@ -60,7 +61,7 @@ Problem::Problem(TimeUnit unit, const std::string& target_midi, SharedPtr<Machin if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) { const uint8_t note = buf[1]; /*++_target._note_frequency[note]; - ++_target.n_notes();*/ + ++_target.n_notes();*/ ++_target._counts[note]; _target._notes.push_back(note); } @@ -81,8 +82,9 @@ Problem::fitness(const Machine& const_machine) const Machine& machine = const_cast<Machine&>(const_machine); map<Machine*, float>::const_iterator cached = _fitness.find(&machine); - if (cached != _fitness.end()) + if (cached != _fitness.end()) { return cached->second; + } SharedPtr<Evaluator> eval(new Evaluator(*this)); @@ -94,20 +96,21 @@ Problem::fitness(const Machine& const_machine) const // FIXME: timing stuff here isn't right at all... static const unsigned ppqn = MACHINA_PPQN; - Raul::TimeSlice time(ppqn, ppqn, 120.0); + Raul::TimeSlice time(ppqn, ppqn, 120.0); time.set_slice(TimeStamp(_unit, 0, 0), TimeDuration(_unit, 2 * ppqn)); machine.run(time); - if (eval->n_notes() == 0) + if (eval->n_notes() == 0) { return 0.0f; // bad dog - + } TimeStamp end(_unit, time.start_ticks().ticks() + 2 * ppqn); time.set_slice(end, TimeStamp(_unit, 0, 0)); while (eval->n_notes() < _target.n_notes()) { machine.run(time); - if (machine.is_finished()) + if (machine.is_finished()) { machine.reset(time.start_ticks()); + } time.set_slice(end, TimeStamp(end.unit(), 0, 0)); } @@ -117,11 +120,11 @@ Problem::fitness(const Machine& const_machine) const #if 0 float f = 0; - for (uint8_t i=0; i < 128; ++i) { + for (uint8_t i = 0; i < 128; ++i) { /*if (eval->_note_frequency[i] <= _target._note_frequency[i]) - f += eval->_note_frequency[i]; - else - f -= _target._note_frequency[i] - eval->_note_frequency[i];*/ + f += eval->_note_frequency[i]; + else + f -= _target._note_frequency[i] - eval->_note_frequency[i];*/ //f -= fabs(eval->_note_frequency[i] - _target._note_frequency[i]); } #endif @@ -132,24 +135,30 @@ Problem::fitness(const Machine& const_machine) const float f = 0.0; - for (Evaluator::Patterns::const_iterator i = eval->_patterns.begin(); i != eval->_patterns.end(); ++i) { + for (Evaluator::Patterns::const_iterator i = eval->_patterns.begin(); + i != eval->_patterns.end(); ++i) { // Reward for matching patterns if (_target._patterns.find(i->first) != _target._patterns.end()) { - Evaluator::Patterns::const_iterator c = _target._patterns.find(i->first); - const uint32_t cnt = (c == _target._patterns.end()) ? 1 : c->second; - f += min(i->second, cnt) * (i->first.length()); + Evaluator::Patterns::const_iterator c = _target._patterns.find( + i->first); + const uint32_t cnt + = (c == _target._patterns.end()) ? 1 : c->second; + f += min(i->second, cnt) * (i->first.length()); - // Punish for bad patterns } else { + // Punish for bad patterns const uint32_t invlen = (eval->_order - i->first.length() + 1); - f -= (i->second / (float)eval->_patterns.size() * (float)(invlen*invlen*invlen)) * 4; + f -= (i->second / (float)eval->_patterns.size() + * (float)(invlen * invlen * invlen)) * 4; } } // Punish for missing patterns - for (Evaluator::Patterns::const_iterator i = _target._patterns.begin(); i != _target._patterns.end(); ++i) { + for (Evaluator::Patterns::const_iterator i = _target._patterns.begin(); + i != _target._patterns.end(); ++i) { if (eval->_patterns.find(i->first) == _target._patterns.end()) { - f -= i->second / (float)_target.n_notes() * (float)(eval->_order - i->first.length() + 1); + f -= i->second / (float)_target.n_notes() + * (float)(eval->_order - i->first.length() + 1); } } @@ -169,28 +178,31 @@ Problem::Evaluator::write_event(Raul::TimeStamp time, const uint8_t note = ev[1]; - if (_first_note == 0) + if (_first_note == 0) { _first_note = note; + } /*++_note_frequency[note]; - ++n_notes();*/ + ++n_notes();*/ //_notes.push_back(note); if (_read.length() == 0) { _read = note; return; } - if (_read.length() == _order) + if (_read.length() == _order) { _read = _read.substr(1); + } _read = _read + (char)note; for (size_t i = 0; i < _read.length(); ++i) { - const string pattern = _read.substr(i); - Patterns::iterator i = _patterns.find(pattern); - if (i != _patterns.end()) + const string pattern = _read.substr(i); + Patterns::iterator i = _patterns.find(pattern); + if (i != _patterns.end()) { ++(i->second); - else + } else { _patterns[pattern] = 1; + } } ++_counts[note]; @@ -203,12 +215,12 @@ void Problem::Evaluator::compute() { /* - for (uint8_t i=0; i < 128; ++i) { - if (_note_frequency[i] > 0) { - _note_frequency[i] /= (float)n_notes(); - //cout << (int)i << ":\t" << _note_frequency[i] << endl; - } - }*/ + for (uint8_t i=0; i < 128; ++i) { + if (_note_frequency[i] > 0) { + _note_frequency[i] /= (float)n_notes(); + //cout << (int)i << ":\t" << _note_frequency[i] << endl; + } + }*/ } boost::shared_ptr<Problem::Population> @@ -219,10 +231,10 @@ 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)); - for (uint8_t i=0; i < 128; ++i) { + 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))); + SharedPtr<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); @@ -235,10 +247,12 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const Machine m(*base.get()); set< SharedPtr<Node> > unreachable; - SharedPtr<Node> initial; + SharedPtr<Node> initial; - for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); ++i) { - if (PtrCast<MidiAction>((*i)->enter_action())->event()[1] == _target.first_note()) { + for (Machine::Nodes::iterator i = m.nodes().begin(); i != m.nodes().end(); + ++i) { + if (PtrCast<MidiAction>((*i)->enter_action())->event()[1] == + _target.first_note()) { (*i)->set_initial(true); initial = *i; } else { @@ -250,13 +264,14 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const unreachable.erase(cur); SharedPtr<Node> head; - while ( ! unreachable.empty()) { - if (rand() % 2) + while (!unreachable.empty()) { + if (rand() % 2) { head = m.random_node(); - else + } else { head = *unreachable.begin(); + } - if ( ! head->connected_to(head) ) { + if (!head->connected_to(head) ) { cur->add_edge(SharedPtr<Edge>(new Edge(cur, head))); unreachable.erase(head); cur = head; @@ -266,7 +281,7 @@ Problem::initial_population(size_t gene_size, size_t pop_size) const ret->push_back(m); /*cout << "initial # nodes: " << m.nodes().size(); - cout << "initial fitness: " << fitness(m) << endl;*/ + cout << "initial fitness: " << fitness(m) << endl;*/ } return ret; @@ -286,22 +301,27 @@ Problem::distance(const std::vector<uint8_t>& source, const uint16_t n = source.size(); const uint16_t m = target.size(); - if (n == 0) + if (n == 0) { return m; - if (m == 0) + } + if (m == 0) { return n; + } _matrix.resize(n + 1); - for (uint16_t i = 0; i <= n; i++) - _matrix[i].resize (m + 1); + for (uint16_t i = 0; i <= n; i++) { + _matrix[i].resize(m + 1); + } // Step 2 - for (uint16_t i = 0; i <= n; i++) + for (uint16_t i = 0; i <= n; i++) { _matrix[i][0] = i; + } - for (uint16_t j = 0; j <= m; j++) + for (uint16_t j = 0; j <= m; j++) { _matrix[0][j] = j; + } // Step 3 @@ -327,9 +347,9 @@ Problem::distance(const std::vector<uint8_t>& source, // Step 6 const uint16_t above = _matrix[i - 1][j]; - const uint16_t left = _matrix[i][j - 1]; - const uint16_t diag = _matrix[i - 1][j - 1]; - uint16_t cell = min (above + 1, min (left + 1, diag + cost)); + const uint16_t left = _matrix[i][j - 1]; + const uint16_t diag = _matrix[i - 1][j - 1]; + uint16_t cell = min(above + 1, min(left + 1, diag + cost)); // Step 6A: Cover transposition, in addition to deletion, // insertion and substitution. This step is taken from: @@ -337,14 +357,17 @@ Problem::distance(const std::vector<uint8_t>& source, // Enhanced Dynamic Programming ASM Algorithm" // (http://www.acm.org/~hlb/publications/asm/asm.html) - if (i > 2 && j > 2) { + if (( i > 2) && ( j > 2) ) { uint16_t trans = _matrix[i - 2][j - 2] + 1; - if (source[i - 2] != t_j) + if (source[i - 2] != t_j) { trans++; - if (s_i != target[j - 2]) + } + if (s_i != target[j - 2]) { trans++; - if (cell > trans) + } + if (cell > trans) { cell = trans; + } } _matrix[i][j] = cell; @@ -357,4 +380,3 @@ Problem::distance(const std::vector<uint8_t>& source, } } // namespace Machina - diff --git a/src/engine/Recorder.cpp b/src/engine/Recorder.cpp index 4a67f65..8bc9a4a 100644 --- a/src/engine/Recorder.cpp +++ b/src/engine/Recorder.cpp @@ -35,9 +35,8 @@ Recorder::Recorder(Raul::Forge& forge, , _unit(unit) , _record_buffer(buffer_size) , _builder(new MachineBuilder( - SharedPtr<Machine>(new Machine(unit)), q, step)) -{ -} + SharedPtr<Machine>(new Machine(unit)), q, step)) +{} void Recorder::_whipped() @@ -72,4 +71,3 @@ Recorder::finish() } } - diff --git a/src/engine/Recorder.hpp b/src/engine/Recorder.hpp index 9c669b9..ce9cad8 100644 --- a/src/engine/Recorder.hpp +++ b/src/engine/Recorder.hpp @@ -29,7 +29,9 @@ namespace Machina { class MachineBuilder; -class Recorder : public Slave { +class Recorder + : public Slave +{ public: Recorder(Raul::Forge& forge, size_t buffer_size, @@ -37,8 +39,10 @@ public: double q, bool step); - inline void write(Raul::TimeStamp time, size_t size, const unsigned char* buf) { - if (_record_buffer.write_space() < (sizeof(TimeStamp) + sizeof(size_t) + size)) { + inline void write(Raul::TimeStamp time, size_t size, + const unsigned char* buf) { + if (_record_buffer.write_space() < + (sizeof(TimeStamp) + sizeof(size_t) + size)) { std::cerr << "Record buffer overflow" << std::endl; return; } else { diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp index 641baf9..a07f47d 100644 --- a/src/engine/SMFDriver.cpp +++ b/src/engine/SMFDriver.cpp @@ -49,11 +49,13 @@ SMFDriver::SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit) * @return the resulting machine. */ SharedPtr<Machine> -SMFDriver::learn(const string& filename, unsigned track, double q, Raul::TimeDuration max_duration) +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>(new MachineBuilder(m, q, false)); + SharedPtr<Machine> m(new Machine(max_duration.unit())); + SharedPtr<MachineBuilder> builder = SharedPtr<MachineBuilder>( + new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { @@ -61,17 +63,19 @@ SMFDriver::learn(const string& filename, unsigned track, double q, Raul::TimeDur return SharedPtr<Machine>(); } - if (track > reader.num_tracks()) + if (track > reader.num_tracks()) { return SharedPtr<Machine>(); - else + } else { learn_track(builder, reader, track, q, max_duration); + } m->reset(NULL, m->time()); - if (m->nodes().size() > 1) + if (m->nodes().size() > 1) { return m; - else + } else { return SharedPtr<Machine>(); + } } /** Learn all tracks from a MIDI file into a single machine. @@ -81,8 +85,9 @@ SMFDriver::learn(const string& filename, unsigned track, double q, Raul::TimeDur SharedPtr<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>(new MachineBuilder(m, q, false)); + SharedPtr<Machine> m(new Machine(max_duration.unit())); + SharedPtr<MachineBuilder> builder = SharedPtr<MachineBuilder>( + new MachineBuilder(m, q, false)); SMFReader reader; if (!reader.open(filename)) { @@ -90,17 +95,18 @@ SMFDriver::learn(const string& filename, double q, Raul::TimeStamp max_duration) return SharedPtr<Machine>(); } - for (unsigned t=1; t <= reader.num_tracks(); ++t) { + for (unsigned t = 1; t <= reader.num_tracks(); ++t) { builder->reset(); learn_track(builder, reader, t, q, max_duration); } m->reset(NULL, m->time()); - if (m->nodes().size() > 1) + if (m->nodes().size() > 1) { return m; - else + } else { return SharedPtr<Machine>(); + } } void @@ -111,27 +117,30 @@ SMFDriver::learn_track(SharedPtr<MachineBuilder> builder, Raul::TimeDuration max_duration) { const bool found_track = reader.seek_to_track(track); - if (!found_track) + if (!found_track) { return; + } uint8_t buf[4]; uint32_t ev_size; uint32_t ev_delta_time; - uint64_t t = 0; + uint64_t t = 0; double unquantized_t = 0; while (reader.read_event(4, buf, &ev_size, &ev_delta_time) >= 0) { unquantized_t += ev_delta_time; - t = quantize(q, unquantized_t); + t = quantize(q, unquantized_t); builder->set_time(TimeStamp(max_duration.unit(), (double)t)); - if ((!max_duration.is_zero()) && t > max_duration.to_double()) + if ((!max_duration.is_zero()) && ( t > max_duration.to_double()) ) { break; + } - if (ev_size > 0) + if (ev_size > 0) { builder->event(TimeStamp(max_duration.unit(), 0, 0), ev_size, buf); + } } builder->resolve(); @@ -141,9 +150,11 @@ void SMFDriver::run(SharedPtr<Machine> machine, Raul::TimeStamp max_time) { // FIXME: unit kludge (tempo only) - Context context(_forge, machine->time().unit().ppt(), _writer->unit().ppt(), 120.0); + Context context(_forge, machine->time().unit().ppt(), + _writer->unit().ppt(), 120.0); context.set_sink(this); - context.time().set_slice(TimeStamp(max_time.unit(), 0, 0), context.time().beats_to_ticks(max_time)); + context.time().set_slice(TimeStamp(max_time.unit(), 0, 0), + context.time().beats_to_ticks(max_time)); machine->run(context, SharedPtr<UpdateBuffer>()); } diff --git a/src/engine/SMFDriver.hpp b/src/engine/SMFDriver.hpp index f978849..abed40d 100644 --- a/src/engine/SMFDriver.hpp +++ b/src/engine/SMFDriver.hpp @@ -34,7 +34,9 @@ namespace Machina { class Node; class Machine; -class SMFDriver : public Driver { +class SMFDriver + : public Driver +{ public: SMFDriver(Raul::Forge& forge, Raul::TimeUnit unit); @@ -54,8 +56,8 @@ public: const unsigned char* ev) throw (std::logic_error) { _writer->write_event(time, ev_size, ev); } - void set_bpm(double /*bpm*/) { } - void set_quantization(double /*quantization*/) { } + void set_bpm(double /*bpm*/) {} + void set_quantization(double /*quantization*/) {} SharedPtr<SMFWriter> writer() { return _writer; } @@ -72,4 +74,3 @@ private: } // namespace Machina #endif // MACHINA_SMFDRIVER_HPP - diff --git a/src/engine/SMFReader.cpp b/src/engine/SMFReader.cpp index cba1fb0..62cdd26 100644 --- a/src/engine/SMFReader.cpp +++ b/src/engine/SMFReader.cpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -40,31 +40,31 @@ midi_event_size(unsigned char status) } switch (status) { - case LV2_MIDI_MSG_NOTE_OFF: - case LV2_MIDI_MSG_NOTE_ON: - case LV2_MIDI_MSG_NOTE_PRESSURE: - case LV2_MIDI_MSG_CONTROLLER: - case LV2_MIDI_MSG_BENDER: - case LV2_MIDI_MSG_SONG_POS: - return 2; - - case LV2_MIDI_MSG_PGM_CHANGE: - case LV2_MIDI_MSG_CHANNEL_PRESSURE: - case LV2_MIDI_MSG_MTC_QUARTER: - case LV2_MIDI_MSG_SONG_SELECT: - return 1; - - case LV2_MIDI_MSG_TUNE_REQUEST: - case LV2_MIDI_MSG_CLOCK: - case LV2_MIDI_MSG_START: - case LV2_MIDI_MSG_CONTINUE: - case LV2_MIDI_MSG_STOP: - case LV2_MIDI_MSG_ACTIVE_SENSE: - case LV2_MIDI_MSG_RESET: - return 0; + case LV2_MIDI_MSG_NOTE_OFF: + case LV2_MIDI_MSG_NOTE_ON: + case LV2_MIDI_MSG_NOTE_PRESSURE: + case LV2_MIDI_MSG_CONTROLLER: + case LV2_MIDI_MSG_BENDER: + case LV2_MIDI_MSG_SONG_POS: + return 2; + + case LV2_MIDI_MSG_PGM_CHANGE: + case LV2_MIDI_MSG_CHANNEL_PRESSURE: + case LV2_MIDI_MSG_MTC_QUARTER: + case LV2_MIDI_MSG_SONG_SELECT: + return 1; + + case LV2_MIDI_MSG_TUNE_REQUEST: + case LV2_MIDI_MSG_CLOCK: + case LV2_MIDI_MSG_START: + case LV2_MIDI_MSG_CONTINUE: + case LV2_MIDI_MSG_STOP: + case LV2_MIDI_MSG_ACTIVE_SENSE: + case LV2_MIDI_MSG_RESET: + return 0; - case LV2_MIDI_MSG_SYSTEM_EXCLUSIVE: - return -1; + case LV2_MIDI_MSG_SYSTEM_EXCLUSIVE: + return -1; } return -1; @@ -76,21 +76,26 @@ SMFReader::SMFReader(const std::string filename) , _track(0) , _track_size(0) { - if (filename.length() > 0) + if (filename.length() > 0) { open(filename); + } } SMFReader::~SMFReader() { - if (_fd) + if (_fd) { close(); + } } bool -SMFReader::open(const std::string& filename) throw (std::logic_error, UnsupportedTime) +SMFReader::open(const std::string& filename) + throw (std::logic_error, UnsupportedTime) { - if (_fd) - throw std::logic_error("Attempt to start new read while write in progress."); + if (_fd) { + throw std::logic_error( + "Attempt to start new read while write in progress."); + } std::cout << "Opening SMF file " << filename << " for reading." << endl; @@ -126,8 +131,9 @@ SMFReader::open(const std::string& filename) throw (std::logic_error, Unsupporte _ppqn = GUINT16_FROM_BE(ppqn_be); // TODO: Absolute (SMPTE seconds) time support - if ((_ppqn & 0x8000) != 0) + if ((_ppqn & 0x8000) != 0) { throw UnsupportedTime(); + } seek_to_track(1); @@ -143,11 +149,13 @@ SMFReader::open(const std::string& filename) throw (std::logic_error, Unsupporte bool SMFReader::seek_to_track(unsigned track) throw (std::logic_error) { - if (track == 0) + if (track == 0) { throw std::logic_error("Seek to track 0 out of range (must be >= 1)"); + } - if (!_fd) + if (!_fd) { throw std::logic_error("Attempt to seek to track on unopened SMF file."); + } unsigned track_pos = 0; @@ -169,14 +177,15 @@ SMFReader::seek_to_track(unsigned track) throw (std::logic_error) fread(&chunk_size_be, 4, 1, _fd); chunk_size = GUINT32_FROM_BE(chunk_size_be); - if (track_pos == track) + if (track_pos == track) { break; + } fseek(_fd, chunk_size, SEEK_CUR); } - if (!feof(_fd) && track_pos == track) { - _track = track; + if (!feof(_fd) && ( track_pos == track) ) { + _track = track; _track_size = chunk_size; return true; } else { @@ -204,8 +213,9 @@ SMFReader::read_event(size_t buf_len, uint32_t* delta_time) throw (std::logic_error, PrematureEOF, CorruptFile) { - if (_track == 0) + if (_track == 0) { throw std::logic_error("Attempt to read from unopened SMF file"); + } if (!_fd || feof(_fd)) { return -1; @@ -222,30 +232,33 @@ SMFReader::read_event(size_t buf_len, *delta_time = read_var_len(_fd); int status = fgetc(_fd); - if (status == EOF) + if (status == EOF) { throw PrematureEOF(); - else if (status > 0xFF) + } else if (status > 0xFF) { throw CorruptFile(); + } if (status < 0x80) { - if (last_status == 0) + if (last_status == 0) { throw CorruptFile(); - status = last_status; + } + status = last_status; *ev_size = last_size; fseek(_fd, -1, SEEK_CUR); } else { last_status = status; - *ev_size = midi_event_size(status) + 1; - last_size = *ev_size; + *ev_size = midi_event_size(status) + 1; + last_size = *ev_size; } buf[0] = static_cast<uint8_t>(status); if (status == 0xFF) { *ev_size = 0; - if (feof(_fd)) + if (feof(_fd)) { throw PrematureEOF(); - uint8_t type = fgetc(_fd); + } + uint8_t type = fgetc(_fd); const uint32_t size = read_var_len(_fd); if (type == 0x2F) { @@ -256,18 +269,19 @@ SMFReader::read_event(size_t buf_len, } } - if (*ev_size > buf_len || *ev_size == 0 || feof(_fd)) { + if ((*ev_size > buf_len) || (*ev_size == 0) || feof(_fd)) { // Skip event, return 0 fseek(_fd, *ev_size - 1, SEEK_CUR); return 0; } else { // Read event, return size - if (ferror(_fd)) + if (ferror(_fd)) { throw CorruptFile(); + } - fread(buf+1, 1, *ev_size - 1, _fd); + fread(buf + 1, 1, *ev_size - 1, _fd); - if ((buf[0] & 0xF0) == 0x90 && buf[2] == 0) { + if (((buf[0] & 0xF0) == 0x90) && (buf[2] == 0) ) { buf[0] = (0x80 | (buf[0] & 0x0F)); buf[2] = 0x40; } @@ -279,8 +293,9 @@ SMFReader::read_event(size_t buf_len, void SMFReader::close() { - if (_fd) + if (_fd) { fclose(_fd); + } _fd = NULL; } @@ -288,17 +303,19 @@ SMFReader::close() uint32_t SMFReader::read_var_len(FILE* fd) throw (PrematureEOF) { - if (feof(fd)) + if (feof(fd)) { throw PrematureEOF(); + } uint32_t value; uint8_t c; - if ( (value = getc(fd)) & 0x80 ) { + if ( (value = getc(fd)) & 0x80) { value &= 0x7F; do { - if (feof(fd)) + if (feof(fd)) { throw PrematureEOF(); + } value = (value << 7) + ((c = getc(fd)) & 0x7F); } while (c & 0x80); } @@ -307,4 +324,3 @@ SMFReader::read_var_len(FILE* fd) throw (PrematureEOF) } } // namespace Machina - diff --git a/src/engine/SMFReader.hpp b/src/engine/SMFReader.hpp index 053a62c..a3f38ed 100644 --- a/src/engine/SMFReader.hpp +++ b/src/engine/SMFReader.hpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -14,8 +14,8 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef RAUL_SMF_READER_HPP -#define RAUL_SMF_READER_HPP +#ifndef MACHINA_SMF_READER_HPP +#define MACHINA_SMF_READER_HPP #include <stdexcept> #include <string> @@ -29,22 +29,31 @@ namespace Machina { * Currently this only reads SMF files with tempo-based timing. * \ingroup raul */ -class SMFReader { +class SMFReader +{ public: - class PrematureEOF : public std::exception { + class PrematureEOF + : public std::exception + { const char* what() const throw() { return "Unexpected end of file"; } }; - class CorruptFile : public std::exception { + class CorruptFile + : public std::exception + { const char* what() const throw() { return "Corrupted file"; } }; - class UnsupportedTime : public std::exception { - const char* what() const throw() { return "Unsupported time stamp type (SMPTE)"; } + class UnsupportedTime + : public std::exception + { + const char* what() const throw() { return + "Unsupported time stamp type (SMPTE)"; } }; - explicit SMFReader(const std::string filename=""); + explicit SMFReader(const std::string filename = ""); ~SMFReader(); - bool open(const std::string& filename) throw (std::logic_error, UnsupportedTime); + bool open(const std::string& filename) throw (std::logic_error, + UnsupportedTime); bool seek_to_track(unsigned track) throw (std::logic_error); @@ -56,7 +65,7 @@ public: uint8_t* buf, uint32_t* ev_size, uint32_t* ev_delta_time) - throw (std::logic_error, PrematureEOF, CorruptFile); + throw (std::logic_error, PrematureEOF, CorruptFile); void close(); @@ -77,5 +86,4 @@ protected: } // namespace Machina -#endif // RAUL_SMF_READER_HPP - +#endif // MACHINA_SMF_READER_HPP diff --git a/src/engine/SMFWriter.cpp b/src/engine/SMFWriter.cpp index ad993bc..41abfe0 100644 --- a/src/engine/SMFWriter.cpp +++ b/src/engine/SMFWriter.cpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -45,14 +45,16 @@ SMFWriter::SMFWriter(Raul::TimeUnit unit) , _track_size(0) , _header_size(0) { - if (unit.type() == Raul::TimeUnit::BEATS) + if (unit.type() == Raul::TimeUnit::BEATS) { assert(unit.ppt() < std::numeric_limits<uint16_t>::max()); + } } SMFWriter::~SMFWriter() { - if (_fd) + if (_fd) { finish(); + } } /** Start a write to an SMF file. @@ -65,17 +67,19 @@ bool SMFWriter::start(const std::string& filename, Raul::TimeStamp start_time) throw (std::logic_error) { - if (_fd) - throw std::logic_error("Attempt to start new write while write in progress."); + if (_fd) { + throw std::logic_error( + "Attempt to start new write while write in progress."); + } std::cout << "Opening SMF file " << filename << " for writing." << endl; _fd = fopen(filename.c_str(), "w+"); if (_fd) { - _track_size = 0; - _filename = filename; - _start_time = start_time; + _track_size = 0; + _filename = filename; + _start_time = start_time; _last_ev_time = 0; // write a tentative header to pad file out so writing starts at the right offset write_header(); @@ -95,19 +99,21 @@ SMFWriter::write_event(Raul::TimeStamp time, size_t ev_size, const unsigned char* ev) throw (std::logic_error) { - if (time < _start_time) + if (time < _start_time) { throw std::logic_error("Event time is before file start time"); - else if (time < _last_ev_time) + } else if (time < _last_ev_time) { throw std::logic_error("Event time not monotonically increasing"); - else if (time.unit() != _unit) + } else if (time.unit() != _unit) { throw std::logic_error("Event has unexpected time unit"); + } Raul::TimeStamp delta_time = time; delta_time -= _start_time; fseek(_fd, 0, SEEK_END); - uint64_t delta_ticks = delta_time.ticks() * _unit.ppt() + delta_time.subticks(); + uint64_t delta_ticks = delta_time.ticks() * _unit.ppt() + + delta_time.subticks(); size_t stamp_size = 0; /* If delta time is too long (i.e. overflows), write out empty @@ -127,21 +133,24 @@ SMFWriter::write_event(Raul::TimeStamp time, fwrite(ev, 1, ev_size, _fd); _last_ev_time = time; - _track_size += stamp_size + ev_size; + _track_size += stamp_size + ev_size; } void SMFWriter::flush() { - if (_fd) + if (_fd) { fflush(_fd); + } } void SMFWriter::finish() throw (std::logic_error) { - if (!_fd) - throw std::logic_error("Attempt to finish write with no write in progress."); + if (!_fd) { + throw std::logic_error( + "Attempt to finish write with no write in progress."); + } write_footer(); fclose(_fd); @@ -161,8 +170,8 @@ SMFWriter::write_header() char data[6]; memcpy(data, &type, 2); - memcpy(data+2, &ntracks, 2); - memcpy(data+4, &division, 2); + memcpy(data + 2, &ntracks, 2); + memcpy(data + 4, &division, 2); //data[4] = _ppqn & 0xF0; //data[5] = _ppqn & 0x0F; @@ -214,21 +223,21 @@ SMFWriter::write_var_len(uint32_t value) while ( (value >>= 7) ) { buffer <<= 8; - buffer |= ((value & 0x7F) | 0x80); + buffer |= ((value & 0x7F) | 0x80); } while (true) { //printf("Writing var len byte %X\n", (unsigned char)buffer); ++ret; fputc(buffer, _fd); - if (buffer & 0x80) + if (buffer & 0x80) { buffer >>= 8; - else + } else { break; + } } return ret; } } // namespace Machina - diff --git a/src/engine/SMFWriter.hpp b/src/engine/SMFWriter.hpp index 3e47e9f..ba530a8 100644 --- a/src/engine/SMFWriter.hpp +++ b/src/engine/SMFWriter.hpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -14,8 +14,8 @@ along with Raul. If not, see <http://www.gnu.org/licenses/>. */ -#ifndef RAUL_SMF_WRITER_HPP -#define RAUL_SMF_WRITER_HPP +#ifndef MACHINA_SMF_WRITER_HPP +#define MACHINA_SMF_WRITER_HPP #include <stdexcept> #include <string> @@ -29,7 +29,9 @@ namespace Machina { /** Standard Midi File (Type 0) Writer * \ingroup raul */ -class SMFWriter : public MIDISink { +class SMFWriter + : public MIDISink +{ public: explicit SMFWriter(Raul::TimeUnit unit); ~SMFWriter(); @@ -53,9 +55,9 @@ protected: void write_header(); void write_footer(); - void write_chunk_header(const char id[4], uint32_t length); - void write_chunk(const char id[4], uint32_t length, void* data); - size_t write_var_len(uint32_t val); + void write_chunk_header(const char id[4], uint32_t length); + void write_chunk(const char id[4], uint32_t length, void* data); + size_t write_var_len(uint32_t val); std::string _filename; FILE* _fd; @@ -68,5 +70,4 @@ protected: } // namespace Machina -#endif // RAUL_SMF_WRITER_HPP - +#endif // MACHINA_SMF_WRITER_HPP diff --git a/src/engine/Schrodinbit.hpp b/src/engine/Schrodinbit.hpp index 16effbf..aca88a2 100644 --- a/src/engine/Schrodinbit.hpp +++ b/src/engine/Schrodinbit.hpp @@ -1,5 +1,5 @@ /* This file is part of Machina. - * Copyright 2007-2011 David Robillard <http://drobilla.net> + * Copyright 2007-2012 David Robillard <http://drobilla.net> * * Machina is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,9 +18,10 @@ #ifndef SCHRODINBIT_HPP #define SCHRODINBIT_HPP -/** A flag which becomes false when it's value is observed +/** A flag which becomes false when its value is observed */ -class Schrodinbit { +class Schrodinbit +{ public: Schrodinbit() : _flag(false) {} @@ -40,4 +41,3 @@ private: }; #endif // SCHRODINBIT_HPP - diff --git a/src/engine/Stateful.cpp b/src/engine/Stateful.cpp index c2d2731..d778b85 100644 --- a/src/engine/Stateful.cpp +++ b/src/engine/Stateful.cpp @@ -23,8 +23,7 @@ uint64_t Stateful::_next_id = 1; Stateful::Stateful() : _id(next_id()) -{ -} +{} const Sord::Node& Stateful::rdf_id(Sord::World& world) const diff --git a/src/engine/Stateful.hpp b/src/engine/Stateful.hpp index 779d8f7..928be3b 100644 --- a/src/engine/Stateful.hpp +++ b/src/engine/Stateful.hpp @@ -24,13 +24,16 @@ #include "machina/types.hpp" -namespace Raul { class Atom; } +namespace Raul { +class Atom; +} namespace Machina { class URIs; -class Stateful { +class Stateful +{ public: Stateful(); @@ -39,7 +42,7 @@ public: virtual void set(URIInt key, const Raul::Atom& value) {} virtual void write_state(Sord::Model& model) = 0; - uint64_t id() const { return _id; } + uint64_t id() const { return _id; } const Sord::Node& rdf_id(Sord::World& world) const; static uint64_t next_id() { return _next_id++; } @@ -50,11 +53,13 @@ protected: private: static uint64_t _next_id; - uint64_t _id; + uint64_t _id; mutable Sord::Node _rdf_id; }; -class StatefulKey : public Stateful { +class StatefulKey + : public Stateful +{ public: StatefulKey(uint64_t id) : Stateful(id) {} diff --git a/src/engine/Updates.cpp b/src/engine/Updates.cpp index aabd4fb..16717e4 100644 --- a/src/engine/Updates.cpp +++ b/src/engine/Updates.cpp @@ -61,8 +61,8 @@ read_set(SharedPtr<UpdateBuffer> buf, *value = Raul::Atom(atom.size, atom.type, NULL); buf->read(atom.size, value->get_body()); - return sizeof(update_type) + sizeof(*subject) + sizeof(*key) + - sizeof(LV2_Atom) + atom.size; + return sizeof(update_type) + sizeof(*subject) + sizeof(*key) + + sizeof(LV2_Atom) + atom.size; } } diff --git a/src/engine/jack_compat.h b/src/engine/jack_compat.h deleted file mode 100644 index 3d8e990..0000000 --- a/src/engine/jack_compat.h +++ /dev/null @@ -1,55 +0,0 @@ -/* JACK MIDI API compatibility hacks. - * Copyright 2007 Nedko Arnaudov <nedko@arnaudov.name> - * - * Machina is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Machina is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with Machina. If not, see <http://www.gnu.org/licenses/>. - */ - -#ifndef JACK_COMPAT_H -#define JACK_COMPAT_H - -#if defined(JACK_MIDI_NEEDS_NFRAMES) - -jack_nframes_t -jack_midi_get_event_count_compat( - void * port_buffer) -{ -#if defined(HAVE_OLD_JACK_MIDI) - return jack_midi_port_get_info(port_buffer, 0)->event_count; -#else - return jack_midi_get_event_count(port_buffer, 0); -#endif -} - -#define jack_midi_get_event_count jack_midi_get_event_count_compat - -int -jack_midi_event_get_compat( - jack_midi_event_t * event, - void * port_buffer, - jack_nframes_t event_index) -{ - return jack_midi_event_get(event, port_buffer, event_index, 0); -} - -#define jack_midi_event_get jack_midi_event_get_compat - -#else - -#if defined(HAVE_OLD_JACK_MIDI) -#error "Old (0.102.20) JACK MIDI API needs nframes (autotools probably gone mad)" -#endif - -#endif /* #if defined(JACK_MIDI_NEEDS_NFRAMES) */ - -#endif /* JACK_COMPAT_H */ diff --git a/src/engine/machina/Context.hpp b/src/engine/machina/Context.hpp index c863cc4..8f86602 100644 --- a/src/engine/machina/Context.hpp +++ b/src/engine/machina/Context.hpp @@ -26,7 +26,8 @@ namespace Machina { class MIDISink; -class Context { +class Context +{ public: Context(Raul::Forge& forge, uint32_t rate, uint32_t ppqn, double bpm) : _forge(forge) diff --git a/src/engine/machina/Controller.hpp b/src/engine/machina/Controller.hpp index f5bf631..ddd1d4c 100644 --- a/src/engine/machina/Controller.hpp +++ b/src/engine/machina/Controller.hpp @@ -30,7 +30,9 @@ #include "Stateful.hpp" -namespace Raul { class Atom; class Maid; } +namespace Raul { +class Atom; class Maid; +} namespace Machina { @@ -38,18 +40,25 @@ class Engine; class Machine; class Stateful; -namespace Client { class ClientModel; class ClientObject; } +namespace Client { +class ClientModel; class ClientObject; +} -class Controller { +class Controller +{ public: Controller(SharedPtr<Engine> engine, Client::ClientModel& client_model); uint64_t create(const Client::ClientObject& obj); uint64_t connect(uint64_t tail_id, uint64_t head_id); - void set_property(uint64_t object_id, URIInt key, const Raul::Atom& value); - void learn(SharedPtr<Raul::Maid> maid, uint64_t node_id); - void disconnect(uint64_t tail_id, uint64_t head_id); - void erase(uint64_t id); + + void set_property(uint64_t object_id, + URIInt key, + const Raul::Atom& value); + + void learn(SharedPtr<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); @@ -59,9 +68,11 @@ private: SharedPtr<Stateful> find(uint64_t id); struct StatefulComparator { - inline bool operator()(SharedPtr<Stateful> lhs, SharedPtr<Stateful> rhs) const { + inline bool operator()(SharedPtr<Stateful> lhs, + SharedPtr<Stateful> rhs) const { return lhs->id() < rhs->id(); } + }; typedef std::set<SharedPtr<Stateful>, StatefulComparator> Objects; diff --git a/src/engine/machina/Driver.hpp b/src/engine/machina/Driver.hpp index 69bf951..778fd22 100644 --- a/src/engine/machina/Driver.hpp +++ b/src/engine/machina/Driver.hpp @@ -28,7 +28,9 @@ namespace Machina { class Machine; -class Driver : public MIDISink { +class Driver + : public MIDISink +{ public: Driver(Raul::Forge& forge, SharedPtr<Machine> machine) : _forge(forge) @@ -38,23 +40,29 @@ public: virtual ~Driver() {} SharedPtr<Machine> machine() { return _machine; } - virtual void set_machine(SharedPtr<Machine> machine) { _machine = machine; } + + virtual void set_machine(SharedPtr<Machine> machine) { + _machine = machine; + } SharedPtr<UpdateBuffer> update_sink() { return _updates; } - void set_update_sink(SharedPtr<UpdateBuffer> b) { _updates = b; } - virtual void set_bpm(double bpm) = 0; + void set_update_sink(SharedPtr<UpdateBuffer> b) { + _updates = b; + } + + virtual void set_bpm(double bpm) = 0; virtual void set_quantization(double q) = 0; virtual bool is_activated() const { return false; } - virtual void activate() {} - virtual void deactivate() {} + virtual void activate() {} + virtual void deactivate() {} virtual void stop() {} - virtual bool recording() { return false; } + virtual bool recording() { return false; } virtual void start_record(bool step) {} - virtual void finish_record() {} + virtual void finish_record() {} protected: Raul::Forge& _forge; diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index 5f11d55..7040082 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -32,7 +32,8 @@ namespace Machina { class Machine; -class Engine { +class Engine +{ public: Engine(Raul::Forge& forge, SharedPtr<Driver> driver, diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp index 8dd8e12..1aec3e8 100644 --- a/src/engine/machina/Evolver.hpp +++ b/src/engine/machina/Evolver.hpp @@ -25,16 +25,22 @@ #include "Schrodinbit.hpp" -namespace Eugene { template <typename G> class HybridMutation; } +namespace Eugene { +template<typename G> +class HybridMutation; +} namespace Machina { class Machine; class Problem; -class Evolver : public Raul::Thread { +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, + SharedPtr<Machine> seed); void seed(SharedPtr<Machine> parent); bool improvement() { return _improvement; } diff --git a/src/engine/machina/Loader.hpp b/src/engine/machina/Loader.hpp index 470b109..f97618a 100644 --- a/src/engine/machina/Loader.hpp +++ b/src/engine/machina/Loader.hpp @@ -31,7 +31,8 @@ namespace Machina { class Machine; -class Loader { +class Loader +{ public: Loader(Raul::Forge& forge, Sord::World& rdf_world); diff --git a/src/engine/machina/Machine.hpp b/src/engine/machina/Machine.hpp index 164c2e4..acfc599 100644 --- a/src/engine/machina/Machine.hpp +++ b/src/engine/machina/Machine.hpp @@ -41,7 +41,9 @@ class LearnRequest; /** A (Finite State) Machine. */ -class Machine : public Stateful { +class Machine + : public Stateful +{ public: Machine(TimeUnit unit); Machine(const Machine& copy); @@ -72,8 +74,8 @@ public: // Any context inline Raul::TimeStamp time() const { return _time; } - SharedPtr<LearnRequest> pending_learn() { return _pending_learn; } - void clear_pending_learn() { _pending_learn.reset(); } + SharedPtr<LearnRequest> pending_learn() { return _pending_learn; } + void clear_pending_learn() { _pending_learn.reset(); } typedef std::list< SharedPtr<Node> > Nodes; Nodes& nodes() { return _nodes; } @@ -85,10 +87,16 @@ public: private: // Audio context SharedPtr<Node> earliest_node() const; - bool enter_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffer> updates); - void exit_node(Context& context, SharedPtr<Node> node, SharedPtr<UpdateBuffer> updates); - static const size_t MAX_ACTIVE_NODES = 128; + bool enter_node(Context& context, + SharedPtr<Node> node, + SharedPtr<UpdateBuffer> updates); + + void exit_node(Context& context, + SharedPtr<Node> node, + SharedPtr<UpdateBuffer> updates); + + static const size_t MAX_ACTIVE_NODES = 128; std::vector< SharedPtr<Node> > _active_nodes; SharedPtr<LearnRequest> _pending_learn; diff --git a/src/engine/machina/Mutation.hpp b/src/engine/machina/Mutation.hpp index 437165f..dd2dbbf 100644 --- a/src/engine/machina/Mutation.hpp +++ b/src/engine/machina/Mutation.hpp @@ -21,10 +21,10 @@ #include "machina_config.h" #ifdef HAVE_EUGENE - #include "eugene/Mutation.hpp" - #define SUPER : public Eugene::Mutation<Machine> +# include "eugene/Mutation.hpp" +# define SUPER : public Eugene::Mutation <Machine> #else - #define SUPER : public Mutation +# define SUPER : public Mutation #endif namespace Machina { @@ -33,7 +33,11 @@ class Machine; namespace Mutation { -struct Mutation { virtual ~Mutation() {}; virtual void mutate(Machine& machine) = 0; }; +struct Mutation { + virtual ~Mutation() {} + + virtual void mutate(Machine& machine) = 0; +}; struct Compress SUPER { void mutate(Machine& machine); }; struct AddNode SUPER { void mutate(Machine& machine); }; diff --git a/src/engine/machina/URIs.hpp b/src/engine/machina/URIs.hpp index 4838761..caed196 100644 --- a/src/engine/machina/URIs.hpp +++ b/src/engine/machina/URIs.hpp @@ -26,7 +26,8 @@ namespace Machina { -class URIs { +class URIs +{ public: static void init() { _instance = new URIs(); } diff --git a/src/engine/machina/types.hpp b/src/engine/machina/types.hpp index 8bae27a..7603d3c 100644 --- a/src/engine/machina/types.hpp +++ b/src/engine/machina/types.hpp @@ -24,7 +24,7 @@ namespace Machina { typedef unsigned char byte; -typedef Raul::RingBuffer UpdateBuffer; +typedef Raul::RingBuffer UpdateBuffer; typedef uint32_t URIInt; diff --git a/src/engine/quantize.hpp b/src/engine/quantize.hpp index 5d1b4f2..de4d20b 100644 --- a/src/engine/quantize.hpp +++ b/src/engine/quantize.hpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -37,8 +37,8 @@ inline double quantize(double q, double t) { return (q > 0) - ? lrint(t / q) * q - : t; + ? lrint(t / q) * q + : t; } } // namespace Machina diff --git a/src/engine/quantize_test.cpp b/src/engine/quantize_test.cpp index 2a2c11a..d690990 100644 --- a/src/engine/quantize_test.cpp +++ b/src/engine/quantize_test.cpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -30,12 +30,12 @@ main() TimeStamp beats(TimeUnit(TimeUnit::BEATS, 19200), in); /*cout << "Q(" << in << ", 1/4) = " - << quantize(q, beats) << endl;*/ + << quantize(q, beats) << endl;*/ - if (quantize(q, beats).subticks() % (19200/4) != 0) + if (quantize(q, beats).subticks() % (19200 / 4) != 0) { return 1; + } } return 0; } - diff --git a/src/engine/smf_test.cpp b/src/engine/smf_test.cpp index 4787b7d..ce0a7d6 100644 --- a/src/engine/smf_test.cpp +++ b/src/engine/smf_test.cpp @@ -1,5 +1,5 @@ /* - This file is part of Raul. + This file is part of Machina. Copyright 2007-2012 David Robillard <http://drobilla.net> Raul is free software: you can redistribute it and/or modify it under the @@ -28,9 +28,12 @@ main(int argc, char** argv) { #define CHECK(cond) \ do { if (!(cond)) { \ - error << "Test at " << __FILE__ << ":" << __LINE__ << " failed: " << __STRING(cond) << endl; \ - return 1; \ - } } while (0) + error << "Test at " << __FILE__ << ":" << __LINE__ \ + << " failed: " << __STRING(cond) << endl; \ + return 1; \ + } \ + } \ + while (0) static const uint16_t ppqn = 19200; @@ -46,7 +49,7 @@ main(int argc, char** argv) } SMFReader reader; - bool opened = reader.open(filename); + bool opened = reader.open(filename); if (!opened) { cerr << "Unable to open SMF file " << filename << endl; @@ -57,7 +60,7 @@ main(int argc, char** argv) CHECK(reader.num_tracks() == 1); CHECK(reader.ppqn() == ppqn); - for (unsigned t=1; t <= reader.num_tracks(); ++t) { + for (unsigned t = 1; t <= reader.num_tracks(); ++t) { reader.seek_to_track(t); unsigned char buf[4]; @@ -65,10 +68,11 @@ main(int argc, char** argv) uint32_t ev_delta_time; while (reader.read_event(4, buf, &ev_size, &ev_delta_time) >= 0) { - cout << t << ": Event, size = " << ev_size << ", time = " << ev_delta_time; + cout << t << ": Event, size = " << ev_size << ", time = " + << ev_delta_time; cout << ":\t"; cout.flags(ios::hex); - for (uint32_t i=0; i < ev_size; ++i) { + for (uint32_t i = 0; i < ev_size; ++i) { cout << "0x" << static_cast<int>(buf[i]) << " "; } cout.flags(ios::dec); |