#include #include #include #include #include #include #include /* #define PI 3.141565 */ int fd; FILE *fp; int steps; int cur_sampleid=0; #define MAXLENGHT 10*44100 /* 10 sec. samples enough ? */ #define MAXNAMEL 32 #define MAXSAMPLEID 128 #define MSEC(t) (int)(t*44.1) short os[MAXLENGHT]; struct sample { char name[MAXNAMEL]; int id; int samplerate; int channels; int length; short *data; } ASAMPLE; struct sample * samples[MAXSAMPLEID]; int consume_until(fd,str,len) int fd, len; char *str; { int i; char b[4]; read(fd,b,len); printf("[c,%s,%d]:%c%c%c%c,%d\n",str,len,b[0],b[1],b[2],b[3], strncmp(b,str,len)); while (strncmp(b,str,len) != 0) { for (i=3; i>0; i--) b[i-1]=b[i]; /* printf("[c ]:%d %d %d %d\n",(int) b[0],(int) b[1], (int) b[2], (int) b[3] ); */ if (read(fd,b+3,1) != 1) { printf("Error: file format error\n"); return(-1); } } return(0); /* match */ } struct sample * readwav(s) /* Proto wav header, n=nnodes, filename */ char *s; { char bb[4]; short is, channels; int ii, length, samplerate; struct sample *p; fd = open(s,O_RDONLY|O_BINARY); if (fd < 0) return(NULL); /* read(fd,bb,4); */ consume_until(fd,"RIFF",4); read(fd,&ii,4); consume_until(fd,"WAVE",4); consume_until(fd,"fmt ",4); read(fd,bb,4); /* 0x10 */ read(fd,bb,2); /* 0x01 */ read(fd,&channels,2); /* (short) 1*/ /* Mono */ if (channels != 1) { printf("Error: number of channels must be one.\n"); } read(fd,&samplerate,4); if (samplerate != 44100) { printf("Error: Sample rate should be 44100 Hz\n"); } read(fd,&ii,4); read(fd,&is,2); read(fd,&is,2); consume_until(fd,"data",4); read(fd,&length,4); /* 2* steps (length of data) */ if ((p= (struct sample *) malloc(sizeof(ASAMPLE) + length*sizeof(short))) == NULL ) { printf("Error: memory allocation failed.\n"); exit(-3); } strncpy(p->name, s, MAXNAMEL); p->id = cur_sampleid; p->samplerate = samplerate; p->channels = (int) channels; p->data = (short *) (p+1); p->length = length/2; if (read(fd,p->data,length) != length) { printf("Error: not all data could be read of %s\n",s); } samples[p->id] = p; cur_sampleid++; return(p); } int print_sample(s1) struct sample *s1; { if (s1 != NULL) { printf("name=%s",s1->name); printf(" length: %d\n", s1->length); printf(" id=%d",s1->id); printf(" samplerate=%d",s1->samplerate); printf(" channels=%d",s1->channels); printf(" length=%d",s1->length); printf(" {%d %d %d %d}\n",s1->data[0],s1->data[1],s1->data[2],s1->data[3]); } return(0); } iwrite(fd,n,l) int fd,l; unsigned int n; { write(fd,&n,l); } int initwav(s) /* Proto wav header, n=nnodes, filename */ char *s; { fd = open(s,O_WRONLY|O_CREAT|O_TRUNC|O_BINARY); if (fd == NULL) return(-1); write(fd,"RIFF",4); iwrite(fd,(2*steps+36),4); write(fd,"WAVE",4); write(fd,"fmt ",4); iwrite(fd,(0x10),4); iwrite(fd,((short) 0x01),2); iwrite(fd,((short) 1),2); /* Mono */ iwrite(fd,(44100/1),4); /* Sample rate */ iwrite(fd,(2*44100/1),4); iwrite(fd,((short) 2),2); iwrite(fd,((short) 16),2); write(fd,"data",4); iwrite(fd,(2*steps),4); return(0); } writewav(fd) { int i; for (i=0; iname,n) == 0) { return(sp); printf("Found loaded sample %d %s\n",i,n); } } return(NULL); } #define FRAC 0x10000 #define SINCR(i,fi,di) fi+=di; i+= fi/FRAC; fi=fi%FRAC; int sample_copy(to, from, len, ampl, tune) short to[], from[]; int len; float ampl, tune; { int i, j, fi, di; /* printf("%ld %ld %d %d %d\n",(int)to,(int)from,(int)len, (int) to[0], (int) from[0]); */ j = 0; fi = 0; di = (int) ((float) FRAC *tune); for (i=0; jlength; } else { if (length > sp->length) alength = sp->length; else alength = length; } printf("alength = %d\n",alength); sample_copy( os+start, sp->data, alength, ampl, tune ); return(0); } gen_bd(start,length,ampl,tune) int start, length; float ampl, tune; { int i; short t; /* printf("start= %d length = %d\n",start, length); */ for (i=0; i=MAXCOM) {write_com("error : data missing"); exit(-1); } if (sscanf(b+i+1,"%f %f %f %f",&t,&l,&a,&d) == 4) { b[i] = '\0'; add_sample(b,t,l,a,d); } } } } write_com(s) char s[]; { printf("%s\n",s); } create_sample() /* 120 BPM. 16th Quantized basic track */ { /*************** NO TUNE PARA YET ***************/ int t, n16; steps = MSEC(2000.0); clear_os(); gen_bd(MSEC( 0),MSEC(200),0.9); gen_bd(MSEC( 750),MSEC(200),0.45); gen_bd(MSEC(1000),MSEC(200),0.9); n16=0; for (t=0; t\n",argv[0]); exit(-1); } init_samples(); read_com(); /* create_sample(); */ /* clear_os(); */ if (initwav(argv[1]) != 0) { printf("Cannot open output file %s.\n", argv[2]); exit(-3); } writewav(fd); write_com("end\n"); return(0); } trymain() { init_samples(); add_sample("len",2000,0,0, 1.0); add_sample("s_bd",0,0,0.9, 1.0); add_sample("s_bd",500,0,0.9, 1.0); }