Skip to main content
gamedev.net
🔒 Locked

Sound Parse Problems

Started by Darkpatch 0 replies 250+ views
Original Post
Darkpatch
Darkpatch
I am currently have problems creating a small wave file wrapper. I apologize ahead of time for the length. Here what I know, and it might be my problem. I know the m_wFX waveformatex struct does get filled. It crashes when it reaches CreateSoundBuffer. I don''t know if m_Sound is being created correctly.
  
#include <windows.h> 
#include <mmreg.h>
//#include <mmsystem.h>
#include <dsound.h>
#include "cs180tracelib.h"



///////////////////////////////////////////////////////////

//	Class:  soundFX

//	Loads and Plays Sound FXs

//	Owner: Ben Horbul

////////////////////////////////////////////////////////////

class Soundfx
{

private:
	LPCTSTR			m_name;	// The sound file that the soundfx loads.

	IDirectSoundBuffer *	m_Sound;
	DSBUFFERDESC		m_dsbd;
		
	DWORD			currentPlayPosition;
	DWORD			lastPlayPosition;
	DWORD			cbFirstSection;
	VOID *			pvFirstSection;			
	DWORD			m_secondOSound;	// Length in bytes of 1 second of sound.

	BYTE *			m_pbData;
	DWORD			cbRemaining;
	DWORD			cbSubchunk;
	FOURCC			fourccSubchunk;
	WAVEFORMATEX	*	m_wFX;

public:

/////////////////////////////////////////////

// Constructor

//////////////////////////////////////////////

	Soundfx(IDirectSound * IDSound, LPCTSTR name)
	{	
		m_name = name;
		HRESULT			hr;
		HANDLE		waveFile;
		DWORD		waveSize;
		DWORD		bufferSize;
		HANDLE		hHeap;
		BYTE *          waveBuffer;
		DWORD		tempSize;
		tempSize = 0;
		
		Trace("Parsing Wav File... ");

		DWORD readResult;
		waveFile = CreateFile("ap2.wav", GENERIC_READ,0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);
		//waveFile = CreateFile(name, GENERIC_READ,0,NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,NULL);

		
		waveSize = SetFilePointer(waveFile,	0,  NULL,FILE_END);
		SetFilePointer(waveFile,0,NULL,FILE_BEGIN);		
		hHeap = GetProcessHeap();
		// Allocates memory the size of the wav file.

		waveBuffer = (BYTE *)HeapAlloc(hHeap, HEAP_ZERO_MEMORY, waveSize);  
		
		ReadFile(waveFile, waveBuffer, waveSize, &readResult, NULL); // Puts the wav into the allocated memory.


		// Check to make sure we''re dealing with a RIFF file.

		bool failed = false;
		if (*(FOURCC *)waveBuffer != mmioFOURCC(''R'', ''I'', ''F'', ''F''))
		{
			// Not a RIFF file, so certainly not a .WAV file

			// Bail out or whatever.

			failed = true;
			Trace("Error: File is not a RIFF");
		}
		else 
		{
			waveBuffer += sizeof(FOURCC);			// Advance past RIFF FOURCC

			tempSize += sizeof(FOURCC); 
			cbRemaining = *(DWORD *)waveBuffer;		// Obtain size of RIFF chunk

			waveBuffer += sizeof(DWORD);			// Skip size DWORD

			tempSize += sizeof(DWORD); 
		}
		if (!failed)
		{	
			
			// Check the form type -- it should be "WAVE".		

			if (*(FOURCC *)waveBuffer != mmioFOURCC(''W'', ''A'', ''V'', ''E''))
			{
				// Not a .WAV file -- some other kind of RIFF file.

				// Bail out or whatever.

				//fAILED

				failed = true;
				Trace("Error: Not a Wave File");
			}
		}
		
		if (!failed)
		{
			waveBuffer += sizeof(FOURCC);     // Skip form type

			tempSize += sizeof(FOURCC);  
			cbRemaining -= sizeof(FOURCC);
		}

		while (cbRemaining > 0 && !failed)
		{
			// Fetch the FOURCC for the current subchunk, then

			// move past it.

			fourccSubchunk = *(FOURCC *)waveBuffer;
			waveBuffer += sizeof(FOURCC);
			tempSize += sizeof(FOURCC);  
			cbRemaining -= sizeof(FOURCC);

			// Fetch the size of the current subchunk, then

			// move past that DWORD.

			cbSubchunk = *(DWORD *)waveBuffer;
			waveBuffer += sizeof(DWORD);
			tempSize += sizeof(FOURCC);  
			cbRemaining -= sizeof(DWORD);

			if (fourccSubchunk == mmioFOURCC(''f'', ''m'', ''t'', '' ''))
			{
				// "fmt " subchunk: save a pointer to the WAVEFORMATEX

				// struct.

				//WaveFormatEx = (WAVEFORMATEX *)waveBuffer;

				m_wFX = (WAVEFORMATEX *)waveBuffer;
			}
			else if (fourccSubchunk == mmioFOURCC(''d'', ''a'', ''t'', ''a''))
			{
				// "data" subchunk: save a pointer to the actual

				// sample data.

				m_pbData = waveBuffer;
				bufferSize = tempSize;
			}
			else
			{
				// Unknown subchunk type. Ignore it.

			}

			// Skip over the subchunk data.

			waveBuffer += cbSubchunk;
			tempSize += cbSubchunk;
			cbRemaining -= cbSubchunk;
		}
		 
		Trace("Parse Complete. \n");
		if(!failed)
		{
			m_dsbd.dwSize = sizeof(m_dsbd);
			m_dsbd.lpwfxFormat = m_wFX;
			m_dsbd.dwFlags = 0 | DSBCAPS_CTRL3D;
			m_dsbd.dwBufferBytes = bufferSize;		// The size of the file;

			m_dsbd.dwReserved = 0;
			m_dsbd.guid3DAlgorithm = GUID_NULL;
			
	 		
			m_secondOSound = m_dsbd.lpwfxFormat->nAvgBytesPerSec; 
			Trace("Sound FX Struct Created \n");

			if(&m_Sound == NULL)
				Trace("Bad Sound \n");
			if(&m_dsbd == NULL)
				Trace("Bad Struct \n");
			if(IDSound == NULL)
				Trace("Bad BAD BAD");			
		}
		else 
		{
			Trace("FAILURE");
		}


		Trace("Wav Format EX Struct:\n");
		Trace("Size: %s", m_wFX->cbSize);
		
		Trace("BPS: %s", m_wFX->nAvgBytesPerSec);
		

		Trace("Preparing Sound Buffer: ");
		if(FAILED(hr=IDSound->CreateSoundBuffer(&m_dsbd, &m_Sound, NULL)))
		{
			Trace("Error Creating Secondary Buffer: ");
			TraceHr(hr);	
		}

/*
		//m_Sound->Lock(0, m_wf.GetSampleSize(), &pvFirstSection, &cbFirstSection, NULL, 0, 0);
		memcpy(pvFirstSection, m_pbData, cbFirstSection);
		m_Sound->Unlock(pvFirstSection, cbFirstSection, NULL, 0);
*/
		Trace("-SoundFX Constructed: %s\n", m_name);
		
	     CloseHandle(waveFile); 
	}

	void play(void)
	{
		Trace("Playing Sound \n");
			m_Sound->Play(0,0,NULL);
	}

	~Soundfx(void)
	{
		if (m_Sound != NULL)
		{
			m_Sound->Release();
			m_Sound = NULL;
		}
		Trace("Sound FX Deconstructed %s\n", m_name);
	}
};

  
Darkpatch
Darkpatch
Okay it seems to be working now. I don''t think I fixed it, but it doesn''t seem to be quitting on me. Heres what I did.
1) Added a line IDirectSoundBuffer * bob = NULL and changed the CreateSoundBuffer command to take the &bob. This worked.
2) Removed bob and added a line m_Sound = NULL;. This too worked.
3) Removed the m_Sound line and rebuilt again. It still works. I don''t understand it. It seems to me like not all the files are linked when I recompile. I have multiple .cpp files and when I modify some it doesn''t change the program while the file that has the WinMain() seems needing modification in order to get a change out of the executable?

Topic Locked

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

Sign in to reply to this topic.