aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2007-03-17 03:15:44 +0000
committerDavid Robillard <d@drobilla.net>2007-03-17 03:15:44 +0000
commita716b2c0571f5bdcd817835cecb30cb7a4c745e0 (patch)
tree138a04f89579a435c5f6d93b2bf5e6d9e61ca466
parentcb6ecc0c93b7ea7624dba5d633ff0f15980c4274 (diff)
downloadmachina-a716b2c0571f5bdcd817835cecb30cb7a4c745e0.tar.gz
machina-a716b2c0571f5bdcd817835cecb30cb7a4c745e0.tar.bz2
machina-a716b2c0571f5bdcd817835cecb30cb7a4c745e0.zip
Nicer automata diagrams from machina2dot.py.
Added midi2machina program. git-svn-id: http://svn.drobilla.net/lad/machina@362 a436a847-0d15-0410-975c-d299462d15a1
-rw-r--r--src/Makefile.am9
-rw-r--r--src/engine/SMFDriver.cpp8
-rw-r--r--src/engine/machina/SMFDriver.hpp4
-rw-r--r--src/gui/main.cpp2
-rw-r--r--src/machina.cpp (renamed from src/main.cpp)0
-rw-r--r--src/midi2machina.cpp83
-rwxr-xr-xutil/machina2dot.py15
7 files changed, 102 insertions, 19 deletions
diff --git a/src/Makefile.am b/src/Makefile.am
index 23a14da..1928c51 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -3,9 +3,10 @@
SUBDIRS = engine gui
-bin_PROGRAMS = machina
+bin_PROGRAMS = machina midi2machina
-machina_CXXFLAGS = @RAUL_CFLAGS@ @JACK_CFLAGS@ @GLIBMM_CFLAGS@ -I$(top_srcdir)/src/engine
-machina_LDADD = @RAUL_LIBS@ @JACK_LIBS@ @GLIBMM_LIBS@ engine/libmachina.la
+AM_CXXFLAGS = @RAUL_CFLAGS@ @JACK_CFLAGS@ @GLIBMM_CFLAGS@ -I$(top_srcdir)/src/engine
+LDADD = @RAUL_LIBS@ @JACK_LIBS@ @GLIBMM_LIBS@ engine/libmachina.la
-machina_SOURCES = main.cpp
+machina_SOURCES = machina.cpp
+midi2machina_SOURCES = midi2machina.cpp
diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp
index 70756d1..cdf2ad7 100644
--- a/src/engine/SMFDriver.cpp
+++ b/src/engine/SMFDriver.cpp
@@ -38,10 +38,8 @@ namespace Machina {
* @return the resulting machine.
*/
SharedPtr<Machine>
-SMFDriver::learn(const Glib::ustring& uri, unsigned track, Raul::BeatTime max_duration)
+SMFDriver::learn(const string& filename, unsigned track, Raul::BeatTime max_duration)
{
- const string filename = Glib::filename_from_uri(uri);
-
SharedPtr<Machine> m(new Machine());
Raul::SMFReader reader;
@@ -64,10 +62,8 @@ SMFDriver::learn(const Glib::ustring& uri, unsigned track, Raul::BeatTime max_du
* This will result in a disjoint subgraph in the machine, one for each track.
*/
SharedPtr<Machine>
-SMFDriver::learn(const Glib::ustring& uri, Raul::BeatTime max_duration)
+SMFDriver::learn(const string& filename, Raul::BeatTime max_duration)
{
- const string filename = Glib::filename_from_uri(uri);
-
SharedPtr<Machine> m(new Machine());
Raul::SMFReader reader;
diff --git a/src/engine/machina/SMFDriver.hpp b/src/engine/machina/SMFDriver.hpp
index 17c196e..3e74726 100644
--- a/src/engine/machina/SMFDriver.hpp
+++ b/src/engine/machina/SMFDriver.hpp
@@ -35,8 +35,8 @@ class Machine;
class SMFDriver : public Raul::SMFWriter,
public boost::enable_shared_from_this<SMFDriver> {
public:
- SharedPtr<Machine> learn(const Glib::ustring& uri, Raul::BeatTime max_duration=0);
- SharedPtr<Machine> learn(const Glib::ustring& uri, unsigned track, Raul::BeatTime max_duration=0);
+ SharedPtr<Machine> learn(const std::string& filename, Raul::BeatTime max_duration=0);
+ SharedPtr<Machine> learn(const std::string& filename, unsigned track, Raul::BeatTime max_duration=0);
void run(SharedPtr<Machine> machine, Raul::BeatTime max_time);
diff --git a/src/gui/main.cpp b/src/gui/main.cpp
index 20f75b9..b6dea9c 100644
--- a/src/gui/main.cpp
+++ b/src/gui/main.cpp
@@ -38,7 +38,7 @@ main(int argc, char** argv)
string filename = argv[1];
cout << "Building machine from MIDI file " << filename << endl;
SharedPtr<Machina::SMFDriver> file_driver(new Machina::SMFDriver());
- machine = file_driver->learn(string("file://") + filename, 0.0);
+ machine = file_driver->learn(filename, 0.0);
}
// Build engine
diff --git a/src/main.cpp b/src/machina.cpp
index f04a646..f04a646 100644
--- a/src/main.cpp
+++ b/src/machina.cpp
diff --git a/src/midi2machina.cpp b/src/midi2machina.cpp
new file mode 100644
index 0000000..fb5951f
--- /dev/null
+++ b/src/midi2machina.cpp
@@ -0,0 +1,83 @@
+/* This file is part of Machina.
+ * Copyright (C) 2007 Dave 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 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 Street, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include <iostream>
+#include <signal.h>
+#include <raul/RDFWriter.h>
+#include "machina/Engine.hpp"
+#include "machina/Machine.hpp"
+#include "machina/Node.hpp"
+#include "machina/Action.hpp"
+#include "machina/Edge.hpp"
+#include "machina/SMFDriver.hpp"
+#include "machina/MidiAction.hpp"
+
+using namespace std;
+using namespace Machina;
+
+
+bool quit = false;
+
+
+void
+catch_int(int)
+{
+ signal(SIGINT, catch_int);
+ signal(SIGTERM, catch_int);
+
+ std::cout << "Interrupted" << std::endl;
+
+ quit = true;
+}
+
+
+int
+main(int argc, char** argv)
+{
+ if (argc != 2) {
+ cout << "Usage: " << argv[0] << " FILE" << endl;
+ return -1;
+ }
+
+ SharedPtr<SMFDriver> driver(new SMFDriver());
+
+ SharedPtr<Machine> machine = driver->learn(argv[1]);
+
+ if (!machine) {
+ cout << "Failed to load MIDI file." << endl;
+ return -1;
+ }
+
+ string out_filename = argv[1];
+ if (out_filename.find_last_of("/") != string::npos)
+ out_filename = out_filename.substr(out_filename.find_last_of("/")+1);
+
+ const string::size_type last_dot = out_filename.find_last_of(".");
+ if (last_dot != string::npos && last_dot != 0)
+ out_filename = out_filename.substr(0, last_dot);
+ out_filename += ".machina";
+
+ cout << "Writing output to " << out_filename << endl;
+
+ Raul::RDFWriter writer;
+ writer.start_to_filename(out_filename);
+ machine->write_state(writer);
+ writer.finish();
+
+ return 0;
+}
+
diff --git a/util/machina2dot.py b/util/machina2dot.py
index eb89ec8..cb704ef 100755
--- a/util/machina2dot.py
+++ b/util/machina2dot.py
@@ -16,7 +16,7 @@ parser.parse_into_model(model, "file:" + sys.argv[1])
print """
digraph finite_state_machine {
rankdir=LR;
- node [shape = doublecircle ];
+ node [ shape = circle ];
""",
node_durations = { }
@@ -30,14 +30,17 @@ SELECT DISTINCT ?n ?dur WHERE {
}
""")
+invis_id = 0;
+
for result in initial_nodes_query.execute(model):
node_id = result['n'].blank_identifier
duration = float(result['dur'].literal_value['string'])
node_durations[node_id] = duration
+ print "\t{ node [ style = invis ] ",
+ print "invis%d" % invis_id, " }"
print '\t', node_id, "[ label = \"d =", duration, "\"];"
-
-
-print "\tnode [shape = circle ];"
+ print '\t', "invis%d" % invis_id, " -> ", node_id
+ invis_id += 1
nodes_query = RDF.SPARQLQuery("""
@@ -53,7 +56,7 @@ for result in nodes_query.execute(model):
node_id = result['n'].blank_identifier
duration = float(result['dur'].literal_value['string'])
node_durations[node_id] = duration
- print '\t', node_id, "[ label = \"d =", duration, "\"]; "
+ print '\t', node_id, "[ label = \"d =", "%.2f" % duration, "\"]; "
edge_query = RDF.SPARQLQuery("""
@@ -69,7 +72,7 @@ SELECT DISTINCT ?tail ?head ?prob WHERE {
for edge in edge_query.execute(model):
print '\t', edge['tail'].blank_identifier, ' -> ',
print edge['head'].blank_identifier, ' ',
- print "[ label = \"", edge['prob'].literal_value['string'], "\" ",
+ print "[ label = \"", "%1.2f" % float(edge['prob'].literal_value['string']), "\" ",
print "minlen = ", node_durations[edge['tail'].blank_identifier] * 2, " ];"
print "}"