Endianness in wav filesIs the endianness of format params guaranteed in RIFF WAV files?Why can templates only be implemented in the header file?How to convert a .WAV audio data sample into an double type?Finding number of samples in .wav file and Hex EditoriPhone SDK: Reversing the playback of a wav fileWav file recorded using AudioRecord sounds speeded up and skips frames when replayingReversing a .wav fileChanging samplerate and byterate of a wav file without encountering problems opening the file?Struct C/C++ - Reading WAV Header 32 Bit Float - byte offset wrongInterpretation of .wav samplesWhy saving of 32-bit sample rate, PCM float byte[] to wav file creates distortion/crackling?

What are the children of two Muggle-borns called?

How soon after takeoff can you recline your airplane seat?

What was the first science fiction or fantasy multiple choice book?

"I am [the / an] owner of a bookstore"?

Delete all files from a folder using a bat that match a certain pattern in Windows 10

Dynamic Sql Query - how to add an int to the code?

What does 5d4 x 10 gp mean?

Rear derailleur got caught in the spokes, what could be a root cause

Deducing Lambda Capture Types

Can I take Amul cottage cheese from India to Netherlands?

Can US Supreme Court justices / judges be "rotated" out against their will?

Fast method to cut/shred glue stick into small pieces

How do I present a future free of gender stereotypes without being jarring or overpowering the narrative?

Is this house-rule removing the increased effect of cantrips at higher character levels balanced?

What happens if a caster is surprised while casting a spell with a long casting time?

Robots in a spaceship

What verb for taking advantage fits in "I don't want to ________ on the friendship"?

Can I submit a paper to two or more journals at the same time?

Why are examinees often not allowed to leave during the start and end of an exam?

A* pathfinding algorithm too slow

Russian equivalents of 能骗就骗 (if you can cheat, then cheat)

How can an inexperienced GM keep a game fun for experienced players?

Hard for me to understand one tip written in "The as-if rule" of cppreference

Is it theoretically possible to hack printer using scanner tray?



Endianness in wav files


Is the endianness of format params guaranteed in RIFF WAV files?Why can templates only be implemented in the header file?How to convert a .WAV audio data sample into an double type?Finding number of samples in .wav file and Hex EditoriPhone SDK: Reversing the playback of a wav fileWav file recorded using AudioRecord sounds speeded up and skips frames when replayingReversing a .wav fileChanging samplerate and byterate of a wav file without encountering problems opening the file?Struct C/C++ - Reading WAV Header 32 Bit Float - byte offset wrongInterpretation of .wav samplesWhy saving of 32-bit sample rate, PCM float byte[] to wav file creates distortion/crackling?













1















I have tried to make a simple wav writer. 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. Input files could be 16 bitsPerSample or 32 bitsPerSample and I wanted to save the resampled audio with the same number of 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. 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.
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?



Assuming the relevant variables have already been set here is my code for the wav writer:



// 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? 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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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?










share|improve this question


























    1















    I have tried to make a simple wav writer. 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. Input files could be 16 bitsPerSample or 32 bitsPerSample and I wanted to save the resampled audio with the same number of 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. 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.
    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?



    Assuming the relevant variables have already been set here is my code for the wav writer:



    // 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? 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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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?










    share|improve this question
























      1












      1








      1








      I have tried to make a simple wav writer. 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. Input files could be 16 bitsPerSample or 32 bitsPerSample and I wanted to save the resampled audio with the same number of 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. 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.
      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?



      Assuming the relevant variables have already been set here is my code for the wav writer:



      // 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? 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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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?










      share|improve this question














      I have tried to make a simple wav writer. 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. Input files could be 16 bitsPerSample or 32 bitsPerSample and I wanted to save the resampled audio with the same number of 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. 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.
      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?



      Assuming the relevant variables have already been set here is my code for the wav writer:



      // 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? 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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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?







      c++ audio wav






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Mar 25 at 15:47









      nats117nats117

      84 bronze badges




      84 bronze badges




















          1 Answer
          1






          active

          oldest

          votes


















          1















          In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't? 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).




          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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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.






          share|improve this answer























          • FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

            – Patrick Roberts
            Mar 25 at 16:12












          • @PatrickRoberts OK, but RIFX files are vanishingly rare.

            – Paul Sanders
            Mar 25 at 20:28











          • Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

            – nats117
            Mar 25 at 21:23











          • Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

            – Paul Sanders
            Mar 25 at 23:27










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55341596%2fendianness-in-wav-files%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1















          In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't? 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).




          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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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.






          share|improve this answer























          • FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

            – Patrick Roberts
            Mar 25 at 16:12












          • @PatrickRoberts OK, but RIFX files are vanishingly rare.

            – Paul Sanders
            Mar 25 at 20:28











          • Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

            – nats117
            Mar 25 at 21:23











          • Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

            – Paul Sanders
            Mar 25 at 23:27















          1















          In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't? 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).




          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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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.






          share|improve this answer























          • FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

            – Patrick Roberts
            Mar 25 at 16:12












          • @PatrickRoberts OK, but RIFX files are vanishingly rare.

            – Paul Sanders
            Mar 25 at 20:28











          • Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

            – nats117
            Mar 25 at 21:23











          • Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

            – Paul Sanders
            Mar 25 at 23:27













          1












          1








          1








          In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't? 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).




          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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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.






          share|improve this answer














          In the soundData vector why does the endianness of a vector of shorts matter but the vector of floats doesn't? 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).




          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 (i.e. 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 e.g. sampleRate, numOfChannels etc but this does not seem to affect the playing of the wav file. 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.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 25 at 16:01









          Paul SandersPaul Sanders

          6,6622 gold badges6 silver badges21 bronze badges




          6,6622 gold badges6 silver badges21 bronze badges












          • FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

            – Patrick Roberts
            Mar 25 at 16:12












          • @PatrickRoberts OK, but RIFX files are vanishingly rare.

            – Paul Sanders
            Mar 25 at 20:28











          • Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

            – nats117
            Mar 25 at 21:23











          • Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

            – Paul Sanders
            Mar 25 at 23:27

















          • FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

            – Patrick Roberts
            Mar 25 at 16:12












          • @PatrickRoberts OK, but RIFX files are vanishingly rare.

            – Paul Sanders
            Mar 25 at 20:28











          • Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

            – nats117
            Mar 25 at 21:23











          • Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

            – Paul Sanders
            Mar 25 at 23:27
















          FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

          – Patrick Roberts
          Mar 25 at 16:12






          FYI for WAV file format, a RIFF header requires little endian byte order, and a RIFX header requires big endian byte order. See this.

          – Patrick Roberts
          Mar 25 at 16:12














          @PatrickRoberts OK, but RIFX files are vanishingly rare.

          – Paul Sanders
          Mar 25 at 20:28





          @PatrickRoberts OK, but RIFX files are vanishingly rare.

          – Paul Sanders
          Mar 25 at 20:28













          Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

          – nats117
          Mar 25 at 21:23





          Thanks for the answer, yes upon looking at the code again I wasn't changing the endianness of the shorts, didn't realise that for some reason. I had thought that my CPU was big endian but it makes sense now if the CPU is little-endian. Are floats the same both ways round i.e. big endian and little endian? I thought I read somewhere that floats are the only variable type where endianness doesn't matter.

          – nats117
          Mar 25 at 21:23













          Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

          – Paul Sanders
          Mar 25 at 23:27





          Yes, (IEEE 754) floats can be either big endian or little endian, and RIFF WAV files use the little endian format.

          – Paul Sanders
          Mar 25 at 23:27






          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







          Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55341596%2fendianness-in-wav-files%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

          Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

          Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript