本文介绍了将PCM转换为WAV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <stdio.h>
#include <string.h>

int main() {

    FILE *fin,*fout;
    char buffer[1028];


    int readcount=0;
    short NumChannels = 1;
    short BitsPerSample = 16;
    int SamplingRate =8000;
    short numOfSamples = 160;

    int ByteRate = NumChannels*BitsPerSample*SamplingRate/8;
    short BlockAlign = NumChannels*BitsPerSample/8;
    int DataSize = NumChannels*numOfSamples *  BitsPerSample/8;
    int chunkSize = 16;
    int totalSize = 36 + DataSize;
    short audioFormat = 1;

    if((fout = fopen("sample.wav", "w")) == NULL)
    {
        printf("Error opening out file ");
    }

    //totOutSample = 0;
    fwrite("RIFF", sizeof(char), 4,fout);
    fwrite(&totalSize, sizeof(int), 1, fout);
    fwrite("WAVE", sizeof(char), 4, fout);
    fwrite("fmt ", sizeof(char), 4, fout);
    fwrite(&chunkSize, sizeof(int),1,fout);
    fwrite(&audioFormat, sizeof(short), 1, fout);
    fwrite(&NumChannels, sizeof(short),1,fout);
    fwrite(&SamplingRate, sizeof(int), 1, fout);
    fwrite(&ByteRate, sizeof(int), 1, fout);
    fwrite(&BlockAlign, sizeof(short), 1, fout);
    fwrite(&BitsPerSample, sizeof(short), 1, fout);
    fwrite("data", sizeof(char), 4, fout);
    fwrite(&DataSize, sizeof(int), 1, fout);
    fclose(fout);
    fin=fopen("sample.raw","r");
    fout=fopen("sample.wav","a");

    while(!feof(fin))
    {
        fgets(buffer,sizeof(buffer),fin);
        fputs(buffer,fout);
    }

    fclose(fin);
    fclose(fout);
}

任何人都可以帮助我解决此代码中的问题.标头是正确的,甚至可以正确地写在文件上.但是,每当打开.wav文件时,它就会显示一个额外的字段.而且文件没有播放.如果我尝试使用媒体信息打开文件,则显示的属性将与预期的一样,但持续时间除外

Can anyone help me to solve the problem in this code. Header is proper and its even properly written on file. But whenever opening in frhed the .wav file, its showing one extra field. And also file is not playing. If I try opening the file using media info its showing properties as expected except duration

音频格式:PCM格式设置, 字节序:小格式设置,符号
:签名编解码器ID:1时长
:20ms比特率模式:恒定比特率
:128 Kbps通道:1通道 采样率:8000 Hz位深度
:16位流大小:320字节(4%)

Audio Format : PCM Format settings, Endianness : Little Format settings, Sign
: Signed Codec ID : 1 Duration
: 20ms Bit rate mode : Constant Bit rate
: 128 Kbps Channel(s) : 1 channel Sampling rate : 8 000 Hz Bit depth
: 16 bits Stream size : 320 Bytes (4%)

推荐答案

我对文件格式一无所知,但是我很难相信它们会有二进制头文件,后跟ascii数据.因此,由于fgets寻找\ n终止符,因此我发现使用fgets和fputs值得怀疑.因此,如果缓冲区在找到\ n(0xa)字节之前就已满,则可能会丢失数据.

I know nothing about the file formats, but I find it hard to believe they would have a binary header followed by ascii data. So the use of fgets and fputs I find questionable since fgets looks for a \n terminator. So you could be losing data if the buffer fills up before it happens to find a \n ( 0xa ) byte.

while(!feof(fin))
{
    fgets(buffer,sizeof(buffer),fin);
    fputs(buffer,fout);
}

像这样的事情会更有意义

Something like this would make more sense

size_t n;
while((n = fread(buffer, 1, sizeof(buffer), fin)) > 0) {
   if(n != fwrite(buffer, 1, n, fout)) {
      perror("fwrite");
      exit(1);
   }
}
if(n < 0) {
   perror("fread");
   exit(1);
}

还,为什么要关闭fout然后再将其重新打开?

Also, why do you close fout and then reopen it?

这篇关于将PCM转换为WAV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 15:18