util.c 1.98 KB

#include "vadpcm.h"

#define		LSBMASK		1
#define		WORDSIZE	16

static uint input_word = 0;	/* Where to keep the current input bits */
static int in_bit_pos = -1;      /* Pointers to bit position */

static uint
getshort(FILE *ifile)
{
  int c1, c2;
  
  if ((c1 = getc(ifile)) == EOF){ 
    return 0;
  }
  if ((c2 = getc(ifile)) == EOF){
    return 0;
  }
  return (c1<<8) | c2;
}

uint
readbits(uint nbits, FILE *ifile)
{
  int	c;
  uint	b, left;
  
/* Maximum of WORDSIZE bits can be read - This is in MSB first
  style - ie first bit is MSB in returned value */

  if (nbits <= in_bit_pos+1){
    uint mask = (uint) (1L<<nbits) - 1;
    b = mask & (input_word>>(in_bit_pos-nbits+1));
    in_bit_pos -= nbits;
    if (in_bit_pos < 0){
      c = getshort(ifile);
      input_word = (uint) c;
      in_bit_pos = WORDSIZE-1;
    }
    return b;
  }
  else{
    b = input_word & ((1L<<(in_bit_pos+1)) - 1);     
    left = nbits - in_bit_pos - 1;
    c = getshort(ifile);
    input_word = (uint) c;
    in_bit_pos = WORDSIZE-1;
    b = (b<<left) | readbits(left, ifile);
    return b;
  }
}

char *ReadPString(FILE *ifile)


{
    unsigned char c;
    char        *st;

    fread(&c, 1, 1, ifile);
    st = (char *) malloc(c+1);
    fread(st, c, 1, ifile);
    *(st+c) = 0;
    
    /*
     * Read the pad byte if necessary
     */
    if (!(c & 1))
        fread(&c, 1, 1, ifile);
    return st;
}

int
lookupMarker(unsigned long *sample, MarkerID loopPoint, Marker *markers, int nmarkers)

{
    int i;

    for (i=0; i<nmarkers; i++)
        if (loopPoint == markers[i].id){
            *sample = (markers[i].positionH<<16) + markers[i].positionL;
            return 0;
        }

    return 1;
}

ALADPCMloop *
readlooppoints(FILE *ifile, short *nloops)

{
    int
        i;
    ALADPCMloop
        *al;
    
    fread(nloops, sizeof(short), 1, ifile);
    al = (ALADPCMloop *) malloc(*nloops * sizeof(ALADPCMloop));
    for (i=0; i<*nloops; i++)
            fread(&al[i], sizeof(ALADPCMloop), 1, ifile);
    return al;
}