Skip to main content
gamedev.net
🔒 Locked

XAudio2 bug

Started by JackOfAllTrades Sep 17, 2010 at 1:12 PM 3 replies 2k views
Original Post
JackOfAllTrades
JackOfAllTrades
I copied this tutorial almost exactly and when I run my program from inside VC express it works fine but when I run the exe file the audio makes a popping sound and only plays out of the left speaker. I've had this problem for awhile and I've not been able to figure out what is causing it but it must be a memory leak...
MaulingMonkey
MaulingMonkey
Memory leaks do not do that. Memory leaks waste memory, eventually causing your program or computer to run out of available memory if they add up enough, but that's it.

It sounds more like you didn't properly initialize something -- running things under VS or not can alter what uninitialized memory is filled with. Among other things, this could change what channels (e.g. left/right speaker channels) it should play the sound on, or the balance between the two, as well as any number of other things.

Unfortunately, there's no easy way for us to tell without seeing what your source code looks like. What you've linked is more of a high level overview than a tutorial per se -- which is fine, but doesn't really give us much to go on as to what could be wrong.

Consider pasting your source code inside some [source] tags.
JackOfAllTrades
JackOfAllTrades
Initialization:

	XAudio2Create( &g_XAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR );	g_XAudio2->CreateMasteringVoice( &g_MasteringVoice );	g_XAudio2->GetDeviceDetails( 0, &g_DeviceDetails );	X3DAudioInitialize( g_DeviceDetails.OutputFormat.dwChannelMask, X3DAUDIO_SPEED_OF_SOUND, g_X3DAudio );	ZeroMemory( &g_DSPSettings, sizeof( g_DSPSettings ) );	g_DSPSettings.SrcChannelCount = 1;	g_DSPSettings.DstChannelCount = g_DeviceDetails.OutputFormat.Format.nChannels;	g_DSPSettings.pMatrixCoefficients = new FLOAT32[ g_DeviceDetails.OutputFormat.Format.nChannels];


Buffer class:
class E_AUDIO_BUFFER{public:	void load_sound( std::string file )	{		FILE *f;		fopen_s( &f, file.c_str(), "rb" );		ov_open_callbacks(f, &info, NULL, 0, OV_CALLBACKS_DEFAULT);		vorbis_info *vi = ov_info(&info, -1);		memset(&format, 0, sizeof(format));		format.cbSize          = sizeof(format);		format.nChannels       = vi->channels;		format.wBitsPerSample  = 16;		format.nSamplesPerSec  = vi->rate;		format.nAvgBytesPerSec = format.nSamplesPerSec * format.nChannels * 2;		format.nBlockAlign     = 2 * format.nChannels;		format.wFormatTag      = 1;		data.resize( vi->channels * 2 * (UINT)ov_pcm_total( &info, -1 ));		DWORD pos = 0;		int sec = 0;		int ret = 1;		while( ret && pos < data.size() )		{			ret = ov_read( &info, &data[0] + pos, data.size() - pos, 0, 2, 1, &sec );			pos += ret;		}	}	OggVorbis_File						info;	std::vector<char>					data;	WAVEFORMATEX						format;};

Instance class:
class E_AUDIO_INSTANCE{public:	E_AUDIO_EMITTER*					emitter;	IXAudio2SourceVoice*				voice;};


Emitter class:
class E_AUDIO_EMITTER{public:	void play( E_AUDIO_BUFFER* buffer, bool loop )	{		for( UINT i = 0; i < g_RenderTargets.size(); i++ )		{			if(g_RenderTargets->view != NULL)			{				if(g_RenderTargets->view->scene == scene)				{					E_AUDIO_INSTANCE* instance = new E_AUDIO_INSTANCE;					IXAudio2SourceVoice* source_voice;					g_XAudio2->CreateSourceVoice( &source_voice, &buffer->format, XAUDIO2_VOICE_USEFILTER );					XAUDIO2_BUFFER XA2_Buffer = {0};					XA2_Buffer.pAudioData = (BYTE*)&buffer->data[0];					XA2_Buffer.AudioBytes = buffer->data.size();					if(loop)					{						XA2_Buffer.LoopCount = XAUDIO2_LOOP_INFINITE;					}					XA2_Buffer.Flags = XAUDIO2_END_OF_STREAM;;					source_voice->SubmitSourceBuffer( &XA2_Buffer );					source_voice->Start( 0 );					instance->emitter = this;					instance->voice = source_voice;					g_RenderTargets->view->audio_instances.push_back(instance);					voices.push_back( source_voice );					paused = false;				}			}		}	}	void stop()	{		for( UINT i = 0; i < voices.size(); i++ )		{			voices->Stop( 0 );			voices->FlushSourceBuffers();			voices->DestroyVoice();		}		for( UINT c = 0; c < scene->cameras.size(); c++ )		{			for( UINT i = 0; i < scene->cameras[c]->audio_instances.size(); i++ )			{				for( UINT v = 0; v < voices.size(); v++ )				{					if( scene->cameras[c]->audio_instances->voice == voices[v] )					{						delete scene->cameras[c]->audio_instances;						scene->cameras[c]->audio_instances = NULL;					}				}			}		}		voices.clear();	}	void pause()	{		if( paused)		{			for( UINT i = 0; i < voices.size(); i++ )			{				voices->Start( 0 );				paused = false;			}		}		else		{			for( UINT i = 0; i < voices.size(); i++ )			{				voices->Stop( 0 );				paused = true;			}		}	}	void set_pose( cml::matrix44f_r matrix )	{		NxMat34 temp;		temp.setColumnMajor44( &matrix.data()[0] );		cmesh->actor->setGlobalPose(temp);	}	cml::matrix44f_r get_pose()	{		cml::matrix44f_r matrix;		cmesh->actor->getGlobalPose().getColumnMajor44( &matrix.data()[0] );		return matrix;	}	std::vector<IXAudio2SourceVoice*>	voices;	E_SCENE*							scene;	E_CMESH*							cmesh;	bool								paused;};


Calculate 3D audio:
	for( UINT c = 0; c < cameras.size(); c++)	{		cml::matrix44f_r cmatrix = cameras[c]->get_pose();		X3DAUDIO_LISTENER listener = {0};		listener.OrientFront = D3DXVECTOR3( cmatrix.data()[8], cmatrix.data()[9], cmatrix.data()[10]);		listener.OrientTop = D3DXVECTOR3( cmatrix.data()[4], cmatrix.data()[5], cmatrix.data()[6]);		listener.Position = D3DXVECTOR3( cmatrix.data()[12], cmatrix.data()[13], cmatrix.data()[14]);		for( UINT i = 0; i < cameras[c]->audio_instances.size(); i++)		{			if(cameras[c]->audio_instances != NULL)			{				XAUDIO2_VOICE_STATE state = {0};				cameras[c]->audio_instances->voice->GetState(&state);				if(state.BuffersQueued != 0)				{					cml::matrix44f_r ematrix = cameras[c]->audio_instances->emitter->get_pose();					X3DAUDIO_EMITTER emitter = {0};					emitter.ChannelCount = 1;					emitter.CurveDistanceScaler = 10.0f;					emitter.OrientFront = D3DXVECTOR3( ematrix.data()[8], ematrix.data()[9], ematrix.data()[10]);					emitter.OrientTop = D3DXVECTOR3( ematrix.data()[4], ematrix.data()[5], ematrix.data()[6]);					emitter.Position = D3DXVECTOR3( ematrix.data()[12], ematrix.data()[13], ematrix.data()[14]);					X3DAudioCalculate(g_X3DAudio, &listener, &emitter, X3DAUDIO_CALCULATE_MATRIX | X3DAUDIO_CALCULATE_DOPPLER | X3DAUDIO_CALCULATE_LPF_DIRECT | X3DAUDIO_CALCULATE_REVERB, &g_DSPSettings );					cameras[c]->audio_instances->voice->SetOutputMatrix( g_MasteringVoice, 2, g_DeviceDetails.OutputFormat.Format.nChannels, g_DSPSettings.pMatrixCoefficients ) ;					cameras[c]->audio_instances->voice->SetFrequencyRatio( g_DSPSettings.DopplerFactor);					XAUDIO2_FILTER_PARAMETERS FilterParameters = { LowPassFilter, 2.0f * sinf(X3DAUDIO_PI/6.0f * g_DSPSettings.LPFDirectCoefficient), 1.0f };					cameras[c]->audio_instances->voice->SetFilterParameters(&FilterParameters);				}			}		}	}


[Edited by - SteveDeFacto on September 18, 2010 1:33:57 AM]
MaulingMonkey
MaulingMonkey
A cursory glance reveals no super obvious problems, although I didn't look terribly hard.

Any chance you're using relative file paths? I notice you're doing zero error checking... which means the file may not be getting loaded in the first place.

Visual Studio will set the current working directory to the project directory
Explorer will set the current working directory to the one containing the EXE you've just double clicked on.

Even if this isn't the issue, some basic error checking could go a long way.
JackOfAllTrades
JackOfAllTrades
Quote:
Original post by MaulingMonkey
A cursory glance reveals no super obvious problems, although I didn't look terribly hard.

Any chance you're using relative file paths? I notice you're doing zero error checking... which means the file may not be getting loaded in the first place.

Visual Studio will set the current working directory to the project directory
Explorer will set the current working directory to the one containing the EXE you've just double clicked on.

Even if this isn't the issue, some basic error checking could go a long way.


That's not the problem. It's something to do with X3DAudio since I can play the sound fine before I call the X3DAudioCalculate command.

Topic Locked

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

Sign in to reply to this topic.