*** cs.h Thu Mar 8 18:23:28 1990 --- cs.h.new Thu Mar 8 18:23:28 1990 *************** *** 19,24 **** --- 21,36 ---- #define BYTREVS(n) (n>>8 & 0xFF | n<<8 & 0xFF00) #define BYTREVL(n) (n>>24 & 0xFF | n>>8 & 0xFF00 | n<<8 & 0xFF0000 | n<<24 & 0xFF000000) + + /* Manifests for short/float/ulaw output format (main.c:int output_format) */ + #define OF_SHORT 0 + #define OF_FLOAT 1 + #ifdef ULAW + # ifdef SFIRCAM + barf barf barf Cannot combine SFIRCAM and ULAW compilation flags barf barf barf + # endif + #define OF_ULAW 2 + #endif typedef struct polish { char opcod[12]; *** main.c Thu Mar 8 18:23:29 1990 --- main.c.new Thu Mar 8 18:23:30 1990 *************** *** 8,14 **** int odebug = 0; /* orch control switches */ int initonly = 0; /* and default values */ int sfwrite = 1; ! int floatout = 0, sampsize = 2, bufsmps = BUFSIZE/2; int ftdisplay = 1; int dsploption = 1; int msglevel = 7; --- 11,18 ---- int odebug = 0; /* orch control switches */ int initonly = 0; /* and default values */ int sfwrite = 1; ! int output_format = OF_SHORT; ! int sampsize = 2, bufsmps = BUFSIZE/2; int ftdisplay = 1; int dsploption = 1; int msglevel = 7; *************** *** 34,43 **** case 'i': initonly = 1; /* i-only implies */ case 'n': sfwrite = 0; /* nosound */ break; ! case 'f': floatout = 1; /* sndfile floats */ sampsize = 4; bufsmps = BUFSIZE/4; break; case 'v': odebug = 1; /* verbose otran */ break; case 't': ftdisplay = 0; /* no ftable dsplays */ --- 38,54 ---- case 'i': initonly = 1; /* i-only implies */ case 'n': sfwrite = 0; /* nosound */ break; ! case 'f': output_format = OF_FLOAT;/* sndfile floats */ sampsize = 4; bufsmps = BUFSIZE/4; break; + #ifdef ULAW + case 'u': + output_format = OF_ULAW; + sampsize = 1; + bufsmps = BUFSIZE; + break; + #endif case 'v': odebug = 1; /* verbose otran */ break; case 't': ftdisplay = 0; /* no ftable dsplays */ *************** *** 55,60 **** --- 66,74 ---- die("unknown orch flag"); } } while (--argc); + + if (!argc) + die("insufficient orch arguments"); if ((term = getenv("TERM")) == NULL) die("environment variable TERM undefined"); *** musmon.c Thu Mar 8 18:23:31 1990 --- musmon.c.new Thu Mar 8 18:23:32 1990 *************** *** 5,11 **** extern INSTRTXT *instrtxtp[]; extern INSDS *frstoff; ! extern int initonly, sfwrite, floatout, msglevel; /* orch control flags */ extern int nchnls, synterrcnt, perferrcnt; extern float hfkprd, ekr; extern char sfout[]; --- 8,14 ---- extern INSTRTXT *instrtxtp[]; extern INSDS *frstoff; ! extern int initonly, sfwrite, output_format, msglevel; /* orch control flags */ extern int nchnls, synterrcnt, perferrcnt; extern float hfkprd, ekr; extern char sfout[]; *************** *** 188,203 **** printf("end of score.\t\t overall amps:"); /* else we're done */ for (maxp=omaxamp, n=nchnls; n--; ) printf("%9.1f", *maxp++); ! if (!floatout) { printf("\n\t overall samples out of range:"); for (rngp=orngcnt, n=nchnls; n--; ) printf("%9d", *rngp++); } printf("\n%d errors in performance\n",perferrcnt); if (sfwrite) { sfclose(); printf("%d records written to %s (%s)\n", ! nrecs, sfout, (floatout ? "floats":"shorts")); } else printf("0 records written to disk\n"); } --- 191,221 ---- printf("end of score.\t\t overall amps:"); /* else we're done */ for (maxp=omaxamp, n=nchnls; n--; ) printf("%9.1f", *maxp++); ! switch (output_format) { ! case OF_FLOAT: ! break; ! default: printf("\n\t overall samples out of range:"); for (rngp=orngcnt, n=nchnls; n--; ) printf("%9d", *rngp++); + break; } printf("\n%d errors in performance\n",perferrcnt); if (sfwrite) { + char *p; + sfclose(); + + switch (output_format) { + case OF_FLOAT: p = "floats"; break; + case OF_SHORT: p = "shorts"; break; + #ifdef ULAW + case OF_ULAW: p = "ulaw bytes"; break; + #endif + default: abort(); + } printf("%d records written to %s (%s)\n", ! nrecs, sfout, p); } else printf("0 records written to disk\n"); } *** perf.c Thu Mar 8 18:23:34 1990 --- perf.c.new Thu Mar 8 18:23:34 1990 *************** *** 15,20 **** --- 18,26 ---- int perff; int perfv; int perft; + #ifdef ULAW + int perfu; + #endif int orchP; int scoreP; int scorex; *************** *** 130,136 **** catorchscor() /* build command from orch, score & flags */ { ! if(args.perfi || args.perfn || args.perff || args.perfv || args.perft){ strcat(command," -"); if(args.perfi) strcat(command,"i"); if(args.perfn) strcat(command,"n"); --- 136,142 ---- catorchscor() /* build command from orch, score & flags */ { ! if(args.perfi || args.perfn || args.perff || args.perfv || args.perft || args.perfu){ strcat(command," -"); if(args.perfi) strcat(command,"i"); if(args.perfn) strcat(command,"n"); *************** *** 137,142 **** --- 143,151 ---- if(args.perff) strcat(command,"f"); if(args.perfv) strcat(command,"v"); if(args.perft) strcat(command,"t"); + #ifdef ULAW + if(args.perfu) strcat(command,"u"); + #endif } if(args.dcodes != NULL) { strcat(command," -d"); *************** *** 196,201 **** --- 205,213 ---- case 'f': args.perff++; break; case 'v': args.perfv++; break; case 't': args.perft++; break; + #ifdef ULAW + case 'u': args.perfu++; break; + #endif case 'd': if (*s == NULL) /* dcodes follow directly */ s = argv[++i]; /* or in next arg */ args.dcodes = s; *************** *** 217,222 **** --- 229,237 ---- fprintf(stderr,"-i\ti-time only orch run\n"); fprintf(stderr,"-n\tno sound onto disk\n"); fprintf(stderr,"-f\tfloat sound samples\n"); + #ifdef ULAW + fprintf(stderr,"-u\tulaw-encoded sound output\n"); + #endif fprintf(stderr,"-v\tverbose mode\n"); fprintf(stderr,"-t\tsupress ftable display\n"); fprintf(stderr,"-d..\tdisplay option: 0=nodisplay, 1=simple ascii, 2=Xwindow slow,\n\t\t3=Xwindow rapid, 4=high-resolution display\n"); *** soundio.c Thu Mar 8 18:23:36 1990 --- soundio.c.new Thu Mar 8 18:23:37 1990 *************** *** 5,10 **** --- 8,14 ---- static char *outbuf; /* (unused if nosound) */ static short *outbufp; /* ditto */ static float *floutbufp; /* ditto */ + static char *uloutbufp; static int sfd, sfopen = 0, bufrem = 4; /* (real set in sfinit) */ static float fzero = 0.; *************** *** 13,19 **** extern long maxloc[], omaxloc[], rngcnt[]; extern short rngflg; extern int nspout, nrecs, nchnls, ksmps; ! extern int floatout, sampsize, bufsmps; extern int remotein, remoteout; extern char errmsg[], *buildsfname(); --- 17,23 ---- extern long maxloc[], omaxloc[], rngcnt[]; extern short rngflg; extern int nspout, nrecs, nchnls, ksmps; ! extern int output_format, sampsize, bufsmps; extern int remotein, remoteout; extern char errmsg[], *buildsfname(); *************** *** 34,40 **** sfh = (SFHEADER *)outbuf; /* positn header blk */ sfsrate(sfh) = esr; /* at begin outbuf */ sfchans(sfh) = nchnls; /* & assgn headrvals */ ! sfclass(sfh) = (floatout ? SF_FLOAT : SF_SHORT); sfmagic(sfh) = SF_MAGIC; outbufp = (short *) (outbuf+sizeof(SFHEADER)); /* step over hdrblk */ floutbufp = (float *) outbufp; /* for fix & float */ --- 38,44 ---- sfh = (SFHEADER *)outbuf; /* positn header blk */ sfsrate(sfh) = esr; /* at begin outbuf */ sfchans(sfh) = nchnls; /* & assgn headrvals */ ! sfclass(sfh) = ((output_format == OF_FLOAT) ? SF_FLOAT : SF_SHORT); sfmagic(sfh) = SF_MAGIC; outbufp = (short *) (outbuf+sizeof(SFHEADER)); /* step over hdrblk */ floutbufp = (float *) outbufp; /* for fix & float */ *************** *** 42,54 **** #else outbufp = (short *) outbuf; floutbufp = (float *) outbuf; bufrem = BUFSIZE / sampsize; #endif sfopen = 1; ! printf("writing %s to %s\n", (floatout ? "floats":"shorts"), sfname); strcpy(sfout, sfname); } fixtran() /* fix spout vals and put in outbuf */ { /* write buffer when full */ register float *sp, *maxampp; --- 46,70 ---- #else outbufp = (short *) outbuf; floutbufp = (float *) outbuf; + uloutbufp = (char *) outbuf; bufrem = BUFSIZE / sampsize; #endif sfopen = 1; ! printf("writing "); ! switch (output_format) { ! case OF_FLOAT: fputs("floats", stdout); break; ! case OF_SHORT: fputs("shorts", stdout); break; ! #ifdef ULAW ! case OF_ULAW : fputs("ulaw bytes", stdout); break; ! #endif ! default: abort(); ! } ! printf(" to %s\n", sfname); ! strcpy(sfout, sfname); } + static fixtran() /* fix spout vals and put in outbuf */ { /* write buffer when full */ register float *sp, *maxampp; *************** *** 115,120 **** --- 131,271 ---- } } + #ifdef ULAW + /* + ** This routine converts from linear to ulaw. + ** + ** Craig Reese: IDA/Supercomputing Research Center + ** Joe Campbell: Department of Defense + ** 29 September 1989 + ** + ** References: + ** 1) CCITT Recommendation G.711 (very difficult to follow) + ** 2) "A New Digital Technique for Implementation of Any + ** Continuous PCM Companding Law," Villeret, Michel, + ** et al. 1973 IEEE Int. Conf. on Communications, Vol 1, + ** 1973, pg. 11.12-11.17 + ** 3) MIL-STD-188-113,"Interoperability and Performance Standards + ** for Analog-to_Digital Conversion Techniques," + ** 17 February 1987 + ** + ** Input: Signed 16 bit linear sample + ** Output: 8 bit ulaw sample + */ + + #define ZEROTRAP /* turn on the trap as per the MIL-STD */ + #define BIAS 0x84 /* define the add-in bias for 16 bit samples */ + #define CLIP 32635 + + static unsigned char + lintoulaw( sample ) + int sample; + { + static int exp_lut[256] = {0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3, + 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, + 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7}; + int sign, exponent, mantissa; + unsigned char ulawbyte; + + /* Get the sample into sign-magnitude. */ + sign = (sample >> 8) & 0x80; /* set aside the sign */ + if ( sign != 0 ) sample = -sample; /* get magnitude */ + if ( sample > CLIP ) sample = CLIP; /* clip the magnitude */ + + /* Convert from 16 bit linear to ulaw. */ + sample = sample + BIAS; + exponent = exp_lut[( sample >> 7 ) & 0xFF]; + mantissa = ( sample >> ( exponent + 3 ) ) & 0x0F; + ulawbyte = ~ ( sign | ( exponent << 4 ) | mantissa ); + #ifdef ZEROTRAP + if ( ulawbyte == 0 ) ulawbyte = 0x02; /* optional CCITT trap */ + #endif + + return ulawbyte; + } + + static + ulawtran() /* fix spout vals and put in outbuf */ + { /* write buffer when full */ + register float *sp, *maxampp; + register long longsmp, *maxlocp, *rngp; + register int n, spoutrem; + float absamp; + + sp = spout; /* adr spout */ + spoutrem = nspout; /* smps to go */ + maxampp = maxamp; + maxlocp = maxloc; + + nchk: if ((n = spoutrem) > bufrem) /* if nspout remaining > buf rem, */ + n = bufrem; /* prepare to send in parts */ + spoutrem -= n; + bufrem -= n; + do { + if ((longsmp = *sp) >= 0) { /* +ive samp: */ + if (*sp > *maxampp) { /* maxamp this seg */ + *maxampp = *sp; /* and its location */ + *maxlocp = nrecstobytes(nrecs) + + (uloutbufp - outbuf); + } + if (longsmp > 32767) { /* out of range? */ + longsmp = 32767; /* clip and report */ + rngp = rngcnt + (maxampp - maxamp); + (*rngp)++; + rngflg = 1; + } + } + else { /* ditto -ive samp */ + if ((absamp = -*sp) > *maxampp) { + *maxampp = absamp; + *maxlocp = nrecstobytes(nrecs) + + (uloutbufp - outbuf); + } + if (longsmp < -32768) { + longsmp = -32768; + rngp = rngcnt + (maxampp - maxamp); + (*rngp)++; + rngflg = 1; + } + } + if (sfopen) + *uloutbufp++ = lintoulaw((int)longsmp); + if (nchnls > 1) { + maxlocp++; + if (++maxampp - maxamp >= nchnls) { + maxlocp = maxloc; + maxampp = maxamp; + } + } + sp++; + } while (--n); + if (!bufrem) { + if (sfopen) { + if ((n = write(sfd,outbuf,BUFSIZE)) < BUFSIZE) + sounderr('w',n); + nrecs++; + uloutbufp = (char *) outbuf; + } + bufrem = bufsmps; + if (spoutrem) goto nchk; + } + } + #endif + + static floatran() /* send float spout vals to outbuf */ { /* write buffer when full */ register float *sp, *maxampp; *************** *** 162,172 **** } } ! zerotran(kcnt) /* copy kcnt zerospouts to short soundbuf, */ ! int kcnt; /* sending buffer whenever full */ { ! register int n, smpsrem, clearcnt = 0; if (!sfopen) return; smpsrem = nspout * kcnt; /* calculate total smps to go */ nchk: if ((n = smpsrem) > bufrem) /* if smps remaining > buf rem, */ --- 313,336 ---- } } ! /* Select one of the above */ ! tran() { ! switch (output_format) { ! case OF_FLOAT: floatran(); break; ! case OF_SHORT: fixtran(); break; ! #ifdef ULAW ! case OF_ULAW: ulawtran(); break; ! #endif ! default: abort(); ! } ! } + static + szerotran(kcnt) + int kcnt; + { + register int n, smpsrem, clearcnt = 0; if (!sfopen) return; smpsrem = nspout * kcnt; /* calculate total smps to go */ nchk: if ((n = smpsrem) > bufrem) /* if smps remaining > buf rem, */ *************** *** 189,194 **** --- 353,390 ---- } } + #ifdef ULAW + static + uzerotran(kcnt) + int kcnt; + { + register int n, smpsrem, clearcnt = 0; + char ulaw_zero = lintoulaw(0); /* precalculate */ + + if (!sfopen) return; + smpsrem = nspout * kcnt; /* calculate total smps to go */ + nchk: if ((n = smpsrem) > bufrem) /* if smps remaining > buf rem, */ + n = bufrem; /* prepare to send in parts */ + smpsrem -= n; + bufrem -= n; + if (clearcnt < bufsmps) { + clearcnt += n; /* clear buf only till clean */ + do *uloutbufp++ = ulaw_zero; + while (--n); + } + else uloutbufp += n; + if (!bufrem) { + if ((n = write(sfd,outbuf,BUFSIZE)) < BUFSIZE) + sounderr('w',n); + nrecs++; + uloutbufp = (char *) outbuf; + bufrem = bufsmps; + if (smpsrem) goto nchk; + } + } + #endif + + static fzerotran(kcnt) /* copy kcnt zerospouts to float soundbuf, */ int kcnt; /* sending buffer whenever full */ { *************** *** 213,218 **** --- 409,428 ---- floutbufp = (float *) outbuf; bufrem = bufsmps; if (smpsrem) goto nchk; + } + } + + zerotran(kcnt) /* copy kcnt zerospouts to short soundbuf, */ + int kcnt; /* sending buffer whenever full */ + { + + switch (output_format) { + case OF_FLOAT: fzerotran(kcnt); break; + case OF_SHORT: szerotran(kcnt); break; + #ifdef ULAW + case OF_ULAW: uzerotran(kcnt); break; + #endif + default: abort(); } } *** insert.c Thu Mar 8 18:31:33 1990 --- insert.c.new Thu Mar 8 18:31:34 1990 *************** *** 9,15 **** extern OENTRY opcodlst[]; extern EVTBLK nxtevt; extern float ekr; ! extern int odebug, msglevel, floatout; INSDS actanchor, *curip, *frstoff = NULL, *instance(); int inerrcnt = 0, perferrcnt = 0; --- 12,18 ---- extern OENTRY opcodlst[]; extern EVTBLK nxtevt; extern float ekr; ! extern int odebug, msglevel; INSDS actanchor, *curip, *frstoff = NULL, *instance(); int inerrcnt = 0, perferrcnt = 0; *************** *** 264,271 **** if (odebug) printf("perfing %d kprds\n",kcnt); if (actanchor.nxtact == NULL) { /* if no instrs active now, */ ! if (floatout) fzerotran(kcnt); ! else zerotran(kcnt); /* send kcnt zerospouts */ } else while (kcnt--) { /* else for each kcnt: */ lp = (long *) spout; --- 267,273 ---- if (odebug) printf("perfing %d kprds\n",kcnt); if (actanchor.nxtact == NULL) { /* if no instrs active now, */ ! zerotran(kcnt); /* send kcnt zerospouts */ } else while (kcnt--) { /* else for each kcnt: */ lp = (long *) spout; *************** *** 278,286 **** while ((pds = pds->nxtp) != NULL) pds->opadr(pds); /* run each opcod */ } ! if (floatout) /* send spout to disk*/ ! floatran(); /* as floats */ ! else fixtran(); /* or shorts */ } } --- 280,286 ---- while ((pds = pds->nxtp) != NULL) pds->opadr(pds); /* run each opcod */ } ! tran(); /* send spout to disk*/ } } *** Makefile Thu Mar 8 19:44:13 1990 --- Makefile.new Thu Mar 8 19:45:48 1990 *************** *** 12,17 **** --- 12,19 ---- # -DSFIRCAM: read/write IRCAM-format soundfiles, with 1024-byte header; # else read/write sound with no headers # -DSFREMOTE: enable remote soundfile access using rexec + # -DULAW: include code for -u flag to output ulaw-encoded bytes + # (cannot be used with SFIRCAM) CFLAGS = -O -f $(DEFINES) # -f compile single-precision floats (4.3, VAX)