Audio playback and window focus

Hi there,

I noticed an odd behaviour on sound playback and window focus.

  • Under Windows, the audio playback stops whenever the window looses
    focus.
  • Under Linux it always plays.

This “turn-the-audio” behaviour might be desirable for fullscreen
applications but is undesirable for windowed applications. I think it
should be under the users-control.

How can one change this?

(I am still using SDL-1.1.6 DLLs …)

Ciao
Andreas–
| Andreas Schiffler aschiffler at home.com |
| Senior Systems Engineer - Deskplayer Inc., Buffalo |
| 4707 Eastwood Cres., Niagara Falls, Ont L2E 1B4, Canada |
| +1-905-371-3652 (private) - +1-905-371-8834 (work/fax) |

Hi there,

I noticed an odd behaviour on sound playback and window focus.

  • Under Windows, the audio playback stops whenever the window looses
    focus.

Normal DirectSound behavior.

  • Under Linux it always plays.

Normal behavior of all Linux audio drivers I know of. (The simply don’t talk
to the window manager about anything.)

This “turn-the-audio” behaviour might be desirable for fullscreen
applications but is undesirable for windowed applications. I think it
should be under the users-control.

How can one change this?

There is a flag (EXCLUSIVE something, IIRC) involved when setting up
DirectSound, but I don’t know what SDL does with it.

//David

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Tuesday 06 February 2001 18:19, Andreas Schiffler wrote:

This is a multi-part message in MIME format.
--------------DFEC5305F0DCB865300A6048
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

There is a flag (EXCLUSIVE something, IIRC) involved when setting up
DirectSound, but I don’t know what SDL does with it.

Thanks for the tip - with this lead, I figured it out …

Here is a patch to SDL-1.1.7 the will enable it. Works fine now. This is a
useful feature for things other than full-screen games and should become an
option to the sound API in my view.

Ciao
Andreas–
| Andreas Schiffler aschiffler at home.com |
| Senior Systems Engineer - Deskplayer Inc., Buffalo |
| 4707 Eastwood Cres., Niagara Falls, Ont L2E 1B4, Canada |
| +1-905-371-3652 (private) - +1-905-371-8834 (work/fax) |

--------------DFEC5305F0DCB865300A6048
Content-Type: text/plain; charset=us-ascii;
name="SDL-1.1.7-relaxedsound.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename=“SDL-1.1.7-relaxedsound.patch”

diff -rbd -U 5 SDL-1.1.7-orig/configure.in SDL-1.1.7-patched/configure.in
— SDL-1.1.7-orig/configure.in Tue Feb 6 15:53:03 2001
+++ SDL-1.1.7-patched/configure.in Tue Feb 6 15:57:16 2001
@@ -134,10 +134,11 @@
[ --enable-audio Enable the audio subsystem [default=yes]],
, enable_audio=yes)
if test x$enable_audio = xyes; then
SDL_EXTRADIRS="$SDL_EXTRADIRS audio"
SDL_EXTRALIBS="$SDL_EXTRALIBS audio/libaudio.la"

  • CFLAGS="$CFLAGS -DRELAXED_DX5AUDIO"
    else
    CFLAGS="$CFLAGS -DDISABLE_AUDIO"
    fi
    AC_ARG_ENABLE(video,
    [ --enable-video Enable the video subsystem [default=yes]],
    diff -rbd -U 5 SDL-1.1.7-orig/src/audio/windx5/SDL_dx5audio.c SDL-1.1.7-patched/src/audio/windx5/SDL_dx5audio.c
    — SDL-1.1.7-orig/src/audio/windx5/SDL_dx5audio.c Tue Feb 6 15:53:03 2001
    +++ SDL-1.1.7-patched/src/audio/windx5/SDL_dx5audio.c Tue Feb 6 15:52:11 2001
    @@ -426,23 +426,32 @@
    DSBUFFERDESC format;
    DSBCAPS caps;
    int numchunks;
/* Try to set primary mixing privileges */

+#ifdef RELAXED_DX5AUDIO

  • result = IDirectSound_SetCooperativeLevel(sndObj, focus,
  •   					DSSCL_NORMAL);
    

+#else
result = IDirectSound_SetCooperativeLevel(sndObj, focus,
DSSCL_WRITEPRIMARY);
+#endif
if ( result != DS_OK ) {
#ifdef DEBUG_SOUND
SetDSerror(“DirectSound SetCooperativeLevel”, result);
#endif
return(-1);
}

/* Try to create the primary buffer */
memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);

+#ifdef RELAXED_DX5AUDIO

  • format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS);
    +#else
    format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2);
    +#endif
    #ifdef USE_POSITION_NOTIFY
    format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
    #endif
    result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
    if ( result != DS_OK ) {
    @@ -499,12 +508,17 @@
    DSBUFFERDESC format;
    const int numchunks = 2;

    /* Try to set primary mixing privileges */
    if ( focus ) {
    +#ifdef RELAXED_DX5AUDIO

  •   result = IDirectSound_SetCooperativeLevel(sndObj,
    
  •   			focus, DSSCL_NORMAL);
    

+#else
result = IDirectSound_SetCooperativeLevel(sndObj,
focus, DSSCL_PRIORITY);
+#endif
} else {
result = IDirectSound_SetCooperativeLevel(sndObj,
GetDesktopWindow(), DSSCL_NORMAL);
}
if ( result != DS_OK ) {
@@ -515,11 +529,15 @@
}

/* Try to create the secondary buffer */
memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);

+#ifdef RELAXED_DX5AUDIO

  • format.dwFlags=(DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS);
    +#else
    format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
    +#endif
    #ifdef USE_POSITION_NOTIFY
    format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
    #endif
    if ( ! focus ) {
    format.dwFlags |= DSBCAPS_GLOBALFOCUS;

--------------DFEC5305F0DCB865300A6048–

Hi,
when the window loses focus, the sound is off. How to solve the problem ?
I am even using sdl 1.2.

Looking forward to the replies.
ronaldz.

This happens with Windows and DirectX, I think. Try changing the
audio driver (see the FAQ) to “waveout”?–

Olivier A. Dagenais - Software Architect and Developer

“ronaldz” <kiertech at 263.net> wrote in message
news:9e64h5$arf$1 at ftp.lokigames.com

Hi,
when the window loses focus, the sound is off. How to solve the
problem ?
I am even using sdl 1.2.

Looking forward to the replies.
ronaldz.

Yes, it’s caused by DX; it’s the default behavior. (This has been discussed
before.)

It’s possible to tell DX to do it differently, but SDL didn’t support it at
thi time this was discussed the first time, and IFAIK, that’s still the case.
(Which seems strange, as most other platforms don’t care about sound in
relation to focus. IMHO, either all other targets should be “fixed” to work
like DX, or the DX driver should set DX up to act like the other targets,
ignoring focus changes.)

//David Olofson — Programmer, Reologica Instruments AB

.- M A I A -------------------------------------------------.
| Multimedia Application Integration Architecture |
| A Free/Open Source Plugin API for Professional Multimedia |
----------------------> http://www.linuxaudiodev.com/maia -' .- David Olofson -------------------------------------------. | Audio Hacker - Open Source Advocate - Singer - Songwriter |--------------------------------------> david at linuxdj.com -'On Saturday 19 May 2001 18:14, Olivier Dagenais wrote:

This happens with Windows and DirectX, I think. Try changing the
audio driver (see the FAQ) to “waveout”?

This is a multi-part message in MIME format.
--------------12971DA689C39BC721CE74D2
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

ronaldz wrote:

Hi,
when the window loses focus, the sound is off. How to solve the problem ?
I am even using sdl 1.2.

This patch will change this behaviour … note the compile time options
that is added. Might be nice addition as compile-time flag. Patch works
with SDL-1.2 as well.

Ciao
Andreas
--------------12971DA689C39BC721CE74D2
Content-Type: text/plain; charset=us-ascii;
name="relaxedsound-SDL-1.1.x.patch"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
filename=“relaxedsound-SDL-1.1.x.patch”

diff -rbd -U 5 SDL-1.1.7-orig/configure.in SDL-1.1.7-patched/configure.in
— SDL-1.1.7-orig/configure.in Tue Feb 6 15:53:03 2001
+++ SDL-1.1.7-patched/configure.in Tue Feb 6 15:57:16 2001
@@ -134,10 +134,11 @@
[ --enable-audio Enable the audio subsystem [default=yes]],
, enable_audio=yes)
if test x$enable_audio = xyes; then
SDL_EXTRADIRS="$SDL_EXTRADIRS audio"
SDL_EXTRALIBS="$SDL_EXTRALIBS audio/libaudio.la"

  • CFLAGS="$CFLAGS -DRELAXED_DX5AUDIO"
    else
    CFLAGS="$CFLAGS -DDISABLE_AUDIO"
    fi
    AC_ARG_ENABLE(video,
    [ --enable-video Enable the video subsystem [default=yes]],
    diff -rbd -U 5 SDL-1.1.7-orig/src/audio/windx5/SDL_dx5audio.c SDL-1.1.7-patched/src/audio/windx5/SDL_dx5audio.c
    — SDL-1.1.7-orig/src/audio/windx5/SDL_dx5audio.c Tue Feb 6 15:53:03 2001
    +++ SDL-1.1.7-patched/src/audio/windx5/SDL_dx5audio.c Tue Feb 6 15:52:11 2001
    @@ -426,23 +426,32 @@
    DSBUFFERDESC format;
    DSBCAPS caps;
    int numchunks;
/* Try to set primary mixing privileges */

+#ifdef RELAXED_DX5AUDIO

  • result = IDirectSound_SetCooperativeLevel(sndObj, focus,
  •   					DSSCL_NORMAL);
    

+#else
result = IDirectSound_SetCooperativeLevel(sndObj, focus,
DSSCL_WRITEPRIMARY);
+#endif
if ( result != DS_OK ) {
#ifdef DEBUG_SOUND
SetDSerror(“DirectSound SetCooperativeLevel”, result);
#endif
return(-1);
}

/* Try to create the primary buffer */
memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);

+#ifdef RELAXED_DX5AUDIO

  • format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS);
    +#else
    format.dwFlags=(DSBCAPS_PRIMARYBUFFER|DSBCAPS_GETCURRENTPOSITION2);
    +#endif
    #ifdef USE_POSITION_NOTIFY
    format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
    #endif
    result = IDirectSound_CreateSoundBuffer(sndObj, &format, sndbuf, NULL);
    if ( result != DS_OK ) {
    @@ -499,12 +508,17 @@
    DSBUFFERDESC format;
    const int numchunks = 2;

    /* Try to set primary mixing privileges */
    if ( focus ) {
    +#ifdef RELAXED_DX5AUDIO

  •   result = IDirectSound_SetCooperativeLevel(sndObj,
    
  •   			focus, DSSCL_NORMAL);
    

+#else
result = IDirectSound_SetCooperativeLevel(sndObj,
focus, DSSCL_PRIORITY);
+#endif
} else {
result = IDirectSound_SetCooperativeLevel(sndObj,
GetDesktopWindow(), DSSCL_NORMAL);
}
if ( result != DS_OK ) {
@@ -515,11 +529,15 @@
}

/* Try to create the secondary buffer */
memset(&format, 0, sizeof(format));
format.dwSize = sizeof(format);

+#ifdef RELAXED_DX5AUDIO

  • format.dwFlags=(DSBCAPS_GETCURRENTPOSITION2|DSBCAPS_GLOBALFOCUS);
    +#else
    format.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
    +#endif
    #ifdef USE_POSITION_NOTIFY
    format.dwFlags |= DSBCAPS_CTRLPOSITIONNOTIFY;
    #endif
    if ( ! focus ) {
    format.dwFlags |= DSBCAPS_GLOBALFOCUS;

--------------12971DA689C39BC721CE74D2–