Web Excursions 2021-05-28

Have you ever hurt yourself from your own code?

[The author was trying to remove silence from a bunch of WAV files.]

  • A WAV file's data is usually raw PCM audio [1], meaning every "value" in the file represents the amplitude at that point in time.

    • suggest if we have true silence, and not just white noise, then this silence should be all zeros

  • How to read WAV files

    • Diagram of a canonical WAV file

    • just has a header of 44 bytes followed by the "data".

    • I would just skip the first 44 bytes, remove all of the consecutive zeros in the beginning of the data section, and output the rest as is.

  • The code [version 1]

    • iterated over the file one byte at a time, skipping the first 44 bytes, and counting the number of consecutive zeros.

    • Once it hit a non-zero it would stop reading the file, save that index, and then start reading the file again from the beginning.

    • output the bytes one by one to standard output, and it would skip the bytes up to the index (except the header).

  • [The code outputs audio file that is extremely loud]

  • The mistake

    • The code is including an extra zero byte in the beginning of the data section

    • Each sample is contained in an integer i. The size of i is the smallest number of bytes required to contain the specified sample size. The least significant byte is stored first.

    • because it was 16 bits per sample, I was essentially shifting all of the samples, and causing the least significant byte to be read as the most significant byte.

MDN Plus

MDN Plus builds on top of your much-loved core content, providing constantly-updated guides to highly-requested topics, helping you keep your knowledge fresh and your skills sharp. In addition, MDN Plus includes tools to make MDN more powerful for you, creating a more personalized experience.