From 47f59aaad7a5ab8c189905544a761940ae9f8509 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 18 Mar 2007 02:11:51 +0000 Subject: Improved automata diagram drawing (include notes, use standard initial note notation). Made compilation without Jack possible. git-svn-id: http://svn.drobilla.net/lad/machina@363 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/JackDriver.cpp | 6 +++++ src/engine/Machine.cpp | 16 ++++++++++- src/engine/Makefile.am | 6 +++-- src/engine/SMFDriver.cpp | 26 ++++++++++++++---- src/engine/machina/Driver.hpp | 25 ++++++++++------- src/engine/machina/Engine.hpp | 11 ++++---- src/engine/machina/JackDriver.hpp | 12 ++++++--- src/engine/machina/Makefile.am | 7 +++-- src/engine/machina/MidiDriver.hpp | 56 --------------------------------------- src/engine/machina/SMFDriver.hpp | 18 +++++++++++-- 10 files changed, 96 insertions(+), 87 deletions(-) delete mode 100644 src/engine/machina/MidiDriver.hpp (limited to 'src/engine') diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp index 0261dd5..ee5a0d6 100644 --- a/src/engine/JackDriver.cpp +++ b/src/engine/JackDriver.cpp @@ -39,6 +39,12 @@ JackDriver::JackDriver(SharedPtr machine) } +JackDriver::~JackDriver() +{ + detach(); +} + + void JackDriver::attach(const std::string& client_name) { diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp index f76dd5c..844d2ce 100644 --- a/src/engine/Machine.cpp +++ b/src/engine/Machine.cpp @@ -22,6 +22,8 @@ #include "machina/Edge.hpp" #include "machina/MidiAction.hpp" +using namespace std; + namespace Machina { @@ -231,8 +233,12 @@ Machine::write_state(Raul::RDFWriter& writer) RdfId(RdfId::RESOURCE, "rdf:type"), RdfId(RdfId::RESOURCE, "machina:Machine")); + size_t count = 0; + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { - + + cerr << "Writing node " << count++ << " state." << endl; + (*n)->write_state(writer); if ((*n)->is_initial()) { @@ -246,11 +252,17 @@ Machine::write_state(Raul::RDFWriter& writer) } } + count = 0; + for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n) { + + cerr << "Writing node " << count++ << " edges: "; for (Node::Edges::const_iterator e = (*n)->outgoing_edges().begin(); e != (*n)->outgoing_edges().end(); ++e) { + cerr << "."; + (*e)->write_state(writer); writer.write(RdfId(RdfId::RESOURCE, ""), @@ -258,6 +270,8 @@ Machine::write_state(Raul::RDFWriter& writer) (*e)->id()); } + cerr << endl; + } } diff --git a/src/engine/Makefile.am b/src/engine/Makefile.am index 74308fd..eab7970 100644 --- a/src/engine/Makefile.am +++ b/src/engine/Makefile.am @@ -5,7 +5,6 @@ lib_LTLIBRARIES = libmachina.la libmachina_la_CXXFLAGS = @RAUL_CFLAGS@ @JACK_CFLAGS@ @GLIBMM_CFLAGS@ libmachina_la_LIBADD = @RAUL_LIBS@ @JACK_LIBS@ @GLIBMM_LIBS@ - libmachina_la_SOURCES = \ Node.cpp \ Edge.cpp \ @@ -13,7 +12,10 @@ libmachina_la_SOURCES = \ Machine.cpp \ Loader.cpp \ MidiAction.cpp \ - JackDriver.cpp \ SMFDriver.cpp \ Engine.cpp \ LearnRequest.cpp + +if WITH_JACK + JackDriver.cpp +endif diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp index cdf2ad7..60ee1af 100644 --- a/src/engine/SMFDriver.cpp +++ b/src/engine/SMFDriver.cpp @@ -18,6 +18,7 @@ #include #include #include +#include #include #include #include @@ -30,6 +31,13 @@ using namespace std; namespace Machina { +SMFDriver::SMFDriver(SharedPtr machine) + : Driver(machine) +{ + _writer = SharedPtr(new Raul::SMFWriter()); +} + + /** Learn a single track from the MIDI file at @a uri * * @track selects which track of the MIDI file to import, starting from 1. @@ -43,7 +51,11 @@ SMFDriver::learn(const string& filename, unsigned track, Raul::BeatTime max_dura SharedPtr m(new Machine()); Raul::SMFReader reader; - reader.open(filename); + + if (!reader.open(filename)) { + cerr << "Unable to open MIDI file " << filename << endl; + return SharedPtr(); + } if (track > reader.num_tracks()) return SharedPtr(); @@ -67,7 +79,10 @@ SMFDriver::learn(const string& filename, Raul::BeatTime max_duration) SharedPtr m(new Machine()); Raul::SMFReader reader; - reader.open(filename); + if (!reader.open(filename)) { + cerr << "Unable to open MIDI file " << filename << endl; + return SharedPtr(); + } for (unsigned t=1; t <= reader.num_tracks(); ++t) { learn_track(m, reader, t, max_duration); @@ -87,7 +102,8 @@ SMFDriver::learn_track(SharedPtr m, Raul::BeatTime max_duration) { const bool found_track = reader.seek_to_track(track); - assert(found_track); + if (!found_track) + return; list > active_nodes; @@ -113,7 +129,7 @@ SMFDriver::learn_track(SharedPtr m, //cerr << "t = " << t << endl; if (ev_size > 0) { if ((buf[0] & 0xF0) == MIDI_CMD_NOTE_ON) { - cerr << "NOTE ON: " << (int)buf[1] << ", channel = " << (int)(buf[0] & 0x0F) << endl; + //cerr << "NOTE ON: " << (int)buf[1] << ", channel = " << (int)(buf[0] & 0x0F) << endl; SharedPtr node(new Node()); node->add_enter_action(SharedPtr(new MidiAction(ev_size, buf))); assert(connect_node_end_time <= t); @@ -195,7 +211,7 @@ SMFDriver::learn_track(SharedPtr m, void SMFDriver::run(SharedPtr machine, Raul::BeatTime max_time) { - Raul::TimeSlice time(1.0/(double)_ppqn, 120); + Raul::TimeSlice time(1.0/(double)_writer->ppqn(), 120); time.set_length(time.beats_to_ticks(max_time)); machine->set_sink(shared_from_this()); machine->run(time); diff --git a/src/engine/machina/Driver.hpp b/src/engine/machina/Driver.hpp index f2816ba..b900da0 100644 --- a/src/engine/machina/Driver.hpp +++ b/src/engine/machina/Driver.hpp @@ -15,22 +15,29 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ -#ifndef MACHINA_JACKDRIVER_HPP -#define MACHINA_JACKDRIVER_HPP +#ifndef MACHINA_DRIVER_HPP +#define MACHINA_DRIVER_HPP -#include +#include -namespace Machine { +namespace Machina { +class Machine; -class JackDriver : public Raul::JackDriver { + +class Driver : public Raul::MIDISink { public: - JackDriver(SharedPtr machine); + Driver(SharedPtr machine) : _machine(machine) {} + virtual ~Driver() {} - virtual void set_machine(SharedPtr machine); + SharedPtr machine() { return _machine; } + virtual void set_machine(SharedPtr machine) { _machine = machine; } + + virtual void set_bpm(double bpm) = 0; + virtual void set_quantization(double quantization) = 0; -protected: - virtual void on_process(jack_nframes_t nframes); + virtual void activate() {} + virtual void deactivate() {} private: SharedPtr _machine; diff --git a/src/engine/machina/Engine.hpp b/src/engine/machina/Engine.hpp index 32fab23..82c21ba 100644 --- a/src/engine/machina/Engine.hpp +++ b/src/engine/machina/Engine.hpp @@ -20,22 +20,21 @@ #include #include -#include "machina/JackDriver.hpp" +#include "machina/Driver.hpp" namespace Machina { class Machine; -class JackDriver; class Engine { public: - Engine(SharedPtr driver) + Engine(SharedPtr driver) : _driver(driver) { } - SharedPtr driver() { return _driver; } - SharedPtr machine() { return _driver->machine(); } + SharedPtr driver() { return _driver; } + SharedPtr machine() { return _driver->machine(); } SharedPtr load_machine(const Glib::ustring& uri); SharedPtr learn_midi(const Glib::ustring& uri); @@ -44,7 +43,7 @@ public: void set_quantization(double beat_fraction); private: - SharedPtr _driver; + SharedPtr _driver; }; diff --git a/src/engine/machina/JackDriver.hpp b/src/engine/machina/JackDriver.hpp index 0754741..13005c0 100644 --- a/src/engine/machina/JackDriver.hpp +++ b/src/engine/machina/JackDriver.hpp @@ -18,14 +18,15 @@ #ifndef MACHINA_JACKDRIVER_HPP #define MACHINA_JACKDRIVER_HPP +#include #include #include #include #include #include #include "Machine.hpp" -#include "MidiDriver.hpp" -#include +#include "Driver.hpp" + namespace Machina { @@ -38,15 +39,18 @@ 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 Raul::JackDriver, public Machina::MidiDriver, +class JackDriver : public Raul::JackDriver, + public Machina::Driver, public boost::enable_shared_from_this { public: JackDriver(SharedPtr machine = SharedPtr()); void attach(const std::string& client_name); void detach(); + + void activate() { Raul::JackDriver::activate(); } + void deactivate() { Raul::JackDriver::deactivate(); } - SharedPtr machine() { return _machine; } void set_machine(SharedPtr machine); void write_event(Raul::BeatTime time, diff --git a/src/engine/machina/Makefile.am b/src/engine/machina/Makefile.am index 4501028..ff8eab5 100644 --- a/src/engine/machina/Makefile.am +++ b/src/engine/machina/Makefile.am @@ -7,8 +7,11 @@ libmachinainclude_HEADERS = \ Edge.hpp \ Machine.hpp \ Loader.hpp \ - JackDriver.hpp \ MidiAction.hpp \ - MidiDriver.hpp \ + Driver.hpp \ LearnRequest.hpp \ Engine.hpp + +if WITH_JACK + JackDriver.hpp +endif diff --git a/src/engine/machina/MidiDriver.hpp b/src/engine/machina/MidiDriver.hpp deleted file mode 100644 index a4d712c..0000000 --- a/src/engine/machina/MidiDriver.hpp +++ /dev/null @@ -1,56 +0,0 @@ -/* This file is part of Machina. - * Copyright (C) 2007 Dave 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_MIDIDRIVER_HPP -#define MACHINA_MIDIDRIVER_HPP - -#include -#include "machina/types.hpp" - -namespace Machina { - -class Node; - - -class MidiDriver : public Raul::MIDISink { -public: - virtual ~MidiDriver() {} - - /** Emit a MIDI event at the given time */ - /*virtual void write_event(Raul::BeatTime time, - size_t size, - const unsigned char* event) = 0; - */ - - /** Beginning of current cycle in absolute time. - */ - //virtual Raul::TickTime cycle_start() = 0; - - /** Length of current cycle in ticks. - * - * A "tick" is the smallest recognizable unit of (discrete) time. - * Ticks could be single audio frames, MIDI clock at a certain ppqn, etc. - */ - //virtual Raul::TickCount cycle_length() = 0; - -}; - - -} // namespace Machina - -#endif // MACHINA_MIDIDRIVER_HPP - diff --git a/src/engine/machina/SMFDriver.hpp b/src/engine/machina/SMFDriver.hpp index 3e74726..0d58db3 100644 --- a/src/engine/machina/SMFDriver.hpp +++ b/src/engine/machina/SMFDriver.hpp @@ -24,7 +24,7 @@ #include #include #include "machina/types.hpp" -#include "machina/MidiDriver.hpp" +#include "machina/Driver.hpp" namespace Machina { @@ -32,15 +32,29 @@ class Node; class Machine; -class SMFDriver : public Raul::SMFWriter, +class SMFDriver : public Driver, public boost::enable_shared_from_this { public: + SMFDriver(SharedPtr machine = SharedPtr()); + SharedPtr learn(const std::string& filename, Raul::BeatTime max_duration=0); SharedPtr learn(const std::string& filename, unsigned track, Raul::BeatTime max_duration=0); void run(SharedPtr machine, Raul::BeatTime max_time); + + void write_event(Raul::BeatTime time, + size_t ev_size, + 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*/) { } + + SharedPtr writer() { return _writer; } private: + SharedPtr _writer; + void learn_track(SharedPtr machine, Raul::SMFReader& reader, unsigned track, -- cgit v1.2.1