Handmade Hero » Forums » Code » [SOLVED] day 008 - squarewave to directsound: DSERR_PRIOLEVELNEEDED
C_Worm
7 posts
#21095 [SOLVED] day 008 - squarewave to directsound: DSERR_PRIOLEVELNEEDED
1 month ago Edited by C_Worm on May 27, 2019, 8:19 p.m.

Hello!

im writing along at this episode (008 directsoud squarewave).

but i can't get the sound to work.

i've debugged and went through the code and the problem seem to be that GetCurrentPosition(); fails.

1
2
3
4
DWORD playCursor;
		DWORD writeCursor;
		hr = global_pDsSecondaryBuffer->GetCurrentPosition(&playCursor, &writeCursor);
		if(SUCCEEDED(hr))  // <---- this returns 0x88780046 (seems to be DSERR_PRIOLEVELNEEDED)




it returns and HRESULT written in hex 0x88780046, which after some searching seemed to correspond to DSERR_PRIOLEVELNEEDED.

however, in my initSound() function i manage to set the cooperativelevel successfully.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
void initDSound(HWND hwnd, int samplesPerSecond, int secBufferSize)
{

	LPDIRECTSOUND pDSound = {};
	
	DSBUFFERDESC dsSecondaryBufferDesc = {}; // SECONDARY BUFFER
	
	DSBUFFERDESC dsPrimaryBufferDesc = {};	// PRIMARY BUFFER

	LPDIRECTSOUNDBUFFER pDsPrimaryBuffer = {};
	
	WAVEFORMATEX waveFormat = {};		// WAVEFORMAT
	waveFormat.wFormatTag = WAVE_FORMAT_PCM;
	waveFormat.nChannels = 2;
	waveFormat.nSamplesPerSec = samplesPerSecond;
	waveFormat.wBitsPerSample = 16;
	waveFormat.nBlockAlign = (waveFormat.nChannels * waveFormat.wBitsPerSample) / 8; 
	waveFormat.nAvgBytesPerSec = (waveFormat.nSamplesPerSec * waveFormat.nBlockAlign);

	HRESULT hr;  
	if(SUCCEEDED(hr = DirectSoundCreate(0, &pDSound, 0)))
	{
		OutputDebugString("DirectSound created successfully!");

		if(SUCCEEDED(hr = pDSound->SetCooperativeLevel(hwnd, DSSCL_PRIORITY)))
		{
			OutputDebugString("\nSet CooperativeLevel successfully!");
		}

		dsPrimaryBufferDesc.dwSize = sizeof(dsPrimaryBufferDesc);
		dsPrimaryBufferDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;

		if (SUCCEEDED(hr = pDSound->CreateSoundBuffer(&dsPrimaryBufferDesc, &pDsPrimaryBuffer, 0)))
		{
			OutputDebugString("\nCreate Primary Sound Buffer successfully!");

			if (SUCCEEDED(hr = pDsPrimaryBuffer->SetFormat(&waveFormat)))
			{
				OutputDebugString("\nPrim Buffer Format set Success!");
			}
		}


		dsSecondaryBufferDesc.dwSize = sizeof(dsSecondaryBufferDesc);
		dsSecondaryBufferDesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
		dsSecondaryBufferDesc.dwBufferBytes = secBufferSize;
		dsSecondaryBufferDesc.guid3DAlgorithm = GUID_NULL;

		if (SUCCEEDED(hr = pDSound->CreateSoundBuffer(&dsPrimaryBufferDesc, &global_pDsSecondaryBuffer, 0)))
		{
			OutputDebugString("\nCreate Secondary Sound Buffer successfully!");
		}
	}

}


does anyone have an idea of what the problem might be?
I know there could be many reasons for why this doesn't work but do you have any suggestions? :)


i don't wanna post the code in the "game-loop" cuz i don't wanna bother you with to much code.
but if you would like to look at that aswell. please let me know!

cheers!
mrmixer
Simon Anciaux
618 posts
#21098 [problem] day 008 - squarewave to directsound: DSERR_PRIOLEVELNEEDED
4 weeks, 1 day ago Edited by Simon Anciaux on May 27, 2019, 8:15 p.m. Reason: link to the doc

I think you are missing the "lpwfxFormat" field of "dsSecondaryBufferDesc". You should set it the address of the WAVEFORMATEX structure for the secondary buffer. Here is a link to the DSBUFFERDESC documentation on MSDN.

lpwfxFormat
Address of a WAVEFORMATEX or WAVEFORMATEXTENSIBLE structure specifying the waveform format for the buffer. This value must be NULL for primary buffers.


1
2
3
4
5
6
7
...
dsSecondaryBufferDesc.dwSize = sizeof(dsSecondaryBufferDesc);
dsSecondaryBufferDesc.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
dsSecondaryBufferDesc.dwBufferBytes = secBufferSize;
dsSecondaryBufferDesc.guid3DAlgorithm = GUID_NULL;
dsSecondaryBufferDesc.lpwfxFormat = &waveFormat;
...
C_Worm
7 posts
#21099 [problem] day 008 - squarewave to directsound: DSERR_PRIOLEVELNEEDED
4 weeks, 1 day ago Edited by C_Worm on May 27, 2019, 8:20 p.m.

yes that was it, i actually solved by going through it again. but thanks anyway. owl of shame on me! :P