Skip to content

Instantly share code, notes, and snippets.

@Jon-Schneider
Created November 29, 2016 15:59
Show Gist options
  • Save Jon-Schneider/8b7c53d27a7a13346a643dac9c19d34f to your computer and use it in GitHub Desktop.
Save Jon-Schneider/8b7c53d27a7a13346a643dac9c19d34f to your computer and use it in GitHub Desktop.
C Wav Header Struct
// WAV header spec information:
//https://web.archive.org/web/20140327141505/https://ccrma.stanford.edu/courses/422/projects/WaveFormat/
//http://www.topherlee.com/software/pcm-tut-wavformat.html
typedef struct wav_header {
// RIFF Header
char riff_header[4]; // Contains "RIFF"
int wav_size; // Size of the wav portion of the file, which follows the first 8 bytes. File size - 8
char wave_header[4]; // Contains "WAVE"
// Format Header
char fmt_header[4]; // Contains "fmt " (includes trailing space)
int fmt_chunk_size; // Should be 16 for PCM
short audio_format; // Should be 1 for PCM. 3 for IEEE Float
short num_channels;
int sample_rate;
int byte_rate; // Number of bytes per second. sample_rate * num_channels * Bytes Per Sample
short sample_alignment; // num_channels * Bytes Per Sample
short bit_depth; // Number of bits per sample
// Data
char data_header[4]; // Contains "data"
int data_bytes; // Number of bytes in data. Number of samples * num_channels * sample byte size
// uint8_t bytes[]; // Remainder of wave file is bytes
} wav_header;
@Kasslim
Copy link

Kasslim commented Sep 21, 2020

The descriptions are helpful. But the datatypes are just plain wrong. Use <stdint.h> definitions since int may be 2 bytes on some platforms and make to use the unsigned variants unlike the ones you're using right now. If you're planning to stamp a byte array into this, also make sure the struct is __attribute__((packed))

The current version may work, but it may not, correct me if I'm wrong. I hope this saves someone some headaches.

@Jon-Schneider
Copy link
Author

@Kasslim You're spot on. I've come a long way since I wrote this. This was written for OSX/iOS where ints are guaranteed to be 32 bit and wouldn't necessarily work with other platforms or compilers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment