feat: generate bounded ascending and descending MIDI arpeggio patterns
This commit is contained in:
parent
6be4f9803a
commit
ea0b4ca3da
9 changed files with 89 additions and 5 deletions
|
|
@ -331,6 +331,7 @@ MainWindow::MainWindow(bool prompt_unsaved,const char *configuration)
|
|||
menu->add("Edit/Nudge selection right",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->nudge_selection(1);},sequencer_);
|
||||
menu->add("Edit/Audio tools/Set boundary fades...",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->fades_dialog();},sequencer_);
|
||||
menu->add("Edit/Audio tools/Remove boundary fades",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->transform_selection(XTC_CLIPS_FADE,0);},sequencer_);
|
||||
menu->add("Edit/MIDI tools/Generate arpeggio...",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->arpeggio_dialog();},sequencer_);
|
||||
menu->add("Edit/MIDI tools/Build chords...",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->chord_dialog();},sequencer_);
|
||||
menu->add("Edit/MIDI tools/Humanize...",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->humanize_dialog();},sequencer_);
|
||||
menu->add("Edit/MIDI tools/Shape velocities...",0,[](Fl_Widget *,void *c){static_cast<SequencerWidget *>(c)->velocity_dialog();},sequencer_);
|
||||
|
|
|
|||
|
|
@ -624,6 +624,19 @@ xtc_result SequencerWidget::make_chord(const std::vector<int> &intervals)
|
|||
if(r==XTC_OK)r=xtc_project_chordify(project_,ids.data(),ids.size(),intervals.data(),intervals.size());
|
||||
changed(r);return r;
|
||||
}
|
||||
xtc_result SequencerWidget::arpeggiate(uint64_t step,unsigned int gate,bool descending)
|
||||
{
|
||||
auto ids=selected_clips();xtc_result r=before_edit ? before_edit(context_) : XTC_OK;
|
||||
if(r==XTC_OK)r=xtc_project_arpeggiate(project_,ids.data(),ids.size(),step,gate,descending);
|
||||
changed(r);return r;
|
||||
}
|
||||
void SequencerWidget::arpeggio_dialog()
|
||||
{
|
||||
const char *input=fl_input("Step ticks (1..3840), gate percent (1..100), direction (0=up, 1=down)","240 80 0");if(!input)return;
|
||||
uint64_t step=0;unsigned int gate=0;int down=0;char extra;std::istringstream values(input);
|
||||
if(!(values>>step>>gate>>down) || (values>>extra) || !step || step>3840 || !gate || gate>100 || down<0 || down>1){fl_alert("Enter a valid step, gate and direction.");return;}
|
||||
arpeggiate(step,gate,down!=0);
|
||||
}
|
||||
void SequencerWidget::chord_dialog()
|
||||
{
|
||||
const char *input=fl_input("Additional semitone intervals: 1..8 unique nonzero values (-127..127)","4 7");if(!input)return;
|
||||
|
|
|
|||
|
|
@ -53,6 +53,8 @@ class SequencerWidget : public Fl_Group {
|
|||
void humanize_dialog();
|
||||
xtc_result make_chord(const std::vector<int> &intervals);
|
||||
void chord_dialog();
|
||||
xtc_result arpeggiate(uint64_t step,unsigned int gate,bool descending);
|
||||
void arpeggio_dialog();
|
||||
void fades_dialog();
|
||||
xtc_result repeat_selection(unsigned int copies);
|
||||
void repeat_dialog();
|
||||
|
|
|
|||
|
|
@ -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,©);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,¬e);
|
||||
}
|
||||
}
|
||||
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