summaryrefslogtreecommitdiffstats
path: root/src/libs/engine/MidiNoteNode.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/libs/engine/MidiNoteNode.cpp')
-rw-r--r--src/libs/engine/MidiNoteNode.cpp144
1 files changed, 71 insertions, 73 deletions
diff --git a/src/libs/engine/MidiNoteNode.cpp b/src/libs/engine/MidiNoteNode.cpp
index 9dc32f4a..4c0280bd 100644
--- a/src/libs/engine/MidiNoteNode.cpp
+++ b/src/libs/engine/MidiNoteNode.cpp
@@ -20,7 +20,6 @@
#include "MidiMessage.h"
#include "InputPort.h"
#include "OutputPort.h"
-#include "PortInfo.h"
#include "Plugin.h"
#include "Array.h"
#include "Om.h"
@@ -37,41 +36,40 @@ namespace Om {
MidiNoteNode::MidiNoteNode(const string& path, size_t poly, Patch* parent, samplerate srate, size_t buffer_size)
: InternalNode(path, poly, parent, srate, buffer_size),
- m_voices(new Voice[poly]),
- m_sustain(false)
+ _voices(new Voice[poly]),
+ _sustain(false)
{
- m_num_ports = 5;
- m_ports.alloc(m_num_ports);
+ _num_ports = 5;
+ _ports = new Array<Port*>(_num_ports);
- m_midi_in_port = new InputPort<MidiMessage>(this, "MIDI In", 0, 1,
- new PortInfo("MIDI In", MIDI, INPUT), m_buffer_size);
- m_ports.at(0) = m_midi_in_port;
+ _midi_in_port = new InputPort<MidiMessage>(this, "DataType::MIDI In", 0, 1, DataType::MIDI, _buffer_size);
+ _ports->at(0) = _midi_in_port;
- m_freq_port = new OutputPort<sample>(this, "Frequency", 1, poly,
- new PortInfo("Frequency", AUDIO, OUTPUT, 440, 0, 99999), m_buffer_size);
- m_ports.at(1) = m_freq_port;
+ _freq_port = new OutputPort<sample>(this, "Frequency", 1, poly, DataType::FLOAT, _buffer_size);
+ // new PortInfo("Frequency", AUDIO, OUTPUT, 440, 0, 99999), _buffer_size);
+ _ports->at(1) = _freq_port;
- m_vel_port = new OutputPort<sample>(this, "Velocity", 2, poly,
- new PortInfo("Velocity", AUDIO, OUTPUT, 0, 0, 1), m_buffer_size);
- m_ports.at(2) = m_vel_port;
+ _vel_port = new OutputPort<sample>(this, "Velocity", 2, poly, DataType::FLOAT, _buffer_size);
+ // new PortInfo("Velocity", AUDIO, OUTPUT, 0, 0, 1), _buffer_size);
+ _ports->at(2) = _vel_port;
- m_gate_port = new OutputPort<sample>(this, "Gate", 3, poly,
- new PortInfo("Gate", AUDIO, OUTPUT, 0, 0, 1), m_buffer_size);
- m_ports.at(3) = m_gate_port;
+ _gate_port = new OutputPort<sample>(this, "Gate", 3, poly, DataType::FLOAT, _buffer_size);
+ // new PortInfo("Gate", AUDIO, OUTPUT, 0, 0, 1), _buffer_size);
+ _ports->at(3) = _gate_port;
- m_trig_port = new OutputPort<sample>(this, "Trigger", 4, poly,
- new PortInfo("Trigger", AUDIO, OUTPUT, 0, 0, 1), m_buffer_size);
- m_ports.at(4) = m_trig_port;
+ _trig_port = new OutputPort<sample>(this, "Trigger", 4, poly, DataType::FLOAT, _buffer_size);
+ // new PortInfo("Trigger", AUDIO, OUTPUT, 0, 0, 1), _buffer_size);
+ _ports->at(4) = _trig_port;
- m_plugin.type(Plugin::Internal);
- m_plugin.plug_label("note_in");
- m_plugin.name("Om Note Node (MIDI, OSC)");
+ _plugin.type(Plugin::Internal);
+ _plugin.plug_label("note_in");
+ _plugin.name("Om Note Node (MIDI, OSC)");
}
MidiNoteNode::~MidiNoteNode()
{
- delete[] m_voices;
+ delete[] _voices;
}
@@ -82,8 +80,8 @@ MidiNoteNode::run(size_t nframes)
MidiMessage ev;
- for (size_t i=0; i < m_midi_in_port->buffer(0)->filled_size(); ++i) {
- ev = m_midi_in_port->buffer(0)->value_at(i);
+ for (size_t i=0; i < _midi_in_port->buffer(0)->filled_size(); ++i) {
+ ev = _midi_in_port->buffer(0)->value_at(i);
switch (ev.buffer[0] & 0xF0) {
case MIDI_CMD_NOTE_ON:
@@ -123,17 +121,17 @@ MidiNoteNode::note_on(uchar note_num, uchar velocity, samplecount offset)
{
const jack_nframes_t time_stamp = om->audio_driver()->time_stamp();
- assert(offset < m_buffer_size);
+ assert(offset < _buffer_size);
assert(note_num <= 127);
- Key* key = &m_keys[note_num];
+ Key* key = &_keys[note_num];
Voice* voice = NULL;
size_t voice_num = 0;
// Look for free voices
- for (size_t i=0; i < m_poly; ++i) {
- if (m_voices[i].state == Voice::Voice::FREE) {
- voice = &m_voices[i];
+ for (size_t i=0; i < _poly; ++i) {
+ if (_voices[i].state == Voice::Voice::FREE) {
+ voice = &_voices[i];
voice_num = i;
break;
}
@@ -142,26 +140,26 @@ MidiNoteNode::note_on(uchar note_num, uchar velocity, samplecount offset)
// If we didn't find a free one, steal the oldest
if (voice == NULL) {
voice_num = 0;
- voice = &m_voices[0];
- jack_nframes_t oldest_time = m_voices[0].time;
- for (size_t i=1; i < m_poly; ++i) {
- if (m_voices[i].time < oldest_time) {
- voice = &m_voices[i];
+ voice = &_voices[0];
+ jack_nframes_t oldest_time = _voices[0].time;
+ for (size_t i=1; i < _poly; ++i) {
+ if (_voices[i].time < oldest_time) {
+ voice = &_voices[i];
voice_num = i;
oldest_time = voice->time;
}
}
}
assert(voice != NULL);
- assert(voice == &m_voices[voice_num]);
+ assert(voice == &_voices[voice_num]);
//cerr << "[MidiNoteNode] Note on. Key " << (int)note_num << ", Voice " << voice_num << endl;
// Update stolen key, if applicable
if (voice->state == Voice::Voice::ACTIVE) {
- assert(m_keys[voice->note].voice == voice_num);
- assert(m_keys[voice->note].state == Key::ON_ASSIGNED);
- m_keys[voice->note].state = Key::Key::ON_UNASSIGNED;
+ assert(_keys[voice->note].voice == voice_num);
+ assert(_keys[voice->note].state == Key::ON_ASSIGNED);
+ _keys[voice->note].state = Key::Key::ON_UNASSIGNED;
//cerr << "[MidiNoteNode] Stole voice " << voice_num << endl;
}
@@ -175,45 +173,45 @@ MidiNoteNode::note_on(uchar note_num, uchar velocity, samplecount offset)
voice->note = note_num;
voice->time = time_stamp;
- assert(m_keys[voice->note].state == Key::Key::ON_ASSIGNED);
- assert(m_keys[voice->note].voice == voice_num);
+ assert(_keys[voice->note].state == Key::Key::ON_ASSIGNED);
+ assert(_keys[voice->note].voice == voice_num);
// one-sample jitter hack to avoid having to deal with trigger sample "next time"
- if (offset == (samplecount)(m_buffer_size-1))
+ if (offset == (samplecount)(_buffer_size-1))
--offset;
- m_freq_port->buffer(voice_num)->set(note_to_freq(note_num), offset);
- m_vel_port->buffer(voice_num)->set(velocity/127.0, offset);
- m_gate_port->buffer(voice_num)->set(1.0f, offset);
+ _freq_port->buffer(voice_num)->set(note_to_freq(note_num), offset);
+ _vel_port->buffer(voice_num)->set(velocity/127.0, offset);
+ _gate_port->buffer(voice_num)->set(1.0f, offset);
// trigger (one sample)
- m_trig_port->buffer(voice_num)->set(1.0f, offset, offset);
- m_trig_port->buffer(voice_num)->set(0.0f, offset+1);
+ _trig_port->buffer(voice_num)->set(1.0f, offset, offset);
+ _trig_port->buffer(voice_num)->set(0.0f, offset+1);
assert(key->state == Key::Key::ON_ASSIGNED);
assert(voice->state == Voice::Voice::ACTIVE);
assert(key->voice == voice_num);
- assert(m_voices[key->voice].note == note_num);
+ assert(_voices[key->voice].note == note_num);
}
void
MidiNoteNode::note_off(uchar note_num, samplecount offset)
{
- assert(offset < m_buffer_size);
+ assert(offset < _buffer_size);
- Key* key = &m_keys[note_num];
+ Key* key = &_keys[note_num];
if (key->state == Key::ON_ASSIGNED) {
// Assigned key, turn off voice and key
- assert(m_voices[key->voice].state == Voice::ACTIVE);
- assert(m_voices[key->voice].note == note_num);
+ assert(_voices[key->voice].state == Voice::ACTIVE);
+ assert(_voices[key->voice].note == note_num);
key->state = Key::OFF;
- if ( ! m_sustain)
+ if ( ! _sustain)
free_voice(key->voice, offset);
else
- m_voices[key->voice].state = Voice::HOLDING;
+ _voices[key->voice].state = Voice::HOLDING;
}
key->state = Key::OFF;
@@ -228,31 +226,31 @@ MidiNoteNode::free_voice(size_t voice, samplecount offset)
uchar replace_key_num = 0;
for (uchar i = 0; i <= 127; ++i) {
- if (m_keys[i].state == Key::ON_UNASSIGNED) {
- if (replace_key == NULL || m_keys[i].time > replace_key->time) {
- replace_key = &m_keys[i];
+ if (_keys[i].state == Key::ON_UNASSIGNED) {
+ if (replace_key == NULL || _keys[i].time > replace_key->time) {
+ replace_key = &_keys[i];
replace_key_num = i;
}
}
}
if (replace_key != NULL) { // Found a key to assign to freed voice
- assert(&m_keys[replace_key_num] == replace_key);
+ assert(&_keys[replace_key_num] == replace_key);
assert(replace_key->state == Key::ON_UNASSIGNED);
// Change the freq but leave the gate high and don't retrigger
- m_freq_port->buffer(voice)->set(note_to_freq(replace_key_num), offset);
+ _freq_port->buffer(voice)->set(note_to_freq(replace_key_num), offset);
replace_key->state = Key::ON_ASSIGNED;
replace_key->voice = voice;
- m_keys[m_voices[voice].note].state = Key::ON_UNASSIGNED;
- m_voices[voice].note = replace_key_num;
- m_voices[voice].state = Voice::ACTIVE;
+ _keys[_voices[voice].note].state = Key::ON_UNASSIGNED;
+ _voices[voice].note = replace_key_num;
+ _voices[voice].state = Voice::ACTIVE;
} else {
// No new note for voice, deactivate (set gate low)
//cerr << "[MidiNoteNode] Note off. Key " << (int)note_num << ", Voice " << voice << " Killed" << endl;
- m_gate_port->buffer(voice)->set(0.0f, offset);
- m_voices[voice].state = Voice::FREE;
+ _gate_port->buffer(voice)->set(0.0f, offset);
+ _voices[voice].state = Voice::FREE;
}
}
@@ -261,13 +259,13 @@ void
MidiNoteNode::all_notes_off(samplecount offset)
{
//cerr << "Note off starting at sample " << offset << endl;
- assert(offset < m_buffer_size);
+ assert(offset < _buffer_size);
// FIXME: set all keys to Key::OFF?
- for (size_t i=0; i < m_poly; ++i) {
- m_gate_port->buffer(i)->set(0.0f, offset);
- m_voices[i].state = Voice::FREE;
+ for (size_t i=0; i < _poly; ++i) {
+ _gate_port->buffer(i)->set(0.0f, offset);
+ _voices[i].state = Voice::FREE;
}
}
@@ -285,17 +283,17 @@ MidiNoteNode::note_to_freq(int num)
void
MidiNoteNode::sustain_on()
{
- m_sustain = true;
+ _sustain = true;
}
void
MidiNoteNode::sustain_off(samplecount offset)
{
- m_sustain = false;
+ _sustain = false;
- for (size_t i=0; i < m_poly; ++i)
- if (m_voices[i].state == Voice::HOLDING)
+ for (size_t i=0; i < _poly; ++i)
+ if (_voices[i].state == Voice::HOLDING)
free_voice(i, offset);
}