read_data

Digital Signal Processing Library

Voice Lab

Program name: read_data

Language: C

In file: file_lib.c

Objective: Read data from file.

Usage: int read_data(FILE *in_file, int data_size, int sample_rate, int *result);

Parameters:

  • in_file - FILE Pointer to open file to process.
  • data_size - Length of data word - typically 2 bytes.
  • sample_rate - number of bits per sample - typically 16.
  • result - data read
Return:
  • count - should be equal to data_size, typically 2

Mathematical Description:

Comments: Routine is used to read data file and return a value

User Comments

Code:

int read_data(FILE *in_file, int data_size, int sample_rate, int *result)
{
unsigned char c[4];
int i, count;

/* read and return the data */
switch(data_size) {
case 1:
count = fread(&c, sizeof(char), data_size, in_file);
(*result) = (int) c[0];
break;
case 2:
count = fread(&c, sizeof(char), data_size, in_file);
(*result) = c[0];
(*result) |= c[1] << 8;
if(c[1] & 0x80) {
for(i = 2; i < sizeof(int); i++) {
(*result) |= 0xFF<<(i*8);
}
}
break;
case 3:
count = fread(&c, sizeof(char), data_size, in_file);
(*result) = c[0];
(*result) |= c[1] << 8;
(*result) |= c[2] << 16;
if(c[2] & 0x80) {
for(i = 3; i < sizeof(int); i++) {
(*result) |= 0xFF<<(i*8);
}
}
break;
case 4:
count = fread(&c, sizeof(char), data_size, in_file);
(*result) = c[0];
(*result) |= c[1] << 8;
(*result) |= c[2] << 16;
(*result) |= c[3] << 24;
if(c[3] & 0x80) {
for(i = 4; i < sizeof(int); i++) {
(*result) |= 0xFF<<(i*8);
}
}
break;
default:
fprintf(stderr, "Data_length out of range:(%d)! What to do?\n", data_size);
exit(1);
}
return(count);
}