[英]Correct reading of samples from .wav file
我正在嘗試正確讀取 WAVE 文件,PCM,mono,16 位(每個樣本 2 個字節)。 我設法閱讀了header 。 問題是讀取(寫入)數據部分。
據我了解,數據塊中的 16 位樣本是小字節序的,並且“拆分”為兩個 8 位的塊。 所以對我來說,讀取正確數據的方法應該是:
int8_t
變量(或std::vector<int8_t>
..)int16_t
並能夠對其進行處理。問題是我不知道如何處理小字節序以及這些樣本不是無符號的事實,所以我不能使用 << 運算符。
這是我做過的測試之一,但沒有成功:
int8_t buffer[], firstbyte,secondbyte;
int16_t result;
std::vector<int16_t> data;
while(Read bytes and put them in buffer){
for (int j=0;j<bytesReadFromTheFile;j+=2){
firstbyte = buffer[j];
secondbyte = buffer[j+1];
result = (firstbyte);
result = (result << 8)+secondbyte; //shift first byte and add second
data.push_back(result);
}
}
更詳細地說,我使用在網上找到的這段代碼並從它開始創建一個 class(過程是相同的,但是 Class 配置很長並且有很多功能對這個問題沒有用):
#include <iostream>
#include <string>
#include <fstream>
#include <cstdint>
using std::cin;
using std::cout;
using std::endl;
using std::fstream;
using std::string;
typedef struct WAV_HEADER
{
/* RIFF Chunk Descriptor */
uint8_t RIFF[4]; // RIFF Header Magic header
uint32_t ChunkSize; // RIFF Chunk Size
uint8_t WAVE[4]; // WAVE Header
/* "fmt" sub-chunk */
uint8_t fmt[4]; // FMT header
uint32_t Subchunk1Size; // Size of the fmt chunk
uint16_t AudioFormat; // Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
uint16_t NumOfChan; // Number of channels 1=Mono 2=Sterio
uint32_t SamplesPerSec; // Sampling Frequency in Hz
uint32_t bytesPerSec; // bytes per second
uint16_t blockAlign; // 2=16-bit mono, 4=16-bit stereo
uint16_t bitsPerSample; // Number of bits per sample
/* "data" sub-chunk */
uint8_t Subchunk2ID[4]; // "data" string
uint32_t Subchunk2Size; // Sampled data length
} wav_hdr;
// Function prototypes
int getFileSize(FILE* inFile);
int main(int argc, char* argv[])
{
wav_hdr wavHeader;
int headerSize = sizeof(wav_hdr), filelength = 0;
const char* filePath;
string input;
if (argc <= 1)
{
cout << "Input wave file name: ";
cin >> input;
cin.get();
filePath = input.c_str();
}
else
{
filePath = argv[1];
cout << "Input wave file name: " << filePath << endl;
}
FILE* wavFile = fopen(filePath, "r");
if (wavFile == nullptr)
{
fprintf(stderr, "Unable to open wave file: %s\n", filePath);
return 1;
}
//Read the header
size_t bytesRead = fread(&wavHeader, 1, headerSize, wavFile);
cout << "Header Read " << bytesRead << " bytes." << endl;
if (bytesRead > 0)
{
//Read the data
uint16_t bytesPerSample = wavHeader.bitsPerSample / 8; //Number of bytes per sample
uint64_t numSamples = wavHeader.ChunkSize / bytesPerSample; //How many samples are in the wav file?
static const uint16_t BUFFER_SIZE = 4096;
int8_t* buffer = new int8_t[BUFFER_SIZE];
while ((bytesRead = fread(buffer, sizeof buffer[0], BUFFER_SIZE / (sizeof buffer[0]), wavFile)) > 0)
{
* /** DO SOMETHING WITH THE WAVE DATA HERE **/ *
cout << "Read " << bytesRead << " bytes." << endl;
}
delete [] buffer;
buffer = nullptr;
filelength = getFileSize(wavFile);
cout << "File is :" << filelength << " bytes." << endl;
cout << "RIFF header :" << wavHeader.RIFF[0] << wavHeader.RIFF[1] << wavHeader.RIFF[2] << wavHeader.RIFF[3] << endl;
cout << "WAVE header :" << wavHeader.WAVE[0] << wavHeader.WAVE[1] << wavHeader.WAVE[2] << wavHeader.WAVE[3] << endl;
cout << "FMT :" << wavHeader.fmt[0] << wavHeader.fmt[1] << wavHeader.fmt[2] << wavHeader.fmt[3] << endl;
cout << "Data size :" << wavHeader.ChunkSize << endl;
// Display the sampling Rate from the header
cout << "Sampling Rate :" << wavHeader.SamplesPerSec << endl;
cout << "Number of bits used :" << wavHeader.bitsPerSample << endl;
cout << "Number of channels :" << wavHeader.NumOfChan << endl;
cout << "Number of bytes per second :" << wavHeader.bytesPerSec << endl;
cout << "Data length :" << wavHeader.Subchunk2Size << endl;
cout << "Audio Format :" << wavHeader.AudioFormat << endl;
// Audio format 1=PCM,6=mulaw,7=alaw, 257=IBM Mu-Law, 258=IBM A-Law, 259=ADPCM
cout << "Block align :" << wavHeader.blockAlign << endl;
cout << "Data string :" << wavHeader.Subchunk2ID[0] << wavHeader.Subchunk2ID[1] << wavHeader.Subchunk2ID[2] << wavHeader.Subchunk2ID[3] << endl;
}
fclose(wavFile);
return 0;
}
// find the file size
int getFileSize(FILE* inFile)
{
int fileSize = 0;
fseek(inFile, 0, SEEK_END);
fileSize = ftell(inFile);
fseek(inFile, 0, SEEK_SET);
return fileSize;
}
問題出在 /** 在這里使用波形數據 **/。 我不知道如何獲得樣本值。
我是 Java 程序員,不是 C++,但我經常處理這個問題。
PCM 數據按幀組織。 如果是 mono,little-endian,16 位,第一個字節是值的下半部分,第二個字節是上半部分,包括符號位。 Big-endian 將反轉字節。 如果它是立體聲的,則在繼續下一幀之前,完整的幀(我認為它是從左到右,但我不確定)會完整呈現。
我對所有顯示的代碼感到驚訝。 在 Java 中,對於編碼為有符號值的 PCM,以下內容就足夠了:
public short[] fromBufferToPCM(short[] audioPCM, byte[] buffer)
{
for (int i = 0, n = buffer.length; i < n; i += 2)
{
audioPCM[i] = (buffer[i] & 0xff) | (buffer[i + 1] << 8);
}
return audioBytes;
}
IDK 如何將其直接轉換為 C++,但我們只是將兩個字節 OR-ing 在一起,第二個字節首先向左移動 8 個位置。 純移位拾取符號位。 (我不記得為什么要包含 & 0xff ——我很久以前寫過這個並且它有效。)
很好奇為什么評論中有這么多答案而不是作為答案發布。 我認為評論是為了要求澄清 OP 的問題。
這樣的事情有效:
int8_t * tempBuffer = new int8_t [numSamples];
int index_for_loop = 0;
float INT16_FAC = pow(2,15) - 1;
double * outbuffer = new double [numSamples];
在 while 循環內:
for(int i = 0; i < BUFFER_SIZE; i += 2)
{
firstbyte = buffer[i];
secondbyte = buffer[i + 1];
result = firstbyte;
result = (result << 8) +secondbyte;
tempBuffer[index_for_loop] = result;
index_for_loop += 1;
}
然后通過執行以下操作在 -1 和 1 之間歸一化:
for(int i = 0; i <numSamples; i ++)
{
outbuffer[i] = float(tempBuffer[i]) / INT16_FAC;
}
規范化來自: sms-tools
注意:這適用於具有 44100 采樣率和 16 位分辨率的 mono 文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.