2

I've got a simple wav header reader i found online a long time ago, i've gotten back round to using it but it seems to replace around 1200 samples towards the end of the data chunk with a single random repeated number, eg -126800. At the end of the sample is expected silence so the number should be zero.

Here is the simple program:

void main() {
    WAV_HEADER* wav = loadWav(".\\audio\\test.wav");
    double sample_count = wav->SubChunk2Size * 8 / wav->BitsPerSample;

    printf("Sample count: %i\n", (int)sample_count);

    vector<int16_t> samples = vector<int16_t>();

    for (int i = 0; i < wav->SubChunk2Size; i++)
    {
        int val = ((wav->data[i] & 0xff) << 8) | (wav->data[i + 1] & 0xff);
        samples.push_back(val);
    }
    printf("done\n");
}

And here is the Wav reader:

typedef struct
{
    //riff
    uint32_t Chunk_ID;
    uint32_t ChunkSize;
    uint32_t Format;

    //fmt
    uint32_t SubChunk1ID;
    uint32_t SubChunk1Size;
    uint16_t AudioFormat;
    uint16_t NumberOfChanels;
    uint32_t SampleRate;
    uint32_t ByteRate;

    uint16_t BlockAlignment;
    uint16_t BitsPerSample;

    //data
    uint32_t SubChunk2ID;
    uint32_t SubChunk2Size;

    //Everything else is data. We note it's offset
    char data[];

} WAV_HEADER;
#pragma pack()

inline WAV_HEADER* loadWav(const char* filePath)
{
    long size;
    WAV_HEADER* header;
    void* buffer;

    FILE* file;

    fopen_s(&file,filePath, "r");
    assert(file);

    fseek(file, 0, SEEK_END);
    size = ftell(file);
    rewind(file);

    std::cout << "Size of file: " << size << std::endl;

    buffer = malloc(sizeof(char) * size);
    fread(buffer, 1, size, file);

    header = (WAV_HEADER*)buffer;

    //Assert that data is in correct memory location
    assert((header->data - (char*)header) == sizeof(WAV_HEADER));

    //Extra assert to make sure that the size of our header is actually 44 bytes
    assert((header->data - (char*)header) == 44);

    fclose(file);

    return header;
}

Im not sure what the problem is, i've confirmed that there is no meta data, nor is there a mis match between the numbers read from the header of the file and the actual file. Im assuming its a size/offset misallignment on my side, but i cannot see it. Any help welcomed. Sulkyoptimism

11
  • You should be opening the file in binary mode and checking the return value from fread to make sure you read the amount of data you expected. Commented Jan 7, 2022 at 21:15
  • 1
    header = (WAV_HEADER*)buffer; is not safe. Any object can be viewed as an array of char, but the reverse isn't guaranteed to be true. See What is the strict aliasing rule? for details. That said, because memory is typically allocated on nice 32 and 64 bit boundaries, you'll get away with this most of the time. Commented Jan 7, 2022 at 21:44
  • 1
    @user4581301 let's not go language-lawyer on this, because of course you're right, once you load something from anywhere it's impossible to know for the program whether it's valid data. However, file io semantics as defined by the standard guarantee that you can, on the same platform, write a WAV_HEADER to a file, load it from that file into a region of the same alignment strictness, and get a similar type, so that it's not undefined behaviour what happens! Commented Jan 7, 2022 at 22:01
  • 1
    @MarcusMüller: Instead of casting the pointer, you call memcpy to transfer the data to a variable of the correct structure type. Then you meet all alignment and aliasing requirements and don't have type punning problems. (Or just read it into the correct variable in the first place)
    – Ben Voigt
    Commented Nov 2, 2023 at 17:46

2 Answers 2

2

WAV is just a container for different audio sample formats.

You're making assumptions on a wav file that would have been OK on Windows 3.11 :) These don't hold in 2021.

Instead of rolling your own Wav file reader, simply use one of the available libraries. I personally have good experiences using libsndfile, which has been around roughly forever, is very slim, can deal with all prevalent WAV file formats, and with a lot of other file formats as well, unless you disable that.

This looks like a windows program (one notices by the fact you're using very WIN32API style capital struct names – that's a bit oldschool); so, you can download libsndfile's installer from the github releases and directly use it in your visual studio (another blind guess).

6
  • While i appreciate this answer and this is something i will absolutely do, im still just trying to get this working as a learning exercise. I get that im using an old C file for this but the wav im using is pretty standard as far as im aware. Other programs confirm the number of bytes and samples are the same as the numbers read here. Commented Jan 8, 2022 at 0:52
  • @Sulkyoptimism your WAV file doesn't fit the structure you're assuming. There's no way to fix this other than to write a correct WAV parser and accomodate for the data your WAV is actually containing. "pretty standard": as said, this is 30 (!) years old stuff, "pretty standard" has a changing meaning over time. Commented Jan 8, 2022 at 11:20
  • Okay well if thats the case is there any type of documentation that would outline the differences between common wav formats found? As i've not been able to find any, and im assuming you're answering from experience as you've not linked any resources? Commented Jan 9, 2022 at 12:53
  • I've linked to libsndfile's source code! github.com/libsndfile/libsndfile/blob/master/src/wav.c tells you how they parse a wav file. It's not totally different to what you do, but there's way more decisions to be made. My experience is that I worked on a piece of software that was used to open wav files, often such generated by appropriate matlab functions, but at some point we realized we stopped being able to open scipy-written wav files. Friend of mine had written the original wavfile source, and it worked beautifully – for its very limited set of compatible .wavs. Commented Jan 9, 2022 at 13:04
  • 1
    Thats perfect thank you, i'll try reading this and then eventually use the sndfilelib anyway but i like to know this stuff. And i want to know the answer to the question, at a lower level. Commented Jan 9, 2022 at 13:18
0

Apple (macOS and iOS) software often does not create WAVE/RIFF files with just a canonical Microsoft 44-byte header at the beginning. Those Wave files can instead can use a longer header followed by a padding block.

So you need to use the full WAVE RIFF format parsing specification instead of just reading from a fixed size 44 byte struct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.