feat: reverse selected sample frames with immutable snapshots and local undo
This commit is contained in:
parent
342a93385c
commit
9ae2e2a11c
8 changed files with 49 additions and 5 deletions
|
|
@ -49,3 +49,20 @@ xtc_result xtc_sample_write(const xtc_sample *s,size_t begin,size_t end,const ch
|
|||
if(r!=XTC_OK)xtc_recorder_abort(rec);
|
||||
xtc_recorder_destroy(rec);return r;
|
||||
}
|
||||
|
||||
xtc_result xtc_sample_process(const xtc_sample *s,size_t begin,size_t end,
|
||||
xtc_sample_operation operation,double amount,xtc_sample **out)
|
||||
{
|
||||
xtc_sample *copy;size_t i;xtc_result r=XTC_OK;
|
||||
if(!range(s,begin,end)||!out||operation!=XTC_SAMPLE_REVERSE||!isfinite(amount))return XTC_ERR_INVALID_ARGUMENT;
|
||||
copy=calloc(1,sizeof(*copy));if(!copy)return XTC_ERR_OUT_OF_MEMORY;
|
||||
copy->wave=s->wave;copy->wave.samples=malloc(s->wave.frames*sizeof(float));
|
||||
if(!copy->wave.samples){free(copy);return XTC_ERR_OUT_OF_MEMORY;}
|
||||
for(i=0;i<s->wave.frames;++i)copy->wave.samples[i]=s->wave.samples[i];
|
||||
for(i=begin;i<end;++i){double value;
|
||||
switch(operation){case XTC_SAMPLE_REVERSE:value=s->wave.samples[end-1-(i-begin)];break;default:value=0;r=XTC_ERR_INVALID_ARGUMENT;break;}
|
||||
if(!isfinite(value)||fabs(value)>3.4028234663852886e38){r=XTC_ERR_CAPACITY;break;}
|
||||
copy->wave.samples[i]=(float)value;
|
||||
}
|
||||
if(r!=XTC_OK){xtc_sample_destroy(copy);return r;}*out=copy;return XTC_OK;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -41,6 +41,8 @@ SampleEditor::SampleEditor(xtc_sample *sample):Fl_Double_Window(1100,650,"xtc
|
|||
auto *trim_button=new Fl_Button(16,520,180,30,"Trim clip to selection");trim_button->callback([](Fl_Widget *,void *c){auto r=static_cast<SampleEditor *>(c)->trim_clip();if(r!=XTC_OK)fl_alert("Clip trim unchanged: %s",xtc_result_string(r));},this);
|
||||
auto *save=new Fl_Button(210,520,210,30,"Save full copy and relink...");save->callback([](Fl_Widget *,void *c){auto *e=static_cast<SampleEditor *>(c);const char *path=fl_input("NEW absolute WAV path (full source; project undo keeps the new file)","");if(path){auto r=e->save_copy(path,true);if(r!=XTC_OK)fl_alert("Copy not adopted: %s",xtc_result_string(r));}},this);
|
||||
auto *export_button=new Fl_Button(434,520,200,30,"Export selected frames...");export_button->callback([](Fl_Widget *,void *c){auto *e=static_cast<SampleEditor *>(c);const char *path=fl_input("NEW absolute WAV path (selection only)","");if(path){auto r=e->export_selection(path);if(r!=XTC_OK)fl_alert("Export failed: %s",xtc_result_string(r));}},this);
|
||||
auto *reverse=new Fl_Button(16,562,160,30,"Reverse selection");reverse->callback([](Fl_Widget *,void *c){auto r=static_cast<SampleEditor *>(c)->process(XTC_SAMPLE_REVERSE);if(r!=XTC_OK)fl_alert("Reversal failed: %s",xtc_result_string(r));},this);
|
||||
auto *undo=new Fl_Button(850,604,110,30,"Undo / redo");undo->callback([](Fl_Widget *,void *c){auto r=static_cast<SampleEditor *>(c)->undo_sample();if(r!=XTC_OK)fl_alert("No local sample edit to undo.");},this);
|
||||
auto *close=new Fl_Button(970,604,110,30,"Close");close->callback([](Fl_Widget *,void *c){static_cast<SampleEditor *>(c)->hide();},this);callback([](Fl_Widget *,void *c){static_cast<SampleEditor *>(c)->hide();},this);
|
||||
Fl_Group::end();xtc_ui::style(this);refresh();
|
||||
}
|
||||
|
|
@ -53,3 +55,10 @@ xtc_result SampleEditor::trim_clip(){return trim ? trim(rate_,begin_,end_) : XTC
|
|||
|
||||
xtc_result SampleEditor::save_copy(const char *path,bool use_in_project){if(use_in_project)return adopt ? adopt(sample_.get(),path) : XTC_ERR_INVALID_STATE;return xtc_sample_write(sample_.get(),0,frames_,path);}
|
||||
xtc_result SampleEditor::export_selection(const char *path){return xtc_sample_write(sample_.get(),begin_,end_,path);}
|
||||
|
||||
xtc_result SampleEditor::process(xtc_sample_operation operation,double amount)
|
||||
{
|
||||
xtc_sample *next=nullptr;auto r=xtc_sample_process(sample_.get(),begin_,end_,operation,amount,&next);if(r!=XTC_OK)return r;
|
||||
previous_=std::move(sample_);sample_.reset(next);copy_label("xtc — Sample editor / local sample changes; save a copy to keep");refresh();return XTC_OK;
|
||||
}
|
||||
xtc_result SampleEditor::undo_sample(){if(!previous_)return XTC_ERR_NOT_FOUND;sample_.swap(previous_);refresh();return XTC_OK;}
|
||||
|
|
|
|||
|
|
@ -16,6 +16,8 @@ public:
|
|||
std::function<xtc_result(const xtc_sample *,const char *)> adopt;
|
||||
xtc_result save_copy(const char *path,bool use_in_project);
|
||||
xtc_result export_selection(const char *path);
|
||||
xtc_result process(xtc_sample_operation operation,double amount=0);
|
||||
xtc_result undo_sample();
|
||||
void run();
|
||||
xtc_result select(size_t begin,size_t end);
|
||||
xtc_result view(size_t begin,unsigned int zoom);
|
||||
|
|
@ -26,6 +28,7 @@ public:
|
|||
void refresh();
|
||||
private:
|
||||
std::unique_ptr<xtc_sample,decltype(&xtc_sample_destroy)> sample_{nullptr,xtc_sample_destroy};
|
||||
std::unique_ptr<xtc_sample,decltype(&xtc_sample_destroy)> previous_{nullptr,xtc_sample_destroy};
|
||||
size_t frames_=0,begin_=0,end_=0,origin_=0;unsigned int rate_=0,zoom_=1;
|
||||
Fl_Value_Input *begin_input_,*end_input_,*origin_input_;Fl_Box *stats_;SampleCanvas *canvas_;
|
||||
};
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue