/* 
             CREATOR: Colin Ligertwood
                DATE: ?????
         DESCRIPTION: operations for verifying and reading wav files. 
            FILENAME: wav_read.c
             STARTED: ?????
           PLATFORMS: Linux (DOS)
	 MOD_HISTORY: 
                  - ????? ????? initial creation
                  - Sep 5 2000 Dan Williams, general cleanup.

*/

#include <sys/stat.h>
#include <sys/types.h>

//!!!???!!!
//#include <IO.h> // for DOS

#include <fcntl.h>
#include <stdio.h>

#include "wav_lib.h"


/*
----------------------------- open_wav ----------------------------
does: opens a wav file and reads data into header
  in: pointer to header area
 out: 
  success: handle to wav file
     fail: -1
*/
int open_wav(char *header, const char *file_name){
  int handle;
  
  if ((handle = open(file_name, O_RDONLY, S_IREAD)) == (-1)) {
    return (-1);
  }
  read(handle,(char *) header, 44);
  return(handle);
}


/*
--------------------------- get_wav_format ------------------------
does: retrieves the format ID
  in: wav_sig structure
 out: should be 16, if not then screem
*/
int16_t get_wav_format(wav_sig *info){
  return(*(int16_t *)&info->head[20]);
}

/*
---------------------------- init_wav_read ----------------------
does: initialize a wav file for reading
  in: filename, wav_sig structure
 out: 
  success: handle to file
     fail: -1
*/
int init_wav_read(const char *file_name, wav_sig *info){

  if ((info->handle = open_wav(info->head, file_name)) == (-1)) {
    return (-1);
  }

  info->name = file_name; 
  info->chans = *(int16_t *)&info->head[22];
  info->srate = *(int32_t *)&info->head[24];
  info->bps   = *(int32_t *)&info->head[28];
  info->blkalign = *(int32_t *)&info->head[32];
  info->depth = *(int16_t *)&info->head[34];
  info->len   = *(int32_t *)&info->head[40];
  return(info->handle);  
}

/*
--------------------------- fin_wav_read -----------------------------
does: finalizes a wav read session
  in: wav_sig structure
 out: 0
*/
int fin_wav_read(wav_sig *info){
  close(info->handle);
  return 0;
}














