From ec0744d2a0773d1aa858c9c1135f77baf25d5a21 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sat, 12 Jan 2013 04:15:34 +0000 Subject: Add missing file. Break up deeply nested code. git-svn-id: http://svn.drobilla.net/lad/trunk/machina@4930 a436a847-0d15-0410-975c-d299462d15a1 --- src/engine/JackDriver.cpp | 147 +++++++++++++++++++----------------------- src/engine/JackDriver.hpp | 7 +- src/engine/MachineBuilder.cpp | 2 +- src/engine/Slave.hpp | 73 +++++++++++++++++++++ src/gui/MachinaCanvas.cpp | 18 ++++-- 5 files changed, 158 insertions(+), 89 deletions(-) create mode 100644 src/engine/Slave.hpp 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) } void -JackDriver::process_input(SharedPtr machine, - const Raul::TimeSlice& time) +JackDriver::read_input_recording(SharedPtr 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, + 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 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 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 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 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, - const Raul::TimeSlice& time); + void read_input_recording(SharedPtr machine, + const Raul::TimeSlice& time); + + void read_input_playing(SharedPtr 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 + * + * 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 . + */ + +#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 object) { const Machina::URIs& uris = URIs::instance(); const Raul::Atom& type = object->get(uris.rdf_type); + if (!type.is_valid()) { + return; + } + if (type.get() == uris.machina_Node) { SharedPtr view( new NodeView(_app->window(), *this, object, @@ -115,18 +119,18 @@ MachinaCanvas::on_new_object(SharedPtr object) // view->set_base_color(0x101010FF); view->signal_clicked().connect( - sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked), - WeakPtr(view))); + sigc::bind<0>(sigc::mem_fun(this, &MachinaCanvas::node_clicked), + WeakPtr(view))); object->set_view(view); } else if (type.get() == uris.machina_Edge) { SharedPtr 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 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; -- cgit v1.2.1