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

## 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)
{
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
//test ID
if(1)
{
//get size
//get second string
//test second stinrg
if(1)
{
//get third string

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;

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 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 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

1. 1
2. 2
Rutin
24
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 17
• 11
• 11
• 9
• ### Forum Statistics

• Total Topics
631760
• Total Posts
3002173
×