Phase 35: add persistent preferences and sequencer loop playback
This commit is contained in:
parent
4873b70672
commit
47b7d38a66
21 changed files with 852 additions and 108 deletions
173
src/settings/settings.c
Normal file
173
src/settings/settings.c
Normal file
|
|
@ -0,0 +1,173 @@
|
|||
#define _POSIX_C_SOURCE 200809L
|
||||
#include "xtc/xtc_settings.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
#include <inttypes.h>
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
#include <ctype.h>
|
||||
#define NUM(section,field,label,min,max) {section,#field,label,offsetof(xtc_settings,field),0,min,max}
|
||||
#define STR(section,field,label) {section,#field,label,offsetof(xtc_settings,field),sizeof(((xtc_settings *)0)->field),0,0}
|
||||
static const xtc_setting_info schema[] = {
|
||||
STR("audio",client_name,"JACK client name (Reconnect)"),
|
||||
NUM("audio",max_frames,"Maximum block capacity (Reconnect)",32,65536),
|
||||
NUM("audio",channels,"Output channels: 1 mono, 2 stereo (Reconnect)",1,2),
|
||||
NUM("audio",transition_ms,"Start/stop and source fades, ms (Reconnect)",0,1000),
|
||||
NUM("audio",offline_rate,"Offline preparation sample rate, Hz",8000,192000),
|
||||
NUM("audio",recording_seconds,"Recording ring capacity, seconds (next take)",1,30),
|
||||
STR("plugins",clap_paths,"CLAP search paths (colon-separated)"),
|
||||
STR("plugins",vst2_paths,"VST2 search paths (colon-separated)"),
|
||||
STR("plugins",vst3_paths,"VST3 search paths (colon-separated)"),
|
||||
STR("plugins",ladspa_paths,"LADSPA search paths (colon-separated)"),
|
||||
NUM("interface",width,"Main window width",1220,3840), NUM("interface",height,"Main window height",720,2160),
|
||||
NUM("interface",piano_width,"Piano window width",800,3840), NUM("interface",piano_height,"Piano window height",520,2160),
|
||||
NUM("interface",refresh_ms,"Display refresh interval, ms",20,1000),
|
||||
NUM("editing",sequencer_snap,"Sequencer snap ticks (1 means Off)",1,3840),
|
||||
NUM("editing",piano_snap,"Piano snap ticks (1 means Off)",1,3840),
|
||||
NUM("editing",sequencer_zoom,"Sequencer pixels per beat: 24/48/96/192/384",24,384),
|
||||
NUM("editing",clip_ticks,"New MIDI clip length, ticks",1,38400000),
|
||||
NUM("editing",note_ticks,"New note length, ticks",1,38400000),
|
||||
NUM("editing",note_velocity,"New note velocity",1,127), NUM("editing",note_channel,"New note channel",0,15),
|
||||
NUM("editing",filter_channel,"Piano channel filter (-1 means all)",-1,15),
|
||||
NUM("editing",filter_low,"Piano minimum pitch",0,127), NUM("editing",filter_high,"Piano maximum pitch",0,127),
|
||||
NUM("project_defaults",tempo,"New project tempo, milli-BPM",20000,300000),
|
||||
NUM("project_defaults",volume_cc,"New project volume CC (128 disables)",0,128),
|
||||
NUM("project_defaults",pan_cc,"New project pan CC (128 disables)",0,128),
|
||||
NUM("project_defaults",sustain_cc,"New project sustain CC (128 disables)",0,128),
|
||||
NUM("transport",loop_start,"Loop start, ticks",0,38399999), NUM("transport",loop_end,"Loop end, ticks",1,38400000),
|
||||
NUM("transport",loop_enabled,"Loop enabled: 0 or 1",0,1)
|
||||
};
|
||||
#define COUNT (sizeof(schema)/sizeof(schema[0]))
|
||||
const xtc_setting_info *xtc_settings_schema(size_t *count) { if (count) *count = COUNT; return schema; }
|
||||
void xtc_settings_defaults(xtc_settings *s)
|
||||
{
|
||||
if (!s) return;
|
||||
memset(s,0,sizeof(*s)); strcpy(s->client_name,"xtc");
|
||||
strcpy(s->clap_paths,"~/.clap:/usr/local/lib/clap:/usr/lib/clap");
|
||||
strcpy(s->vst2_paths,"~/.vst:/usr/local/lib/vst:/usr/lib/vst");
|
||||
strcpy(s->vst3_paths,"~/.vst3:/usr/local/lib/vst3:/usr/lib/vst3");
|
||||
strcpy(s->ladspa_paths,"~/.ladspa:/usr/local/lib/ladspa:/usr/lib/ladspa");
|
||||
s->max_frames=65536; s->channels=2; s->transition_ms=5; s->offline_rate=48000; s->recording_seconds=2;
|
||||
s->width=1280; s->height=800; s->piano_width=900; s->piano_height=620; s->refresh_ms=100;
|
||||
s->sequencer_snap=s->piano_snap=240; s->sequencer_zoom=48; s->clip_ticks=3840; s->note_ticks=960;
|
||||
s->note_velocity=100; s->filter_channel=-1; s->filter_high=127; s->tempo=120000;
|
||||
s->volume_cc=7; s->pan_cc=10; s->sustain_cc=64; s->loop_end=3840;
|
||||
}
|
||||
static int snap(int64_t v) { return v==1 || v==120 || v==160 || v==240 || v==320 || v==480 || v==960 || v==1920 || v==3840; }
|
||||
xtc_result xtc_settings_validate(const xtc_settings *s)
|
||||
{
|
||||
size_t i;
|
||||
if (!s) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (i=0;i<COUNT;++i) {
|
||||
const xtc_setting_info *p=schema+i; const char *field=(const char *)s+p->offset;
|
||||
if (p->capacity) { size_t j; if (!memchr(field,0,p->capacity)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for (j=0;field[j];++j) if ((unsigned char)field[j]<32 || (unsigned char)field[j]==127) return XTC_ERR_INVALID_ARGUMENT;
|
||||
} else { int64_t v; memcpy(&v,field,sizeof(v)); if (v<p->minimum || v>p->maximum) return XTC_ERR_INVALID_ARGUMENT; }
|
||||
}
|
||||
if (!s->client_name[0] || !snap(s->sequencer_snap) || !snap(s->piano_snap) ||
|
||||
(s->sequencer_zoom!=24 && s->sequencer_zoom!=48 && s->sequencer_zoom!=96 && s->sequencer_zoom!=192 && s->sequencer_zoom!=384) ||
|
||||
s->filter_low>s->filter_high || s->loop_end<=s->loop_start) return XTC_ERR_INVALID_ARGUMENT;
|
||||
{ int64_t cc[3]={s->volume_cc,s->pan_cc,s->sustain_cc};
|
||||
for(i=0;i<3;++i) { size_t j; if(cc[i]==120 || cc[i]==123) return XTC_ERR_INVALID_ARGUMENT;
|
||||
for(j=0;j<i;++j) if(cc[i]!=128 && cc[i]==cc[j]) return XTC_ERR_INVALID_ARGUMENT; } }
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_settings_get(const xtc_settings *s,size_t i,char *out,size_t size)
|
||||
{
|
||||
int n; if(!s || i>=COUNT || !out || !size) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if(schema[i].capacity && !memchr((const char *)s+schema[i].offset,0,schema[i].capacity)) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if(schema[i].capacity) n=snprintf(out,size,"%s",(const char *)s+schema[i].offset);
|
||||
else { int64_t v; memcpy(&v,(const char *)s+schema[i].offset,sizeof(v)); n=snprintf(out,size,"%" PRId64,v); }
|
||||
return n<0 || (size_t)n>=size ? XTC_ERR_CAPACITY : XTC_OK;
|
||||
}
|
||||
xtc_result xtc_settings_set(xtc_settings *s,size_t i,const char *value)
|
||||
{
|
||||
const xtc_setting_info *p; char *out;
|
||||
if(!s || i>=COUNT || !value) return XTC_ERR_INVALID_ARGUMENT;
|
||||
p=schema+i; out=(char *)s+p->offset;
|
||||
if(p->capacity) { size_t j,n=strlen(value); if(n>=p->capacity) return XTC_ERR_CAPACITY;
|
||||
for(j=0;j<n;++j) if((unsigned char)value[j]<32 || (unsigned char)value[j]==127) return XTC_ERR_INVALID_ARGUMENT;
|
||||
memcpy(out,value,n+1);
|
||||
} else { char *end; int64_t v; if(!*value) return XTC_ERR_INVALID_ARGUMENT;
|
||||
errno=0; v=strtoimax(value,&end,10); if(errno || *end || v<p->minimum || v>p->maximum) return XTC_ERR_INVALID_ARGUMENT;
|
||||
memcpy(out,&v,sizeof(v)); }
|
||||
return XTC_OK;
|
||||
}
|
||||
xtc_result xtc_settings_path(char *out,size_t size)
|
||||
{
|
||||
const char *home=getenv("HOME"); int n;
|
||||
if(!out || !size) return XTC_ERR_INVALID_ARGUMENT;
|
||||
if(!home || !*home) { struct passwd *pw=getpwuid(getuid()); home=pw ? pw->pw_dir : NULL; }
|
||||
if(!home || home[0]!='/') return XTC_ERR_NOT_FOUND;
|
||||
n=snprintf(out,size,"%s/.xtc.conf",home);
|
||||
return n<0 || (size_t)n>=size ? XTC_ERR_CAPACITY : XTC_OK;
|
||||
}
|
||||
static char *trim(char *p) { char *end; while(*p && isspace((unsigned char)*p)) ++p; end=p+strlen(p); while(end>p && isspace((unsigned char)end[-1])) --end; *end=0; return p; }
|
||||
xtc_result xtc_settings_load(xtc_settings *s,const char *path)
|
||||
{
|
||||
FILE *f; xtc_settings candidate; char line[8192],section[64]="",data[65537]; unsigned char seen[COUNT]; size_t bytes=0,offset=0; xtc_result r=XTC_OK;
|
||||
if(!s || !path || !*path) return XTC_ERR_INVALID_ARGUMENT;
|
||||
f=fopen(path,"rb"); if(!f) return errno==ENOENT ? XTC_ERR_NOT_FOUND : XTC_ERR_IO;
|
||||
xtc_settings_defaults(&candidate); memset(seen,0,sizeof(seen));
|
||||
bytes=fread(data,1,sizeof(data),f);
|
||||
if(ferror(f)) r=XTC_ERR_IO;
|
||||
if(fclose(f)!=0) r=XTC_ERR_IO;
|
||||
if(r!=XTC_OK)return r;
|
||||
if(bytes==sizeof(data) || memchr(data,0,bytes))return XTC_ERR_FORMAT;
|
||||
while (offset<bytes) {
|
||||
char *p,*eq; size_t i,n=0;
|
||||
while(offset<bytes && data[offset]!='\n') {
|
||||
if(n+1>=sizeof(line)){r=XTC_ERR_FORMAT;break;}
|
||||
line[n++]=data[offset++];
|
||||
}
|
||||
if(r!=XTC_OK)break;
|
||||
if(offset<bytes)++offset;
|
||||
line[n]=0;
|
||||
p=trim(line); if(!*p || *p=='#' || *p==';') continue;
|
||||
if(*p=='[') { char *end=strchr(p,']'); if(!end || end[1] || end==p+1 || (size_t)(end-p-1)>=sizeof(section)) {r=XTC_ERR_FORMAT;break;}
|
||||
*end=0;
|
||||
for(i=0;i<COUNT;++i) if(!strcmp(p+1,schema[i].section))break;
|
||||
if(i==COUNT){r=XTC_ERR_FORMAT;break;}
|
||||
strcpy(section,p+1); continue; }
|
||||
eq=strchr(p,'='); if(!eq) {r=XTC_ERR_FORMAT;break;} *eq=0; p=trim(p); eq=trim(eq+1);
|
||||
for(i=0;i<COUNT;++i) if(!strcmp(section,schema[i].section) && !strcmp(p,schema[i].key)) break;
|
||||
if(i==COUNT || seen[i]) {r=XTC_ERR_FORMAT;break;} seen[i]=1;
|
||||
if(*eq=='"') { char *src=eq+1,*dst=eq; int closed=0;
|
||||
while(*src) { if(*src=='"') { ++src; closed=1; break; }
|
||||
if(*src=='\\') { ++src; if(*src!='\\' && *src!='"') break; }
|
||||
*dst++=*src++; }
|
||||
if(!closed || *trim(src)) {r=XTC_ERR_FORMAT;break;} *dst=0;
|
||||
}
|
||||
if(xtc_settings_set(&candidate,i,eq)!=XTC_OK) {r=XTC_ERR_FORMAT;break;}
|
||||
}
|
||||
if(r==XTC_OK && xtc_settings_validate(&candidate)!=XTC_OK) r=XTC_ERR_FORMAT;
|
||||
if(r==XTC_OK) *s=candidate;
|
||||
return r;
|
||||
}
|
||||
xtc_result xtc_settings_save(const xtc_settings *s,const char *path)
|
||||
{
|
||||
char temporary[4096]; FILE *f; int fd,n; size_t i; const char *section=""; xtc_result r;
|
||||
if(!path || !*path || xtc_settings_validate(s)!=XTC_OK) return XTC_ERR_INVALID_ARGUMENT;
|
||||
n=snprintf(temporary,sizeof(temporary),"%s.tmp-XXXXXX",path); if(n<0 || (size_t)n>=sizeof(temporary)) return XTC_ERR_CAPACITY;
|
||||
fd=mkstemp(temporary); if(fd<0) return XTC_ERR_IO;
|
||||
f=fdopen(fd,"wb"); if(!f) {close(fd);unlink(temporary);return XTC_ERR_IO;}
|
||||
r=XTC_OK; if(fputs("; xtc application preferences. Saved by Preferences / Apply.\n",f)<0) r=XTC_ERR_IO;
|
||||
for(i=0;i<COUNT && r==XTC_OK;++i) {
|
||||
char value[4096]; const char *p;
|
||||
if(strcmp(section,schema[i].section)) { section=schema[i].section; if(fprintf(f,"\n[%s]\n",section)<0) {r=XTC_ERR_IO;break;} }
|
||||
if(xtc_settings_get(s,i,value,sizeof(value))!=XTC_OK || fprintf(f,"%s = ",schema[i].key)<0) {r=XTC_ERR_IO;break;}
|
||||
if(schema[i].capacity) {
|
||||
if(fputc('"',f)==EOF) r=XTC_ERR_IO;
|
||||
for(p=value;*p;++p) { if((*p=='"' || *p=='\\') && fputc('\\',f)==EOF) r=XTC_ERR_IO; if(fputc(*p,f)==EOF) r=XTC_ERR_IO; }
|
||||
if(fputc('"',f)==EOF) r=XTC_ERR_IO;
|
||||
} else if(fputs(value,f)<0) r=XTC_ERR_IO;
|
||||
if(fputc('\n',f)==EOF) r=XTC_ERR_IO;
|
||||
}
|
||||
if(fflush(f)!=0 || fsync(fd)!=0) r=XTC_ERR_IO;
|
||||
if(fclose(f)!=0) r=XTC_ERR_IO;
|
||||
if(r==XTC_OK && rename(temporary,path)!=0) r=XTC_ERR_IO;
|
||||
if(r!=XTC_OK) unlink(temporary);
|
||||
return r;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue