rename enum values, move event count to voice
This commit is contained in:
@@ -9,10 +9,10 @@ using namespace std;
|
|||||||
namespace trnr {
|
namespace trnr {
|
||||||
|
|
||||||
enum midi_event_type {
|
enum midi_event_type {
|
||||||
note_on = 0,
|
NOTE_ON = 0,
|
||||||
note_off,
|
NOTE_OFF,
|
||||||
pitch_wheel,
|
PITCH_WHEEL,
|
||||||
mod_wheel
|
MOD_WHEEL
|
||||||
};
|
};
|
||||||
|
|
||||||
struct midi_event {
|
struct midi_event {
|
||||||
@@ -23,6 +23,34 @@ struct midi_event {
|
|||||||
double data;
|
double data;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
inline void make_note_on(midi_event& ev, int _midi_note, float _velocity, int _offset = 0)
|
||||||
|
{
|
||||||
|
ev.type = midi_event_type::NOTE_ON;
|
||||||
|
ev.midi_note = _midi_note;
|
||||||
|
ev.velocity = _velocity;
|
||||||
|
ev.offset = _offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void make_note_off(midi_event& ev, int _midi_note, float _velocity, int _offset = 0)
|
||||||
|
{
|
||||||
|
ev.type = midi_event_type::NOTE_OFF;
|
||||||
|
ev.midi_note = _midi_note;
|
||||||
|
ev.velocity = _velocity;
|
||||||
|
ev.offset = _offset;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void make_pitch_wheel(midi_event& ev, double _pitch, int _offset = 0)
|
||||||
|
{
|
||||||
|
ev.type = midi_event_type::PITCH_WHEEL;
|
||||||
|
ev.data = _pitch;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void make_mod_wheel(midi_event& ev, double _mod, int _offset = 0)
|
||||||
|
{
|
||||||
|
ev.type = midi_event_type::PITCH_WHEEL;
|
||||||
|
ev.data = _mod;
|
||||||
|
}
|
||||||
|
|
||||||
#ifndef MAX_VOICES
|
#ifndef MAX_VOICES
|
||||||
#define MAX_VOICES 16
|
#define MAX_VOICES 16
|
||||||
#endif
|
#endif
|
||||||
@@ -38,18 +66,18 @@ struct voice_state {
|
|||||||
bool trigger;
|
bool trigger;
|
||||||
float velocity;
|
float velocity;
|
||||||
array<midi_event, MAX_EVENTS_PER_VOICE> events;
|
array<midi_event, MAX_EVENTS_PER_VOICE> events;
|
||||||
|
size_t event_count = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct voice_allocator {
|
struct voice_allocator {
|
||||||
array<voice_state, MAX_VOICES> voices;
|
array<voice_state, MAX_VOICES> voices;
|
||||||
array<size_t, MAX_EVENTS_PER_VOICE> counts;
|
|
||||||
int active_voice_count = 1;
|
int active_voice_count = 1;
|
||||||
size_t index_to_steal = 0;
|
size_t index_to_steal = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
inline void voice_allocator_init(voice_allocator& va)
|
inline void voice_allocator_init(voice_allocator& va)
|
||||||
{
|
{
|
||||||
for (size_t i = 0; i < MAX_VOICES; ++i) { va.counts[i] = 0; }
|
for (size_t i = 0; i < MAX_VOICES; ++i) { va.voices[i].event_count = 0; }
|
||||||
}
|
}
|
||||||
|
|
||||||
inline void voice_allocator_process_block(voice_allocator& va, const vector<midi_event>& midi_events)
|
inline void voice_allocator_process_block(voice_allocator& va, const vector<midi_event>& midi_events)
|
||||||
@@ -57,16 +85,17 @@ inline void voice_allocator_process_block(voice_allocator& va, const vector<midi
|
|||||||
// reset voice events and counts
|
// reset voice events and counts
|
||||||
for (int i = 0; i < MAX_VOICES; i++) {
|
for (int i = 0; i < MAX_VOICES; i++) {
|
||||||
for (int j = 0; j < MAX_EVENTS_PER_VOICE; j++) { va.voices[i].events[j] = midi_event {}; }
|
for (int j = 0; j < MAX_EVENTS_PER_VOICE; j++) { va.voices[i].events[j] = midi_event {}; }
|
||||||
|
va.voices[i].event_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const auto& ev : midi_events) {
|
for (const auto& ev : midi_events) {
|
||||||
switch (ev.type) {
|
switch (ev.type) {
|
||||||
case note_on: {
|
case NOTE_ON: {
|
||||||
bool found = false;
|
bool found = false;
|
||||||
// attempt to find a free voice
|
// attempt to find a free voice
|
||||||
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
||||||
if (!va.voices[i].is_busy) {
|
if (!va.voices[i].is_busy) {
|
||||||
va.voices[i].events[va.counts[i]++] = ev;
|
va.voices[i].events[va.voices[i].event_count++] = ev;
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -77,7 +106,7 @@ inline void voice_allocator_process_block(voice_allocator& va, const vector<midi
|
|||||||
// try to find a voice that is not gated
|
// try to find a voice that is not gated
|
||||||
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
||||||
if (!va.voices[i].gate) {
|
if (!va.voices[i].gate) {
|
||||||
va.voices[i].events[va.counts[i]++] = ev;
|
va.voices[i].events[va.voices[i].event_count++] = ev;
|
||||||
found = true;
|
found = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@@ -86,20 +115,20 @@ inline void voice_allocator_process_block(voice_allocator& va, const vector<midi
|
|||||||
if (found) break;
|
if (found) break;
|
||||||
|
|
||||||
// if all voices are gated, steal one round-robin
|
// if all voices are gated, steal one round-robin
|
||||||
va.voices[va.index_to_steal].events[va.counts[va.index_to_steal]++] = ev;
|
va.voices[va.index_to_steal].events[va.voices[va.index_to_steal].event_count++] = ev;
|
||||||
va.index_to_steal++;
|
va.index_to_steal++;
|
||||||
if (va.index_to_steal >= va.active_voice_count) va.index_to_steal = 0;
|
if (va.index_to_steal >= va.active_voice_count) va.index_to_steal = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case note_off: {
|
case NOTE_OFF: {
|
||||||
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
for (size_t i = 0; i < va.active_voice_count; ++i) {
|
||||||
if (va.voices[i].midi_note == ev.midi_note) va.voices[i].events[va.counts[i]++] = ev;
|
if (va.voices[i].midi_note == ev.midi_note) va.voices[i].events[va.voices[i].event_count++] = ev;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case pitch_wheel:
|
case PITCH_WHEEL:
|
||||||
case mod_wheel: {
|
case MOD_WHEEL: {
|
||||||
for (size_t i = 0; i < va.active_voice_count; ++i) { va.voices[i].events[va.counts[i]++] = ev; }
|
for (size_t i = 0; i < va.active_voice_count; ++i) { va.voices[i].events[va.voices[i].event_count++] = ev; }
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -114,24 +143,24 @@ inline void voice_process_event_for_frame(voice_state& v, size_t frame)
|
|||||||
const midi_event& ev = v.events[i];
|
const midi_event& ev = v.events[i];
|
||||||
if (ev.offset == frame) {
|
if (ev.offset == frame) {
|
||||||
best_event = &ev;
|
best_event = &ev;
|
||||||
if (ev.type == note_on) break;
|
if (ev.type == NOTE_ON) break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (best_event) switch (best_event->type) {
|
if (best_event) switch (best_event->type) {
|
||||||
case note_on:
|
case NOTE_ON:
|
||||||
v.midi_note = best_event->midi_note;
|
v.midi_note = best_event->midi_note;
|
||||||
v.velocity = best_event->velocity;
|
v.velocity = best_event->velocity;
|
||||||
v.is_busy = true;
|
v.is_busy = true;
|
||||||
v.gate = true;
|
v.gate = true;
|
||||||
v.trigger = true;
|
v.trigger = true;
|
||||||
break;
|
break;
|
||||||
case note_off:
|
case NOTE_OFF:
|
||||||
v.gate = false;
|
v.gate = false;
|
||||||
break;
|
break;
|
||||||
// TODO: handle pitch wheel and mod wheel events
|
// TODO: handle pitch wheel and mod wheel events
|
||||||
case pitch_wheel:
|
case PITCH_WHEEL:
|
||||||
case mod_wheel:
|
case MOD_WHEEL:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user