#define _POSIX_C_SOURCE 200809L #include "xtc/xtc_sample.h" #include #include #include #include #include int main(void) { char root[]="/tmp/xtc-sample-test-XXXXXX",path[256];float values[]={0,.25f,-.5f,.75f,0,0,.5f,-.25f}; xtc_recorder *rec=NULL;xtc_sample *s=NULL,*sentinel=NULL;xtc_sample_stats stats;size_t frames;unsigned int rate;float v=123; assert(mkdtemp(root));snprintf(path,sizeof(path),"%s/source.wav",root); assert(xtc_recorder_create(path,8000,8,&rec)==XTC_OK);xtc_recorder_push(rec,values,8);assert(xtc_recorder_finish(rec)==XTC_OK);xtc_recorder_destroy(rec); assert(xtc_sample_load(path,&s)==XTC_OK && xtc_sample_info(s,&rate,&frames)==XTC_OK && rate==8000 && frames==8); assert(xtc_sample_analyze(s,0,8,&stats)==XTC_OK && stats.minimum==-.5f && stats.maximum==.75f && stats.mean==.09375); assert(fabs(stats.rms-sqrt(1.1875/8))<.000001); assert(xtc_sample_read(s,8,&v)==XTC_ERR_INVALID_ARGUMENT && v==123); assert(xtc_sample_read(s,2,&v)==XTC_OK && v==-.5f); assert(xtc_sample_analyze(s,2,2,&stats)==XTC_ERR_INVALID_ARGUMENT); assert(xtc_sample_load("/missing/xtc.wav",&sentinel)==XTC_ERR_IO && sentinel==NULL); {xtc_project *p=NULL;uint64_t t,c;xtc_clip_info clip;assert(xtc_project_create(&p)==XTC_OK && xtc_project_add_track(p,"Audio",&t)==XTC_OK); assert(xtc_project_add_clip(p,t,"Audio",10,960,&c)==XTC_OK && xtc_project_set_clip_asset(p,c,path)==XTC_OK); assert(xtc_project_trim_source_frames(p,c,48000,1000,2000)==XTC_OK); assert(xtc_project_find_clip(p,c,&clip)==XTC_OK && clip.start_tick==50 && clip.offset_tick==40 && clip.length_tick==40); assert(xtc_project_trim_source_frames(p,c,48000,1001,1002)==XTC_ERR_INVALID_ARGUMENT); assert(xtc_project_find_clip(p,c,&clip)==XTC_OK && clip.offset_tick==40 && clip.length_tick==40); xtc_project_destroy(p);} {char copy[256];xtc_sample *q=NULL;snprintf(copy,sizeof(copy),"%s/copy.wav",root); assert(xtc_sample_write(s,1,7,copy)==XTC_OK && xtc_sample_write(s,1,7,copy)==XTC_ERR_IO); assert(xtc_sample_load(copy,&q)==XTC_OK && xtc_sample_info(q,&rate,&frames)==XTC_OK && frames==6 && rate==8000); for(size_t j=0;j<6;++j){assert(xtc_sample_read(q,j,&v)==XTC_OK && v==values[j+1]);} xtc_sample_destroy(q);assert(unlink(copy)==0);assert(xtc_sample_write(s,0,8,"relative.wav")==XTC_ERR_INVALID_ARGUMENT);} {xtc_sample *q=NULL,*twice=NULL;assert(xtc_sample_process(s,1,7,XTC_SAMPLE_REVERSE,0,&q)==XTC_OK); for(size_t j=0;j<8;++j){assert(xtc_sample_read(q,j,&v)==XTC_OK && v==(j>=1&&j<7 ? values[7-j] : values[j]));} assert(xtc_sample_process(q,1,7,XTC_SAMPLE_REVERSE,0,&twice)==XTC_OK); for(size_t j=0;j<8;++j){assert(xtc_sample_read(twice,j,&v)==XTC_OK && v==values[j]);} xtc_sample_destroy(q);xtc_sample_destroy(twice);} {xtc_sample *q=NULL,*twice=NULL;assert(xtc_sample_process(s,1,7,XTC_SAMPLE_INVERT,0,&q)==XTC_OK); for(size_t j=0;j<8;++j){assert(xtc_sample_read(q,j,&v)==XTC_OK && v==(j>=1&&j<7 ? -values[j] : values[j]));} assert(xtc_sample_process(q,1,7,XTC_SAMPLE_INVERT,0,&twice)==XTC_OK); for(size_t j=0;j<8;++j){assert(xtc_sample_read(twice,j,&v)==XTC_OK && v==values[j]);} xtc_sample_destroy(q);xtc_sample_destroy(twice);} xtc_sample_destroy(s);assert(unlink(path)==0 && rmdir(root)==0);puts("Sample editor core tests passed.");return 0; }