Music fade task retired; also absolve audio drivers from managing the stream decoder task

git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@3411 8092fc87-c524-0410-9efc-e669fe64eaf9
This commit is contained in:
avolkov
2009-12-10 04:38:35 +00:00
parent 4dfdd607ad
commit 2d13f9a9d7
6 changed files with 146 additions and 85 deletions
@@ -25,7 +25,6 @@
#include <stdlib.h> #include <stdlib.h>
static Task StreamDecoderTask;
static Task PlaybackTask; static Task PlaybackTask;
static uint32 nosound_freq = 22050; static uint32 nosound_freq = 22050;
@@ -138,14 +137,21 @@ noSound_Init (audio_Driver *driver, sint32 flags)
soundSource[i].stream_mutex = CreateMutex ("Nosound stream mutex", SYNC_CLASS_AUDIO); soundSource[i].stream_mutex = CreateMutex ("Nosound stream mutex", SYNC_CLASS_AUDIO);
} }
if (InitStreamDecoder ())
{
log_add (log_Error, "Stream decoder initialization failed.");
// TODO: cleanup source mutexes [or is it "muti"? :) ]
SoundDecoder_Uninit ();
mixer_Uninit ();
return -1;
}
atexit (unInitAudio); atexit (unInitAudio);
SetSFXVolume (sfxVolumeScale); SetSFXVolume (sfxVolumeScale);
SetSpeechVolume (speechVolumeScale); SetSpeechVolume (speechVolumeScale);
SetMusicVolume ((COUNT)musicVolume); SetMusicVolume ((COUNT)musicVolume);
StreamDecoderTask = AssignTask (StreamDecoderTaskFunc, 1024,
"audio stream decoder");
PlaybackTask = AssignTask (PlaybackTaskFunc, 1024, PlaybackTask = AssignTask (PlaybackTaskFunc, 1024,
"nosound audio playback"); "nosound audio playback");
@@ -157,11 +163,7 @@ noSound_Uninit (void)
{ {
int i; int i;
if (StreamDecoderTask) UninitStreamDecoder ();
{
ConcludeTask (StreamDecoderTask);
StreamDecoderTask = NULL;
}
for (i = 0; i < NUM_SOUNDSOURCES; ++i) for (i = 0; i < NUM_SOUNDSOURCES; ++i)
{ {
+12 -11
View File
@@ -19,14 +19,11 @@
#include "audiodrv_sdl.h" #include "audiodrv_sdl.h"
#include "../../sndintrn.h" #include "../../sndintrn.h"
#include "libs/tasklib.h"
#include "libs/log.h" #include "libs/log.h"
#include "libs/memlib.h" #include "libs/memlib.h"
#include <stdlib.h> #include <stdlib.h>
static Task StreamDecoderTask;
static const audio_Driver mixSDL_Driver = static const audio_Driver mixSDL_Driver =
{ {
mixSDL_Uninit, mixSDL_Uninit,
@@ -202,15 +199,23 @@ mixSDL_Init (audio_Driver *driver, sint32 flags)
soundSource[i].stream_mutex = CreateMutex ("MixSDL stream mutex", SYNC_CLASS_AUDIO); soundSource[i].stream_mutex = CreateMutex ("MixSDL stream mutex", SYNC_CLASS_AUDIO);
} }
if (InitStreamDecoder ())
{
log_add (log_Error, "Stream decoder initialization failed.");
// TODO: cleanup source mutexes [or is it "muti"? :) ]
SDL_CloseAudio ();
SoundDecoder_Uninit ();
mixer_Uninit ();
SDL_QuitSubSystem (SDL_INIT_AUDIO);
return -1;
}
atexit (unInitAudio); atexit (unInitAudio);
SetSFXVolume (sfxVolumeScale); SetSFXVolume (sfxVolumeScale);
SetSpeechVolume (speechVolumeScale); SetSpeechVolume (speechVolumeScale);
SetMusicVolume ((COUNT)musicVolume); SetMusicVolume ((COUNT)musicVolume);
StreamDecoderTask = AssignTask (StreamDecoderTaskFunc, 1024,
"audio stream decoder");
SDL_PauseAudio (0); SDL_PauseAudio (0);
return 0; return 0;
@@ -221,11 +226,7 @@ mixSDL_Uninit (void)
{ {
int i; int i;
if (StreamDecoderTask) UninitStreamDecoder ();
{
ConcludeTask (StreamDecoderTask);
StreamDecoderTask = NULL;
}
for (i = 0; i < NUM_SOUNDSOURCES; ++i) for (i = 0; i < NUM_SOUNDSOURCES; ++i)
{ {
+14 -10
View File
@@ -22,7 +22,6 @@
#include "audiodrv_openal.h" #include "audiodrv_openal.h"
#include "../sndintrn.h" #include "../sndintrn.h"
#include "libs/tasklib.h"
#include "libs/log.h" #include "libs/log.h"
#include "libs/memlib.h" #include "libs/memlib.h"
#include <stdlib.h> #include <stdlib.h>
@@ -34,7 +33,6 @@ ALfloat defaultPos[] = {0.0f, 0.0f, -1.0f};
ALfloat listenerPos[] = {0.0f, 0.0f, 0.0f}; ALfloat listenerPos[] = {0.0f, 0.0f, 0.0f};
ALfloat listenerVel[] = {0.0f, 0.0f, 0.0f}; ALfloat listenerVel[] = {0.0f, 0.0f, 0.0f};
ALfloat listenerOri[] = {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f}; ALfloat listenerOri[] = {0.0f, 0.0f, -1.0f, 0.0f, 1.0f, 0.0f};
static Task StreamDecoderTask;
static const audio_Driver openAL_Driver = static const audio_Driver openAL_Driver =
{ {
@@ -185,6 +183,19 @@ openAL_Init (audio_Driver *driver, sint32 flags)
soundSource[i].stream_mutex = CreateMutex ("OpenAL stream mutex", SYNC_CLASS_AUDIO); soundSource[i].stream_mutex = CreateMutex ("OpenAL stream mutex", SYNC_CLASS_AUDIO);
} }
if (InitStreamDecoder ())
{
log_add (log_Error, "Stream decoder initialization failed.");
// TODO: cleanup source mutexes [or is it "muti"? :) ]
SoundDecoder_Uninit ();
alcMakeContextCurrent (NULL);
alcDestroyContext (alcContext);
alcContext = NULL;
alcCloseDevice (alcDevice);
alcDevice = NULL;
return -1;
}
SetSFXVolume (sfxVolumeScale); SetSFXVolume (sfxVolumeScale);
SetSpeechVolume (speechVolumeScale); SetSpeechVolume (speechVolumeScale);
@@ -195,9 +206,6 @@ openAL_Init (audio_Driver *driver, sint32 flags)
else else
alDistanceModel (AL_NONE); alDistanceModel (AL_NONE);
StreamDecoderTask = AssignTask (StreamDecoderTaskFunc, 1024,
"audio stream decoder");
(void) driver; // eat compiler warning (void) driver; // eat compiler warning
return 0; return 0;
@@ -208,11 +216,7 @@ openAL_Uninit (void)
{ {
int i; int i;
if (StreamDecoderTask) UninitStreamDecoder ();
{
ConcludeTask (StreamDecoderTask);
StreamDecoderTask = NULL;
}
for (i = 0; i < NUM_SOUNDSOURCES; ++i) for (i = 0; i < NUM_SOUNDSOURCES; ++i)
{ {
+7 -54
View File
@@ -19,15 +19,10 @@
#include "sound.h" #include "sound.h"
#include "sndintrn.h" #include "sndintrn.h"
#include "libs/compiler.h" #include "libs/compiler.h"
#include "libs/tasklib.h"
#include "libs/inplib.h" #include "libs/inplib.h"
#include "libs/memlib.h" #include "libs/memlib.h"
static Task FadeTask;
static SIZE TTotal;
static SIZE volume_end;
int musicVolume = NORMAL_VOLUME; int musicVolume = NORMAL_VOLUME;
float musicVolumeScale; float musicVolumeScale;
float sfxVolumeScale; float sfxVolumeScale;
@@ -160,66 +155,24 @@ SetSpeechVolume (float volume)
audio_Sourcef (soundSource[SPEECH_SOURCE].handle, audio_GAIN, volume); audio_Sourcef (soundSource[SPEECH_SOURCE].handle, audio_GAIN, volume);
} }
static int
fade_task (void *data)
{
SIZE TDelta, volume_beg;
DWORD StartTime, CurTime;
Task task = (Task) data;
volume_beg = musicVolume;
StartTime = CurTime = GetTimeCounter ();
do
{
SleepThreadUntil (CurTime + ONE_SECOND / 120);
CurTime = GetTimeCounter ();
if ((TDelta = (SIZE) (CurTime - StartTime)) > TTotal)
TDelta = TTotal;
SetMusicVolume ((COUNT) (volume_beg + (SIZE)
((long) (volume_end - volume_beg) * TDelta / TTotal)));
} while (TDelta < TTotal);
FadeTask = 0;
FinishTask (task);
return 1;
}
DWORD DWORD
FadeMusic (BYTE end_vol, SIZE TimeInterval) FadeMusic (BYTE end_vol, SIZE TimeInterval)
{ {
DWORD TimeOut;
if (QuitPosted) // Don't make users wait for fades if (QuitPosted) // Don't make users wait for fades
TimeInterval = 0; TimeInterval = 0;
if (FadeTask) if (TimeInterval < 0)
{ TimeInterval = 0;
volume_end = musicVolume;
TTotal = 1;
do
TaskSwitch ();
while (FadeTask);
TaskSwitch ();
}
TTotal = TimeInterval; if (!SetMusicStreamFade (TimeInterval, end_vol))
if (TTotal <= 0) { // fade rejected, maybe due to TimeInterval==0
TTotal = 1; /* prevent divide by zero and negative fade */ SetMusicVolume (end_vol);
volume_end = end_vol; return GetTimeCounter ();
if (TTotal > 1 && (FadeTask = AssignTask (fade_task, 0,
"fade music")))
{
TimeOut = GetTimeCounter () + TTotal + 1;
} }
else else
{ {
SetMusicVolume (end_vol); return GetTimeCounter () + TimeInterval + 1;
TimeOut = GetTimeCounter ();
} }
return TimeOut;
} }
+97 -1
View File
@@ -21,10 +21,21 @@
#include "sound.h" #include "sound.h"
#include "sndintrn.h" #include "sndintrn.h"
#include "libs/tasklib.h" #include "libs/tasklib.h"
#include "libs/timelib.h"
#include "libs/threadlib.h"
#include "libs/log.h" #include "libs/log.h"
#include "libs/memlib.h" #include "libs/memlib.h"
static Task decoderTask;
static TimeCount musicFadeStartTime;
static sint32 musicFadeInterval;
static int musicFadeStartVolume;
static int musicFadeDelta;
// Mutex protects fade structures
static Mutex fade_mutex;
static void add_scope_data (TFB_SoundSource *source, uint32 bytes); static void add_scope_data (TFB_SoundSource *source, uint32 bytes);
@@ -491,7 +502,37 @@ process_stream (TFB_SoundSource *source)
} }
} }
int static void
processMusicFade (void)
{
TimeCount Now;
sint32 elapsed;
int newVolume;
LockMutex (fade_mutex);
if (!musicFadeInterval)
{ // there is no fade set
UnlockMutex (fade_mutex);
return;
}
Now = GetTimeCounter ();
elapsed = Now - musicFadeStartTime;
if (elapsed > musicFadeInterval)
elapsed = musicFadeInterval;
newVolume = musicFadeStartVolume + (long)musicFadeDelta * elapsed
/ musicFadeInterval;
SetMusicVolume (newVolume);
if (elapsed >= musicFadeInterval)
musicFadeInterval = 0; // fade is over
UnlockMutex (fade_mutex);
}
static int
StreamDecoderTaskFunc (void *data) StreamDecoderTaskFunc (void *data)
{ {
Task task = (Task)data; Task task = (Task)data;
@@ -502,6 +543,8 @@ StreamDecoderTaskFunc (void *data)
{ {
active_streams = 0; active_streams = 0;
processMusicFade ();
for (i = MUSIC_SOURCE; i < NUM_SOUNDSOURCES; ++i) for (i = MUSIC_SOURCE; i < NUM_SOUNDSOURCES; ++i)
{ {
TFB_SoundSource *source = &soundSource[i]; TFB_SoundSource *source = &soundSource[i];
@@ -719,3 +762,56 @@ GraphForegroundStream (uint8 *data, sint32 width, sint32 height)
return 1; return 1;
} }
// This function is normally called on the Starcon2Main thread
bool
SetMusicStreamFade (sint32 howLong, int endVolume)
{
bool ret = true;
LockMutex (fade_mutex);
if (howLong < 0)
howLong = 0;
musicFadeStartTime = GetTimeCounter ();
musicFadeInterval = howLong;
musicFadeStartVolume = musicVolume;
musicFadeDelta = endVolume - musicFadeStartVolume;
if (!musicFadeInterval)
ret = false; // reject
UnlockMutex (fade_mutex);
return ret;
}
int
InitStreamDecoder (void)
{
fade_mutex = CreateMutex ("Stream fade mutex", SYNC_CLASS_AUDIO);
if (!fade_mutex)
return -1;
decoderTask = AssignTask (StreamDecoderTaskFunc, 1024,
"audio stream decoder");
if (!decoderTask)
return -1;
return 0;
}
void
UninitStreamDecoder (void)
{
if (decoderTask)
{
ConcludeTask (decoderTask);
decoderTask = NULL;
}
if (fade_mutex)
{
DestroyMutex (fade_mutex);
fade_mutex = NULL;
}
}
+6 -1
View File
@@ -17,6 +17,9 @@
#ifndef STREAM_H #ifndef STREAM_H
#define STREAM_H #define STREAM_H
int InitStreamDecoder (void);
void UninitStreamDecoder (void);
void PlayStream (TFB_SoundSample *sample, uint32 source, bool looping, void PlayStream (TFB_SoundSample *sample, uint32 source, bool looping,
bool scope, bool rewind); bool scope, bool rewind);
void StopStream (uint32 source); void StopStream (uint32 source);
@@ -24,8 +27,10 @@ void PauseStream (uint32 source);
void ResumeStream (uint32 source); void ResumeStream (uint32 source);
void SeekStream (uint32 source, uint32 pos); void SeekStream (uint32 source, uint32 pos);
BOOLEAN PlayingStream (uint32 source); BOOLEAN PlayingStream (uint32 source);
int StreamDecoderTaskFunc (void *data);
int GraphForegroundStream (uint8 *data, sint32 width, sint32 height); int GraphForegroundStream (uint8 *data, sint32 width, sint32 height);
// returns TRUE if the fade was accepted by stream decoder
bool SetMusicStreamFade (sint32 howLong, int endVolume);
#endif #endif