Some proposals to enhance JUCE DirectSound code


Hello Julian.

Please, consider the following information and make the following changes to your code.

  1. Please, avoid an obsolete code and change the cooperative level of an output buffer from DSSCL_EXCLUSIVE (3) to DSSCL_PRIORITY (2). That’s because Microsoft does not support the DSSCL_EXCLUSVE cooperative level any more. Consider the following note from MSDN:

Note The DSSCL_EXCLUSIVE cooperative level is obsolete. It is no longer possible for a DirectX application to mute other applications. Applications that request the exclusive level are granted the priority level instead.

  1. Please, insert the following code to the ‘DSoundInternalOutChannel::service’ function (possibly to other functions too) to avoid sound glitches when another sound application tries to gain the priority level. I’ve tested it and it solved my problem at last:
        if (bytesEmpty >= bytesPerBuffer)
            LPBYTE lpbuf1 = 0;
            LPBYTE lpbuf2 = 0;
            DWORD dwSize1 = 0;
            DWORD dwSize2 = 0;

            HRESULT hr = pOutputBuffer->Lock (writeOffset,
                                              (void**) &lpbuf1, &dwSize1,
                                              (void**) &lpbuf2, &dwSize2, 0);

	/////////////////////////////////////////////////// this code restores the lost buffer
	    if (hr == MAKE_HRESULT(1, 0x878, 150)) ///// DSERR_BUFFERLOST == MAKE_HRESULT(1, 0x878, 150)

		hr = pOutputBuffer->Lock (writeOffset,
                                              (void**) &lpbuf1, &dwSize1,
                                              (void**) &lpbuf2, &dwSize2, 0);

            if (hr == S_OK)
                if (bitDepth == 16)
                    const float gainL = 32767.0f;
                    const float gainR = 32767.0f;

P.S. Please, add ‘fadeInComponent’ code provided by kraken 2 years ago to JUCE at last. It is a matter of 2 minutes to accomplish. The code is 100% functional.


you can change the Windows code like this:

#ifndef JUCE_GCC  //xxx should add this fn for gcc..
        _fpreset(); // because some graphics cards can unmask FP exceptions

to the direct function call because MinGW (GCC) supports the _fpreset() function.


Great, thanks, I’ll make those changes. I’m actually in the middle of rewriting the way the audio works, to separate the input and output devices, so that the user can choose any two devices to use for the in/out, and it’ll sync them up. This means that the DSound stuff can all be simplified, and might hopefully improve performance.


Thanks for the DSOUND tips!