Jump to content
  • Advertisement
Sign in to follow this  
Ilankt

Problem loading wav files with DirectSound

This topic is 4812 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

I'm using the code from the (in)famous book of Jim Adams to load wave files, the wav is loaded and played, but just for a very short time. the code:
	struct sWaveHeader //from Jim Adam's book, the .wav file header
	{
		char RiffSig[4];
		long WaveFormChucnkSize;
		char WaveSig[4];
		char FormatSig[4];
		long FormatChunkSize;
		short FormatTag;
		short Channels;
		long SampleRate;
		long BytesPerSecond;
		short BlockAlign;
		short BitsPerSample;
		char DataSig[4];
		long DataSize;
	};

HRESULT SoundFile::loadFromWave(const char* file,const LPDIRECTSOUND8 DirectSound)
{
	FILE *f = fopen(file,"rb");
	
	HRESULT hr = S_OK;
	if (!f)
	{
		//file do not exist;
		return S_FALSE;
	}
	fseek(f,0,SEEK_SET);
	fread(&m_WavHeader,1,sizeof(sWaveHeader),f);

	//fill wave format
    ZeroMemory(&m_wfex,sizeof(WAVEFORMATEX));
	m_wfex.wFormatTag = WAVE_FORMAT_PCM;
	m_wfex.nChannels = m_WavHeader.Channels;
	m_wfex.nSamplesPerSec = m_WavHeader.SampleRate;
	m_wfex.wBitsPerSample = m_WavHeader.BitsPerSample;
	m_wfex.nBlockAlign = m_wfex.wBitsPerSample/8 * m_wfex.nChannels;
	m_wfex.nAvgBytesPerSec = m_wfex.nSamplesPerSec * m_wfex.nBlockAlign;

	//create the sound buffer
	ZeroMemory(&m_dsbd,sizeof(DSBUFFERDESC));
	m_dsbd.dwSize = sizeof(DSBUFFERDESC);
	m_dsbd.dwFlags = DSBCAPS_CTRLVOLUME | DSBCAPS_CTRLPAN; //control pan + volume
	m_dsbd.dwBufferBytes = m_WavHeader.DataSize;
	m_dsbd.lpwfxFormat = &m_wfex;
	hr = DirectSound->CreateSoundBuffer(&m_dsbd,&m_pBuffer,NULL);
	if (FAILED(hr))
	{
		fclose(f);
		return hr;
	}
    
	//fill the buffer with wav data
	BYTE *ptr1,*ptr2;
	DWORD size1,size2;
	fseek(f,sizeof(sWaveHeader),SEEK_SET);
	hr = m_pBuffer->Lock(0,m_WavHeader.DataSize,(void**)&ptr1,&size1,(void**)&ptr2,&size2,0);
	if (FAILED(hr))
	{
		fclose(f);
		return hr;
	}
	fread(ptr1,1,size1,f);
	if (ptr2)
	{
		fread(ptr2,1,size2,f);
	}
	hr = m_pBuffer->Unlock(ptr1,size1,ptr2,size2);
	if (FAILED(hr))
	{
		fclose(f);
		return hr;
	}
	fclose(f);
	return S_OK;
}

//this is how I play the sound file:
HRESULT SoundFile::play(const bool looping)
{
	m_pBuffer->SetCurrentPosition(0);
	if (looping)
	{
        return m_pBuffer->Play(0,0,DSBPLAY_LOOPING);
	}
	else
	{
		return m_pBuffer->Play(0,0,0);
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Looks alright to me, you might want to look at your streaming code.

#pragma pack(1)
typedef struct sWaveHeader
{
char RiffSig[4];
long WaveformChunkSize;
char WaveSig[4];
char FormatSig[4];
long FormatChunkSize;
short FormatTag;
short Channels;
long SampleRate;
long BytesPerSec;
short BlockAlign;
short BitsPerSample;
char DataSig[4];
long DataSize;
} sWaveHeader;
#pragma pack()


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!