diff options
author | Florian Jung <flo@windfisch.org> | 2013-05-19 19:24:06 +0200 |
---|---|---|
committer | Florian Jung <flo@windfisch.org> | 2013-05-19 19:24:06 +0200 |
commit | f7e75057a6d08336d04111a87c3baa22a214461c (patch) | |
tree | cab14b5a39601684b8760004593b6e936b7ebad3 | |
parent | beec5cf34a653b7834398bc9729ee05589dfea87 (diff) |
config parsing, recording queue
-rw-r--r-- | midirip.c | 299 |
1 files changed, 295 insertions, 4 deletions
@@ -92,6 +92,8 @@ int channel = 9; #define SILENCE_ADD 0.005 //schon ziemlich knapp. lieber weniger #define QUIET_LEN 441 #define WAIT_AFTER 22050 +#define MAX_NOTES 10 +#define DEFAULT_LEN 10 //TODO richtigen wert finden! jack_default_audio_sample_t buf1[RECBUF_LEN], buf2[RECBUF_LEN]; jack_default_audio_sample_t *recbuf, *workbuf; @@ -105,15 +107,118 @@ int bytes=2; char wavheader[44]; int flo_start=0; -//typedef struct {} entry_t; struct entry_t { + char chan; + char patch; + + char loud; + char note[MAX_NOTES]; + int notelen; struct entry_t *next; }; - typedef struct entry_t entry_t; +entry_t *recentry, *workentry, *firstentry; + +const int notemap[7]={33,35,24,26,28,29,31}; + +int getnum(char *s, int *i) +{ + int minus=0; + int num=0; + + if (s[*i]=='-') + { + minus=1; + (*i)++; + } + while((s[*i]>='0') && (s[*i]<='9')) + { + num=num*10+s[*i]-'0'; + (*i)++; + } + + if (minus) return -num; else return num; +} + +int parserange(char *s, int l[]) +{ + int i, state=0; + int j=0; + int num, end; + + for (i=0;;i++) + if ((s[i]!=' ') && (s[i]!='\t')) + { + switch(state) + { + case 0: //expects the (first) number + if ((s[i]>='0') && (s[i]<='9')) //a number? + { + num=getnum(s,&i); + i--; + state=1; + } + break; + case 1: //expects a , or a - + if (s[i]=='-') + { + state=2; + } + else if ((s[i]==',') || (s[i]==0)) + { + l[j]=num; + j++; +// printf ("num=%i\n",num); + state=0; + } + else + { + printf("ERROR: expected ',' or '-', found '%c'\n",s[i]); + return -1; + } + break; + + case 2: //expects the second number that ends a range + if ((s[i]>='0') && (s[i]<='9')) //a number? + { + end=getnum(s,&i); +// printf("numrange=%i-%i\n",num,end); + for (;num<=end;num++) + { + l[j]=num; + j++; + } + + i--; + state=3; + } + else + { + printf("ERROR: expected number, found '%c'\n",s[i]); + return -1; + } + break; + + case 3: //expects ',' + if ((s[i]==',') || (s[i]==0)) + state=0; + else + { + printf("ERROR: expected ',', found '%c'\n",s[i]); + return -1; + } + } + if (s[i]==0) break; + } + + return j; + +} + + void u16cpy(char *dest, int src) { dest[0]=src%256; @@ -364,7 +469,187 @@ void init_jack(void) //WICHTIG !!! int main(int argc, char *argv[]) { - int i; + int i,l; + FILE* f; + char line[1000]; + char *range, *param; + int patchlist[128]; + + int oct,loud,len; + int notenum, note[10]; + int j,k; + int state; + int dontignore; + int rangelen; + + entry_t *curr_entry; + + curr_entry=firstentry=NULL; + + f=fopen ("config.txt","r"); + l=0; + while(!feof(f)) + { + fgets(line,sizeof(line),f); + l++; + + dontignore=0; + for (i=0;line[i];i++) + if ((line[i]!=' ') && (line[i]!='\t') && (line[i]!='\n')) + { + dontignore=1; + } + + if (dontignore) + { + for (i=0;(line[i]!=0) && (line[i]!=')');i++); //find ')' + if (line[i]==0) + { + printf("ERROR: expected ), found EOL in line %i\n",l); + goto invalid; + } + + line[i]=0; + range=line; + param=line+i+1; + + rangelen=parserange(range,patchlist); + if (rangelen==-1) + { + printf ("ERROR: invalid range specified in line %i\n",l); + goto invalid; + } + + while(1) + { + for (i=0;(param[i]!=0) && (param[i]!=',') && (param[i]!='\n');i++); //find ',' + + state=0; + len=DEFAULT_LEN; + notenum=0; + note[notenum]=0; oct=99; + + for (j=0;j<i;j++) + { + switch (state) + { + case 0: //expects a note (a4) or loudness (<num>) or \0 + if ((param[j]>='a') && (param[j]<='g')) //found a note + { +// printf("found a note\n"); + note[notenum]=notemap[param[j]-'a']; + if ((param[j+1]=='b') || param[j+1]=='#') //vorzeichen? + { + if (param[j+1]=='b') + note[notenum]--; + else + note[notenum]++; + j++; + } + state=1; + } + else if ((param[j]>='0') && (param[j]<='9')) //found a number + { +// printf("found loudness\n"); + loud=getnum(param,&j); + state=2; + } + else if ((param[j]!=' ') && (param[j]!='\t')) + { + printf("ERROR: expected note or number, found %c in line %i\n",param[j],l); + goto invalid; + } + break; + + case 1: //expects octave (-1 or 4) + if ((param[j]=='-') || ((param[j]>='0') && (param[j]<='9'))) + { +// printf("found octave\n"); + oct=getnum(param,&j); + + note[notenum]+=oct*12; + if ((note[notenum]<0) || (note[notenum]>127)) //illegal note + { + printf("ERROR: invalid note in line %i\n",l); + goto invalid; + } + else + notenum++; + state=0; + } + else + { + printf ("ERROR: expected - or 0-9, found %c in line %i\n",param[j],l); + goto invalid; + } + break; + + + case 2: //expects notelen or 0 + if ((param[j]>='0') && (param[j]<='9')) + { +// printf("found notelen\n"); + len=getnum(param,&j); + if (len<0) + { + printf("WARNING: len was negative in line %i; using absolute value\n",l); + len=-len; + } + state=4; + } + else if ((param[j]!=' ') && (param[j]!='\t')) + { + printf("ERROR: expected number, found %c in line %i\n",param[j],l); + goto invalid; + } + } + } + + //in die liste eintragen + for (k=0;k<rangelen;k++) + { + entry_t *tmpptr=malloc(sizeof(entry_t)); + if (tmpptr==NULL) + { + printf("GAAAH... malloc error\n"); + exit(1); + } + if (firstentry==NULL) + firstentry=tmpptr; + else + curr_entry->next=tmpptr; + + tmpptr->loud=loud; + tmpptr->notelen=len*samp_rate /1000; //von msec in samples umrechnen + tmpptr->chan=0; + tmpptr->patch=patchlist[k]; + + for (j=0;j<notenum;j++) + tmpptr->note[j]=note[j]; + tmpptr->note[notenum]=-1; + tmpptr->next=NULL; + + curr_entry=tmpptr; + } + + if (param[i]==',') //is there a next param? + param=¶m[i+1]; + else + break; + } + } + } + + curr_entry=firstentry; + while(curr_entry) + { + printf("%i\n",curr_entry->loud); + + curr_entry=curr_entry->next; + } + + + //init recbuf=buf1; workbuf=buf2; @@ -443,7 +728,7 @@ int main(int argc, char *argv[]) u32cpy(wavheader+ 4, filelen-8); u32cpy(wavheader+40, datalen); - FILE* f; + f=fopen("flo1.wav","w"); unsigned char tmp[2]; @@ -477,5 +762,11 @@ int main(int argc, char *argv[]) } return 0; + + + + invalid: + printf("config file invalid, exiting...\n"); + exit(1); } |