Skip to content

Commit

Permalink
Fade the master gain separately; slight change in meter level
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Licameli committed May 16, 2024
1 parent 774b81c commit 03cdb98
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 12 deletions.
35 changes: 23 additions & 12 deletions libraries/lib-audio-io/AudioIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,8 @@ bool AudioIO::AllocateBuffers(
mMasterBuffers[ii] = std::make_unique<RingBuffer>(
floatSample, playbackBufferSize);

// Bug 1763 - We must fade in from zero to avoid a click on starting.
mOldMasterGain = 0.0f;
mOldChannelGains.resize(mPlaybackSequences.size());
size_t iBuffer = 0;
for (unsigned int i = 0; i < mPlaybackSequences.size(); i++) {
Expand Down Expand Up @@ -2592,20 +2594,16 @@ void AudioIoCallback::AddToOutputChannel(unsigned int chan,
if (drop || mForceFadeOut.load(std::memory_order_relaxed) || IsPaused())
gain = 0.0;

// Output volume emulation: possibly copy meter samples, then
// apply volume, then copy to the output buffer
if (outputMeterFloats != outputFloats)
for ( unsigned i = 0; i < len; ++i)
outputMeterFloats[numPlaybackChannels*i+chan] +=
gain*tempBuf[i];

// DV: We use gain to emulate panning.
// Let's keep the old behavior for panning.
const float goal = gain * ExpGain(GetMixerOutputVol());
const float goal = gain;
// if no microfades, jump in volume.
float diff = (mbMicroFades ? goal - laggingChannelGain : 0.0f);
for (size_t i = 0; i < len; ++i) {
outputFloats[numPlaybackChannels * i + chan] +=
(goal - diff) * tempBuf[i];
const auto term = (goal - diff) * tempBuf[i];
outputFloats[numPlaybackChannels * i + chan] += term;
if (outputMeterFloats != outputFloats)
// The level shown in output meters also sums the track level
// microfaded for gain, pan, and mute, but before applying master gain
outputMeterFloats[numPlaybackChannels * i + chan] += term;
diff *= factor;
}
laggingChannelGain = goal - diff;
Expand Down Expand Up @@ -2733,6 +2731,19 @@ bool AudioIoCallback::FillOutputBuffers(

AddToOutputChannel(1, outputMeterFloats, outputFloats,
tempBufs[1], drop, len, *vt, gains[1]);

// Output volume emulation
const auto factor = decayFactor(mRate);
const float goal = ExpGain(GetMixerOutputVol());
// if no microfades, jump in volume.
auto diff = (mbMicroFades ? goal - mOldMasterGain : 0);
for (size_t i = 0; i < len; ++i) {
const auto multiplier = goal - diff;
outputFloats[numPlaybackChannels * i] *= multiplier;
outputFloats[numPlaybackChannels * i + 1] *= multiplier;
diff *= factor;
}
mOldMasterGain = goal - diff;
}

CallbackCheckCompletion(mCallbackReturn, len);
Expand Down
1 change: 1 addition & 0 deletions libraries/lib-audio-io/AudioIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ class AUDIO_IO_API AudioIoCallback /* not final */
// Old gain is used in playback in linearly interpolating
// the gain.
std::vector<OldChannelGains> mOldChannelGains;
float mOldMasterGain{};
// Temporary buffers, each as large as the playback buffers
std::vector<SampleBuffer> mScratchBuffers;
std::vector<float *> mScratchPointers; //!< pointing into mScratchBuffers
Expand Down

0 comments on commit 03cdb98

Please sign in to comment.