Skip to content

Detecting audiocallback invocation after a pause, and purging residual samples - #8587

Merged
IliasBergstromMuse merged 2 commits into
audacity:masterfrom
IliasBergstromMuse:8469-buffer-residuals-after-pause-better-solution
Apr 3, 2025
Merged

IliasBergstromMuse merged 2 commits into
audacity:masterfrom
IliasBergstromMuse:8469-buffer-residuals-after-pause-better-solution

Conversation

@IliasBergstromMuse

@IliasBergstromMuse IliasBergstromMuse commented Apr 2, 2025 •

Copy link
Copy Markdown
Contributor

Resolves: #8469

Only when pausing, setting track volume to 0, and unpausing, there is playback from the mPlaybackBuffer RingBuffer samples that is a residual from the previous playback.

With full start/stop, this doesn't happen.

Identified cause:
While the buffers written to the RingBuffers in ProcessPlaybackSlices are 0-value samples, for a few callbacks the samples consumed in FillOutputBuffers from the RingBuffers are non-zero, because they are residuals from before the pause.
With pause, the RingBuffer content is intact, with stop/start these are cleared, hence the difference in behaviour.

Solution:
When resuming playback from pause, any samples pending from before pause, are now purged and not used.

  • I signed CLA
  • The title of the pull request describes an issue it addresses
  • If changes are extensive, then there is a sequence of easily reviewable commits
  • Each commit's message describes its purpose and effects
  • There are no behavior changes unnecessary for the stated purpose of the PR

Recommended:

  • Each commit compiles and runs on my machine without known undesirable changes of behavior

Test cases:

  • Play back audio, press spacebar/pause, and change volume to 0, and then press spacebar again. The sound should immediately reflect the new volume.
  • Optionally try the above with a few audio interfaces and buffer size settings to be sure it's stable across the spectrum.

@IliasBergstromMuse
IliasBergstromMuse force-pushed the 8469-buffer-residuals-after-pause-better-solution branch from e655f19 to 3d56380 Compare April 2, 2025 09:29
@IliasBergstromMuse IliasBergstromMuse changed the title Detecting if audiocallback is invoked after a pause, to purge samples from before Detecting audiocallback invocation after a pause, and purging residual samples Apr 2, 2025
@IliasBergstromMuse IliasBergstromMuse self-assigned this Apr 2, 2025
@IliasBergstromMuse
IliasBergstromMuse marked this pull request as ready for review April 2, 2025 09:54

@saintmatthieu saintmatthieu left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the approach is a good one. I raised a clarification/simplification point I'd like to discuss before approving.

Comment thread au3/libraries/lib-audio-io/AudioIO.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor: to make it a bit simpler, did you consider/try calling ->Get with framesPerBuffer as last argument in all calls? Looks like RingBuffer::Get has some protection against the user asking for too many samples.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did that and it worked, so yes, a bit less math needed indeed! I didn't think of Get having that protection.

Comment thread au3/libraries/lib-audio-io/AudioIO.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would not work if this statement were executed without the if clause (meaning state would be stored even if false) ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed removing the check breaks the functionality.
I'm guessing it is because of the order of execution, but haven't checked more deeply.

Comment thread au3/libraries/lib-audio-io/AudioIO.cpp Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that we want/need to do this purge only once when transitioning from pause to play, right? In which case, a small simplification would be to call here const auto wasPaused = mWasPaused.exchange(false). Then we wouldn't need the

    if (!IsPaused()) {
        mWasPaused.store(false, std::memory_order_relaxed);
    }

at the end of the method.
If that makes sense, then mWasPaused should probably be renamed to mPurgeNeeded or something like this.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then I'd need to move this out of the for loop, and have an extra for loop. Which actually may be just as well, since then it can be extracted into it's own method. I'll give it a spin!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An extra loop ? I meant

   const auto wasPaused = mWasPaused.exchange(false);
    for (unsigned n = 0; n < numPlaybackChannels; ++n) {
        //! Purging queued samples from before pause.
        if (wasPaused && !IsPaused()) {
            auto totalPurged = static_cast<int>(nAvailable);

wouldn't that work? (Not that I want to restrain your from giving it its own method :) )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, that should have worked too, but you made me look twice and I remembered that it's better to extract to its own method even if I iterate over n channels an extra time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saintmatthieu Initially I thought this name was best because it describes what it detects. But you're right it's better renamed (and I also moved it), since it's only re-set in the purging logic.

@IliasBergstromMuse
IliasBergstromMuse force-pushed the 8469-buffer-residuals-after-pause-better-solution branch from c64f40b to 31f6f5c Compare April 2, 2025 14:28
@IliasBergstromMuse
IliasBergstromMuse force-pushed the 8469-buffer-residuals-after-pause-better-solution branch from 31f6f5c to 78b6ec0 Compare April 3, 2025 07:32
@IliasBergstromMuse
IliasBergstromMuse merged commit 9e6ec65 into audacity:master Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When starting playback by pressing the Space key, volume changes with delay

2 participants