XAudio2: Does not play sound

Started by
11 comments, last by itsdbest 13 years, 5 months ago
This is my first attempt at writing a sound class manager using XAudio2 and I ran into some trouble. I have used code from the MSDN and the SDK samples.

The issue is that even after the sound is loaded into memory, I cannot hear it after I start the sound. Also, I run into an 'Access violation reading location' error.

I have tried to debug and noticed that the sound is loaded successfully and the Start function returns an S_OK too. The unhandled exception error takes place after the Start function and no sound is heard.

This is the error I receive....
Unhandled exception at 0x5f7d032d in SoundManagerTest.exe: 0xC0000005: Access violation reading location 0x9a3a99d8.

And the following is the code

SoundManager.h
/** Sound Manager v 0.1 - To handle all sound related tasks* Coded by - Andy Dbest*/#ifndef _SOUND_MANAGER#define _SOUND_MANAGER#include <XAudio2.h>#include <windows.h>#include <iostream>using namespace std;namespace GameZombies {	#define fourccRIFF 'FFIR'	#define fourccDATA 'atad'	#define fourccFMT ' tmf'	#define fourccWAVE 'EVAW'	#define fourccXWMA 'AMWX'	#define fourccDPDS 'sdpd'	class SoundManager 	{	private:		IXAudio2* pXAudio2; //core of the XAudio2 engine		IXAudio2MasteringVoice* pMasterVoice; //encapsulates an audio device, 											  //and is the ultimate destination for all audio		UINT32 flags; //flag to set while creating XAudio2 interface		XAUDIO2_BUFFER Buffer; 		IXAudio2SourceVoice* pSourceVoice;		HRESULT FindChunk(HANDLE, DWORD, DWORD &, DWORD &); //To find a chunk in a RIFF file		HRESULT ReadChunkData(HANDLE , void *, DWORD, DWORD); //To read data in a chunk after it has been located	public:		SoundManager(void);		~SoundManager(void);		bool InitializeSound();		int LoadSound(LPWSTR);		HRESULT Play();	};}#endif


SoundManager.cpp
#include "SoundManager.h"using namespace GameZombies;#ifndef SAFE_RELEASE#define SAFE_RELEASE(p)      { if(p) { (p)-&gt;Release(); (p)=NULL; } }#endif//DestructorSoundManager::~SoundManager(void){	// All XAudio2 interfaces are released when the engine is destroyed, but being tidy	if (pSourceVoice != NULL)	{		pSourceVoice-&gt;Stop(0);		pSourceVoice-&gt;DestroyVoice();	}    pMasterVoice-&gt;DestroyVoice();    SAFE_RELEASE( pXAudio2 );    CoUninitialize();}//ConstructorSoundManager::SoundManager(void){	pXAudio2 = NULL;	pMasterVoice = NULL;	flags = 0;	CoInitializeEx(NULL, COINIT_MULTITHREADED);	cout&lt;&lt;"COM initialized\n";}bool SoundManager::InitializeSound(){	HRESULT hr;	if (FAILED(hr = XAudio2Create( &pXAudio2, flags)))	{		cout&lt;&lt;"Failed to create Audio2\n";		return false;	}	if (FAILED(hr = pXAudio2-&gt;CreateMasteringVoice(&pMasterVoice)))	{		cout&lt;&lt;"Failed to create MasteringVoice\n";		return false;	}	cout&lt;&lt;"SoundManager Initialized Successfully\n";		return true;}int SoundManager::LoadSound(LPWSTR fileName){	WAVEFORMATEXTENSIBLE wfx = {0}; //defines the format of the audio file		//Open the file	HANDLE hFile = CreateFile(fileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);	if( INVALID_HANDLE_VALUE == hFile )		return HRESULT_FROM_WIN32( GetLastError() );	if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, 0, NULL, FILE_BEGIN ) )		return HRESULT_FROM_WIN32( GetLastError() );		//Locate the 'RIFF' chunk in the audio file, and check the file type	DWORD dwChunkSize;	DWORD dwChunkPosition;	//check the file type, should be fourccWAVE or 'XWMA'	FindChunk(hFile,fourccRIFF,dwChunkSize, dwChunkPosition );	DWORD filetype;	ReadChunkData(hFile,&filetype,sizeof(DWORD),dwChunkPosition);	if (filetype != fourccWAVE)		return S_FALSE;		//Locate the 'fmt ' chunk, and copy its contents into a WAVEFORMATEXTENSIBLE structure	FindChunk(hFile,fourccFMT, dwChunkSize, dwChunkPosition );	ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition );	//Locate the 'data' chunk, and read its contents into a buffer	FindChunk(hFile,fourccDATA,dwChunkSize, dwChunkPosition );	BYTE * pDataBuffer = new BYTE[dwChunkSize];	ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition);		//Populate the XAudio2 Buffer	Buffer.AudioBytes = dwChunkSize;	Buffer.pAudioData = pDataBuffer;	Buffer.Flags = XAUDIO2_END_OF_STREAM; 		HRESULT hr;	//Create the source voice	if (FAILED(hr = pXAudio2-&gt;CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx)))	{		cout&lt;&lt;"Error while creating source voice\n";		return false;	}	if( FAILED(hr = pSourceVoice-&gt;SubmitSourceBuffer( &Buffer ) ) )		return hr;		cout&lt;&lt;"Sound Loaded Successfully\n";	return S_OK;}HRESULT SoundManager::Play(){	HRESULT hr;	if ( FAILED(hr = pSourceVoice-&gt;Start( 0 ) ) )		return hr;	cout&lt;&lt;"Sound Playing\n";<span class="cpp-comment">//ERROR OCCURS HERE</span><br>	<span class="cpp-keyword">return</span> hr;<br>}<br><br>HRESULT SoundManager::FindChunk(HANDLE hFile, DWORD fourcc, DWORD &amp; dwChunkSize, DWORD &amp; dwChunkDataPosition)<br>{<br>    HRESULT hr = S_OK;<br>    <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, <span class="cpp-number">0</span>, NULL, FILE_BEGIN ) )<br>        <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() );<br><br>    DWORD dwChunkType;<br>    DWORD dwChunkDataSize;<br>    DWORD dwRIFFDataSize = <span class="cpp-number">0</span>;<br>    DWORD dwFileType;<br>    DWORD bytesRead = <span class="cpp-number">0</span>;<br>    DWORD dwOffset = <span class="cpp-number">0</span>;<br><br>    <span class="cpp-keyword">while</span> (hr == S_OK)<br>    {<br>        DWORD dwRead;<br>        <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &amp;dwChunkType, <span class="cpp-keyword">sizeof</span>(DWORD), &amp;dwRead, NULL ) )<br>            hr = HRESULT_FROM_WIN32( GetLastError() );<br><br>        <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &amp;dwChunkDataSize, <span class="cpp-keyword">sizeof</span>(DWORD), &amp;dwRead, NULL ) )<br>            hr = HRESULT_FROM_WIN32( GetLastError() );<br><br>        <span class="cpp-keyword">switch</span> (dwChunkType)<br>        {<br>        <span class="cpp-keyword">case</span> fourccRIFF:<br>            dwRIFFDataSize = dwChunkDataSize;<br>            dwChunkDataSize = <span class="cpp-number">4</span>;<br>            <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, &amp;dwFileType, <span class="cpp-keyword">sizeof</span>(DWORD), &amp;dwRead, NULL ) )<br>                hr = HRESULT_FROM_WIN32( GetLastError() );<br>            <span class="cpp-keyword">break</span>;<br><br>        <span class="cpp-keyword">default</span>:<br>            <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, dwChunkDataSize, NULL, FILE_CURRENT ) )<br>            <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() );            <br>        }<br><br>        dwOffset += <span class="cpp-keyword">sizeof</span>(DWORD) * <span class="cpp-number">2</span>;<br>        <br>        <span class="cpp-keyword">if</span> (dwChunkType == fourcc)<br>        {<br>            dwChunkSize = dwChunkDataSize;<br>            dwChunkDataPosition = dwOffset;<br>            <span class="cpp-keyword">return</span> S_OK;<br>        }<br><br>        dwOffset += dwChunkDataSize;<br>        <br>        <span class="cpp-keyword">if</span> (bytesRead &amp;gt;= dwRIFFDataSize) <span class="cpp-keyword">return</span> S_FALSE;<br><br>    }<br><br>    <br>    <span class="cpp-keyword">return</span> S_OK;<br>    <br>}<br><br><br>HRESULT SoundManager::ReadChunkData(HANDLE hFile, <span class="cpp-keyword">void</span> * buffer, DWORD buffersize, DWORD bufferoffset)<br>{<br>    HRESULT hr = S_OK;<br>    <span class="cpp-keyword">if</span>( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, bufferoffset, NULL, FILE_BEGIN ) )<br>        <span class="cpp-keyword">return</span> HRESULT_FROM_WIN32( GetLastError() );<br>    DWORD dwRead;<br>    <span class="cpp-keyword">if</span>( <span class="cpp-number">0</span> == ReadFile( hFile, buffer, buffersize, &amp;dwRead, NULL ) )<br>        hr = HRESULT_FROM_WIN32( GetLastError() );<br>    <span class="cpp-keyword">return</span> hr;<br>}<br><br></pre></div><!–ENDSCRIPT–><br><br>main.cpp<br><!–STARTSCRIPT–><!–source lang="cpp"–><div class="source"><pre><br><span class="cpp-directive">#include</span> <span class="cpp-literal">"SoundManager.h"</span><br><br><span class="cpp-keyword">using</span> <span class="cpp-keyword">namespace</span> GameZombies;<br><br><span class="cpp-keyword">int</span> main()<br>{<br>	SoundManager sound;<br>	HRESULT hr;<br>	<span class="cpp-comment">//CoInitializeEx(NULL, COINIT_MULTITHREADED);</span><br>	<span class="cpp-keyword">if</span> (!sound.InitializeSound())<br>		cout &amp;lt;&amp;lt;<span class="cpp-literal">"Error initializing sound\n"</span>;<br>	<span class="cpp-keyword">else</span> {<br>		<span class="cpp-keyword">if</span> (FAILED(hr = sound.LoadSound(L<span class="cpp-literal">"Mono.wav"</span>))){<br>			cout &amp;lt;&amp;lt;<span class="cpp-literal">"Error Loading sound\n"</span>;<br>			cout &amp;lt;&amp;lt;hr;<br>		}<br>		<span class="cpp-keyword">else</span> {<br>			sound.Play();<br>		}<br>	}<br><br>	system (<span class="cpp-literal">"pause"</span>);<br><br>	<span class="cpp-keyword">return</span> <span class="cpp-number">0</span>;<br>}<br><br></pre></div><!–ENDSCRIPT–> 
Advertisement
You are using pSourceVoice without ever initializing, which is genereally the root of 0xC000005 errors, uninitialized pointers.

SoundManager::SoundManager(void){	pXAudio2 = NULL;	pMasterVoice = NULL;	pSourceVoice = NULL; // this should fix	flags = 0;	CoInitializeEx(NULL, COINIT_MULTITHREADED);	cout&lt;&lt;"COM initialized\n";}

Added that line, but it results in the same issue.
This is teh same code in a procedural format and I have the same issue.. The exception occurs before the system("pause") line.

#include <XAudio2.h>#include <windows.h>#include <iostream>using namespace std;#ifndef SAFE_RELEASE#define SAFE_RELEASE(p)      { if(p) { (p)->Release(); (p)=NULL; } }#endif#define fourccRIFF 'FFIR'	#define fourccDATA 'atad'	#define fourccFMT ' tmf'	#define fourccWAVE 'EVAW'	#define fourccXWMA 'AMWX'	#define fourccDPDS 'sdpd'HRESULT FindChunk(HANDLE hFile, DWORD fourcc, DWORD & dwChunkSize, DWORD & dwChunkDataPosition){    HRESULT hr = S_OK;    if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, 0, NULL, FILE_BEGIN ) )        return HRESULT_FROM_WIN32( GetLastError() );    DWORD dwChunkType;    DWORD dwChunkDataSize;    DWORD dwRIFFDataSize = 0;    DWORD dwFileType;    DWORD bytesRead = 0;    DWORD dwOffset = 0;    while (hr == S_OK)    {        DWORD dwRead;        if( 0 == ReadFile( hFile, &dwChunkType, sizeof(DWORD), &dwRead, NULL ) )            hr = HRESULT_FROM_WIN32( GetLastError() );        if( 0 == ReadFile( hFile, &dwChunkDataSize, sizeof(DWORD), &dwRead, NULL ) )            hr = HRESULT_FROM_WIN32( GetLastError() );        switch (dwChunkType)        {        case fourccRIFF:            dwRIFFDataSize = dwChunkDataSize;            dwChunkDataSize = 4;            if( 0 == ReadFile( hFile, &dwFileType, sizeof(DWORD), &dwRead, NULL ) )                hr = HRESULT_FROM_WIN32( GetLastError() );            break;        default:            if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, dwChunkDataSize, NULL, FILE_CURRENT ) )            return HRESULT_FROM_WIN32( GetLastError() );                    }        dwOffset += sizeof(DWORD) * 2;                if (dwChunkType == fourcc)        {            dwChunkSize = dwChunkDataSize;            dwChunkDataPosition = dwOffset;            return S_OK;        }        dwOffset += dwChunkDataSize;                if (bytesRead >= dwRIFFDataSize) return S_FALSE;    }        return S_OK;    }HRESULT ReadChunkData(HANDLE hFile, void * buffer, DWORD buffersize, DWORD bufferoffset){    HRESULT hr = S_OK;    if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, bufferoffset, NULL, FILE_BEGIN ) )        return HRESULT_FROM_WIN32( GetLastError() );    DWORD dwRead;    if( 0 == ReadFile( hFile, buffer, buffersize, &dwRead, NULL ) )        hr = HRESULT_FROM_WIN32( GetLastError() );    return hr;}int main(){	IXAudio2* pXAudio2; //core of the XAudio2 engine	IXAudio2MasteringVoice* pMasterVoice; //encapsulates an audio device, 											  //and is the ultimate destination for all audio	UINT32 flags; //flag to set while creating XAudio2 interface	XAUDIO2_BUFFER Buffer; 	IXAudio2SourceVoice* pSourceVoice;	BYTE * pDataBuffer;		HRESULT hr;	//intialize variables	pXAudio2 = NULL;	pMasterVoice = NULL;	pSourceVoice = NULL;	flags = 0;	CoInitializeEx(NULL, COINIT_MULTITHREADED);	cout<<"COM initialized\n";	//initialize sound	if (FAILED(hr = XAudio2Create( &pXAudio2, flags)))	{		cout<<"Failed to create Audio2\n";		return false;	}	if (FAILED(hr = pXAudio2->CreateMasteringVoice(&pMasterVoice)))	{		cout<<"Failed to create MasteringVoice\n";		return false;	}	cout<<"SoundManager Initialized Successfully\n";		//load sound into memory	LPWSTR fileName = L"Mono.wav";	WAVEFORMATEXTENSIBLE wfx = {0}; //defines the format of the audio file		//Open the file	HANDLE hFile = CreateFile(fileName,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);	if( INVALID_HANDLE_VALUE == hFile )		return HRESULT_FROM_WIN32( GetLastError() );	if( INVALID_SET_FILE_POINTER == SetFilePointer( hFile, 0, NULL, FILE_BEGIN ) )		return HRESULT_FROM_WIN32( GetLastError() );		//Locate the 'RIFF' chunk in the audio file, and check the file type	DWORD dwChunkSize;	DWORD dwChunkPosition;	//check the file type, should be fourccWAVE or 'XWMA'	FindChunk(hFile,fourccRIFF,dwChunkSize, dwChunkPosition );	DWORD filetype;	ReadChunkData(hFile,&filetype,sizeof(DWORD),dwChunkPosition);	if (filetype != fourccWAVE)		return S_FALSE;		//Locate the 'fmt ' chunk, and copy its contents into a WAVEFORMATEXTENSIBLE structure	FindChunk(hFile,fourccFMT, dwChunkSize, dwChunkPosition );	ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition );	//Locate the 'data' chunk, and read its contents into a buffer	FindChunk(hFile,fourccDATA,dwChunkSize, dwChunkPosition );	pDataBuffer = new BYTE[dwChunkSize];	ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition);		//Populate the XAudio2 Buffer	Buffer.AudioBytes = dwChunkSize;	Buffer.pAudioData = pDataBuffer;	Buffer.Flags = XAUDIO2_END_OF_STREAM; 		//HRESULT hr;	//Create the source voice	if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx)))	{		cout<<"Error while creating source voice\n";		return false;	}	if( FAILED(hr = pSourceVoice->SubmitSourceBuffer( &Buffer ) ) )		return hr;	cout<<"Sound Loaded Successfully\n";	//play the sound	if ( FAILED(hr = pSourceVoice->Start( 0, XAUDIO2_COMMIT_NOW ) ) )		return hr;	cout<<"Sound Playing\n";	system ("pause");	// All XAudio2 interfaces are released when the engine is destroyed, but being tidy	if (pSourceVoice != NULL)	{		pSourceVoice->Stop(0);		pSourceVoice->DestroyVoice();	}    pMasterVoice->DestroyVoice();    SAFE_RELEASE( pXAudio2 );    CoUninitialize();	return 0;}
Quote:The exception occurs before the system("pause") line.

Which line exactly? If you don't know, set breakpoints and step through the code. You need to know where the exception is taking place.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

cout<<"Sound Playing\n";

I get the "S" (from Sound) printed on the console and thats where i get the error. Debugging also shows the error there. pSourceVoice->Start( 0, XAUDIO2_COMMIT_NOW ) returns an S_OK.
Interesting! What happened when you commented out that line?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

I just took a quick look at your code and I don't see you doing anything except XAudio2Create and CreateMasteringVoice to initialize XAudio2.

You need to do all of these steps:

	// Initialize XAudio2	XAudio2Create( &instance->XAudio2, 0);	instance->XAudio2->CreateMasteringVoice( &instance->MasteringVoice );	instance->XAudio2->GetDeviceDetails( 0, &instance->DeviceDetails );	X3DAudioInitialize( instance->DeviceDetails.OutputFormat.dwChannelMask, X3DAUDIO_SPEED_OF_SOUND, instance->X3DAudio );	ZeroMemory( &instance->DSPSettings, sizeof( instance->DSPSettings ) );	instance->DSPSettings.SrcChannelCount = 2;	instance->DSPSettings.DstChannelCount = instance->DeviceDetails.OutputFormat.Format.nChannels;	DWORD dwSize = (instance->DSPSettings.SrcChannelCount * instance->DSPSettings.DstChannelCount);	FLOAT32* MatrixCoefficients = new FLOAT32[dwSize];	ZeroMemory( MatrixCoefficients, sizeof( FLOAT32 ) * dwSize );	instance->DSPSettings.pMatrixCoefficients = MatrixCoefficients;


This code was copied from my project.
Quote:Original post by Buckeye
Interesting! What happened when you commented out that line?


The exception still occurs on the next line.

@Steve - I will look into that...
I think that is another red herring, as my initialization of XAudio is nothing more than what you, and the SDK Example have done. It has been a while since i was working with XAudio, and I remember having many issues as well and found little if any help for them. I'll try to work through your code today

This topic is closed to new replies.

Advertisement