Sign in to follow this  
synth_cat

bad results reading .wav file

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;
	fopen_s(&sfile,filename,"rb");
	if(sfile)
	{
		//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
		fread(id,sizeof(BYTE),4,sfile);
		//test ID
		if(1)
		{
			//get size
			fread(&size,sizeof(DWORD),1,sfile);
			//get second string
			fread(&id,sizeof(BYTE),4,sfile);
		    //test second stinrg
			if(1)
			{
				//get third string
				fread(&id,sizeof(BYTE),4,sfile);
				fread(&size_of_fmt_chunk,sizeof(DWORD),1,sfile);
				fread(&format_tag,sizeof(short),1,sfile);
				fread(&num_channels,sizeof(short),1,sfile);
				fread(&sample_rate,sizeof(DWORD),1,sfile);
				fread(&bytes_ps,sizeof(DWORD),1,sfile);
				fread(&block_align,sizeof(short),1,sfile);
				fread(&bits_ps,sizeof(short),1,sfile);
				fread(&id,sizeof(BYTE),4,sfile);

				log->RecordEvent("  ** num_channels ");
				log->RecordInt((int)num_channels);
				log->RecordEvent("  ** sample rate ");
				log->RecordInt((int)sample_rate);
				log->RecordEvent("  ** bytes per second ");
				log->RecordInt((int)bytes_ps);
				log->RecordEvent("  ** bits per second ");
				log->RecordInt((int)bits_ps);

				//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 ");
				log->RecordInt(sound_chunk_size);
				
			}
		}

		//close the file
		fclose(sfile);

                


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!

-synth_cat

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this