/*-------------- Telecommunications & Signal Processing Lab --------------- McGill University Routine: void AFwriteU1 (AFILE *AFp, const float Fbuff[], int Nval) Purpose: Write offset-binary 8-bit integer data to an audio file (float input values) Description: This routine writes a specified number of offset-binary 8-bit integer samples to an audio file. The input to this routine is a buffer of float values. The file must have been opened using subroutine AFopenWrite. A warning message is printed if the input values exceed the dynamic range of the integer representation. Parameters: -> AFILE *AFp Audio file pointer for an audio file opened by AFopenWrite -> const float Fbuff[] Array of floats with the samples to be written -> int Nval Number of samples to be written Author / revision: P. Kabal Copyright (C) 1996 $Revision: 1.1 $ $Date: 1996/08/14 18:18:48 $ -------------------------------------------------------------------------*/ static char rcsid [] = "$Id: AFwriteU1.c 1.1 1996/08/14 AFsp-V2R1 $"; #include #include #include #include #define LW FDL_UINT8 #define MINV(a, b) (((a) < (b)) ? (a) : (b)) #define NBBUF 8192 #define AMIN (-128.) #define AMAX (127.) void AFwriteU1 (AFp, Fbuff, Nval) AFILE *AFp; const float Fbuff[]; int Nval; { int is, N, i; long int Novld; long int offs; uint1_t Buf[NBBUF/LW]; int2_t Iv; float Fv; /* Write data to the audio file */ offs = AFp->End; is = 0; Novld = AFp->Novld; while (is < Nval) { N = MINV (NBBUF / LW, Nval - is); for (i = 0; i < N; ++i) { Fv = AFp->ScaleF * Fbuff[i+is]; if (Fv >= 0.0) { Fv = Fv + 0.5; if (Fv >= AMAX + 1.) { ++Novld; Fv = AMAX; } } else { Fv = Fv - 0.5; if (Fv <= AMIN - 1.) { ++Novld; Fv = AMIN; } } Iv = (int2_t) Fv; Iv += 128; Buf[i] = Iv; } FLwriteFile (AFp->fp, offs, (void *) Buf, (size_t) LW, (size_t) N); is = is + N; offs = offs + LW * N; } /* Update the file position and number of overloads*/ AFp->End = offs; if (Novld > 0L && AFp->Novld == 0L) UTwarn ("AFwriteU1 - Output data clipped"); AFp->Novld = Novld; return; }