简体   繁体   English

wav 文件中的字节序

[英]Endianness in wav files

I have tried to make a simple wav writer.我试图制作一个简单的 wav 作家。 I wanted to do this so that I could read in a wav file (using a pre-existing wav reader), resample the audio data then write the resampled data to another wav file.我想这样做,以便我可以读取 wav 文件(使用预先存在的 wav 阅读器),重新采样音频数据,然后将重新采样的数据写入另一个 wav 文件。 Input files could be 16 bitsPerSample or 32 bitsPerSample and I wanted to save the resampled audio with the same number of bitsPerSample.输入文件可以是 16 bitsPerSample 或 32 bitsPerSample,我想用相同数量的 bitsPerSample 保存重新采样的音频。

The writer is working but there a couple of things I don't understand to do with endianness and I was hoping someone may be able to help me?作者正在工作,但有几件事我不明白与字节序有关,我希望有人能够帮助我?

I previously had no experience of reading or writing binary files.我以前没有读写二进制文件的经验。 I began by looking up the wav file format online and tried to write the data following the correct format.我首先在网上查找 wav 文件格式,并尝试按照正确的格式写入数据。 At first the writing wasn't working but I then found out that wav files are little-endian and it was trying to make my file writer consistent with this that brought up the majority of my problems.起初,写入不起作用,但后来我发现 wav 文件是小端的,它试图使我的文件编写器与此一致,从而引起了我的大部分问题。 I have got the wav writer to work now (by way of a test whereby I read in a wav file and checked I could write the unsampled audio and reproduce the exact same file) however there are a couple of points I am still unsure on to do with endianness and I was hoping someone may be able to help me?我现在已经让 wav 编写器开始工作了(通过一个测试,我读入了一个 wav 文件并检查我可以编写未采样的音频并重现完全相同的文件)但是有几点我仍然不确定用字节顺序做,我希望有人可以帮助我?

Assuming the relevant variables have already been set here is my code for the wav writer:假设相关变量已经设置在这里是我的 wav 编写器代码:

// Write RIFF header
out_stream.write(chunkID.c_str(),4);
out_stream.write((char*)&chunkSize,4);
out_stream.write(format.c_str());

// Write format chunk
out_stream.write(subchunk1ID.c_str(),4);
out_stream.write((char*)&subchunk1Size,4);
out_stream.write((char*)&audioFormat,2);
out_stream.write((char*)&numOfChannels,2);
out_stream.write((char*)&sampleRate,4);
out_stream.write((char*)&byteRate,4);
out_stream.write((char*)&blockAlign,2);
out_stream.write((char*)&bitsPerSample,2);

// Write data chunk
out_stream.write(subchunk2ID.c_str(),4);
out_stream.write((char*)&subchunk2Size,4);

// Variables for writing 16 bitsPerSample data
std::vector<short> soundDataShort;
soundDataShort.resize(numSamples);
char theSoundDataBytes [2];

// soundData samples are written as shorts if bitsPerSample=16 and floats if bitsPerSample=32
switch( bitsPerSample )
{ 
    case (16):
        // cast each of the soundData samples from floats to shorts
        // then save the samples in little-endian form (requires reversal of byte-order of the short variable)
        for (int sample=0; sample < numSamples; sample++)
    {
        soundDataShort[sample] = static_cast<short>(soundData[sample]);
            theSoundDataBytes[0] = (soundDataShort[sample]) & 0xFF;
            theSoundDataBytes[1] = (soundDataShort[sample] >> 8) & 0xFF;
            out_stream.write(theSoundDataBytes,2);          
    }   
    break;

    case (32):
        // save the soundData samples in binary form (does not require change to byte order for floats)
        out_stream.write((char*)&soundData[0],numSamples);
}

The questions that I have are:我的问题是:

  1. In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't?在 soundData 向量中,为什么短裤向量的字节序很重要,而浮点数向量不重要? In my code I have reversed the byte order of the shorts but not the floats.在我的代码中,我颠倒了短裤的字节顺序,但没有颠倒浮点数。

  2. Originally I tried to write the shorts without reversing the byte order.最初我试图在不颠倒字节顺序的情况下编写短裤。 When I wrote the file it ended up being half the size it should have been (ie half the audio data was missing, but the half that was there sounded correct), why would this be?当我编写文件时,它最终只有它应该大小的一半(即丢失了一半的音频数据,但那里的一半听起来是正确的),为什么会这样?

  3. I have not reversed the byte order of the shorts and longs in the other single variables which are essentially all the other fields that make up the wav file eg sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file.我没有颠倒其他单个变量中短和长的字节顺序,这些变量基本上是构成 wav 文件的所有其他字段,例如 sampleRate、numOfChannels 等,但这似乎不会影响 wav 文件的播放。 Is this just because media players do not use these fields (and hence I can't tell that I have got them wrong) or is it because the byte order of these variables does not matter?这仅仅是因为媒体播放器不使用这些字段(因此我不能说我弄错了)还是因为这些变量的字节顺序无关紧要?

In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't?在 soundData 向量中,为什么短裤向量的字节序很重要,而浮点数向量不重要? In my code I have reversed the byte order of the shorts but not the floats.在我的代码中,我颠倒了短裤的字节顺序,但没有颠倒浮点数。

Actually, if you take a closer look at your code, you will see that you are not reversing the endianness of your shorts at all.实际上,如果您仔细查看您的代码,您会发现您根本没有反转短裤的字节序。 Nor do you need to, on Intel CPUs (or on any other low-endian CPU).在 Intel CPU(或任何其他低端 CPU)上,您也不需要这样做。

Originally I tried to write the shorts without reversing the byte order.最初我试图在不颠倒字节顺序的情况下编写短裤。 When I wrote the file it ended up being half the size it should have been (ie half the audio data was missing, but the half that was there sounded correct), why would this be?当我编写文件时,它最终只有它应该大小的一半(即丢失了一半的音频数据,但那里的一半听起来是正确的),为什么会这样?

I have no idea without seeing the code but I suspect that some other factor was in play.我不知道没有看到代码,但我怀疑还有其他一些因素在起作用。

I have not reversed the byte order of the shorts and longs in the other single variables which are essentially all the other fields that make up the wav file eg sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file.我没有颠倒其他单个变量中短和长的字节顺序,这些变量基本上是构成 wav 文件的所有其他字段,例如 sampleRate、numOfChannels 等,但这似乎不会影响 wav 文件的播放。 Is this just because media players do not use these fields (and hence I can't tell that I have got them wrong) or is it because the byte order of these variables does not matter?这仅仅是因为媒体播放器不使用这些字段(因此我不能说我弄错了)还是因为这些变量的字节顺序无关紧要?

These fields are in fact very important and must also be little-endian, but, as we have seen, you don't need to swap those either.这些字段实际上非常重要,也必须是小端的,但是,正如我们所见,您也不需要交换它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM