在c中反转WAV文件

时间:2013-06-28 17:32:47

标签: c binary wav fseek

您好我想要做的是反转二进制文件。文件的类型是wav,例如,如果通道号是2,每个样本的比特是16,每次我将复制32/8 = 4个字节。第一个想做的是按原样复制标题(该部分没问题),然后反转数据。我已经创建了一个代码来复制标题,然后将这部分数据从最后复制10次(用于测试),但是由于某种原因,它不会复制40个字节而是在20处停止(即使它会这样做20次也会仍然只复制20个字节)。这是执行此操作的代码。我不能发现错误,如果你能看到它告诉我:)也许错误是在其他地方,所以我写了全部功能

void reverse(char **array)
{
    int i=0;
    word numberChannels;//word = unsigned short int, byte = unsigned char
    word bitsPerSample;
    FILE *pFile;
    FILE *pOutFile;
    byte head[44];
    byte *rev;
    int count;
    int sum = 0;
    if(checkFileName(array[2]) == 0 || checkFileName(array[3]) == 0)
    {
        printf("wrong file name\n");
        exit(1);
    }
    pFile = fopen (array[2] ,"r");
    fseek(pFile, 22, SEEK_SET);//position of channel
    fread(&numberChannels, sizeof(word), 1, pFile);
    fseek(pFile, 34, SEEK_SET);//position of bitsPerSample
    fread(&bitsPerSample, sizeof(word), 1, pFile);
    count = numberChannels * bitsPerSample;
    rewind(pFile);
    fread(head, sizeof(head), 1, pFile);
    pOutFile = fopen (array[3] ,"w");
    fwrite(head, sizeof(head), 1, pOutFile);//writes the header to the output file
    count = count/8;//in my example count = 32 so count =4
    rev = (byte*)malloc(sizeof(byte) * count);//byte = unsigned char
    fseek(pFile, -count, SEEK_END);
    for(i=0; i<10 ; i++)
    {
            sum = sum + count;
            fseek(pFile, -sum, SEEK_END);
        fread(rev, count, 1, pFile);
        fwrite(rev, count, 1, pOutFile);    
    }
    fclose(pFile);
    fclose(pOutFile);
}

0 个答案:

没有答案