feat(midi): strum chords with preserved note ends and undo (phase 99)

This commit is contained in:
Johannes Findeisen 2026-11-06 02:16:46 +01:00
commit bd0a63c06d
17 changed files with 172 additions and 5 deletions

View file

@ -334,3 +334,32 @@ xtc_result xtc_project_arpeggiate(xtc_project *p,const uint64_t *ids,size_t coun
if(r==XTC_OK)publish(p,copy);
xtc_project_destroy(copy);return r;
}
/* Rank against the original project so edits cannot alter later chord groups. */
xtc_result xtc_project_strum(xtc_project *p,const uint64_t *ids,size_t count,
uint64_t spacing,int descending)
{
xtc_project *copy=NULL;size_t i,j,k,changed=0;
xtc_result r=selection_valid(p,ids,count);
if(r!=XTC_OK)return r;
if(!spacing || spacing>960 || (descending!=0 && descending!=1))return XTC_ERR_INVALID_ARGUMENT;
for(i=0;i<count;++i){xtc_clip_info c;xtc_project_find_clip(p,ids[i],&c);if(!c.midi)return XTC_ERR_INVALID_STATE;}
r=xtc_project_clone(p,&copy);if(r!=XTC_OK)return r;
for(i=0;i<p->note_count;++i) {
const xtc_note_info *n=p->notes+i;size_t rank=0;uint64_t offset;
for(k=0;k<count;++k)if(n->clip_id==ids[k])break;
if(k==count)continue;
for(j=0;j<p->note_count;++j) {
const xtc_note_info *other=p->notes+j;
if(other->clip_id!=n->clip_id || other->start_tick!=n->start_tick || other->channel!=n->channel)continue;
if((descending ? other->pitch>n->pitch : other->pitch<n->pitch) ||
(other->pitch==n->pitch && other->id<n->id))++rank;
}
if(!rank)continue;
offset=(uint64_t)rank*spacing;
if(offset>=n->length_tick){xtc_project_destroy(copy);return XTC_ERR_INVALID_ARGUMENT;}
copy->notes[i].start_tick+=offset;copy->notes[i].length_tick-=offset;++changed;
}
if(changed)publish(p,copy);
xtc_project_destroy(copy);return changed ? XTC_OK : XTC_ERR_NOT_FOUND;
}