Jump to content

  • Log In with Google      Sign In   
  • Create Account


magicstix

Member Since 06 Dec 2011
Offline Last Active Feb 24 2013 05:25 PM
-----

Topics I've Started

Need some help with XAudio2

25 December 2012 - 03:27 PM

Hi all, I've been trying to get some streaming code working with XAudio2. Unfortunately at best all I've gotten are a handful of clicks, and so I was wondering if someone could provide a little help.

 

I've implemented the code according to a few tutorials I've seen, and it's fairly simple. I just have one source voice and a master voice, and I'm submitting buffers to the source voice periodically.

 

Here's the setup code:

//Audio init section

	if(FAILED(CoInitializeEx(NULL, COINIT_MULTITHREADED)))
	{
		return false;
	}
	UINT32 flags = XAUDIO2_DEBUG_ENGINE;

	if(FAILED(XAudio2Create(&g_xAudioEngine)))
	{
		MessageBox(NULL, L"Failed on XAudio2Create", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}

	if(FAILED(g_xAudioEngine->CreateMasteringVoice(&g_masterVoice, XAUDIO2_DEFAULT_CHANNELS,(UINT32) SAMPLERATE, 0,0,NULL)))
	{
		MessageBox(NULL, L"Failed to create mastering voice!", L"Sadface", MB_OK);
		CoUninitialize();
		return false;
	}
	WAVEFORMATEX wfx = {0};

	wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
	wfx.nChannels = 1;
	wfx.nSamplesPerSec = 44100;
	wfx.nAvgBytesPerSec = 44100 * sizeof(float);
	wfx.nBlockAlign = sizeof(float);
	wfx.wBitsPerSample = sizeof(float) * 8;
	wfx.cbSize = 0;

	std::stringstream debugstream;

	debugstream << "nBlockAlign: " << wfx.nBlockAlign << " bitspersample: " << wfx.wBitsPerSample << std::endl;
	OutputDebugStringA(debugstream.str().c_str());

	if(FAILED(g_xAudioEngine->CreateSourceVoice(&g_sourceVoice, (WAVEFORMATEX*)&wfx)))
	{
		MessageBox(NULL, L"Failed to create source voice!", L"sadface", MB_OK);
	}

 

 

I've also tried this with signed 16-bit PCM instead of IEEE float to no avail.

 

Here's where I'm feeding the source voice:

 

void streamNextChunk(const boost::system::error_code& error, boost::asio::deadline_timer & timer)
{
	static bool comInitThisThread = false;
	if(!comInitThisThread)
	{
		CoInitializeEx(NULL, COINIT_MULTITHREADED);
		comInitThisThread = true;
	}
	timer.expires_from_now(boost::posix_time::milliseconds(1000));
	timer.async_wait(boost::bind(streamNextChunk, _1, boost::ref(timer)));

	static __int64 sampleCount = 0;
	float* buff = new float[44100];
	
	for(size_t i = 0 ;i < 44100; i++)
	{
		float t = (float)sampleCount++;
		t /= 44100.0;
		buff[i] =  cos(2 * 3.14159 * t * 260);
		
	}
	XAUDIO2_BUFFER xAudioBuff = {0};
	xAudioBuff.AudioBytes = (44100) * sizeof(float);
	xAudioBuff.pAudioData = (BYTE*) buff;
	

	if(FAILED(g_sourceVoice->SubmitSourceBuffer(&xAudioBuff)))
	{
		OutputDebugStringA("Failed on submit source buffer!!! D:\n");
	}
	g_sourceVoice->Start(0, XAUDIO2_COMMIT_NOW);
	XAUDIO2_VOICE_STATE state;

	g_sourceVoice->GetState(&state);
	float volume; 
    g_sourceVoice->GetVolume(&volume);
	std::stringstream debugstream;
	float gvolume;
	g_masterVoice->GetVolume(&gvolume);
	debugstream << "PTR: " << buff << " Q: " << state.BuffersQueued << " S: " << state.SamplesPlayed << " SC: " << sampleCount << " V: " << volume << " GV: " << gvolume << std::endl;

	OutputDebugStringA(debugstream.str().c_str());
	
}

 

 

 

This function is on a boost asio timer that ensures it's called once per second. The timer is handled in a separate thread from where the XAudio2 library is initialized (the ASIO run thread). The function also generates 1 second of data each call. As you can see I'm trying to generate a simple 2600Hz test tone, but at best all I have been able to get are some short clicks. (Yes I'm aware this leaks memory, but at the moment my main concern is getting any sound output at all). The debug printouts show that the source voice seems to be queuing the buffers and the samplesplayed count is increasing as expected.

 

This code seems to be simple enough, and mostly matches the code examples I've seen for streaming wav files off a disk (with the obvious exception that I'm creating the data on the fly). 

 

Can anyone tell me what I'm missing?


Need help with render to texture

01 December 2012 - 06:14 PM

Hi all, I was wondering if anyone could help me with a minor render to texture problem:

I'm getting:
D3D11: WARNING: ID3D11DeviceContext::OMSetRenderTargets: Resource being set to OM RenderTarget slot 0 is still bound on input! [ STATE_SETTING WARNING #9: DEVICE_OMSETRENDERTARGETS_HAZARD ]
D3D11: WARNING: ID3D11DeviceContext::OMSetRenderTargets[AndUnorderedAccessViews]: Forcing PS shader resource slot 0 to NULL. [ STATE_SETTING WARNING #7: DEVICE_PSSETSHADERRESOURCES_HAZARD ]

When trying to render with the following loop:
void render()
{
static float theta = 0;
float thetaRads = theta * 3.14159/180.0;
float ClearColor[4] = { 0.0f, 0.0, 0, 0.9f }; // red,green,blue,alpha
float r = 0.1 * cos(2 * 3.14159 * 1 * thetaRads);
//Render the texture
g_pImmediateContext->OMSetRenderTargets(1, &g_baseRTV,NULL);
	g_pImmediateContext->ClearRenderTargetView( g_baseRTV, ClearColor );
ConstantBufferType cb;
XMStoreFloat4x4(&cb.worldView, XMMatrixIdentity());
cb.worldView._11 = cos(thetaRads);
cb.worldView._12 = -sin(thetaRads);
cb.worldView._21 = sin(thetaRads);
cb.worldView._22 = cos(thetaRads);
//cb.worldView._14 = r*cos(thetaRads);
//cb.worldView._24 = r*sin(thetaRads);
g_pImmediateContext->UpdateSubresource(g_constantBuffer, 0, NULL, &cb, 0,0);
g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_constantBuffer);
	g_pImmediateContext->PSSetConstantBuffers(0, 1, &g_constantBuffer);
g_pImmediateContext->VSSetShader(g_baseVShader, NULL, 0);
g_pImmediateContext->PSSetShader(g_basePShader, NULL, 0);
g_pImmediateContext->IASetInputLayout(g_layout);
g_pImmediateContext->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
UINT stride = sizeof(BasicVertex);
UINT offset = 0;
g_pImmediateContext->IASetVertexBuffers(0,1,&g_pLineVtxBuffer,&stride,&offset);
g_pImmediateContext->Draw(6,0);
//Render the screen.
g_pImmediateContext->OMSetRenderTargets(1,&g_pRenderTargetView, NULL);
g_pImmediateContext->VSSetShader(g_texVShader, NULL, 0);
g_pImmediateContext->PSSetShader(g_texPShader, NULL, 0);
g_pImmediateContext->IASetVertexBuffers(0,1,&g_pScreenVtxBuffer,&stride,&offset);
g_pImmediateContext->PSSetShaderResources(0,1, &g_baseSRV);
g_pImmediateContext->PSSetSamplers(0,1,&g_pSamplerLinear);
g_pImmediateContext->Draw(6,0);
g_pSwapChain->Present( 0, 0 );
theta += 0.05;
}

g_pRenderTargetView is the main screen's render target view, and g_baseRTV is the render target view I'm using for render to texture.
Any pointers on what I'm doing wrong?
It also doesn't seem to be alpha blending properly (I'm expecting a sort of blurred trail like an accumulation buffer effect, but it doesn't seem to be working), and I'm wondering if it has something to do with the errors I'm seeing D3D throw...

Projected grid water using a sphere instead of a plane?

24 November 2012 - 11:12 PM

Does anyone know of any papers/blogs/whatever out there that discuss doing the projected-grid water approach with a sphere instead of a plane?

I'm using lat/long/altitude coordinates projected onto an ellipsoid for my terrain, and I have concerns that at high altitudes, the water horizon won't bend with the terrain horizon.

I assume this would involve line-sphere intersection tests instead of the line-plane tests in the projected-grid approach, but I don't really have any idea how to go about creating the screen-space grid so that it properly follows the curvature of the horizon...

Simulating CRT persistence?

17 November 2012 - 07:36 PM

HI all,
I want to simulate CRT persistence in a render-to-texture effect. Essentially I'm looking to simulate an old CRT screen, like an analog oscilloscope or a radar screen. If I were using OpenGL, I figure the best way to do this would be to use an accumulation buffer, but DirectX lacks such a capability.

So then, what would be the best way to achieve this effect with hardware acceleration in D3D11?

DirectCompute texture sampling sometimes returns zero?

08 June 2012 - 03:50 PM

Hi all,
I've been working on some HLSL code for DirectCompute, and I'm finding that randomly a texture sampling will return zero. This is causing all sorts of problems, since I'm using the sampled texel in a differential equation that explodes in a spectacular manner when fed garbage data.

The texture in question is a Texture2D created as DXGI_FORMAT_R32_FLOAT (i.e. a single float for each texel). The texture sampler is defined to clamp on the edge of the texture as well as use D3D11_FILTER_MIN_MAG_MIP_LINEAR. There's a single mip map level.

The HLSL code that does the sampling is as follows:
Texture2D<float> inputTex : register(t0);
SamplerState linSamp : register(s0);
float4 getSSPDerivatives(float range, float depth)
{
  float2 tex1;
  float2 tex2;
  //Find the initial sound speed.
  tex1.x = (range / 10000.0);
  tex1.y = (depth / 5000.0);
  //Find the sound speed a little deeper (finite difference)
  tex2.x = (range/10000.0);
  tex2.y = ((depth + 10)/5000.0);
  float localC = inputTex.SampleLevel(linSamp, tex1,0);

  float dcdz = (inputTex.SampleLevel(linSamp, tex2,0) - localC)/10.0;
  //Find the sound speed a little farther away (more finite difference)
  tex2.x = ((range + 10) /10000.0);
  tex2.y = tex1.y;
  float dcdr = (inputTex.SampleLevel(linSamp, tex2,0) - localC)/10.0;

  return float4(dcdr, dcdz, localC, tex1.y);
}

The symptoms of the problem are that the variable localC, which represents the first texture sampling operation, goes to 0. tex1.y is included in the return for debugging purposes, as it's the only texture coordinate rapidly changing (the X coordinate increases monotonically as the diff EQ marches out in range, whereas the Y coordinate varies based on the diff EQ's previous iteration).

I've been unable to determine why the texture sampling operation returns 0, as there are no 0s in the texture. The texture is currently created as a 2x1024 texel piece, with the two X columns duplicated (eventually the data in the X direction will be different, but I'm just working on getting the Y coords working atm). I've looked at the output of different threads in the shader, and none of them seem to fail at the same iteration step.

Is there anything obviously wrong with my texture sampling function? Is there a limit to the number of texture sampling operations that can be done across all threads in a shader?

PARTNERS