www.pudn.com > ¶ÁwavÎļþ.rar > zFileMan.c
#include#include #include #include #include #include /* for speech file header: SPH_Header */ #include "zFileMan.h" /************************************* /* Special Notes for multichannel /*------------------------------------ /* Chunk: fmt /* ChannelNo: uChannelNo /* BytePerSec: uChannelNo x uSmpBytes (2) x uSmpRate /* BlockAlign: uChannelNo x uSmpBytes (2) /* Chunk: data /* ChunkSize: uChannelNo x uSmpBytes (2) x lSmpSize per Channel /************************************/ short fnReadRAWHeaderInfo(SPH_Header *gSphHead, FILE *hInFile) { /* fill in the information to header file */ gSphHead->uSmpBytes = 2; /* 16 bit only */ gSphHead->uSmpRate = 0; /* sample rate not specified */ gSphHead->uChannelNo = 0; /* channel number not specified */ /* size in samples for all channels*/ fseek(hInFile, 0, SEEK_END); gSphHead->lSmpSize = ftell(hInFile)/gSphHead->uSmpBytes; fseek(hInFile, 0, SEEK_SET); return(1); } short fnReadWAVHeaderInfo(SPH_Header *gSphHead, FILE *hInFile) { unsigned char rgchStr[5]; unsigned long lTmp, lFileLen; unsigned short uTmp; rgchStr[4]='\0'; /* CHUNK: RIFF */ fread(rgchStr, sizeof(unsigned char), 4, hInFile); if (0!=strcmp(rgchStr, "RIFF")) { printf("%s is not in RIFF format\n", gSphHead->pchName); return(-1); } fread(&lTmp, sizeof(unsigned long), 1, hInFile); /* Chunk Size */ fread(rgchStr,sizeof(unsigned char), 4, hInFile); /* File Type */ if (0!=strcmp(rgchStr, "WAVE")) { printf("%s is not in WAVE sound file\n", gSphHead->pchName); return(-1); } /* CHUNK: fmt */ fread(rgchStr, sizeof(unsigned char), 4, hInFile); while (0!=strcmp(rgchStr, "fmt ")) { /* keep reading until seeing "fmt "*/ if (0!=ferror(hInFile)) { printf("Format chunk not found.\n"); return(-1); } fread(&lTmp, sizeof(unsigned long), 1, hInFile); /* get the length */ fseek(hInFile, sizeof(unsigned char)*lTmp, SEEK_CUR); /* skip unused chunk */ fread(rgchStr, sizeof(unsigned char), 4, hInFile); /* get next chunk */ } fread(&lFileLen, sizeof(unsigned long), 1, hInFile); /* Chunk Size */ fread(&uTmp, sizeof(unsigned short), 1, hInFile); /* Format Tag */ if (uTmp!=1) { printf("Only 16-bit linear PCM data supported\n"); /* linear PCM only */ return(-1); } fread(&uTmp, sizeof(unsigned short), 1, hInFile); /* Channel No */ gSphHead->uChannelNo = (unsigned short) uTmp; fread(&lTmp, sizeof(unsigned long), 1, hInFile); /* Sampling Rate */ gSphHead->uSmpRate = (unsigned short) lTmp; fread(&lTmp, sizeof(unsigned long), 1, hInFile); /* Bytes Per Sec */ fread(&uTmp, sizeof(unsigned short), 1, hInFile); /* Block Alignment */ gSphHead->uSmpBytes = uTmp / gSphHead->uChannelNo; fread(&uTmp, sizeof(unsigned short), 1, hInFile); /* Bits Per Sample */ if (uTmp!=16) { printf("Only 16-bit data supported\n"); return(-1); } lFileLen -= 16; /* skip other information */ if (lFileLen!=0) fseek(hInFile, sizeof(unsigned char)*lFileLen, SEEK_CUR); /* Chunk: data */ fread(rgchStr, sizeof(unsigned char), 4, hInFile); while (0!=strcmp(rgchStr, "data")) { /* keep reading until seeing "data"*/ if (0!=ferror(hInFile)) { printf("Format chunk not found.\n"); return(-1); } fread(&lTmp, sizeof(unsigned long), 1, hInFile); fseek(hInFile, sizeof(unsigned char)*lTmp, SEEK_CUR); fread(rgchStr, sizeof(unsigned char), 4, hInFile); } /* length of data in bytes */ fread(&lTmp, sizeof(unsigned long), 1, hInFile); /* only consider 16-bit and 8-bit data */ gSphHead->lSmpSize = lTmp / gSphHead->uSmpBytes; //fseek(hInFile, 44, SEEK_SET); return(1); } /* designed to take care of any different files, The program look for the '.' to determined the file extension name */ short fnReadHeaderInfo(SPH_Header *gSphHead, FILE *hInFile) { short i, n, wResult; char *pchPtr, *pchPtr1; char *rgchExtension; /* find the length of the extension name */ pchPtr = strchr(gSphHead->pchName, '.'); pchPtr++; /* point to the next character */ pchPtr1 = strchr(gSphHead->pchName, '\0'); n = (short) (pchPtr1-pchPtr)/sizeof(char); /* allocate the memory for the extension */ rgchExtension = malloc((n+1)*sizeof(char)); strcpy(rgchExtension, pchPtr); /* make sure every character is in lower case */ for(i=0; i lSmpSize*gSphHead->uChannelNo*gSphHead->uSmpBytes; /* CHUNK: RIFF */ fwrite(rgchTmp, sizeof(unsigned char), 4, hInFile); /* Chunk Name */ lTmp = 36 + lDataLen; fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* Chunk Size */ strcpy(rgchTmp, "WAVE"); fwrite(rgchTmp, sizeof(unsigned char), 4, hInFile); /* File Type */ /* CHUNK: fmt */ strcpy(rgchTmp, "fmt "); fwrite(rgchTmp, sizeof(unsigned char), 4, hInFile); /* Chunk Name */ lTmp = 16; fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* Chunk Size */ uTmp = 1; /* for linear PCM */ fwrite(&uTmp, sizeof(unsigned short), 1, hInFile); /* Data Format */ uTmp = gSphHead->uChannelNo; /* for multi-channel, v2.03 MCH */ fwrite(&uTmp, sizeof(unsigned short), 1, hInFile); /* Channel No */ lTmp = (unsigned long) gSphHead->uSmpRate; fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* Sampling Rate */ uTmp = gSphHead->uSmpBytes*gSphHead->uChannelNo; lTmp *= uTmp; fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* Bytes Per Sec */ fwrite(&uTmp, sizeof(unsigned short), 1, hInFile); /* Block Alignment */ uTmp = gSphHead->uSmpBytes*8; fwrite(&uTmp, sizeof(unsigned short), 1, hInFile); /* Bits Per Sample */ /* data chunk */ strcpy(rgchTmp, "data"); fwrite(rgchTmp, sizeof(unsigned char), 4, hInFile); /* total number of bytes */ fwrite(&lDataLen, sizeof(unsigned long), 1, hInFile); return; } /* designed to take care of any different files, The program look for the '.' to determined the file extension name Now support .raw and .wav only */ void fnWriteHeaderInfo(SPH_Header *gSphHead, FILE *hInFile) { short i, n; char *pchPtr, *pchPtr1; char *pchExtension; /* find the length of the extension name */ pchPtr= strchr(gSphHead->pchName, '.'); if (pchPtr!=NULL) { pchPtr++; /* point to the next character */ pchPtr1 = strchr(gSphHead->pchName, '\0'); n = (int) (pchPtr1-pchPtr)/sizeof(char); /* allocate the memory for the extension */ pchExtension = malloc((n+1)*sizeof(char)); strcpy(pchExtension, pchPtr); } else { pchExtension=malloc(4*sizeof(char)); strcpy(pchExtension, "raw"); } /* make sure every character is in lower case */ for(i=0; i uSmpBytes = 2; /* for 16 bits */ } else { pchExtension[3]='\0'; printf("Do not support this kind of voice file: %s \n", pchExtension); exit(-1); } free(pchExtension); return; } /* update length info, v2.02 */ void fnUpdateHeaderInfo(SPH_Header *gSphHead, FILE *hInFile) { short i, n; char *pchPtr, *pchPtr1; char *pchExtension; unsigned long lTmp; /* find the length of the extension name */ pchPtr = strchr(gSphHead->pchName, '.'); if (pchPtr!=NULL) { pchPtr++; /* point to the next character */ pchPtr1 = strchr(gSphHead->pchName, '\0'); n = (int) (pchPtr1-pchPtr)/sizeof(char); /* allocate the memory for the extension */ pchExtension = malloc((n+1)*sizeof(char)); strcpy(pchExtension, pchPtr); for(i=0; i lSmpSize*gSphHead->uChannelNo*gSphHead->uSmpBytes; fseek(hInFile, 40, SEEK_SET); fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* BYTE 40-43: total # of bytes */ lTmp += 36; fseek(hInFile, 4, SEEK_SET); fwrite(&lTmp, sizeof(unsigned long), 1, hInFile); /* BYTE 4-7: 36+(total# of bytes) */ } free(pchExtension); } return; }