簡體   English   中英

讀取Wav文件,輸出錯誤

[英]Reading Wav Files, wrong output

我之前曾發布過一個類似的問題(以為最好重新開始,希望能對此有更多的意見)。 基本上,問題是我正在嘗試從.wav文件中讀取數據,但是輸出與MatLab中的輸出不同。

在C ++中,輸出為:

-128

而在Matlab中:

0

輸出是完全不同的,不僅是很小的差異,而且整個數據集是錯誤的,我似乎也不知道為什么。 我認為這可能與字節序有關,但我不確定。 這是有關.wav文件的頭信息:

**** WAV FILE *****
Chunk IDRIFFN?
Chunk Size: 57934
Format: WAVEfmt 
Format: IDfmt 
FormatSize: 18
Format2: 1
Channel Num: 1
Sample Rate: 22050
Byte Rate: 22050
Align: 1
Bits Per Sample: 8

和代碼:

file.read(this->chunkId,                                 4);
file.read(reinterpret_cast<char*>(&this->chunkSize),     4);
file.read(this->format,                                  4);

file.read(this->formatId,                                4);
file.read(reinterpret_cast<char*>(&this->formatSize),    4);
file.read(reinterpret_cast<char*>(&this->format2),       2);
file.read(reinterpret_cast<char*>(&this->numChannels),   2);
file.read(reinterpret_cast<char*>(&this->sampleRate),    4);
file.read(reinterpret_cast<char*>(&this->byteRate),      4);
file.read(reinterpret_cast<char*>(&this->align),         2);
file.read(reinterpret_cast<char*>(&this->bitsPerSample), 4);

char testing[4] = {0};
int testingSize = 0;

while(file.read(testing, 4) && (testing[0] != 'd' ||
                                testing[1] != 'a' ||
                                testing[2] != 't' ||
                                testing[3] != 'a'))
{

    file.read(reinterpret_cast<char*>(&testingSize), 4);
    file.seekg(testingSize, std::ios_base::cur);

}

this->dataId[0] = testing[0];
this->dataId[1] = testing[1];
this->dataId[2] = testing[2];
this->dataId[3] = testing[3];



file.read(reinterpret_cast<char*>(&this->dataSize),     4);

this->data = new char[this->dataSize];

file.read(data,this->dataSize);

cout << "**** WAV FILE *****" << endl;
cout << "Chunk ID" << this->chunkId << endl;
cout << "Chunk Size" << this->chunkSize << endl;
cout << "Format: " << this->format << endl;
cout << "Format ID" << this->formatId << endl;
cout << "FormatSize" << this->formatSize << endl;
cout << "Format2 " << this->format2 << endl;
cout << "Channel Num" << this->numChannels << endl;
cout << "Sample Rate" << this->sampleRate << endl;
cout << "Byte Rate" << this->byteRate << endl;
cout << "Align" << this->align << endl;
cout << "Bits Per Sample" << this->bitsPerSample << endl;
cout << "Size" << testingSize << endl;



for(unsigned i=0; (i < 20); i++){
    cout << (float) data[i] << endl;
}

return true;

誰能看到我要去哪里錯了? 我已經嘗試調試它,但是沒有喜悅(我正在使用g ++進行編譯)。 任何幫助將不勝感激:)對不起,我一直在問這個,現在真的很煩我!

謝謝

我相信WAV文件中的塊必須與最近的WORD邊界對齊。 如果在您解析的格式部分之后不在下一個單詞邊界,則您將不會進入while循環,並且您的代碼將假定您在數據部分。

同樣,如果您的任何塊的字節數都是奇數個,您可能會遇到麻煩。

好,看一下看起來像轉換錯誤的代碼。 顯然,您正在嘗試輸出二十個浮點數(每個浮點數由四個字節組成),但是實際輸出的只是二十個字節,每個單字節轉換為一個浮點數。 我想你可能想要這個

for(unsigned i=0; (i < 20); i++){
    cout << ((float*) data)[i] << endl;
}

換句話說,您將數據轉換為浮點指針,然后應用索引。 不首先應用索引,然后再轉換為浮點數。

我認為matlab中的值在-1到1之間進行了歸一化,因此為了在c ++中獲得相似的輸出,您需要對其進行縮放。 以-32768至32767為范圍的16位樣本的示例,您希望將它們縮放到-1.0至1.0。 這樣做的方法是除以32768.0(-32768 / 32768.0 == -1,32767 / 32768.0略小於1)。

for(unsigned i=0; (i < 20); i++){
    cout << ((float) data[i])/32768.0 << endl;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM