aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/engine/SMFDriver.cpp70
-rw-r--r--src/engine/machina/SMFDriver.hpp9
-rw-r--r--src/gui/MachinaCanvas.cpp8
-rw-r--r--src/gui/MachinaGUI.cpp8
4 files changed, 71 insertions, 24 deletions
diff --git a/src/engine/SMFDriver.cpp b/src/engine/SMFDriver.cpp
index 3481222..72bd8c6 100644
--- a/src/engine/SMFDriver.cpp
+++ b/src/engine/SMFDriver.cpp
@@ -30,7 +30,9 @@ using namespace std;
namespace Machina {
-/** Learn the MIDI file at @a uri
+/** Learn a single track from the MIDI file at @a uri
+ *
+ * @track selects which track of the MIDI file to import, starting from 1.
*
* Currently only file:// URIs are supported.
* @return the resulting machine.
@@ -40,20 +42,62 @@ SMFDriver::learn(const Glib::ustring& uri, unsigned track)
{
const string filename = Glib::filename_from_uri(uri);
- std::cerr << "Learn MIDI: " << filename << std::endl;
-
SharedPtr<Machine> m(new Machine());
+
+ Raul::SMFReader reader;
+ reader.open(filename);
- list<SharedPtr<Node> > active_nodes;
- SharedPtr<Node> connect_node(new Node());
- connect_node->set_initial(true);
- m->add_node(connect_node);
+ if (track > reader.num_tracks())
+ return SharedPtr<Machine>();
+ else
+ learn_track(m, reader, track);
+
+ if (m->nodes().size() > 1)
+ return m;
+ else
+ return SharedPtr<Machine>();
+}
+
+
+/** Learn all tracks from a MIDI file into a single machine.
+ *
+ * This will result in a disjoint subgraph in the machine, one for each track.
+ */
+SharedPtr<Machine>
+SMFDriver::learn(const Glib::ustring& uri)
+{
+ const string filename = Glib::filename_from_uri(uri);
+ SharedPtr<Machine> m(new Machine());
+
Raul::SMFReader reader;
reader.open(filename);
- if ( ! reader.seek_to_track(track) )
+ for (unsigned t=1; t <= reader.num_tracks(); ++t) {
+ learn_track(m, reader, t);
+ }
+
+ if (m->nodes().size() > 1)
+ return m;
+ else
return SharedPtr<Machine>();
+}
+
+
+void
+SMFDriver::learn_track(SharedPtr<Machine> m,
+ Raul::SMFReader& reader,
+ unsigned track)
+{
+ const bool found_track = reader.seek_to_track(track);
+ assert(found_track);
+
+ list<SharedPtr<Node> > active_nodes;
+
+ SharedPtr<Node> initial_node(new Node());
+ m->add_node(initial_node);
+
+ SharedPtr<Node> connect_node = initial_node;
Raul::BeatTime t = 0;
unsigned char buf[4];
@@ -95,15 +139,9 @@ SMFDriver::learn(const Glib::ustring& uri, unsigned track)
}
}
}
- /*std::cerr << "Event, size = " << ev_size << ", time = " << ev_time << std::endl;
- cerr.flags(ios::hex);
- for (uint32_t i=0; i < ev_size; ++i) {
- cerr << "0x" << (int)buf[i] << " ";
- }
- cerr.flags(ios::dec);
- cerr << endl;*/
}
- return m;
+
+ initial_node->set_initial(true);
}
diff --git a/src/engine/machina/SMFDriver.hpp b/src/engine/machina/SMFDriver.hpp
index f5d1229..f9b266b 100644
--- a/src/engine/machina/SMFDriver.hpp
+++ b/src/engine/machina/SMFDriver.hpp
@@ -22,6 +22,7 @@
#include <glibmm/ustring.h>
#include <raul/SharedPtr.h>
#include <raul/SMFWriter.h>
+#include <raul/SMFReader.h>
#include "machina/types.hpp"
#include "machina/MidiDriver.hpp"
@@ -34,9 +35,15 @@ class Machine;
class SMFDriver : public Raul::SMFWriter,
public boost::enable_shared_from_this<SMFDriver> {
public:
- SharedPtr<Machine> learn(const Glib::ustring& uri, unsigned track=1);
+ SharedPtr<Machine> learn(const Glib::ustring& uri);
+ SharedPtr<Machine> learn(const Glib::ustring& uri, unsigned track);
void run(SharedPtr<Machine> machine, Raul::BeatTime max_time);
+
+private:
+ void learn_track(SharedPtr<Machine> machine,
+ Raul::SMFReader& reader,
+ unsigned track);
};
diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp
index 2c22791..327b2b0 100644
--- a/src/gui/MachinaCanvas.cpp
+++ b/src/gui/MachinaCanvas.cpp
@@ -177,17 +177,17 @@ MachinaCanvas::build(SharedPtr<Machina::Machine> machine)
for (Machina::Machine::Nodes::const_iterator i = machine->nodes().begin();
i != machine->nodes().end(); ++i) {
- const double x = 1600 + rand() % 300;
- const double y = 1200 + rand() % 300;
+ //const double x = 1600 + rand() % 300;
+ //const double y = 1200 + rand() % 300;
SharedPtr<NodeView> view(new NodeView((*i), shared_from_this(),
- "", x, y));
+ "", 10, 10));
view->signal_clicked.connect(sigc::bind<0>(sigc::mem_fun(this,
&MachinaCanvas::node_clicked), WeakPtr<NodeView>(view)));
add_item(view);
- view->resize();
+ //view->resize();
}
for (ItemMap::iterator n = _items.begin(); n != _items.end(); ++n) {
diff --git a/src/gui/MachinaGUI.cpp b/src/gui/MachinaGUI.cpp
index 8f8e607..db5e70c 100644
--- a/src/gui/MachinaGUI.cpp
+++ b/src/gui/MachinaGUI.cpp
@@ -445,7 +445,7 @@ MachinaGUI::menu_import_midi()
dialog.add_button(Gtk::Stock::CANCEL, Gtk::RESPONSE_CANCEL);
dialog.add_button(Gtk::Stock::OPEN, Gtk::RESPONSE_OK);
- Gtk::HBox* extra_widget = Gtk::manage(new Gtk::HBox());
+ /*Gtk::HBox* extra_widget = Gtk::manage(new Gtk::HBox());
Gtk::SpinButton* track_sb = Gtk::manage(new Gtk::SpinButton());
track_sb->set_increments(1, 10);
track_sb->set_range(1, 256);
@@ -454,13 +454,15 @@ MachinaGUI::menu_import_midi()
extra_widget->pack_start(*track_sb, false, false);
dialog.set_extra_widget(*extra_widget);
extra_widget->show_all();
+ */
const int result = dialog.run();
if (result == Gtk::RESPONSE_OK) {
SharedPtr<Machina::SMFDriver> file_driver(new Machina::SMFDriver());
- SharedPtr<Machina::Machine> machine = file_driver->learn(dialog.get_uri(),
- track_sb->get_value_as_int());
+ //SharedPtr<Machina::Machine> machine = file_driver->learn(dialog.get_uri(),
+ // track_sb->get_value_as_int());
+ SharedPtr<Machina::Machine> machine = file_driver->learn(dialog.get_uri());
if (machine) {
machine->activate();