#pragma once #include // portable ;) namespace dust { // WAV-file loading is split into two parts. // // Header-data is stored into a struct. // Raw sampledata is decoded separately. // This is just a collection of whatever data // we want to load from the chunk structure struct WaveHeader { // wave data formats enum Format { WF_UNKNOWN, WF_PCM8, WF_PCM16, WF_PCM24, WF_PCM32, WF_FLOAT }; Format format; // datachunk.. unsigned dataoffset; // offset of data into file unsigned datasize; bool loop = false; unsigned loop_start; unsigned loop_end; // then wav-format details.. // this is currently raw "as-is" byte-packed data struct { unsigned short format, channels; unsigned samplerate, bytesPerSecond; unsigned short sampleBytes, sampleBits; } fmt; // calculated per-channel sample-count unsigned nsamples() { return datasize / fmt.sampleBytes; } }; // Parses the RIFF structure into the header struct. // Seeks to the beginning automatically. // // returns 0 on success, error message on failure const char * waveParse(FILE *, WaveHeader *); // decode data into a buffer.. buffer channel-count is specified // and the loaded count is smaller of buffer and file channels // offset is into the file only (buffer should be offset by caller) void waveDecode(FILE *, WaveHeader *, float * out, unsigned channels, unsigned nsamples, unsigned offset = 0); };