Skip to content

Commit 030bb99

Browse files
Eric LaurentAndroid (Google) Code Review
authored andcommitted
Merge "audioflinger: fix noise when skipping to next song" into ics-mr1
2 parents 2858749 + f9c361d commit 030bb99

File tree

2 files changed

+35
-13
lines changed

2 files changed

+35
-13
lines changed

services/audioflinger/AudioFlinger.cpp

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7028,11 +7028,17 @@ void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
70287028

70297029
AudioFlinger::EffectChain::EffectChain(const wp<ThreadBase>& wThread,
70307030
int sessionId)
7031-
: mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0),
7031+
: mThread(wThread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
70327032
mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
70337033
mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
70347034
{
70357035
mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
7036+
sp<ThreadBase> thread = mThread.promote();
7037+
if (thread == 0) {
7038+
return;
7039+
}
7040+
mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
7041+
thread->frameCount();
70367042
}
70377043

70387044
AudioFlinger::EffectChain::~EffectChain()
@@ -7100,22 +7106,31 @@ void AudioFlinger::EffectChain::process_l()
71007106
}
71017107
bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
71027108
(mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
7103-
bool tracksOnSession = false;
7109+
// always process effects unless no more tracks are on the session and the effect tail
7110+
// has been rendered
7111+
bool doProcess = true;
71047112
if (!isGlobalSession) {
7105-
tracksOnSession = (trackCnt() != 0);
7106-
}
7113+
bool tracksOnSession = (trackCnt() != 0);
71077114

7108-
// if no track is active, input buffer must be cleared here as the mixer process
7109-
// will not do it
7110-
if (tracksOnSession &&
7111-
activeTrackCnt() == 0) {
7112-
size_t numSamples = thread->frameCount() * thread->channelCount();
7113-
memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7115+
if (!tracksOnSession && mTailBufferCount == 0) {
7116+
doProcess = false;
7117+
}
7118+
7119+
if (activeTrackCnt() == 0) {
7120+
// if no track is active and the effect tail has not been rendered,
7121+
// the input buffer must be cleared here as the mixer process will not do it
7122+
if (tracksOnSession || mTailBufferCount > 0) {
7123+
size_t numSamples = thread->frameCount() * thread->channelCount();
7124+
memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7125+
if (mTailBufferCount > 0) {
7126+
mTailBufferCount--;
7127+
}
7128+
}
7129+
}
71147130
}
71157131

71167132
size_t size = mEffects.size();
7117-
// do not process effect if no track is present in same audio session
7118-
if (isGlobalSession || tracksOnSession) {
7133+
if (doProcess) {
71197134
for (size_t i = 0; i < size; i++) {
71207135
mEffects[i]->process();
71217136
}

services/audioflinger/AudioFlinger.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1247,6 +1247,10 @@ class AudioFlinger :
12471247
// corresponding to a suspend all request.
12481248
static const int kKeyForSuspendAll = 0;
12491249

1250+
// minimum duration during which we force calling effect process when last track on
1251+
// a session is stopped or removed to allow effect tail to be rendered
1252+
static const int kProcessTailDurationMs = 1000;
1253+
12501254
void process_l();
12511255

12521256
void lock() {
@@ -1287,7 +1291,8 @@ class AudioFlinger :
12871291
void decTrackCnt() { android_atomic_dec(&mTrackCnt); }
12881292
int32_t trackCnt() { return mTrackCnt;}
12891293

1290-
void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt); }
1294+
void incActiveTrackCnt() { android_atomic_inc(&mActiveTrackCnt);
1295+
mTailBufferCount = mMaxTailBuffers; }
12911296
void decActiveTrackCnt() { android_atomic_dec(&mActiveTrackCnt); }
12921297
int32_t activeTrackCnt() { return mActiveTrackCnt;}
12931298

@@ -1338,6 +1343,8 @@ class AudioFlinger :
13381343
int16_t *mOutBuffer; // chain output buffer
13391344
volatile int32_t mActiveTrackCnt; // number of active tracks connected
13401345
volatile int32_t mTrackCnt; // number of tracks connected
1346+
int32_t mTailBufferCount; // current effect tail buffer count
1347+
int32_t mMaxTailBuffers; // maximum effect tail buffers
13411348
bool mOwnInBuffer; // true if the chain owns its input buffer
13421349
int mVolumeCtrlIdx; // index of insert effect having control over volume
13431350
uint32_t mLeftVolume; // previous volume on left channel

0 commit comments

Comments
 (0)