feat: generate bounded ascending and descending MIDI arpeggio patterns

This commit is contained in:
Johannes Findeisen 2026-11-06 00:42:33 +01:00
commit ea0b4ca3da
9 changed files with 89 additions and 5 deletions

View file

@ -298,3 +298,39 @@ xtc_result xtc_project_chordify(xtc_project *p,const uint64_t *ids,size_t count,
if(r==XTC_OK)publish(p,copy);
xtc_project_destroy(copy);return r;
}
static int chord_order(const void *a,const void *b)
{
const xtc_note_info *x=*(const xtc_note_info *const *)a,*y=*(const xtc_note_info *const *)b;
if(x->pitch!=y->pitch)return x->pitch<y->pitch ? -1 : 1;
return x->id==y->id ? 0 : x->id<y->id ? -1 : 1;
}
xtc_result xtc_project_arpeggiate(xtc_project *p,const uint64_t *ids,size_t count,uint64_t step,unsigned int gate,int descending)
{
xtc_project *copy=NULL;size_t i,j,k,matched=0;xtc_result r=midi_selection(p,ids,count);
if(r!=XTC_OK)return r;
if(!step || step>3840 || !gate || gate>100 || (descending!=0 && descending!=1))return XTC_ERR_INVALID_ARGUMENT;
r=xtc_project_clone(p,&copy);if(r!=XTC_OK)return r;
for(i=copy->note_count;i>0;--i){for(j=0;j<count;++j)if(copy->notes[i-1].clip_id==ids[j])break;if(j<count)r=xtc_project_remove_note(copy,copy->notes[i-1].id);}
for(i=0;r==XTC_OK && i<p->note_count;++i) {
const xtc_note_info *first=p->notes+i,*chord[XTC_PROJECT_MAX_NOTES];size_t voices=0,index=0;uint64_t end=0,at;
for(j=0;j<count;++j)if(first->clip_id==ids[j])break;
if(j==count)continue;
for(j=0;j<i;++j)if(p->notes[j].clip_id==first->clip_id && p->notes[j].start_tick==first->start_tick && p->notes[j].channel==first->channel)break;
if(j<i)continue;
++matched;
for(j=i;j<p->note_count;++j){const xtc_note_info *n=p->notes+j;
if(n->clip_id==first->clip_id && n->start_tick==first->start_tick && n->channel==first->channel){chord[voices++]=n;if(n->start_tick+n->length_tick>end)end=n->start_tick+n->length_tick;}
}
if(!voices){r=XTC_ERR_INVALID_STATE;break;}
qsort(chord,voices,sizeof(*chord),chord_order);
for(at=first->start_tick;r==XTC_OK && at<end;at+=step,++index) {
uint64_t length=(step*gate+50)/100,note;if(!length)length=1;if(length>end-at)length=end-at;
k=index%voices;if(descending)k=voices-1-k;
r=xtc_project_add_note(copy,first->clip_id,at,length,chord[k]->pitch,chord[k]->velocity,chord[k]->channel,&note);
}
}
if(r==XTC_OK && !matched)r=XTC_ERR_NOT_FOUND;
if(r==XTC_OK)publish(p,copy);
xtc_project_destroy(copy);return r;
}