#include #include #include #include #include #define SRATE 16000 #undef TEXT #define BINARY typedef struct _DelayLine { short *data; int length; short *pointer; short *end; } DelayLine; /* Delay line init routine */ static DelayLine *initDelayLine(int len) { DelayLine *dl = (DelayLine *)malloc(sizeof(DelayLine)); dl->length = len; if (len > 0) dl->data = (short *)malloc(len * sizeof(short)); else dl->data = 0; dl->pointer = dl->data; dl->end = dl->data+len-1; return dl; } /* Frees memory associated with a delay line */ static void freeDelayLine(DelayLine *dl) { if (dl && dl->data) free(dl->data); dl->data = 0; free(dl); } /* Initialize delayline contents */ inline static void setDelayLine (DelayLine *dl, double *values, double scale) { int i; for (i = 0; i < dl->length; i++) dl->data[i] = (short)(32768.0 * scale * values[i]); } /* update the delay lines */ inline static short dl_update(DelayLine *dl, short insamp) { short *ptr = dl->pointer; short outsamp = *ptr; *ptr++ = insamp; if (ptr > dl->end) ptr = dl->data; dl->pointer = ptr; return outsamp; } /* get the value of a sample at a certain offset */ static inline short dl_access(DelayLine *dl, int position) { short *outloc = dl->pointer - position; while (outloc < dl->data) outloc += dl->length; while (outloc > dl->end) outloc -= dl->length; return *outloc; } /* allocate global dl's for a waveguide */ static DelayLine *upper_rail, *lower_rail; /* reverse elements of an array */ static inline void flipArray(double *a, int n) { int i, j, nh = n/2; double temp; for (i = 0; i < nh; i++) { j = n - i - 1; temp = a[i]; a[i] = a[j]; a[j] = temp; } } int max(int a, int b) { if (a> 1) + (state1 >> 2) + (state2 >> 2); state2 = state1; state1 = insamp; return(temp); } /* return the next output sample from the simulator at a given picup location */ static inline short nextSample (int pickup_loc) { short yp0, ym0, ypM, ymM; short outsamp; outsamp = dl_access(upper_rail, pickup_loc); outsamp += dl_access(lower_rail, -pickup_loc); ym0 = dl_access(lower_rail, 0); ypM = dl_access(upper_rail, upper_rail->length-1); ymM = -ypM; yp0 = -bridgeReflection(ym0); dl_update(upper_rail, yp0); dl_update(lower_rail, ymM); return outsamp; } /* Here's the output part... */ writeSound(char *name, short *soundData, int sampleCount) { int i, err; FILE *filedesc, *tmp; #ifdef BINARY filedesc = fopen(name, "w"); fwrite(soundData,sizeof(short),sampleCount,filedesc); #endif #ifdef TEXT tmp = fopen("tmp.dat","w"); for(i=0;ilength; short data[sampleCount]; for (i=0; i note) note += 7; if (string == 'G') { if (accidental == 'F') return(*(Gflats + (note - string))); if (accidental == 'N') return(*(Gnaturals + (note - string))); if (accidental == 'S') return(*(Gsharps + (note - string))); } if (string == 'D') { if (accidental == 'F') return(*(Dflats + (note - string))); if (accidental == 'N') return(*(Dnaturals + (note - string))); if (accidental == 'S') return(*(Dsharps + (note - string))); } if (string == 'A') { if (accidental == 'F') return(*(Aflats + (note - string))); if (accidental == 'N') return(*(Anaturals + (note - string))); if (accidental == 'S') return(*(Asharps + (note - string))); } if (string == 'E') { if (accidental == 'F') return(*(Eflats + (note - string))); if (accidental == 'N') return(*(Enaturals + (note - string))); if (accidental == 'S') return(*(Esharps + (note - string))); } } /* main */ void main (int argc, char *argv[]) { int i, sampleCount, pitch; int frequency; short *data; double amp, duration; double pick, pickup, writesample; int pickupSample; char note; char string; char accident; int instrument; char *outfile; if (argc != 6) { fprintf(stderr, "Usage: nbn \n"); exit(1); } note = *argv[1]; accident = *argv[2]; string = *argv[3]; instrument= *argv[4]; outfile = argv[5]; duration = 5; sampleCount = duration * SRATE; data = (short *)malloc(sampleCount * sizeof(short)); frequency = nametofreq(note,string,accident); pickupSample = initString(.1, frequency, .1, .1); for (i=0; i