This is ug.sndfile.c in view mode; [Download] [Up]
/* Soundfile Lookup Generator */ #include "mm.head.h" #include "ug.head.h" #if defined nextsf #import <cthreads.h> #import <sound/sound.h> #import <sys/file.h> #import <sys/types.h> #import <sys/stat.h> SNDSoundStruct *snd; # endif nextsf #define AMP 1 #define INCR 2 #define FNAME 3 #define CHANNEL 4 #define FSTART 5 #define FEND 6 #define FRAME 7 #define NCHAN 8 #define SAMPS 9 #define OFFSET 10 #define FILEPTR 11 sndfile #if defined nextsf UGHEAD { UGINIT; float sample, sample1, sample2; long nchan, size, offset; long last, chan, nsamp; FILE *fptr; float interpsnd(); struct stat buf; int snderr; if(STARTNOTE) { if((fptr = FPLOC(FILEPTR) = fopen(SLOC(FNAME), "r" )) == NULL) { Error++; PROUT("SNDFILE: Error opening file '%s'",SLOC(FNAME)); return; } if((snderr = SNDReadSoundfile(SLOC(FNAME),&snd)) != SND_ERR_NONE) { Error++; PROUT("SNDFILE: %s",SNDSoundError(snderr)); PROUT("SNDFILE: offending file '%s'",SLOC(FNAME)); return; } if(snd->dataFormat != SND_FORMAT_LINEAR_16) { Error++; PROUT( "SNDFILE: file '%s' not shortsams",SLOC(FNAME)); return; } if((int) (VAL(CHANNEL) + 1) > snd->channelCount) { Error++; PROUT("SNDFILE: File does not contain '%.0f' channels",VAL(CHANNEL) + 1.0); return; } chan = (long int) VAL(CHANNEL); nchan = LVAL(NCHAN) = snd->channelCount; LVAL(SAMPS) = snd->dataSize >> 1; LVAL(OFFSET) = snd->dataLocation + (((((long) VAL(FSTART)) * nchan) + chan) << 1); VAL(FRAME) = VAL(FSTART); SNDFree(snd); } fptr = FPLOC(FILEPTR); nsamp = LVAL(SAMPS); chan = VAL(CHANNEL); nchan = LVAL(NCHAN); offset = LVAL(OFFSET); if(VAL(FEND) < 0.0) VAL(FEND) = (float) (nsamp/nchan - 1); size = (long int) (VAL(FEND) - VAL(FSTART) + 1.0); last = (long int) VAL(FEND); { double frame = VAL(FRAME); UGLOOP { VAL(OUT)++ = VAL(AMP) * interpsnd(fptr,frame,size,last,nchan,offset); frame += VAL(INCR); while(frame >= VAL(FEND)) frame -= (double) size; while(frame <= VAL(FSTART)) frame += (double) size; UGEND(1); } VAL( FRAME ) = frame; } if(ENDNOTE) { fclose(fptr); } } float interpsnd( fp, frame, size, last, nchan, offset ) FILE *fp; double frame; long size, last, nchan, offset; { long Int; static int first = 1; static long P1, P2; static float S1, S2, Frac, Diff, floatsam; static FILE * FP = NULL; short sample; if ( first ) { first = 0; floatsam = 1./32767.; } if ( fp != FP ) { FP = fp; P1 = -1; P2 = -2; } if ( ( Int = frame ) != P1 ) { if ( Int == P2 ) { P1 = P2; S1 = S2; } else { P2 = P1 = Int; fseek( fp, offset + ( ( P1*nchan ) << 1 ), 0 ); fread( &sample, sizeof(short), 1, fp ); S1 = sample*floatsam; } if ( ++P2 > last ) P2 -= size; fseek( fp, offset + ( ( P2*nchan ) << 1 ), 0 ); fread( &sample, sizeof(short), 1, fp ); Diff = ( S2 = sample*floatsam ) - S1; } if ( ( Frac = frame - Int ) == 0.0 ) return( S1 ); else return( S1 + Frac*Diff ); } # endif nextsf # if !defined nextsf UGHEAD { UGINIT; fprintf( stderr," Unimplemented Unit Generator: SNDFILE\n" ); } #endif !nextsf
These are the contents of the former NiCE NeXT User Group NeXTSTEP/OpenStep software archive, currently hosted by Netfuture.ch.