From a8eb7f8f5b54b955c6eb23c10f2eaaca7f746ef2 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 18 Dec 2010 20:03:31 +0000 Subject: Remove more things from public engine interface. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@2764 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/Engine.cpp | 22 +++++- src/engine/Evolver.cpp | 7 +- src/engine/JackDriver.cpp | 2 +- src/engine/JackDriver.hpp | 128 ++++++++++++++++++++++++++++++++++ src/engine/MachineBuilder.cpp | 3 +- src/engine/MachineBuilder.hpp | 76 ++++++++++++++++++++ src/engine/Problem.cpp | 8 ++- src/engine/Problem.hpp | 124 ++++++++++++++++++++++++++++++++ src/engine/Recorder.cpp | 7 +- src/engine/Recorder.hpp | 53 ++++++++++++++ src/engine/Schrodinbit.hpp | 45 ++++++++++++ src/engine/machina/Engine.hpp | 7 ++ src/engine/machina/Evolver.hpp | 3 +- src/engine/machina/JackDriver.hpp | 127 --------------------------------- src/engine/machina/MachineBuilder.hpp | 74 -------------------- src/engine/machina/Node.hpp | 5 +- src/engine/machina/Problem.hpp | 124 -------------------------------- src/engine/machina/Recorder.hpp | 52 -------------- src/engine/machina/SMFDriver.hpp | 11 +-- src/engine/machina/Schrodinbit.hpp | 45 ------------ 20 files changed, 483 insertions(+), 440 deletions(-) create mode 100644 src/engine/JackDriver.hpp create mode 100644 src/engine/MachineBuilder.hpp create mode 100644 src/engine/Problem.hpp create mode 100644 src/engine/Recorder.hpp create mode 100644 src/engine/Schrodinbit.hpp delete mode 100644 src/engine/machina/JackDriver.hpp delete mode 100644 src/engine/machina/MachineBuilder.hpp delete mode 100644 src/engine/machina/Problem.hpp delete mode 100644 src/engine/machina/Recorder.hpp delete mode 100644 src/engine/machina/Schrodinbit.hpp (limited to 'src/engine') diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp index b883c2e..30f5b68 100644 --- a/src/engine/Engine.cpp +++ b/src/engine/Engine.cpp @@ -16,14 +16,34 @@ */ #include + +#include "machina-config.h" #include "machina/Engine.hpp" -#include "machina/JackDriver.hpp" #include "machina/Loader.hpp" #include "machina/Machine.hpp" #include "machina/SMFDriver.hpp" +#ifdef HAVE_JACK +#include "JackDriver.hpp" +#endif namespace Machina { +SharedPtr +Engine::new_driver(const std::string& name, SharedPtr machine) +{ + #ifdef HAVE_JACK + if (name == "jack") { + JackDriver* driver = new JackDriver(machine); + driver->attach("machina"); + return SharedPtr(driver); + } + #endif + if (name == "smf") + return SharedPtr(new SMFDriver(machine)); + + std::cerr << "Error: Unknown driver type `" << name << "'" << std::endl; + return SharedPtr(); +} /** Load the machine at @a uri, and run it (replacing current machine). * Safe to call while engine is processing. diff --git a/src/engine/Evolver.cpp b/src/engine/Evolver.cpp index 10b2c11..a16f168 100644 --- a/src/engine/Evolver.cpp +++ b/src/engine/Evolver.cpp @@ -16,12 +16,15 @@ */ #include -#include "eugene/Mutation.hpp" + #include "eugene/HybridMutation.hpp" +#include "eugene/Mutation.hpp" #include "eugene/TournamentSelection.hpp" + #include "machina/Evolver.hpp" #include "machina/Mutation.hpp" -#include "machina/Problem.hpp" + +#include "Problem.hpp" using namespace std; using namespace Eugene; diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index 55b06e2..e274255 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -19,9 +19,9 @@ #include #include "machina-config.h" -#include "machina/JackDriver.hpp" #include "machina/MidiAction.hpp" +#include "JackDriver.hpp" #include "LearnRequest.hpp" #include "jack_compat.h" diff --git a/src/engine/JackDriver.hpp b/src/engine/JackDriver.hpp new file mode 100644 index 0000000..3c98394 --- /dev/null +++ b/src/engine/JackDriver.hpp @@ -0,0 +1,128 @@ +/* This file is part of Machina. + * Copyright (C) 2007-2009 David Robillard + * + * 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 2 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef MACHINA_JACKDRIVER_HPP +#define MACHINA_JACKDRIVER_HPP + +#include + +#include +#include + +#include "raul/Command.hpp" +#include "raul/DoubleBuffer.hpp" +#include "raul/EventRingBuffer.hpp" +#include "raul/Semaphore.hpp" +#include "raul/SharedPtr.hpp" + +#include "machina/Driver.hpp" +#include "machina/Machine.hpp" + +#include "Recorder.hpp" + +namespace Machina { + +class MidiAction; +class Node; + + +/** Realtime JACK Driver. + * + * "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, + public boost::enable_shared_from_this { +public: + JackDriver(SharedPtr machine = SharedPtr()); + ~JackDriver(); + + void attach(const std::string& client_name); + void detach(); + + void activate(); + void deactivate(); + + void set_machine(SharedPtr machine); + + void write_event(Raul::TimeStamp time, + size_t size, + const unsigned char* event) throw (std::logic_error); + + void set_bpm(double bpm) { _bpm.set(bpm); } + void set_quantization(double q) { _quantization.set(q); } + + void stop(); + + bool recording() { return _recording.get(); } + void start_record(bool step); + void finish_record(); + + void start_transport() { jack_transport_start(_client); } + void stop_transport() { jack_transport_stop(_client); } + + void rewind_transport() { + jack_position_t zero; + zero.frame = 0; + zero.valid = (jack_position_bits_t)0; + jack_transport_reposition(_client, &zero); + } + + bool is_activated() const { return _is_activated; } + 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); } + jack_client_t* jack_client() const { return _client; } + +private: + void process_input(SharedPtr machine, + const Raul::TimeSlice& time); + + static void jack_error_cb(const char* msg); + static int jack_process_cb(jack_nframes_t nframes, void* me); + static void jack_shutdown_cb(void* me); + + void on_process(jack_nframes_t nframes); + + jack_client_t* _client; + + Raul::Semaphore _machine_changed; + SharedPtr _last_machine; + + jack_port_t* _input_port; + jack_port_t* _output_port; + + Raul::TimeUnit _frames_unit; + Raul::TimeUnit _beats_unit; + Raul::TimeSlice _cycle_time; + + Raul::DoubleBuffer _bpm; + Raul::DoubleBuffer _quantization; + + Raul::Command _stop; + + Raul::TimeDuration _record_dur; + Raul::AtomicInt _recording; + SharedPtr _recorder; + bool _is_activated; +}; + + +} // namespace Machina + +#endif // MACHINA_JACKDRIVER_HPP diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp index 946b7f5..0c02c7c 100644 --- a/src/engine/MachineBuilder.cpp +++ b/src/engine/MachineBuilder.cpp @@ -23,10 +23,11 @@ #include "machina/Edge.hpp" #include "machina/Machine.hpp" -#include "machina/MachineBuilder.hpp" #include "machina/MidiAction.hpp" #include "machina/Node.hpp" +#include "MachineBuilder.hpp" + using namespace std; using namespace Raul; diff --git a/src/engine/MachineBuilder.hpp b/src/engine/MachineBuilder.hpp new file mode 100644 index 0000000..ad106c1 --- /dev/null +++ b/src/engine/MachineBuilder.hpp @@ -0,0 +1,76 @@ +/* This file is part of Machina. + * Copyright (C) 2007-2009 David Robillard + * + * 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 2 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef MACHINA_MACHINEBUILDER_HPP +#define MACHINA_MACHINEBUILDER_HPP + +#include + +#include "raul/SharedPtr.hpp" +#include "raul/TimeStamp.hpp" + +namespace Machina { + +class Machine; +class Node; + + +class MachineBuilder { +public: + MachineBuilder(SharedPtr machine, + double quantization, + bool step); + + void set_time(Raul::TimeStamp time) { _time = time; } + + void event(Raul::TimeStamp time_offset, size_t size, unsigned char* buf); + + void reset(); + void resolve(); + + SharedPtr finish(); + +private: + bool is_delay_node(SharedPtr node) const; + void set_node_duration(SharedPtr node, Raul::TimeDuration d) const; + + SharedPtr + connect_nodes(SharedPtr m, + SharedPtr tail, Raul::TimeStamp tail_end_time, + SharedPtr head, Raul::TimeStamp head_start_time); + + typedef std::list > ActiveList; + ActiveList _active_nodes; + + typedef std::list > > PolyList; + PolyList _poly_nodes; + + double _quantization; + Raul::TimeStamp _time; + + SharedPtr _machine; + SharedPtr _initial_node; + SharedPtr _connect_node; + Raul::TimeStamp _connect_node_end_time; + + bool _step; +}; + + +} // namespace Machina + +#endif // MACHINA_MACHINEBUILDER_HPP diff --git a/src/engine/Problem.cpp b/src/engine/Problem.cpp index c59088c..4824c07 100644 --- a/src/engine/Problem.cpp +++ b/src/engine/Problem.cpp @@ -21,14 +21,16 @@ #include #include #include -#include "machina/Problem.hpp" -#include "machina/Machine.hpp" + +#include "eugene/Problem.hpp" #include "machina/ActionFactory.hpp" #include "machina/Edge.hpp" +#include "machina/Machine.hpp" #include "raul/SMFReader.hpp" #include "raul/midi_events.h" -#include "eugene/Problem.hpp" + #include "machina-config.h" +#include "Problem.hpp" using namespace std; diff --git a/src/engine/Problem.hpp b/src/engine/Problem.hpp new file mode 100644 index 0000000..182d61a --- /dev/null +++ b/src/engine/Problem.hpp @@ -0,0 +1,124 @@ +/* This file is part of Machina. + * Copyright (C) 2007-2009 David Robillard + * + * 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 2 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef MACHINA_PROBLEM_HPP +#define MACHINA_PROBLEM_HPP + +#include +#include "raul/MIDISink.hpp" +#include "machina/Machine.hpp" +#include "eugene/Problem.hpp" + +namespace Machina { + + +class Problem : public Eugene::Problem { +public: + Problem(TimeUnit unit, const std::string& target_midi, SharedPtr seed = SharedPtr()); + virtual ~Problem() {} + + void seed(SharedPtr parent) { _seed = parent; } + + float fitness(const Machine& machine) const; + + bool fitness_less_than(float a, float b) const { return a < b; } + + void clear_fitness_cache() { _fitness.clear(); } + + boost::shared_ptr + initial_population(size_t gene_size, size_t pop_size) const; + +private: + size_t distance(const std::vector& source, + const std::vector& target) const; + + // count + /*struct FreqEvaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) + : _problem(problem), _n_notes(0), _length(0) { + for (uint8_t i=0; i < 128; ++i) + _note_frequency[i] = 0; + } + void write_event(Raul::BeatTime time, + size_t ev_size, + const uint8_t* ev) throw (std::logic_error); + void compute(); + const Problem& _problem; + + size_t n_notes() const { return _n_notes; } + + float _note_frequency[128]; + size_t _n_notes; + double _length; + };*/ + + // distance + /*struct Evaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) : _problem(problem) {} + void write_event(Raul::BeatTime time, + size_t ev_size, + const uint8_t* ev) throw (std::logic_error); + void compute(); + const Problem& _problem; + + size_t n_notes() const { return _notes.size(); } + + std::vector _notes; + float _counts[128]; + };*/ + + struct Evaluator : public Raul::MIDISink { + Evaluator(const Problem& problem) : _problem(problem), _order(4), _n_notes(0), _first_note(0) { + for (uint8_t i=0; i < 128; ++i) + _counts[i] = 0; + } + void write_event(TimeStamp time, + size_t ev_size, + const uint8_t* ev) throw (std::logic_error); + void compute(); + const Problem& _problem; + + size_t n_notes() const { return _n_notes; } + uint8_t first_note() const { return _first_note; } + + const uint32_t _order; + + std::string _read; + + typedef std::map Patterns; + Patterns _patterns; + uint32_t _counts[128]; + size_t _n_notes; + uint8_t _first_note; + }; + + TimeUnit _unit; + + Evaluator _target; + SharedPtr _seed; + + /// for levenshtein distance + mutable std::vector< std::vector > _matrix; + + /// memoization + mutable std::map _fitness; +}; + + +} // namespace Machina + +#endif // MACHINA_PROBLEM_HPP diff --git a/src/engine/Recorder.cpp b/src/engine/Recorder.cpp index d134d25..a577368 100644 --- a/src/engine/Recorder.cpp +++ b/src/engine/Recorder.cpp @@ -15,10 +15,11 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#include #include -#include "machina/Recorder.hpp" -#include "machina/MachineBuilder.hpp" +#include + +#include "MachineBuilder.hpp" +#include "Recorder.hpp" using namespace std; using namespace Raul; diff --git a/src/engine/Recorder.hpp b/src/engine/Recorder.hpp new file mode 100644 index 0000000..fcb7e7d --- /dev/null +++ b/src/engine/Recorder.hpp @@ -0,0 +1,53 @@ +/* This file is part of Machina. + * Copyright (C) 2007-2009 David Robillard + * + * 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 2 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + */ + +#ifndef MACHINA_RECORDER_HPP +#define MACHINA_RECORDER_HPP + +#include "raul/Slave.hpp" +#include "raul/SharedPtr.hpp" +#include "raul/EventRingBuffer.hpp" + +#include "machina/Machine.hpp" + +namespace Machina { + +class MachineBuilder; + + +class Recorder : public Raul::Slave { +public: + Recorder(size_t buffer_size, TimeUnit unit, double q, bool step); + + inline void write(Raul::TimeStamp time, size_t size, const unsigned char* buf) { + _record_buffer.write(time, size, buf); + } + + SharedPtr finish(); + +private: + virtual void _whipped(); + + TimeUnit _unit; + Raul::EventRingBuffer _record_buffer; + SharedPtr _builder; +}; + + +} // namespace Machina + +#endif // MACHINA_RECORDER_HPP diff --git a/src/engine/Schrodinbit.hpp b/src/engine/Schrodinbit.hpp new file mode 100644 index 0000000..7fe1825 --- /dev/null +++ b/src/engine/Schrodinbit.hpp @@ -0,0 +1,45 @@ +/* This file is part of Machina. + * Copyright (C) 2007-2009 David Robillard + * + * 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 2 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 details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA + */ + +#ifndef SCHRODINBIT_HPP +#define SCHRODINBIT_HPP + + +/** A flag which becomes false when it's value is observed + */ +class Schrodinbit { +public: + Schrodinbit() : _flag(false) {} + + inline operator bool() { + const bool ret = _flag; + _flag = false; + return ret; + } + + inline bool operator=(bool flag) { + _flag = flag; + return flag; + } + +private: + bool _flag; +}; + + +#endif // SCHRODINBIT_HPP + diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index 355a50f..a8a7c9d 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -18,8 +18,12 @@ #ifndef MACHINA_ENGINE_HPP #define MACHINA_ENGINE_HPP +#include + #include + #include "raul/SharedPtr.hpp" + #include "machina/Driver.hpp" #include "machina/Loader.hpp" @@ -39,6 +43,9 @@ public: Redland::World& rdf_world() { return _rdf_world; } + static SharedPtr new_driver(const std::string& name, + SharedPtr machine); + SharedPtr driver() { return _driver; } SharedPtr machine() { return _driver->machine(); } diff --git a/src/engine/machina/Evolver.hpp b/src/engine/machina/Evolver.hpp index 013067d..b633bfc 100644 --- a/src/engine/machina/Evolver.hpp +++ b/src/engine/machina/Evolver.hpp @@ -18,10 +18,11 @@ #ifndef MACHINA_EVOLVER_HPP #define MACHINA_EVOLVER_HPP +#include "eugene/GAImpl.hpp" #include "raul/SharedPtr.hpp" #include "raul/Thread.hpp" #include "raul/TimeStamp.hpp" -#include "eugene/GAImpl.hpp" + #include "Schrodinbit.hpp" namespace Eugene { template class HybridMutation; } diff --git a/src/engine/machina/JackDriver.hpp b/src/engine/machina/JackDriver.hpp deleted file mode 100644 index 0b2851b..0000000 --- a/src/engine/machina/JackDriver.hpp +++ /dev/null @@ -1,127 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007-2009 David Robillard - * - * 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 2 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 details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef MACHINA_JACKDRIVER_HPP -#define MACHINA_JACKDRIVER_HPP - -#include - -#include -#include - -#include "raul/Command.hpp" -#include "raul/DoubleBuffer.hpp" -#include "raul/EventRingBuffer.hpp" -#include "raul/Semaphore.hpp" -#include "raul/SharedPtr.hpp" - -#include "Driver.hpp" -#include "Machine.hpp" -#include "Recorder.hpp" - -namespace Machina { - -class MidiAction; -class Node; - - -/** Realtime JACK Driver. - * - * "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, - public boost::enable_shared_from_this { -public: - JackDriver(SharedPtr machine = SharedPtr()); - ~JackDriver(); - - void attach(const std::string& client_name); - void detach(); - - void activate(); - void deactivate(); - - void set_machine(SharedPtr machine); - - void write_event(Raul::TimeStamp time, - size_t size, - const unsigned char* event) throw (std::logic_error); - - void set_bpm(double bpm) { _bpm.set(bpm); } - void set_quantization(double q) { _quantization.set(q); } - - void stop(); - - bool recording() { return _recording.get(); } - void start_record(bool step); - void finish_record(); - - void start_transport() { jack_transport_start(_client); } - void stop_transport() { jack_transport_stop(_client); } - - void rewind_transport() { - jack_position_t zero; - zero.frame = 0; - zero.valid = (jack_position_bits_t)0; - jack_transport_reposition(_client, &zero); - } - - bool is_activated() const { return _is_activated; } - 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); } - jack_client_t* jack_client() const { return _client; } - -private: - void process_input(SharedPtr machine, - const Raul::TimeSlice& time); - - static void jack_error_cb(const char* msg); - static int jack_process_cb(jack_nframes_t nframes, void* me); - static void jack_shutdown_cb(void* me); - - void on_process(jack_nframes_t nframes); - - jack_client_t* _client; - - Raul::Semaphore _machine_changed; - SharedPtr _last_machine; - - jack_port_t* _input_port; - jack_port_t* _output_port; - - Raul::TimeUnit _frames_unit; - Raul::TimeUnit _beats_unit; - Raul::TimeSlice _cycle_time; - - Raul::DoubleBuffer _bpm; - Raul::DoubleBuffer _quantization; - - Raul::Command _stop; - - Raul::TimeDuration _record_dur; - Raul::AtomicInt _recording; - SharedPtr _recorder; - bool _is_activated; -}; - - -} // namespace Machina - -#endif // MACHINA_JACKDRIVER_HPP diff --git a/src/engine/machina/MachineBuilder.hpp b/src/engine/machina/MachineBuilder.hpp deleted file mode 100644 index d98d91c..0000000 --- a/src/engine/machina/MachineBuilder.hpp +++ /dev/null @@ -1,74 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007-2009 David Robillard - * - * 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 2 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 details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef MACHINA_MACHINEBUILDER_HPP -#define MACHINA_MACHINEBUILDER_HPP - -#include -#include "raul/SharedPtr.hpp" - -namespace Machina { - -class Machine; -class Node; - - -class MachineBuilder { -public: - MachineBuilder(SharedPtr machine, - double quantization, - bool step); - - void set_time(Raul::TimeStamp time) { _time = time; } - - void event(Raul::TimeStamp time_offset, size_t size, unsigned char* buf); - - void reset(); - void resolve(); - - SharedPtr finish(); - -private: - bool is_delay_node(SharedPtr node) const; - void set_node_duration(SharedPtr node, Raul::TimeDuration d) const; - - SharedPtr - connect_nodes(SharedPtr m, - SharedPtr tail, Raul::TimeStamp tail_end_time, - SharedPtr head, Raul::TimeStamp head_start_time); - - typedef std::list > ActiveList; - ActiveList _active_nodes; - - typedef std::list > > PolyList; - PolyList _poly_nodes; - - double _quantization; - Raul::TimeStamp _time; - - SharedPtr _machine; - SharedPtr _initial_node; - SharedPtr _connect_node; - Raul::TimeStamp _connect_node_end_time; - - bool _step; -}; - - -} // namespace Machina - -#endif // MACHINA_MACHINEBUILDER_HPP diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp index 7d90265..730c82b 100644 --- a/src/engine/machina/Node.hpp +++ b/src/engine/machina/Node.hpp @@ -18,10 +18,11 @@ #ifndef MACHINA_NODE_HPP #define MACHINA_NODE_HPP -#include "raul/SharedPtr.hpp" #include "raul/List.hpp" -#include "raul/Stateful.hpp" #include "raul/MIDISink.hpp" +#include "raul/SharedPtr.hpp" +#include "raul/Stateful.hpp" + #include "Action.hpp" #include "Schrodinbit.hpp" diff --git a/src/engine/machina/Problem.hpp b/src/engine/machina/Problem.hpp deleted file mode 100644 index 182d61a..0000000 --- a/src/engine/machina/Problem.hpp +++ /dev/null @@ -1,124 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007-2009 David Robillard - * - * 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 2 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 details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifndef MACHINA_PROBLEM_HPP -#define MACHINA_PROBLEM_HPP - -#include -#include "raul/MIDISink.hpp" -#include "machina/Machine.hpp" -#include "eugene/Problem.hpp" - -namespace Machina { - - -class Problem : public Eugene::Problem { -public: - Problem(TimeUnit unit, const std::string& target_midi, SharedPtr seed = SharedPtr()); - virtual ~Problem() {} - - void seed(SharedPtr parent) { _seed = parent; } - - float fitness(const Machine& machine) const; - - bool fitness_less_than(float a, float b) const { return a < b; } - - void clear_fitness_cache() { _fitness.clear(); } - - boost::shared_ptr - initial_population(size_t gene_size, size_t pop_size) const; - -private: - size_t distance(const std::vector& source, - const std::vector& target) const; - - // count - /*struct FreqEvaluator : public Raul::MIDISink { - Evaluator(const Problem& problem) - : _problem(problem), _n_notes(0), _length(0) { - for (uint8_t i=0; i < 128; ++i) - _note_frequency[i] = 0; - } - void write_event(Raul::BeatTime time, - size_t ev_size, - const uint8_t* ev) throw (std::logic_error); - void compute(); - const Problem& _problem; - - size_t n_notes() const { return _n_notes; } - - float _note_frequency[128]; - size_t _n_notes; - double _length; - };*/ - - // distance - /*struct Evaluator : public Raul::MIDISink { - Evaluator(const Problem& problem) : _problem(problem) {} - void write_event(Raul::BeatTime time, - size_t ev_size, - const uint8_t* ev) throw (std::logic_error); - void compute(); - const Problem& _problem; - - size_t n_notes() const { return _notes.size(); } - - std::vector _notes; - float _counts[128]; - };*/ - - struct Evaluator : public Raul::MIDISink { - Evaluator(const Problem& problem) : _problem(problem), _order(4), _n_notes(0), _first_note(0) { - for (uint8_t i=0; i < 128; ++i) - _counts[i] = 0; - } - void write_event(TimeStamp time, - size_t ev_size, - const uint8_t* ev) throw (std::logic_error); - void compute(); - const Problem& _problem; - - size_t n_notes() const { return _n_notes; } - uint8_t first_note() const { return _first_note; } - - const uint32_t _order; - - std::string _read; - - typedef std::map Patterns; - Patterns _patterns; - uint32_t _counts[128]; - size_t _n_notes; - uint8_t _first_note; - }; - - TimeUnit _unit; - - Evaluator _target; - SharedPtr _seed; - - /// for levenshtein distance - mutable std::vector< std::vector > _matrix; - - /// memoization - mutable std::map _fitness; -}; - - -} // namespace Machina - -#endif // MACHINA_PROBLEM_HPP diff --git a/src/engine/machina/Recorder.hpp b/src/engine/machina/Recorder.hpp deleted file mode 100644 index 623dbfe..0000000 --- a/src/engine/machina/Recorder.hpp +++ /dev/null @@ -1,52 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007-2009 David Robillard - * - * 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 2 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 details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA - */ - -#ifndef MACHINA_RECORDER_HPP -#define MACHINA_RECORDER_HPP - -#include "raul/Slave.hpp" -#include "raul/SharedPtr.hpp" -#include "raul/EventRingBuffer.hpp" -#include "Machine.hpp" - -namespace Machina { - -class MachineBuilder; - - -class Recorder : public Raul::Slave { -public: - Recorder(size_t buffer_size, TimeUnit unit, double q, bool step); - - inline void write(Raul::TimeStamp time, size_t size, const unsigned char* buf) { - _record_buffer.write(time, size, buf); - } - - SharedPtr finish(); - -private: - virtual void _whipped(); - - TimeUnit _unit; - Raul::EventRingBuffer _record_buffer; - SharedPtr _builder; -}; - - -} // namespace Machina - -#endif // MACHINA_RECORDER_HPP diff --git a/src/engine/machina/SMFDriver.hpp b/src/engine/machina/SMFDriver.hpp index 8a22aaa..d66bfa3 100644 --- a/src/engine/machina/SMFDriver.hpp +++ b/src/engine/machina/SMFDriver.hpp @@ -20,12 +20,15 @@ #include #include -#include "raul/SharedPtr.hpp" -#include "raul/SMFWriter.hpp" + #include "raul/SMFReader.hpp" -#include "machina/types.hpp" +#include "raul/SMFWriter.hpp" +#include "raul/SharedPtr.hpp" + #include "machina/Driver.hpp" -#include "machina/MachineBuilder.hpp" +#include "machina/types.hpp" + +#include "MachineBuilder.hpp" namespace Machina { diff --git a/src/engine/machina/Schrodinbit.hpp b/src/engine/machina/Schrodinbit.hpp deleted file mode 100644 index 7fe1825..0000000 --- a/src/engine/machina/Schrodinbit.hpp +++ /dev/null @@ -1,45 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007-2009 David Robillard - * - * 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 2 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 details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA - */ - -#ifndef SCHRODINBIT_HPP -#define SCHRODINBIT_HPP - - -/** A flag which becomes false when it's value is observed - */ -class Schrodinbit { -public: - Schrodinbit() : _flag(false) {} - - inline operator bool() { - const bool ret = _flag; - _flag = false; - return ret; - } - - inline bool operator=(bool flag) { - _flag = flag; - return flag; - } - -private: - bool _flag; -}; - - -#endif // SCHRODINBIT_HPP - -- cgit v1.2.1