feat: quantize MIDI clips with deterministic swing and strength
This commit is contained in:
parent
b0dbf6905f
commit
2ba14f6728
9 changed files with 97 additions and 5 deletions
|
|
@ -115,3 +115,36 @@ xtc_result xtc_project_repeat_clips(xtc_project *p,const uint64_t *ids,size_t co
|
|||
if(r==XTC_OK)publish(p,copy);
|
||||
xtc_project_destroy(copy);return r;
|
||||
}
|
||||
|
||||
static uint64_t swung_start(uint64_t start,const xtc_midi_transform *edit)
|
||||
{
|
||||
uint64_t base=start/edit->grid,best=0,distance=UINT64_MAX;int offset;
|
||||
for(offset=-2;offset<=2;++offset) {
|
||||
int64_t index=(int64_t)base+offset;uint64_t candidate,d;
|
||||
if(index<0)continue;
|
||||
candidate=(uint64_t)index*edit->grid;
|
||||
if(index%2)candidate+=edit->grid*edit->swing/200;
|
||||
d=start>candidate ? start-candidate : candidate-start;
|
||||
if(d<distance || (d==distance && candidate>best)){distance=d;best=candidate;}
|
||||
}
|
||||
return (start*(100-edit->strength)+best*edit->strength+50)/100;
|
||||
}
|
||||
xtc_result xtc_project_transform_midi(xtc_project *p,const uint64_t *ids,size_t count,const xtc_midi_transform *edit)
|
||||
{
|
||||
xtc_project *copy=NULL;size_t i,j,matched=0;xtc_result r=selection_valid(p,ids,count);
|
||||
if(r!=XTC_OK)return r;
|
||||
if(!edit || edit->type!=XTC_MIDI_TRANSFORM_SWING || !edit->grid || edit->grid>3840 || edit->swing>100 || edit->strength>100)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,©);if(r!=XTC_OK)return r;
|
||||
for(i=0;r==XTC_OK && i<copy->note_count;++i) {
|
||||
xtc_note_info *n=copy->notes+i;xtc_clip_info c;
|
||||
for(j=0;j<count;++j)if(n->clip_id==ids[j])break;
|
||||
if(j==count)continue;
|
||||
++matched;xtc_project_find_clip(copy,n->clip_id,&c);
|
||||
n->start_tick=swung_start(n->start_tick,edit);
|
||||
if(n->start_tick>=c.source_length_tick || n->length_tick>c.source_length_tick-n->start_tick)r=XTC_ERR_INVALID_ARGUMENT;
|
||||
}
|
||||
if(r==XTC_OK && !matched)r=XTC_ERR_NOT_FOUND;
|
||||
if(r==XTC_OK)publish(p,copy);
|
||||
xtc_project_destroy(copy);return r;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue