diff options
Diffstat (limited to 'src/engine/Engine.cpp')
-rw-r--r-- | src/engine/Engine.cpp | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/src/engine/Engine.cpp b/src/engine/Engine.cpp new file mode 100644 index 0000000..1aed7b6 --- /dev/null +++ b/src/engine/Engine.cpp @@ -0,0 +1,126 @@ +/* + This file is part of Machina. + Copyright 2007-2014 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 the Free Software + Foundation, either version 3 of the License, or 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/>. +*/ + +#include "machina_config.h" +#include "machina/Engine.hpp" +#include "machina/Loader.hpp" +#include "machina/Machine.hpp" +#include "SMFDriver.hpp" +#ifdef HAVE_JACK +#include "JackDriver.hpp" +#endif + +namespace machina { + +Engine::Engine(Forge& forge, + SPtr<Driver> driver, + Sord::World& rdf_world) + : _driver(driver) + , _rdf_world(rdf_world) + , _loader(_forge, _rdf_world) + , _forge(forge) +{} + +SPtr<Driver> +Engine::new_driver(Forge& forge, + const std::string& name, + SPtr<Machine> machine) +{ + if (name == "jack") { +#ifdef HAVE_JACK + JackDriver* driver = new JackDriver(forge, machine); + driver->attach("machina"); + return SPtr<Driver>(driver); +#else + return SPtr<Driver>(); +#endif + } + if (name == "smf") { + return SPtr<Driver>(new SMFDriver(forge, machine->time().unit())); + } + + std::cerr << "Error: Unknown driver type `" << name << "'" << std::endl; + return SPtr<Driver>(); +} + +/** Load the machine at `uri`, and run it (replacing current machine). + * Safe to call while engine is processing. + */ +SPtr<Machine> +Engine::load_machine(const std::string& uri) +{ + SPtr<Machine> machine = _loader.load(uri); + SPtr<Machine> old_machine = _driver->machine(); // Keep reference + if (machine) { + _driver->set_machine(machine); // Switch driver to new machine and wait + } + + // Drop (possibly last) reference to old_machine in this thread + return machine; +} + +/** Build a machine from the MIDI at `uri`, and run it (replacing current machine). + * Safe to call while engine is processing. + */ +SPtr<Machine> +Engine::load_machine_midi(const std::string& uri, + double q, + Raul::TimeDuration dur) +{ + SPtr<Machine> machine = _loader.load_midi(uri, q, dur); + SPtr<Machine> old_machine = _driver->machine(); // Keep reference + if (machine) { + _driver->set_machine(machine); // Switch driver to new machine and wait + } + + // Drop (possibly last) reference to old_machine in this thread + return machine; +} + +void +Engine::export_midi(const std::string& filename, Raul::TimeDuration dur) +{ + SPtr<Machine> machine = _driver->machine(); + SPtr<machina::SMFDriver> file_driver( + new machina::SMFDriver(_forge, dur.unit())); + + const bool activated = _driver->is_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) { + _driver->activate(); + } +} + +void +Engine::set_bpm(double bpm) +{ + _driver->set_bpm(bpm); +} + +void +Engine::set_quantization(double q) +{ + _driver->set_quantization(q); +} + +} // namespace machina |