aboutsummaryrefslogtreecommitdiffstats
path: root/src/engine
diff options
context:
space:
mode:
Diffstat (limited to 'src/engine')
-rw-r--r--src/engine/Machine.cpp46
-rw-r--r--src/engine/MachineBuilder.cpp55
-rw-r--r--src/engine/Node.cpp16
-rw-r--r--src/engine/machina/MachineBuilder.hpp1
-rw-r--r--src/engine/machina/MidiAction.hpp1
-rw-r--r--src/engine/machina/Node.hpp4
6 files changed, 103 insertions, 20 deletions
diff --git a/src/engine/Machine.cpp b/src/engine/Machine.cpp
index c61bd4f..38e6e11 100644
--- a/src/engine/Machine.cpp
+++ b/src/engine/Machine.cpp
@@ -65,6 +65,9 @@ void
Machine::remove_node(SharedPtr<Node> node)
{
_nodes.erase(_nodes.find(node));
+
+ for (Nodes::const_iterator n = _nodes.begin(); n != _nodes.end(); ++n)
+ (*n)->remove_outgoing_edges_to(node);
}
@@ -164,21 +167,44 @@ Machine::exit_node(SharedPtr<Raul::MIDISink> sink, const SharedPtr<Node> node)
}
}
- // Activate all successors to this node
+ // Activate successors to this node
// (that aren't aready active right now)
- for (Node::Edges::const_iterator s = node->outgoing_edges().begin();
- s != node->outgoing_edges().end(); ++s) {
-
- assert((*s)->head() != node); // no loops
+
+ if (node->is_selector()) {
const double rand_normal = rand() / (double)RAND_MAX; // [0, 1]
-
- if (rand_normal <= (*s)->probability()) {
- SharedPtr<Node> head = (*s)->head();
+ double range_min = 0;
+
+ for (Node::Edges::const_iterator s = node->outgoing_edges().begin();
+ s != node->outgoing_edges().end(); ++s) {
+
+ if (!(*s)->head()->is_active()
+ && rand_normal > range_min
+ && rand_normal < range_min + (*s)->probability()) {
+
+ enter_node(sink, (*s)->head());
+ break;
+
+ } else {
+ range_min += (*s)->probability();
+ }
+ }
+
+ } else {
+
+ for (Node::Edges::const_iterator s = node->outgoing_edges().begin();
+ s != node->outgoing_edges().end(); ++s) {
- if (!head->is_active())
- enter_node(sink, head);
+ const double rand_normal = rand() / (double)RAND_MAX; // [0, 1]
+
+ if (rand_normal <= (*s)->probability()) {
+ SharedPtr<Node> head = (*s)->head();
+
+ if ( ! head->is_active())
+ enter_node(sink, head);
+ }
}
+
}
}
diff --git a/src/engine/MachineBuilder.cpp b/src/engine/MachineBuilder.cpp
index 03d20b9..b62e90e 100644
--- a/src/engine/MachineBuilder.cpp
+++ b/src/engine/MachineBuilder.cpp
@@ -62,6 +62,21 @@ MachineBuilder::is_delay_node(SharedPtr<Node> node) const
}
+/** Set the duration of a node, with quantization.
+ */
+void
+MachineBuilder::set_node_duration(SharedPtr<Node> node, Raul::BeatTime d) const
+{
+ Raul::BeatTime q_dur = Quantizer::quantize(_quantization, d);
+
+ // Never quantize a note to duration 0
+ if (q_dur == 0 && ( node->enter_action() || node->exit_action() ))
+ q_dur = _quantization; // Round up
+
+ node->set_duration(q_dur);
+}
+
+
/** Connect two nodes, inserting a delay node between them if necessary.
*
* If a delay node is added to the machine, it is returned.
@@ -83,7 +98,7 @@ MachineBuilder::connect_nodes(SharedPtr<Machine> m,
if (is_delay_node(tail) && tail->outgoing_edges().size() == 0) {
// Tail is a delay node, just accumulate the time difference into it
//cerr << "Accumulating delay " << tail_end_time << " .. " << head_start_time << endl;
- tail->set_duration(tail->duration() + head_start_time - tail_end_time);
+ set_node_duration(tail, tail->duration() + head_start_time - tail_end_time);
tail->add_outgoing_edge(SharedPtr<Edge>(new Edge(tail, head)));
} else if (head_start_time == tail_end_time) {
// Connect directly
@@ -93,7 +108,7 @@ MachineBuilder::connect_nodes(SharedPtr<Machine> m,
// Need to actually create a delay node
//cerr << "Adding delay node for " << tail_end_time << " .. " << head_start_time << endl;
delay_node = SharedPtr<Node>(new Node());
- delay_node->set_duration(head_start_time - tail_end_time);
+ set_node_duration(delay_node, head_start_time - tail_end_time);
tail->add_outgoing_edge(SharedPtr<Edge>(new Edge(tail, delay_node)));
delay_node->add_outgoing_edge(SharedPtr<Edge>(new Edge(delay_node, head)));
m->add_node(delay_node);
@@ -181,7 +196,7 @@ MachineBuilder::event(Raul::BeatTime time_offset,
SharedPtr<Node> resolved = *i;
resolved->set_exit_action(SharedPtr<Action>(new MidiAction(ev_size, buf)));
- resolved->set_duration(t - resolved->enter_time());
+ set_node_duration(resolved, t - resolved->enter_time());
// Last active note
if (_active_nodes.size() == 1) {
@@ -227,7 +242,7 @@ MachineBuilder::event(Raul::BeatTime time_offset,
_connect_node->set_exit_action(resolved->exit_action());
resolved->remove_enter_action();
resolved->remove_exit_action();
- _connect_node->set_duration(resolved->duration());
+ set_node_duration(_connect_node, resolved->duration());
resolved = _connect_node;
if (_machine->nodes().find(_connect_node) == _machine->nodes().end())
_machine->add_node(_connect_node);
@@ -281,19 +296,20 @@ MachineBuilder::resolve()
if (ev_size == 3 && (ev[0] & 0xF0) == MIDI_CMD_NOTE_ON) {
unsigned char note_off[3] = { ((MIDI_CMD_NOTE_OFF & 0xF0) | (ev[0] & 0x0F)), ev[1], 0x40 };
(*i)->set_exit_action(SharedPtr<Action>(new MidiAction(3, note_off)));
- (*i)->set_duration(_time - (*i)->enter_time());
+ set_node_duration((*i), _time - (*i)->enter_time());
(*i)->exit(SharedPtr<Raul::MIDISink>(), _time);
_machine->add_node((*i));
}
}
_active_nodes.clear();
}
-
+
// Add initial note if necessary
if (_machine->nodes().size() > 0
&& _machine->nodes().find(_initial_node) == _machine->nodes().end())
_machine->add_node(_initial_node);
-
+
+#if 0
// Quantize
if (_quantization > 0) {
for (Machine::Nodes::iterator i = _machine->nodes().begin();
@@ -305,6 +321,31 @@ MachineBuilder::resolve()
(*i)->set_duration(q_dur);
}
}
+
+ // Remove any useless states
+ for (Machine::Nodes::iterator i = _machine->nodes().begin();
+ i != _machine->nodes().end() ; ) {
+
+ if (!(*i)->enter_action() && !(*i)->exit_action() && (*i)->duration() == 0
+ && (*i)->outgoing_edges().size() <= 1) {
+
+ // Connect any predecessors to the successor
+ if ((*i)->outgoing_edges().size() == 1) {
+ SharedPtr<Node> successor = *((*i)->outgoing_edges().begin())->head();
+
+ for (Machine::Nodes::iterator i = _machine->nodes().begin();
+ i != _machine->nodes().end() ; ) {
+
+
+ Machine::Nodes::iterator next = i;
+ ++next;
+
+ _machine->remove_node(*i);
+
+ i = next;
+ }
+ }
+#endif
}
diff --git a/src/engine/Node.cpp b/src/engine/Node.cpp
index e51e33d..b55619d 100644
--- a/src/engine/Node.cpp
+++ b/src/engine/Node.cpp
@@ -34,6 +34,22 @@ Node::Node(BeatCount duration, bool initial)
void
+Node::set_selector(bool yn)
+{
+ _is_selector = yn;
+
+ if (yn) {
+ double prob_sum = 0;
+ for (Edges::iterator i = _outgoing_edges.begin(); i != _outgoing_edges.end(); ++i)
+ prob_sum += (*i)->probability();
+
+ for (Edges::iterator i = _outgoing_edges.begin(); i != _outgoing_edges.end(); ++i)
+ (*i)->set_probability((*i)->probability() / prob_sum);
+ }
+}
+
+
+void
Node::set_enter_action(SharedPtr<Action> action)
{
_enter_action = action;
diff --git a/src/engine/machina/MachineBuilder.hpp b/src/engine/machina/MachineBuilder.hpp
index ac446c9..25104e8 100644
--- a/src/engine/machina/MachineBuilder.hpp
+++ b/src/engine/machina/MachineBuilder.hpp
@@ -44,6 +44,7 @@ public:
private:
bool is_delay_node(SharedPtr<Node> node) const;
+ void set_node_duration(SharedPtr<Node> node, Raul::BeatTime d) const;
SharedPtr<Node>
connect_nodes(SharedPtr<Machine> m,
diff --git a/src/engine/machina/MidiAction.hpp b/src/engine/machina/MidiAction.hpp
index 60d9189..510c0d0 100644
--- a/src/engine/machina/MidiAction.hpp
+++ b/src/engine/machina/MidiAction.hpp
@@ -57,7 +57,6 @@ public:
private:
-
size_t _size;
const size_t _max_size;
Raul::AtomicPtr<byte> _event;
diff --git a/src/engine/machina/Node.hpp b/src/engine/machina/Node.hpp
index b4c72e2..66d1b2d 100644
--- a/src/engine/machina/Node.hpp
+++ b/src/engine/machina/Node.hpp
@@ -66,13 +66,13 @@ public:
bool is_initial() const { return _is_initial; }
void set_initial(bool i) { _is_initial = i; }
- bool is_selector() const { return _is_selector; }
- void set_selector(bool i) { _is_selector = i; }
bool is_active() const { return _is_active; }
BeatTime enter_time() const { assert(_is_active); return _enter_time; }
BeatTime exit_time() const { assert(_is_active); return _enter_time + _duration; }
BeatCount duration() { return _duration; }
void set_duration(BeatCount d) { _duration = d; }
+ bool is_selector() const { return _is_selector; }
+ void set_selector(bool i);
typedef Raul::List<SharedPtr<Edge> > Edges;
Edges& outgoing_edges() { return _outgoing_edges; }