diff options
author | rekado <rekado@elephly.net> | 2013-01-14 22:00:30 +0800 |
---|---|---|
committer | rekado <rekado@elephly.net> | 2013-01-14 22:00:30 +0800 |
commit | 6ecbe702bf74f30e4bac40c3466c957364765315 (patch) | |
tree | bea12146c9331a6dc0c5ab95bec3ea42981c426d | |
parent | 4ef1f4938d535899fea142eaa84e4f2d36ce56dc (diff) |
find next free voice
-rw-r--r-- | src/mdaPiano.cpp | 24 | ||||
-rw-r--r-- | src/mdaPiano.h | 1 | ||||
-rw-r--r-- | src/mdaPianoVoice.h | 1 |
3 files changed, 26 insertions, 0 deletions
diff --git a/src/mdaPiano.cpp b/src/mdaPiano.cpp index a3c0311..bc9a5f8 100644 --- a/src/mdaPiano.cpp +++ b/src/mdaPiano.cpp @@ -51,6 +51,30 @@ void mdaPiano::update() { } +unsigned mdaPiano::find_free_voice(unsigned char key, unsigned char velocity) { + //is this a retriggered note during sustain? + if (sustain) { + for (unsigned i = 0; i < NVOICES; ++i) { + if ((voices[i]->get_key() == key) && (voices[i]->is_sustained())) + return i; + } + } + + //take the next free voice if + // ... notes are sustained but not this new one + // ... notes are not sustained + for (unsigned i = 0; i < NVOICES; ++i) { + if (voices[i]->get_key() == LV2::INVALID_KEY) + { + return i; + } + } + + //TODO: steal quietest note if all voices are used up + return 0; +} + + void mdaPiano::setVolume(float value) { for (uint32_t v=0; v<NVOICES; ++v) diff --git a/src/mdaPiano.h b/src/mdaPiano.h index 3697e5d..cebb777 100644 --- a/src/mdaPiano.h +++ b/src/mdaPiano.h @@ -19,6 +19,7 @@ public: void load_kgrp(KGRP*); void load_samples(short**); + unsigned find_free_voice(unsigned char, unsigned char); void handle_midi(uint32_t size, unsigned char* data); void setVolume(float); void setParameter(unsigned char, float); diff --git a/src/mdaPianoVoice.h b/src/mdaPianoVoice.h index ab37dc5..1cd393f 100644 --- a/src/mdaPianoVoice.h +++ b/src/mdaPianoVoice.h @@ -57,6 +57,7 @@ class mdaPianoVoice : public LV2::Voice { void update(Param); // recalculates internal variables void on(unsigned char key, unsigned char velocity); void reset(void); + bool is_sustained(void) { return (note == SUSTAIN); } unsigned char get_key(void) const { return m_key; } }; |