Jump to content
  • Advertisement
Sign in to follow this  

bad results reading .wav file

This topic is 4391 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello everyone - I am currently working on my function to load a .wav and copy its contents into a DirectSound buffer. The function currently does nothing except look at the file. I use a Log object which records information in a txt file for debugging use. Several calls are made to this object during the execution of the function. Anyway, here's the code:

bool SynthSound::LoadSound(const char* filename, LPDIRECTSOUNDBUFFER& sb)
	//load a file
	FILE* sfile=0;
		//record that you opened the file
		log->RecordEvent("** DS open sound file success\n");

		BYTE id[4];
		DWORD size;
		DWORD size_of_fmt_chunk;

		//pointer to sound chunk
		BYTE* sound_buff=0;

		short format_tag=0;
		short num_channels=0;
		short block_align,bits_ps=0;
		DWORD sample_rate=0;
		DWORD bytes_ps=0;

		//get ID
		//test ID
			//get size
			//get second string
		    //test second stinrg
				//get third string

				log->RecordEvent("  ** num_channels ");
				log->RecordEvent("  ** sample rate ");
				log->RecordEvent("  ** bytes per second ");
				log->RecordEvent("  ** bits per second ");

				//get the whole thing into your chunk
				ULONG sound_chunk_size=0;
				fread(&sound_chunk_size,sizeof(unsigned long),1,sfile);

				log->RecordEvent("  ** reading size of chunk ");

		//close the file


Anyway, I attempted this function on an 8 bps mono wav file which is about five seconds long. Here is what my log txt file says: ** DS open sound file success ** num_channels 1 ** sample rate 11025 ** bytes per second 11025 ** bits per second 8 ** reading size of chunk 291939 Everything seems wrong _except_ for the final value which is supposed to represent the size (in bytes) of the raw sound data. It is huge, much bigger than it should be (about 55,125). I know this is a somewhat vague question, but does anyone know what I did wrong in the code? I've examined the "load wave file" demonstrations around the site (in fact, I pretty much wrote this whole function using one of the tutorials) but I can't find a difference which would explain why I get such odd results. Thanks for any help! -synth_cat

Share this post

Link to post
Share on other sites
I couldn't spot anything wrong with your loading routine. I'll check again tomorrow when I get home. As I can't give you a solution, here are some random pointers that could potentially help you to solve your problem.

WAVE RIFF doesn't guarantee order of the chunks. You are assuming order of chunks you are loading. You also assume that file doesn not contain any excess chunks such as the fact chunk that for instance sndrec32.exe writes into wave files.

You are assuming 'fmt ' chunk size of 18 bytes. This might not be the case as that chunk can contain additional data depending on format (for instance if wave data is compressed). Verify the size of format chunk.

Ideally you really want to make your loading routine to work in a way that it scans throught the file and finds what ever chunks you need ('fmt ' 'data') and then reads them. Don't assume too much. I hope this helps [smile]

Share this post

Link to post
Share on other sites
Thanks for the tips!

However, I thought that this routine was garunteed to work on a "standard" wave file. I realize that .wav is only one version of a rather extensive and flexible file type, but should that really matter as long as I only ever tell this function to only load files that I already know are "normal" .wav's?

An idea: could it be possible that the "sound_chunk_size" value is supposed to indicate a number of _bits_ and not bytes? That would explain the problem (even though 291939 isn't really divisible by 8).

Any help appreciated!


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!