2011-10-28 72 views
0

我想將自定義塊添加到現有的WAV文件。我正在使用mmioCreateChunk函數,但在執行後沒有任何更改文件。下面是我的程序的代碼:創建WAV塊

#include <windows.h> 
#include <mmsystem.h> 
#include <iostream> 
#include <fstream> 

using namespace std; 

#pragma comment(lib, "winmm") 

int main() 
{ 
    HMMIO  hmmio;     // file handle for open file 
    MMCKINFO mmckinfoParent;  // parent chunk information 
    MMCKINFO mmckinfoFormatChunk; // Format chunk information structure 
    MMCKINFO mmckinfoDataChunk;  // Data chunk information structure 
    MMCKINFO mmckinfoRdibChunk;  // Rdib chunk information structure 

    // Open the file for reading with buffered I/O 
    // by using the default internal buffer 
    if(!(hmmio = mmioOpen(L"out.wav", NULL, 
    MMIO_READ))) 
    { 
    cout << "Error opening file." << endl; 
    return 0; 
    } 

    // Locate a "RIFF" chunk with a "WAVE" form type to make 
    // sure the file is a waveform-audio file. 
    mmckinfoParent.fccType = mmioFOURCC('W', 'A', 'V', 'E'); 
    if (mmioDescend(hmmio, (LPMMCKINFO) &mmckinfoParent, NULL, 
    MMIO_FINDRIFF)) 
    { 
    cout << "This is not a waveform-audio file." << endl; 
    mmioClose(hmmio, 0); 
    return 0; 
    } 

    // Find the "FMT" chunk (form type "FMT"); it must be 
    // a subchunk of the "RIFF" chunk. 
    mmckinfoFormatChunk.ckid = mmioFOURCC('f', 'm', 't', ' '); 
    if (mmioDescend(hmmio, &mmckinfoFormatChunk, &mmckinfoParent, 
    MMIO_FINDCHUNK)) 
    { 
    cout << "Waveform-audio file has no \"FMT\" chunk." << endl; 
    mmioClose(hmmio, 0); 
    return 0; 
    } 

    unsigned int fmtSize = mmckinfoFormatChunk.cksize; 
    char * waveFmt = new char[fmtSize]; 
    mmioRead(hmmio, waveFmt, mmckinfoFormatChunk.cksize); 

    mmioAscend(hmmio, &mmckinfoFormatChunk, 0); 

    // Find the data subchunk. The current file position should be at 
    // the beginning of the data chunk; however, you should not make 
    // this assumption. Use mmioDescend to locate the data chunk. 
    mmckinfoDataChunk.ckid = mmioFOURCC('d', 'a', 't', 'a'); 
    if (mmioDescend(hmmio, &mmckinfoDataChunk, &mmckinfoParent, 
    MMIO_FINDCHUNK)) 
    { 
    cout << "Waveform-audio file has no data chunk." << endl; 
    mmioClose(hmmio, 0); 
    return 0; 
    } 

    unsigned int size = mmckinfoDataChunk.cksize; 
    char* data = new char[size]; 
    mmioRead(hmmio, data, size); 
    mmioClose(hmmio, 0); 

    ifstream fs; 
    fs.open("out.txt"); 

    // get length of file: 
    fs.seekg (0, ios::end); 
    int length = fs.tellg(); 
    fs.seekg (0, ios::beg); 

    // allocate memory: 
    char* buffer = new char [length]; 

    // read data as a block: 
    fs.read (buffer,length); 
    fs.close(); 

    HMMIO hmmio_out; 

    //Creating new wav file. 
    hmmio_out = mmioOpen(L"test.wav", 0, MMIO_CREATE | MMIO_WRITE); 

    //Creating RIFF chunk 
    mmioCreateChunk(hmmio_out, &mmckinfoParent, MMIO_CREATERIFF); 

    //Creating format chunk and inserting information from source file 
    mmioCreateChunk(hmmio_out, &mmckinfoFormatChunk, 0); 
    mmioWrite(hmmio_out, waveFmt, mmckinfoFormatChunk.cksize); 

    mmioAscend(hmmio_out, &mmckinfoFormatChunk, 0); 

    //Creating data chunk and inserting information from source file 
    mmioCreateChunk(hmmio_out, &mmckinfoDataChunk, 0); 
    mmioWrite(hmmio_out, data, mmckinfoDataChunk.cksize); 

    mmioAscend(hmmio_out, &mmckinfoDataChunk, 0); 

    //Creating new rdib chunk and inserting information from out.txt 
    mmckinfoRdibChunk.ckid = mmioFOURCC('r', 'd', 'i', 'b'); 
    mmckinfoRdibChunk.cksize = sizeof(char) * length; 
    mmioCreateChunk(hmmio_out, &mmckinfoRdibChunk, 0); 

    mmioWrite(hmmio_out, buffer, sizeof(char) * length); 

    mmioAscend(hmmio_out, &mmckinfoRdibChunk, 0); 

    // Close the file. 
    mmioClose(hmmio_out, 0); 

    return 0; 
} 

我必須做的,創造新的塊?

回答

1

mmioDescend從當前文件位置開始搜索。所以它不會找到你剛纔寫的塊。您應該首先將文件位置重置爲開始或前一個塊。 您是否正確設置了其他塊字段(ckSize和fccType)? 也許你應該在創建塊來獲得填充權後執行mmioAscend。

+0

我正在使用RIFF視圖應用程序來查看即興文件結構,它沒有找到新的塊。但如果我用記事本++打開我的文件,我可以看到插入到它的信息。這是我的問題。您可以在編輯的主題主消息中看到新代碼。 –

+0

您正在將塊長度設置爲文件長度?塊可能不完整(它必須有偶數個字節),這可能是RIFF視圖看不到它的原因。 – Han

0

Inside the RIFF Specification

如果成功的話,mmioCreateChunk()將當前文件位置 新塊的數據區的開始(後塊 類型RIFF或LIST塊)。 塊的內容可能是 ,使用mmioWrite()編寫,或可能會創建子塊,並調用另一個 調用mmioCreateChunk()。請注意,mmioCreateChunk()不能將 塊中途插入已存在的文件中。如果嘗試此操作,將覆蓋文件中的現有數據 。

+0

我也嘗試創建一個新文件並將數據從現有文件複製到它,但結果是相同的。 –

+0

它增加了我想要寫入這個文件的信息,但是它不會將它識別爲一個塊。 –

0
hmmio_out = mmioOpen(L"test.wav", 0, MMIO_CREATE | MMIO_WRITE); 

應該

hmmio_out = mmioOpen(L"test.wav", 0, MMIO_CREATE | MMIO_READWRITE); 

關注它將抹去因MMIO_CREATE文件的內容!;