📄 io.c
字号:
/*LINTLIBRARY*/ /*PROTOLIB1*/#include <math.h>#include <stdlib.h>#include "main.h"#include "celp_io.h"#include "hexline.h"#include "rint.h"#define SPEECH FALSE /* channel is not read */#define CHANNEL TRUE /* channel is read *//*************************************************************************** ** ROUTINE* IO_read** FUNCTION* Read a frame of input data* SYNOPSIS* IO_read(fp_ifile, type, slen, blen, data)** formal** data I/O* name type type function* -------------------------------------------------------------------* fp_ifile FILE i File structure for input speech* type int i Type of data to read (speech or channel)* slen int i Amount of speech data to read* blen int i Amount of channel data to read* data float o Frame of input data***************************************************************************** CALLED BY** main***************************************************************************/int IO_read(FILE *fp_ifile,int type,int slen,int blen,void *data,
int *num_bytes){int status=END;
switch(type) { case SPEECH: *num_bytes = ReadSpeech(fp_ifile, slen, (float *)data, &status); break; case HEX: case BINARY: *num_bytes = ReadChannel(fp_ifile, blen, type, (int *)data, &status); break; case BINARY_INT: *num_bytes = ReadChannel(fp_ifile, blen, type, (int *)data, &status); break; default: printf("Incorrect read attempted ... exiting\n"); status = END; break;}return status;}/*************************************************************************** ** ROUTINE* IO_write** FUNCTION* Write a frame of output speech* SYNOPSIS* IO_write(speech_out, fp_ofile)** formal** data I/O* name type type function* -------------------------------------------------------------------* speech_out float i Frame of output speech* fp_ofile FILE i File structure for output speech***************************************************************************** CALLED BY** main***************************************************************************/void IO_write(float speech_out[F_LEN],FILE *fp_ofile){short spd[F_LEN];int i;for(i=0;i<F_LEN;i++) spd[i] = irint(max(min(speech_out[i], 32767), -32768)); if (fwrite(spd, sizeof(spd[0]), F_LEN, fp_ofile) != F_LEN) { if (ferror(fp_ofile) != 0) { fprintf(stderr, "***** Error writing output file ***** \n"); exit(1); } exit(1);}fflush(fp_ofile);}/*************************************************************************** ** ROUTINE* WriteChannel** FUNCTION* Write a frame of CELP bitstream* SYNOPSIS* WriteChannel(channel, chan_fp)** formal** data I/O* name type type function* -------------------------------------------------------------------* channel int i Frame of channel bits* chan_fp FILE i File pointer to disk file **************************************************************************** CALLED BY** main***************************************************************************/#define CHARS (CELP_BITS/4) + 1void WriteChannel(int channel[],int type,FILE *chan_fp){char line[CHARS];unsigned short bits=0;static unsigned int ibits=0;int i;static int j=0; switch(type) { case HEX:/* Convert bitstream to character array of hex */ puthex(CELP_BITS, channel, line);/* Write character array to disk file */ fprintf(chan_fp, "%s\n", line); break; case BINARY: for(i=0;i<CELP_BITS;i++,j++) { if(j==sizeof(short)*8) { fwrite(&bits, sizeof(short), 1, chan_fp); bits = 0; j = 0; } bits = bits << 1; bits |= channel[i]; } if(j>0) { fwrite(&bits, sizeof(short), 1, chan_fp); j=0; } break; case BINARY_INT: for(i=0;i<CELP_BITS;i++,j++) { if(j==sizeof(int)*8) { fwrite(&ibits, sizeof(int), 1, chan_fp); ibits = 0; j = 0; } ibits = ibits << 1; ibits |= channel[i]; }/* if(j>0) { fwrite(&ibits, sizeof(short), 1, chan_fp); }*/ break; }}/*************************************************************************** ** ROUTINE* ReadSpeech** FUNCTION* Read a frame of input speech* SYNOPSIS* ReadSpeech(speech_fp, len, speech, status)** formal** data I/O* name type type function* -------------------------------------------------------------------* speech_fp FILE i File pointer to disk file * len int i Amount of data to read* speech float o Frame of input speech* status int o Status of read process**************************************************************************/int ReadSpeech(FILE *speech_fp,int len,float speech[],int *status){short spd[F_LEN];int i, num_read, num_this_bytes, this_word;
static int first=1;
char this_char;
unsigned long this_ulong; /* Added by MAK to facilitate writing .wav files */
if (first == 1) {
first = 0;
while (this_char != 'd') {
fread(&this_char, sizeof(char), 1, speech_fp);
}
fread(&this_word, sizeof(char), 3, speech_fp);
fread(&this_ulong, sizeof(long), 1, speech_fp);
num_this_bytes = floor((this_ulong/2) / 240)*240*2;
printf("%d samples to write\n", num_this_bytes/2);
}
else
num_this_bytes = 0;
num_read = fread(spd, sizeof(spd[0]), len, speech_fp); if (num_read != len) { if (feof(speech_fp) != 0) { fprintf(stderr, "\n***** End of input file ***** \n"); *status = END; } if (ferror(speech_fp) != 0) { fprintf(stderr, "***** Error reading input file***** \n"); exit(1); } } else { for(i=0;i<len;i++) speech[i] = (float) spd[i]; *status = MID; }
return num_this_bytes;}/*************************************************************************** ** ROUTINE* ReadChannel** FUNCTION* Read a frame of CELP bitstream* SYNOPSIS* ReadChannel(channel, len, type, chan_fp, status)** formal** data I/O* name type type function* -------------------------------------------------------------------* chan_fp FILE i File pointer to disk file * len int i Number of bits to read* type int i Type of channel to read* channel int o Frame of channel bits* status int o Status of read process**************************************************************************/int ReadChannel(FILE *chan_fp,int len,int type,int channel[],int *status){char line[CHARS];unsigned short bits, mask=0x8000;static unsigned int ibits, imask=0x80000000;int i, length, num_frames, num_this_bytes;static int j=0, first = 1; switch(type) { case HEX:/* Read a line from disk file */ if (fscanf(chan_fp, "%s", line) == EOF) { *status = 0; printf("\n**** End of Input Channel File ****\n"); }/* Convert from hex chars to a bitstream channel */ else { gethex(len, channel, line); *status = MID; } break; case BINARY:
/* This has been added by MAK to facilitate writing of .wav files */ if(first==1) {
first = 0;
fseek(chan_fp,0L,2);
length = ftell(chan_fp);
rewind(chan_fp);
num_frames = length / (144/8);
num_this_bytes = num_frames*240*2;
printf("num_frames = %d num_this_bytes = %d\n", num_frames, num_this_bytes);
}
for(i=0;i<len;i++,j--) { if(j==0) { if(fread(&bits, sizeof(short), 1, chan_fp) != 0) { j=(sizeof(short)*8); mask = 0x8000; *status = MID; } else { *status = END; printf("\n**** End of Input Channel File ****\n"); break; } } channel[i] = ((mask & bits) == 0)?0:1; mask /= 2; } break; case BINARY_INT: /* This has been added by MAK to facilitate writing of .wav files */
if(first==1) {
first = 0;
fseek(chan_fp,0L,2);
length = ftell(chan_fp);
rewind(chan_fp);
num_frames = length / (144/8);
num_this_bytes = num_frames*240*2;
printf("num_frames = %d num_this_bytes = %d\n", num_frames, num_this_bytes);
}
for(i=0;i<len;i++,j--) { if(j==0) { if(fread(&ibits, sizeof(int), 1, chan_fp) != 0) { j=(sizeof(int)*8); imask = 0x80000000; *status = MID; } else { *status = END; printf("\n**** End of Input Channel File ****\n"); break; } } channel[i] = ((imask & ibits) == 0)?0:1; imask /= 2; } break; }
return num_this_bytes;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -