aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/engine/JackDriver.cpp147
-rw-r--r--src/engine/JackDriver.hpp7
-rw-r--r--src/engine/MachineBuilder.cpp2
-rw-r--r--src/engine/Slave.hpp73
-rw-r--r--src/gui/MachinaCanvas.cpp18
5 files changed, 158 insertions, 89 deletions
diff --git a/src/engine/JackDriver.cpp b/src/engine/JackDriver.cpp
index 5319d53..d13bbdd 100644
--- a/src/engine/JackDriver.cpp
+++ b/src/engine/JackDriver.cpp
@@ -178,91 +178,76 @@ JackDriver::set_machine(SharedPtr<Machine> machine)
}
void
-JackDriver::process_input(SharedPtr<Machine> machine,
- const Raul::TimeSlice& time)
+JackDriver::read_input_recording(SharedPtr<Machine> machine,
+ const Raul::TimeSlice& time)
{
- // We only actually read Jack input at the beginning of a cycle
- //assert(time.offset_ticks().is_zero());
- assert(_input_port);
-
- if (_recording) {
-
- const jack_nframes_t nframes = time.length_ticks().ticks();
- void* jack_buffer = jack_port_get_buffer(_input_port,
- nframes);
- const jack_nframes_t event_count = jack_midi_get_event_count(
- jack_buffer);
-
- for (jack_nframes_t i = 0; i < event_count; ++i) {
- jack_midi_event_t ev;
- jack_midi_event_get(&ev, jack_buffer, i);
-
- const TimeStamp rel_time_frames = TimeStamp(_frames_unit, ev.time);
- const TimeStamp time_frames = _record_dur + rel_time_frames;
- _recorder->write(time.ticks_to_beats(
- time_frames), ev.size, ev.buffer);
- }
+ const jack_nframes_t nframes = time.length_ticks().ticks();
+ void* buf = jack_port_get_buffer(_input_port, nframes);
+ const jack_nframes_t n_events = jack_midi_get_event_count(buf);
+
+ for (jack_nframes_t i = 0; i < n_events; ++i) {
+ jack_midi_event_t ev;
+ jack_midi_event_get(&ev, buf, i);
+
+ const TimeStamp rel_time_frames = TimeStamp(_frames_unit, ev.time);
+ const TimeStamp time_frames = _record_dur + rel_time_frames;
+ _recorder->write(time.ticks_to_beats(
+ time_frames), ev.size, ev.buffer);
+ }
- if (event_count > 0) {
- _recorder->whip();
- }
+ if (n_events > 0) {
+ _recorder->whip();
+ }
- _record_dur += time.length_ticks();
+ _record_dur += time.length_ticks();
+}
- } else {
+void
+JackDriver::read_input_playing(SharedPtr<Machine> machine,
+ const Raul::TimeSlice& time)
+{
+ const jack_nframes_t nframes = time.length_ticks().ticks();
+ void* buf = jack_port_get_buffer(_input_port, nframes);
+ const jack_nframes_t n_events = jack_midi_get_event_count(buf);
+
+ for (jack_nframes_t i = 0; i < n_events; ++i) {
+ jack_midi_event_t ev;
+ jack_midi_event_get(&ev, buf, i);
+
+ if (ev.buffer[0] == 0x90) {
+ const SharedPtr<LearnRequest> learn = machine->pending_learn();
+ if (learn) {
+ learn->enter_action()->set_event(ev.size, ev.buffer);
+ learn->start(_quantization.get(),
+ TimeStamp(TimeUnit::frames(sample_rate()),
+ jack_last_frame_time(_client)
+ + ev.time, 0));
+ }
- const jack_nframes_t nframes = time.length_ticks().ticks();
-
- void* jack_buffer = jack_port_get_buffer(_input_port,nframes);
-
- const jack_nframes_t event_count = jack_midi_get_event_count(
- jack_buffer);
-
- for (jack_nframes_t i = 0; i < event_count; ++i) {
- jack_midi_event_t ev;
- jack_midi_event_get(&ev, jack_buffer, i);
-
- if (ev.buffer[0] == 0x90) {
-
- const SharedPtr<LearnRequest> learn = machine->pending_learn();
- if (learn) {
- learn->enter_action()->set_event(ev.size, ev.buffer);
- learn->start(_quantization.get(),
- TimeStamp(TimeUnit::frames(sample_rate()),
- jack_last_frame_time(_client)
- + ev.time, 0));
- }
-
- } else if (ev.buffer[0] == 0x80) {
-
- const SharedPtr<LearnRequest> learn = machine->pending_learn();
-
- if (learn) {
- if (learn->started()) {
- learn->exit_action()->set_event(ev.size, ev.buffer);
- learn->finish(
- TimeStamp(TimeUnit::frames(sample_rate()),
- jack_last_frame_time(_client) + ev.time,
- 0));
-
- const uint64_t id = Stateful::next_id();
- write_set(_updates, id,
- URIs::instance().rdf_type,
- _forge.make_urid(URIs::instance().
- machina_MidiAction));
- write_set(_updates, learn->node()->id(),
- URIs::instance().machina_enter_action,
- _forge.make((int32_t)id));
- write_set(_updates, id,
- URIs::instance().machina_note_number,
- _forge.make((int32_t)ev.buffer[1]));
-
- machine->clear_pending_learn();
- }
- }
+ } else if (ev.buffer[0] == 0x80) {
+ const SharedPtr<LearnRequest> learn = machine->pending_learn();
+ if (learn && learn->started()) {
+ learn->exit_action()->set_event(ev.size, ev.buffer);
+ learn->finish(
+ TimeStamp(TimeUnit::frames(sample_rate()),
+ jack_last_frame_time(_client) + ev.time,
+ 0));
+
+ const uint64_t id = Stateful::next_id();
+ write_set(_updates, id,
+ URIs::instance().rdf_type,
+ _forge.make_urid(URIs::instance().
+ machina_MidiAction));
+ write_set(_updates, learn->node()->id(),
+ URIs::instance().machina_enter_action,
+ _forge.make((int32_t)id));
+ write_set(_updates, id,
+ URIs::instance().machina_note_number,
+ _forge.make((int32_t)ev.buffer[1]));
+
+ machine->clear_pending_learn();
}
}
-
}
}
@@ -359,7 +344,11 @@ JackDriver::on_process(jack_nframes_t nframes)
machine->reset(_context.sink(), _context.time().start_beats());
}
- process_input(machine, _context.time());
+ if (_recording) {
+ read_input_recording(machine, _context.time());
+ } else {
+ read_input_playing(machine, _context.time());
+ }
if (machine->is_empty() || !machine->is_activated()) {
goto end;
diff --git a/src/engine/JackDriver.hpp b/src/engine/JackDriver.hpp
index 8b62424..c0f94ff 100644
--- a/src/engine/JackDriver.hpp
+++ b/src/engine/JackDriver.hpp
@@ -90,8 +90,11 @@ public:
jack_client_t* jack_client() const { return _client; }
private:
- void process_input(SharedPtr<Machine> machine,
- const Raul::TimeSlice& time);
+ void read_input_recording(SharedPtr<Machine> machine,
+ const Raul::TimeSlice& time);
+
+ void read_input_playing(SharedPtr<Machine> machine,
+ const Raul::TimeSlice& time);
static void jack_error_cb(const char* msg);
static int jack_process_cb(jack_nframes_t nframes, void* me);
diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp
index 818c893..8bdd5d4 100644
--- a/src/engine/MachineBuilder.cpp
+++ b/src/engine/MachineBuilder.cpp
@@ -307,7 +307,7 @@ MachineBuilder::resolve()
}
// Add initial note if necessary
- if (( _machine->nodes().size() > 0)
+ if ((_machine->nodes().size() > 0)
&& (std::find(_machine->nodes().begin(), _machine->nodes().end(),
_initial_node)
== _machine->nodes().end())) {
diff --git a/src/engine/Slave.hpp b/src/engine/Slave.hpp
new file mode 100644
index 0000000..7a16c10
--- /dev/null
+++ b/src/engine/Slave.hpp
@@ -0,0 +1,73 @@
+/* This file is part of Machina.
+ * Copyright 2007-2012 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
+ * (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 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/>.
+ */
+
+#ifndef MACHINA_SLAVE_HPP
+#define MACHINA_SLAVE_HPP
+
+#include "raul/Semaphore.hpp"
+#include "raul/Thread.hpp"
+
+namespace Machina {
+
+/** Thread driven by (realtime safe) signals.
+ *
+ * Use this to perform some task in a separate thread you want to 'drive'
+ * from a realtime (or otherwise) thread.
+ */
+class Slave
+ : public Raul::Thread
+{
+public:
+ Slave() : _whip(0) {}
+ ~Slave() { _exit_flag = true; _whip.post(); }
+
+ virtual void join() {
+ _exit_flag = true;
+ _whip.post();
+ Thread::join();
+ }
+
+ /** Tell the slave to do whatever work it does. Realtime safe. */
+ inline void whip() { _whip.post(); }
+
+protected:
+ /** Worker method.
+ *
+ * This is called once from this thread every time whip() is called.
+ * Implementations likely want to put a single (non loop) chunk of code
+ * here, e.g. to process an event.
+ */
+ virtual void _whipped() = 0;
+
+ Raul::Semaphore _whip;
+
+private:
+ inline void _run() {
+ while (true) {
+ _whip.wait();
+ if (_exit_flag) {
+ break;
+ }
+ _whipped();
+ }
+ }
+
+};
+
+} // namespace Machina
+
+#endif // MACHINA_SLAVE_HPP
diff --git a/src/gui/MachinaCanvas.cpp b/src/gui/MachinaCanvas.cpp
index 3d01012..ac59376 100644
--- a/src/gui/MachinaCanvas.cpp
+++ b/src/gui/MachinaCanvas.cpp
@@ -41,7 +41,7 @@ MachinaCanvas::MachinaCanvas(MachinaGUI* app, int width, int height)
widget().grab_focus();
signal_event.connect(
- sigc::mem_fun(this, &MachinaCanvas::on_event));
+ sigc::mem_fun(this, &MachinaCanvas::on_event));
}
bool
@@ -105,6 +105,10 @@ MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object)
{
const Machina::URIs& uris = URIs::instance();
const Raul::Atom& type = object->get(uris.rdf_type);
+ if (!type.is_valid()) {
+ return;
+ }
+
if (type.get<URIInt>() == uris.machina_Node) {
SharedPtr<NodeView> view(
new NodeView(_app->window(), *this, object,
@@ -115,18 +119,18 @@ MachinaCanvas::on_new_object(SharedPtr<Client::ClientObject> object)
// view->set_base_color(0x101010FF);
view->signal_clicked().connect(
- sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked),
- WeakPtr<NodeView>(view)));
+ sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked),
+ WeakPtr<NodeView>(view)));
object->set_view(view);
} else if (type.get<URIInt>() == uris.machina_Edge) {
SharedPtr<Machina::Client::ClientObject> tail
- = _app->client_model()->find(
- object->get(uris.machina_tail_id).get_int32());
+ = _app->client_model()->find(
+ object->get(uris.machina_tail_id).get_int32());
SharedPtr<Machina::Client::ClientObject> head
- = _app->client_model()->find(
- object->get(uris.machina_head_id).get_int32());
+ = _app->client_model()->find(
+ object->get(uris.machina_head_id).get_int32());
if (!tail || !head) {
std::cerr << "Invalid arc" << std::endl;