1 /*-------------- Telecommunications & Signal Processing Lab ---------------
2                              McGill University
3 
4 Routine:
5   int AFdRdU1 (AFILE *AFp, double Dbuff[], int Nreq)
6 
7 Purpose:
8   Read offset-binary 8-bit integer data from an audio file (return double)
9 
10 Description:
11   This routine reads a specified number of offset-binary 8-bit integer samples
12   from an audio file.  The data in the file is converted to double format on
13   output.
14 
15 Parameters:
16   <-  int AFdRdU1
17       Number of data values transferred from the file.  On reaching the end of
18       the file, this value may be less than Nreq.
19    -> AFILE *AFp
20       Audio file pointer for an audio file opened by AFopnRead
21   <-  double Dbuff[]
22       Array of doubles to receive the samples
23    -> int Nreq
24       Number of samples requested.  Nreq may be zero.
25 
26 Author / revision:
27   P. Kabal  Copyright (C) 2003
28   $Revision: 1.2 $  $Date: 2003/05/09 01:11:34 $
29 
30 -------------------------------------------------------------------------*/
31 
32 #include <libtsp/AFdataio.h>
33 #include <libtsp/AFpar.h>
34 #include <libtsp/UTtypes.h>
35 
36 #define LW		FDL_UINT8
37 #define MINV(a, b)	(((a) < (b)) ? (a) : (b))
38 #define NBBUF		8192
39 
40 #define UINT1_OFFSET	((UINT1_MAX+1)/2)
41 
42 #define FREAD(buf,size,nv,fp)	(int) fread ((char *) buf, (size_t) size, \
43 					     (size_t) nv, fp)
44 
45 
46 int
AFdRdU1(AFILE * AFp,double Dbuff[],int Nreq)47 AFdRdU1 (AFILE *AFp, double Dbuff[], int Nreq)
48 
49 {
50   int is, N, i, Nr;
51   uint1_t Buf[NBBUF/LW];
52   double g;
53 
54   for (is = 0; is < Nreq; ) {
55 
56     /* Read data from the audio file */
57     N = MINV (NBBUF / LW, Nreq - is);
58     Nr = FREAD (Buf, LW, N, AFp->fp);
59 
60     /* Convert to double */
61     /* For offset-binary 8-bit data, the zero-point is the value 128 */
62     g = AFp->ScaleF;
63     for (i = 0; i < Nr; ++i) {
64       Dbuff[is] = g * (((int) Buf[i]) - UINT1_OFFSET);
65       ++is;
66     }
67 
68     if (Nr < N)
69       break;
70   }
71 
72   return is;
73 }
74