Audio playback problem when main window not active

I’m using SDL (1.2.6) to playback wav files. The main application has a
(thread safe) message listener and accepts connections from TCP/IP. So,
basically you can either play the wav file from the main application or from
another application. When load/play from main application, there is no
problem, all fine. When load/play from a remote application, everything
seems fine (no error in functions, callback seems to be called) but no
sound.

Interestingly, I accidently discovered that, when I go back to the main app
(focus that window) and moving back to the remote app immediately after
loading the sound, playback works correctly.

In summary;

  • Load wav (when main app window is not active)

  • Start playback (when main app window is not active)
    chokes (no sound heard) while

  • Load wav (when main app window is not active)

  • Activate the main app and deactivate

  • Start playback (when main app window is not active)

works just fine…

any ideas? thx in advance.

k.

below are my loading, playing functions and the callback function.
bool SDLSound::loadWAV(const std::string& filename)
{
SDL_AudioSpec wav_spec;

// Load the WAV 
if( SDL_LoadWAV(filename.c_str(), &wav_spec, &s_audio_chunk,

&m_audio_len) == NULL )
{
std::cerr << "Could not open " << filename << "; " <<
SDL_GetError() << std::endl;
return false;
}
else
std::cout << "Loaded speech file " << filename << std::endl;

// Set the audio format details (callback function)

// wav_spec.samples = 1024; /* Good low-latency value for callback */
wav_spec.callback = SDLSound::playSound;
wav_spec.userdata = NULL;

// Open the audio device, forcing the desired format
SDL_CloseAudio(); // if already open
if ( SDL_OpenAudio(&wav_spec, NULL) < 0 ) 
{
	std::cerr << "Couldn't open audio: " << SDL_GetError() <<

std::endl;
return false;
}

s_audio_pos = s_audio_chunk;
s_audio_remains = m_audio_len;

return true;

}
void SDLSound::play()
{
SDL_LockAudio();
if(m_audio_len)
{
s_audio_pos = s_audio_chunk;
s_audio_remains = m_audio_len;
SDL_PauseAudio(0);
}
else
std::cerr << “Audio length is zero! Cannot playback audio.”
<< std::endl;

SDL_UnlockAudio();

}

void SDLSound::playSound(void *udata, Uint8 stream, int len)
{
/
Only play if we have data left */
if ( s_audio_remains == 0 )
return;

/* Mix as much data as possible */
len = ( len > (int)s_audio_remains ? s_audio_remains : len );
SDL_MixAudio(stream, s_audio_pos, len, SDL_MIX_MAXVOLUME);
s_audio_pos += len;
s_audio_remains -= len;

}

In summary;

  • Load wav (when main app window is not active)
  • Start playback (when main app window is not active)
    chokes (no sound heard) while
  • Load wav (when main app window is not active)
  • Activate the main app and deactivate
  • Start playback (when main app window is not active)

works just fine…

You’ll have to modify SDL’s DirectX audio code to use the “play sounds globally” flag. I don’t remember offhand what the flag is, but SDL uses the default behavior of DirectX which ties audio output to the main window focus.

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment