Skip to main content
GameDev.net gamedev.net
🔒 Locked

WAV Format

Started by Tipotas688 Sep 13, 2010 at 12:48 PM 5 replies 1.3k views
Original Post
Tipotas688
Tipotas688
Hello, I want to better understand the information of the WAV header file
in order to read and write any kind of uncompressed wav, whether it is stereo or mono with chunk size 16 or otherwise.

This article: http://www.gamedev.net/reference/articles/article709.asp didn't help much on this.

I ll add an example I don't understand:

fread(Data, BitsPerSample/8, Subchunk2Size/(BitsPerSample/8), fhandle); // Reading raw audio data


[Edited by - Tipotas688 on September 13, 2010 1:16:59 PM]
nobodynews
nobodynews
Quote:
Original post by Tipotas688
I ll add an example I don't understand:

*** Source Snippet Removed ***
First argument is easy. Just where to put the data. The next two are easy if you understand unit analysis. The unit in the second argument BitsPerSample is bits/sample. Say let's say BitsPerSample contains the value of 1024 bits/sample. You can't load bits so we need to convert this to bytes/samples. The conversion between bits and bytes is 1/8. So by doing (1024 bits/sample) / (8 bits / bytes) = 128 bytes / sample. The next argument is supposed to reduce down to the number of samples to load which means its units must be bytes. 12800 bytes / (128 bytes / sample) = 10 samples.

Then fread multiplies the 2nd argument by the 3rd argument to get the number of bytes to read (128 bytes / sample) * 10 samples = 1280 bytes.

Why, you might ask, does your code do these conversions instead of just reading the number of bytes directly using Subchunk2Size? The code probably relies on integer truncation for some reason. If Subchunk2Size were 12799 then fread would read 1152 bytes instead of 1280. Without seeing more code I couldn't tell you why the writer did it this way and not another.
C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) ,
Tipotas688
Tipotas688
I got this part from this link: http://www.codeguru.com/forum/showthread.php?t=460547

Thing is this guy does it only for 16 bit, plus when I use his code I get a wav file sized 1GB and I do wonder why he's doing the things he's doing that way. Since I didn't find anything else to work with, I couldn't understand what's the proper multiplication depending on number of channels etc. I did know about the bitrate/8 although the way he does it, its certainly wrong :S
Ron AF Greve
Ron AF Greve
Why not read the the wav file format:

[link]http://www.wotsit.org/list.asp?page=4&fc=12&search=&al=[/link]

Ron AF Greve
Tipotas688
Tipotas688
Thanks Ron I had forgotten about that website!

I am trying to do a small test with that block of code but I am getting wrong a couple of things.

1. When I use fwrite to recreate the wav, without the forloop below I get it as 1GB sized, I think this has to do with the Subchunk2Size/(BitsPerSample/8)
2. When I use the for loop below the length of the audio gets doubled when it should raise the volume, shouldn't it

short *Data;Data = new short[Subchunk2Size/(BitsPerSample/8)]; // Create an element for every sample		fread(Data, BitsPerSample/8, Subchunk2Size/(BitsPerSample/8), fhandle); // Reading raw audio datafor(int i=0;i<Subchunk2Size/(BitsPerSample/8);i++){		Data *=2;}


[Edited by - Tipotas688 on September 15, 2010 10:11:07 AM]
Buckeye
Buckeye
Quote:
short *Data;
Data = new short[Subchunk2Size/(BitsPerSample/8)]; // Create an element for every sample

You're creating 2-byte (short) storage for each byte. If you're reading in a byte, allocate a byte, rather than a short.

Also, PCM data audio data has an offset. That is, zero-volume (the zero crossing point for the wave) depends on the data size. For byte data (for instance), although the byte value read in ranges from 0 to 255, the zero crossing is value 128.

This article explains the size of the data and how the audio values depend on the size of the samples.
Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly. You don't forget how to play when you grow old; you grow old when you forget how to play.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.