复制wav文件c ++

时间:2014-07-18 15:07:36

标签: c++ file-io wav fstream

我的程序(见下文)创建了一个包含标题的wav文件,依此类推。我可以打开它,但它不会复制所有的数据块。标题是OK:我可以用WMP打开文件,我可以听到一些噪音,但对于Data,它实际上并没有复制所有文本。我用Wordpad打开了两个wav文件,数据只收集前三行字符然后收集所有空格(所以数据大小合适)。

fstream ifs(FileInputPath->c_str(), ios_base::in);
cout<< "PATH :" << FileInputPath->c_str()<<endl;
ofstream outfile("C:/Users/miguel/Desktop/proj/Automatic_Visual_Speech_v0.9_beta/exemplo.wav", ofstream::out);
if (ifs.is_open() && outfile.is_open())
{
    char First_Chunk_ID[5]=""; //RIFF
    ifs.read(First_Chunk_ID,4);
    outfile.write(First_Chunk_ID,4);

    long File_Size;  // FileSize
    ifs.read( reinterpret_cast<char*>(&File_Size), sizeof(long) );
    outfile.write(reinterpret_cast<char*>(&File_Size),sizeof(long));

    char Form_Type_ID[5] =""; //Formato
    ifs.read(Form_Type_ID,4);
    outfile.write(Form_Type_ID,4);

    char Second_Chunk_ID[5] = ""; //2ºPedaco
    ifs.read(Second_Chunk_ID,4);
    outfile.write(Second_Chunk_ID,4);

    /*char * charArray_Wave_Format_Size = new char;
    ifs.read(charArray_Wave_Format_Size, sizeof(long));*/

    long Wave_Format_Size; //Tamanho do 2º Pedaço
    ifs.read( reinterpret_cast<char*>(&Wave_Format_Size), sizeof(long) );
    outfile.write(reinterpret_cast<char*>(&Wave_Format_Size), sizeof(long));

    char Wave_Format_Info[3] = ""; //Tipo de formato!
    ifs.read(Wave_Format_Info, 2);
    outfile.write(Wave_Format_Info,2);

    short NumChannels; //Canais
    ifs.read(reinterpret_cast<char*>(&NumChannels),2);
    outfile.write(reinterpret_cast<char*>(&NumChannels),2);

    long SampleRate;
    ifs.read(reinterpret_cast<char*>(&SampleRate),4);
    outfile.write(reinterpret_cast<char*>(&SampleRate),4);

    long ByteRate;
    ifs.read(reinterpret_cast<char*>(&ByteRate),4);
    outfile.write(reinterpret_cast<char*>(&ByteRate),4);

    short BlockAlign;
    ifs.read(reinterpret_cast<char*>(&BlockAlign),2);
    outfile.write(reinterpret_cast<char*>(&BlockAlign),2);

    short BitsPerSample;
    ifs.read(reinterpret_cast<char*>(&BitsPerSample),2);
    outfile.write(reinterpret_cast<char*>(&BitsPerSample),2);

    char Third_Chunk_ID[5] = "";
    ifs.read(Third_Chunk_ID, 4);
    outfile.write(Third_Chunk_ID,4);

    long charArray_Data_Size;
    ifs.read(reinterpret_cast<char*>(&charArray_Data_Size), sizeof(long));
    outfile.write(reinterpret_cast<char*>(&charArray_Data_Size),sizeof(long));
            char Data[81600]="";// if you want to read 10000 chars, make a buffer of 10000 chars
    ifs.read(Data,charArray_Data_Size+1);  // use read(), not get().  Everything in the file is binary
    outfile.write(Data,charArray_Data_Size+1);
    outfile.close();
}

1 个答案:

答案 0 :(得分:1)

您没有显示如何打开输出文件,因此我使用了一个通灵猜测:您将文件打开为文本而不是二进制文件。