Phase 21: add filtered MIDI composition edits and triplet piano-roll grids
This commit is contained in:
parent
830b6e1914
commit
73fc5eea8d
12 changed files with 796 additions and 39 deletions
|
|
@ -204,3 +204,90 @@ xtc_result xtc_project_remove_note(xtc_project *p, uint64_t id)
|
|||
--p->note_count;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
||||
/* Selection is evaluated against original properties, never transformed ones. */
|
||||
static int matches(const xtc_note_info *n, const xtc_note_filter *f)
|
||||
{
|
||||
return n->clip_id == f->clip_id && (f->note_id == UINT64_MAX || n->id == f->note_id) &&
|
||||
(f->channel == -1 || n->channel == (unsigned int)f->channel) &&
|
||||
n->pitch >= f->pitch_min && n->pitch <= f->pitch_max;
|
||||
}
|
||||
|
||||
static xtc_result transformed(const xtc_project *p, xtc_note_info *n, const xtc_note_edit *e,
|
||||
unsigned int copy)
|
||||
{
|
||||
int pitch = (int)n->pitch + e->transpose;
|
||||
if (pitch < 0 || pitch > 127) return XTC_ERR_INVALID_ARGUMENT;
|
||||
n->pitch = (unsigned int)pitch;
|
||||
if (e->velocity != -1) n->velocity = (unsigned int)e->velocity;
|
||||
if (e->channel != -1) n->channel = (unsigned int)e->channel;
|
||||
if (e->grid_tick) {
|
||||
uint64_t rem = n->start_tick % e->grid_tick;
|
||||
n->start_tick -= rem;
|
||||
if (rem >= e->grid_tick - rem) {
|
||||
if (n->start_tick > UINT64_MAX - e->grid_tick) return XTC_ERR_INVALID_ARGUMENT;
|
||||
n->start_tick += e->grid_tick;
|
||||
}
|
||||
}
|
||||
if (copy) {
|
||||
if (e->interval_tick > (UINT64_MAX - n->start_tick) / copy) return XTC_ERR_INVALID_ARGUMENT;
|
||||
n->start_tick += e->interval_tick * copy;
|
||||
}
|
||||
return valid_note(p, n->clip_id, n->start_tick, n->length_tick, n->pitch, n->velocity, n->channel);
|
||||
}
|
||||
|
||||
xtc_result xtc_project_edit_notes(xtc_project *p, const xtc_note_filter *f,
|
||||
const xtc_note_edit *e, size_t *out)
|
||||
{
|
||||
size_t i, count = 0, original, write = 0;
|
||||
unsigned int k;
|
||||
xtc_clip_info clip;
|
||||
xtc_result r;
|
||||
if (!p || !f || !e || !out || f->channel < -1 || f->channel > 15 ||
|
||||
f->pitch_min > f->pitch_max || f->pitch_max > 127 ||
|
||||
e->action < XTC_NOTES_EDIT || e->action > XTC_NOTES_DELETE)
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
r = xtc_project_find_clip(p, f->clip_id, &clip);
|
||||
if (r != XTC_OK) return r;
|
||||
if (!clip.midi) return XTC_ERR_INVALID_STATE;
|
||||
if (e->action != XTC_NOTES_DELETE && (e->transpose < -127 || e->transpose > 127 ||
|
||||
e->velocity < -1 || e->velocity == 0 || e->velocity > 127 || e->channel < -1 || e->channel > 15 ||
|
||||
(e->action == XTC_NOTES_REPEAT && (!e->copies || e->copies > XTC_PROJECT_MAX_NOTES || !e->interval_tick))))
|
||||
return XTC_ERR_INVALID_ARGUMENT;
|
||||
original = p->note_count;
|
||||
for (i = 0; i < original; ++i) if (matches(p->notes+i, f)) ++count;
|
||||
if (!count) return XTC_ERR_NOT_FOUND;
|
||||
if (e->action == XTC_NOTES_REPEAT &&
|
||||
(count > (XTC_PROJECT_MAX_NOTES-original) / e->copies ||
|
||||
(uint64_t)count > (UINT64_MAX-p->next_note_id) / e->copies)) return XTC_ERR_CAPACITY;
|
||||
/* Validate every result before changing anything. Bounded by note capacity. */
|
||||
if (e->action != XTC_NOTES_DELETE)
|
||||
for (i = 0; i < original; ++i) if (matches(p->notes+i, f)) {
|
||||
unsigned int copies = e->action == XTC_NOTES_REPEAT ? e->copies : 0;
|
||||
for (k = copies ? 1U : 0U; k <= copies; ++k) {
|
||||
xtc_note_info n = p->notes[i];
|
||||
r = transformed(p, &n, e, k);
|
||||
if (r != XTC_OK) return r;
|
||||
}
|
||||
}
|
||||
for (i = 0; i < original; ++i) {
|
||||
xtc_note_info n = p->notes[i];
|
||||
if (!matches(&n, f)) {
|
||||
if (e->action == XTC_NOTES_DELETE) p->notes[write++] = n;
|
||||
continue;
|
||||
}
|
||||
if (e->action == XTC_NOTES_DELETE) continue;
|
||||
if (e->action == XTC_NOTES_EDIT) {
|
||||
(void)transformed(p, &n, e, 0);
|
||||
p->notes[i] = n;
|
||||
} else for (k = 1; k <= e->copies; ++k) {
|
||||
n = p->notes[i];
|
||||
(void)transformed(p, &n, e, k);
|
||||
n.id = p->next_note_id++;
|
||||
p->notes[p->note_count++] = n;
|
||||
}
|
||||
}
|
||||
if (e->action == XTC_NOTES_DELETE) p->note_count = write;
|
||||
*out = count;
|
||||
return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue