c - 将数据写入wav文件

时间:2017-01-09 22:24:24

标签: c wav

我正在研究一种从wav文件中读取数据的小工具。该工具首先提取标题,然后将音频数据分成左右声道。音频文件只是采样频率为44100Hz,16Bit PCM和双通道的文件。

操作数据后,我想将数据写回输出文件,并在每个通道上追加100个零。这里出现问题:首先只有一半的所需样本附加在每个通道上。其次是附加的零的前半部分'是随机数据。

请参阅下面的代码

#include <stdlib.h>
#include <stdio.h>

#define BUFFSIZE 1024
#define NUM_ZEROS 100

#include <stdio.h>
#include <stdlib.h>

typedef struct header_file
{
    char chunk_id[4];
    int chunk_size;
    char format[4];
    char subchunk1_id[4];
    int subchunk1_size;
    short int audio_format;
    short int num_channels;
    int sample_rate;
    int byte_rate;
    short int block_align;
    short int bits_per_sample;
    char subchunk2_id[4];
    int subchunk2_size;
} header;

typedef struct header_file* header_p;


int main(int argc, char** argv){

    if( argc != 3 ){
        printf("Wrong number of input arguments. Aborting.\n");
        return -1;
    }

    char *inputFile = argv[1];
    char *outputFile = argv[2];

    FILE * infile = fopen(inputFile, "r+");
    FILE * outfile = fopen(outputFile, "w+");

    int count = 0;                                      // For counting number of frames in wave file.
    short int buff16[2*BUFFSIZE];                       // short int used for 16 bit as input data format is 16 bit PCM audio
    short int buffLeft[BUFFSIZE], buffRight[BUFFSIZE];
    header_p meta = (header_p)malloc(sizeof(header));   // header_p points to a header struct that contains the wave file metadata fields
    int nb, cnt;                                        // variable storing number of bytes returned

    printf("Buffers initialized.\n");

    if (infile)
    {
        fread(meta, 1, sizeof(header), infile);
        meta->subchunk2_size = meta->subchunk2_size + 2 * NUM_ZEROS;
        fwrite(meta,1, sizeof(*meta), outfile);


        while (!feof(infile))
        {
            nb = fread(buff16,1,BUFFSIZE,infile);       // Reading data in chunks of BUFSIZE
            count++;                                    // Incrementing Number of frames

            for(cnt = 0; cnt < nb/2; cnt++){
                buffLeft[cnt] = buff16[2*cnt];
                buffRight[cnt] = buff16[2*cnt+1];
            }

            /*
             * TODO: INSERT SIGNAL PROCESSING PART
             */

            for(cnt = 0; cnt < nb/2; cnt++){
                buff16[2*cnt] = buffLeft[cnt];
                buff16[2*cnt+1] = buffRight[cnt];
            }

            fwrite(buff16,1,nb,outfile);
        }

        for(cnt = 0; cnt < 2*NUM_ZEROS; cnt++){
            buff16[cnt] = 0;
        }
        fwrite(buff16,1, 2*NUM_ZEROS,outfile);

        printf("Number of frames in the input wave file are %d.\n", count);
    }

    fclose(infile);
    fclose(outfile);

    return 0;
}

有人知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

你有

#define NUM_ZEROS 100

fwrite(buff16,1, 2*NUM_ZEROS,outfile);

目标:

  

我想将数据写回输出文件并附加100个零   在每个频道上。

我认为每个频道应该是100个样本。 由于你有16位PCM,每个样本是2个字节。 因此,一个通道需要写入200个字节(零)。立体声意味着400字节。

你的fwrite只保存2 * NUM_ZEROS个200字节 - 这是部分关于缺少样本的答案。

另外你宣布

short int buff16 [2 * BUFFSIZE];

同时读取其中一半并使用一半(nb / 2)进行处理。比写入完整缓冲区(实际上是声明的一半),上半部分来自内存中的随机垃圾。

答案 1 :(得分:0)

你确定只有一部分添加的零是垃圾吗?

您弄乱了freadfwrite

的数据大小

您的缓冲区为short int

short int buff16[2*BUFFSIZE]; // BUFFSIZE*2*sizeof(short) bytes

您只阅读该尺寸的1/4:

nb = fread(buff16,1,BUFFSIZE,infile);  // BUFFSIZE bytes

这会读取BUFSIZE 字节,因为您只为每个元素指定了1的大小。 而不是BUFFSIZE*2短裤,您只读取BUFFSIZE个字节。 返回值是读取元素的数量,即再次是字节。 在您的缓冲区中,只有nb/2元素的数据量才足够,但您访问buff16[0] .. buff16[nb-1],其中后半部分未从文件中读取。 幸运的是,您也不会将后半部分写回新文件中 那里也存在相同的长度误差。 最后,当您将零值附加到文件时,会出现同样的问题。

<强> TL;博士

freadfwrite的尺寸参数更改为sizeof(short int)