Fix audio fade

This commit is contained in:
Varun Patil 2020-05-04 19:33:24 +05:30
parent c94323ec16
commit 501d6ae608
2 changed files with 22 additions and 5 deletions

View File

@ -285,7 +285,9 @@ void ALStream::stopStream()
if (thread) if (thread)
{ {
#ifndef __EMSCRIPTEN__
SDL_WaitThread(thread, 0); SDL_WaitThread(thread, 0);
#endif
thread = 0; thread = 0;
needsRewind.set(); needsRewind.set();
} }
@ -312,6 +314,7 @@ void ALStream::startStream(float offset)
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
streamData(); streamData();
thread = (SDL_Thread *) 1;
#else #else
thread = createSDLThread thread = createSDLThread
<ALStream, &ALStream::streamData>(this, threadName); <ALStream, &ALStream::streamData>(this, threadName);
@ -420,7 +423,7 @@ void ALStream::streamData()
#ifndef __EMSCRIPTEN__ #ifndef __EMSCRIPTEN__
while (true) while (true)
{ {
streamDevice(); update();
if (threadTermReq) if (threadTermReq)
break; break;
@ -430,7 +433,7 @@ void ALStream::streamData()
} }
void ALStream::update() { void ALStream::update() {
if (threadTermReq) if (!thread || threadTermReq)
return; return;
shState->rtData().syncPoint.passSecondarySync(); shState->rtData().syncPoint.passSecondarySync();

View File

@ -284,10 +284,8 @@ void AudioStream::finiFadeOutInt()
void AudioStream::startFadeIn() void AudioStream::startFadeIn()
{ {
#ifdef __EMSCRIPTEN__
/* Previous fadein should always be terminated in play() */ /* Previous fadein should always be terminated in play() */
assert(!fadeIn.thread); assert(!fadeIn.thread);
#endif
fadeIn.rqFini.clear(); fadeIn.rqFini.clear();
fadeIn.rqTerm.clear(); fadeIn.rqTerm.clear();
@ -295,6 +293,7 @@ void AudioStream::startFadeIn()
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
fadeInThread(); fadeInThread();
fadeIn.thread = (SDL_Thread *) 1;
#else #else
fadeIn.thread = createSDLThread fadeIn.thread = createSDLThread
<AudioStream, &AudioStream::fadeInThread>(this, fadeIn.threadName); <AudioStream, &AudioStream::fadeInThread>(this, fadeIn.threadName);
@ -357,11 +356,18 @@ void AudioStream::fadeOutThread()
void AudioStream::fadeInThread() void AudioStream::fadeInThread()
{ {
#ifndef __EMSCRIPTEN__
while (true) while (true)
#else
if (fadeIn.thread)
#endif
{ {
if (fadeIn.rqTerm) if (fadeIn.rqTerm)
#ifndef __EMSCRIPTEN__
break; break;
#else
return;
#endif
lockStream(); lockStream();
/* Fade in duration is always 1 second */ /* Fade in duration is always 1 second */
@ -377,7 +383,12 @@ void AudioStream::fadeInThread()
setVolume(FadeIn, 1.0f); setVolume(FadeIn, 1.0f);
unlockStream(); unlockStream();
#ifdef __EMSCRIPTEN__
fadeIn.thread = 0;
return;
#else
break; break;
#endif
} }
/* Quadratic increase (not really the same as /* Quadratic increase (not really the same as
@ -386,12 +397,15 @@ void AudioStream::fadeInThread()
unlockStream(); unlockStream();
#ifndef __EMSCRIPTEN__
SDL_Delay(AUDIO_SLEEP); SDL_Delay(AUDIO_SLEEP);
#endif
} }
} }
void AudioStream::update() void AudioStream::update()
{ {
fadeOutThread(); fadeOutThread();
fadeInThread();
stream.update(); stream.update();
} }