aboutsummaryrefslogtreecommitdiff
path: root/monitors_midi_pianoroll.c
diff options
context:
space:
mode:
authorHanspeter Portner <dev@open-music-kontrollers.ch>2018-04-19 09:27:14 +0200
committerHanspeter Portner <dev@open-music-kontrollers.ch>2018-04-19 09:27:14 +0200
commit4b3679cc7395ffce3a3649eca300a6f9e3d4b1c1 (patch)
tree1a39e0c71baf26aca3d4d29db7b1e72d4fffa2c5 /monitors_midi_pianoroll.c
parentb7612b7ec01e275b456a1c1fd898c9af9581ace6 (diff)
downloadmonitors.lv2-4b3679cc7395ffce3a3649eca300a6f9e3d4b1c1.tar.xz
make midi pianoroll more efficient.
Diffstat (limited to 'monitors_midi_pianoroll.c')
-rw-r--r--monitors_midi_pianoroll.c53
1 files changed, 28 insertions, 25 deletions
diff --git a/monitors_midi_pianoroll.c b/monitors_midi_pianoroll.c
index f46fbf4..6b283e2 100644
--- a/monitors_midi_pianoroll.c
+++ b/monitors_midi_pianoroll.c
@@ -29,6 +29,9 @@
#define MAX_NPROPS 3
#define MAX_KEYS 0x80
#define MAX_NOTES 0x80
+#define MAX_VELOCITY 0x80
+#define MASK_KEYS (MAX_KEYS - 1)
+#define MASK_NOTES (MAX_NOTES - 1)
typedef struct _plugstate_t plugstate_t;
typedef struct _plughandle_t plughandle_t;
@@ -64,9 +67,11 @@ struct _plughandle_t {
double sample_rate;
float update_rate;
uint32_t graph_size;
+ unsigned range;
+ float range_1;
- uint32_t spf;
- uint32_t thresh;
+ unsigned spf;
+ unsigned thresh;
int64_t cnt;
plugstate_t state;
@@ -74,8 +79,8 @@ struct _plughandle_t {
active_note_t actives [MAX_KEYS];
passive_note_t passives [MAX_KEYS][MAX_NOTES];
-
- uint32_t palette [0x80];
+ unsigned offsets [MAX_KEYS];
+ uint32_t palette [MAX_VELOCITY];
PROPS_T(props, MAX_NPROPS);
};
@@ -95,7 +100,7 @@ _render(plughandle_t *handle, int64_t frames)
LV2_Canvas_URID *urid = &handle->urid;
LV2_Atom_Forge_Frame frame;
- const float dy = 1.f / 0x7f;
+ const float dy = 1.f / MASK_KEYS;
lv2_atom_forge_set_buffer(forge, handle->state.graph, MAX_GRAPH);
@@ -108,17 +113,12 @@ _render(plughandle_t *handle, int64_t frames)
fprintf(stderr, "out-of-memory\n"); //FIXME
}
- const int64_t range = 1e-3 * handle->sample_rate * handle->state.window;
- const float range_1 = 1.f / range;
+ const unsigned range = handle->range;
+ const float range_1 = handle->range_1;
const int64_t head = handle->cnt + frames;
const int64_t tail = head - range;
- if(head < range)
- {
- return; //FIXME solve this differently
- }
-
- float y = 1.f - dy * 0.5f;
+ float y = 1.f - dy*0.5f;
for(unsigned i = 0; i < MAX_KEYS; i++, y -= dy)
{
active_note_t *active = &handle->actives[i];
@@ -143,18 +143,19 @@ _render(plughandle_t *handle, int64_t frames)
for(unsigned j = 0; j < MAX_NOTES; j++)
{
- passive_note_t *passive = &passives[j];
+ const unsigned idx = (handle->offsets[i] + j) & MASK_NOTES;
+ passive_note_t *passive = &passives[idx];
if(passive->start == 0)
{
break; // skip this key
}
- const bool start_in_window = (passive->start > tail) && (passive->start < head);
const bool end_in_window = (passive->end > tail) && (passive->end < head);
- if(start_in_window || end_in_window)
+ if(end_in_window)
{
+ const bool start_in_window = (passive->start > tail) && (passive->start < head);
const float x0 = start_in_window
? range_1 * (passive->start - tail)
: 0.f;
@@ -203,6 +204,9 @@ _intercept_window(void *data, int64_t frames, props_impl_t *impl)
memset(handle->actives, 0x0, sizeof(handle->actives));
memset(handle->passives, 0x0, sizeof(handle->passives));
+ handle->range = 1e-3 * handle->sample_rate * handle->state.window;
+ handle->range_1 = 1.f / handle->range;
+
_render(handle, frames);
}
@@ -232,7 +236,7 @@ _fill_palette(uint32_t *palette)
{
const unsigned n = sizeof(palette_colors) / sizeof(uint32_t);
unsigned I = 0;
- unsigned J = ceilf((float)0x80 / (n-1));
+ unsigned J = ceilf((float)MAX_VELOCITY / (n-1));
for(unsigned i = 0; i < (n-1); i++)
{
@@ -257,7 +261,7 @@ _fill_palette(uint32_t *palette)
const uint8_t b = b1 + floorf((b2 - b1)*p);
const uint8_t a = a1 + floorf((a2 - a1)*p);
- if(I + j < 0x80)
+ if(I + j < MAX_VELOCITY)
{
palette[I + j] = (r << 24) | (g << 16) | (b << 8) | a;
}
@@ -331,6 +335,8 @@ instantiate(const LV2_Descriptor* descriptor, double rate,
return NULL;
}
+ handle->cnt = 1;
+
_render(handle, 0);
_fill_palette(handle->palette);
@@ -360,16 +366,13 @@ static inline void
_note_off(plughandle_t *handle, int64_t frames, uint8_t key)
{
active_note_t *active = &handle->actives[key];
- passive_note_t *term = handle->passives[key] + MAX_NOTES;
- passive_note_t *passive;
+ unsigned *offset = &handle->offsets[key];
- // shift notes right
- for(passive = term - 1; passive > handle->passives[key]; passive--)
- {
- passive[0] = passive[-1];
- }
+ // shift offset left
+ *offset = (*offset - 1) & MASK_NOTES;
// prepend
+ passive_note_t *passive = &handle->passives[key][*offset];
passive->start = active->start;
passive->end = handle->cnt + frames;
passive->style = active->style;