Skip to content

11098 device change fixes - #11446

Merged
luapmartin merged 9 commits into
audacity:masterfrom
luapmartin:11098-device-change-fixes
Jul 28, 2026
Merged

luapmartin merged 9 commits into
audacity:masterfrom
luapmartin:11098-device-change-fixes

Conversation

@luapmartin

@luapmartin luapmartin commented Jul 15, 2026 •

Copy link
Copy Markdown
Contributor

Original PR: #11245

Resolves: #11098
Resolves: #9443
Resolves: #11102

  • 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

QA:

  • Testflow test cases have been run

@luapmartin luapmartin self-assigned this Jul 15, 2026
@coderabbitai

coderabbitai Bot commented Jul 15, 2026 •

Copy link
Copy Markdown

Review Change Stack

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

The change routes audio configuration through PlaybackController, coordinating stream teardown, reconfiguration, and playback resumption. Paused positions are preserved across supported changes, explicit repositioning clears pending resumes, playback accepts optional start-time overrides, monitoring is restored after device changes, and preference edits are staged until confirmation. New au3audio and preferences tests have independent CMake options.

Estimated code review effort: 4 (Complex) | ~60 minutes

Sequence Diagram(s)

sequenceDiagram
  participant User
  participant Preferences
  participant PlaybackController
  participant Au3AudioDevicesProvider
  participant Au3AudioEngine
  participant Au3Player
  User->>Preferences: change audio configuration
  Preferences->>PlaybackController: apply configuration
  PlaybackController->>Au3AudioDevicesProvider: apply device or stream setting
  Au3AudioDevicesProvider->>Au3AudioEngine: stop and reconfigure monitoring
  PlaybackController->>Au3Player: resume playback or retain paused position
Loading
🚥 Pre-merge checks | ✅ 4
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The changes align with all three linked issues: device changes, playback-time audio settings, and rescan/resume behavior.
Out of Scope Changes check ✅ Passed The playback, preferences, and test changes all support the linked audio device and playback-state fixes.
Title check ✅ Passed The title is concise and clearly matches the PR’s main focus on device-change fixes.
Description check ✅ Passed The description includes the linked issues, checklist items, and QA section, with only the short motivation summary missing.

Warning

Review ran into problems

🔥 Problems

Linked repositories: Public OSS repositories can only analyze public repositories installed in this organization. No linked repositories were analyzed; skipped musescore/musescore.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@luapmartin
luapmartin requested a review from kryksyh July 16, 2026 18:43

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/au3audio/internal/au3audiodevicesprovider.cpp (1)

226-249: 🩺 Stability & Availability | 🔵 Trivial | ⚡ Quick win

Use RAII for m_monitoringRestoreDepth to ensure exception safety.

If change() throws an exception or otherwise interrupts the flow, m_monitoringRestoreDepth will remain incremented, permanently preventing input monitoring from being restored in future operations. Wrapping the depth counter in an RAII guard ensures it is reliably decremented regardless of how the block exits.

🛡️ Proposed fix
     audioEngine()->stopMonitoring();
 
-    ++m_monitoringRestoreDepth;
-    change();
-    --m_monitoringRestoreDepth;
+    {
+        struct DepthGuard {
+            int& depth;
+            DepthGuard(int& d) : depth(d) { ++depth; }
+            ~DepthGuard() { --depth; }
+        } guard(m_monitoringRestoreDepth);
+        
+        change();
+    }
 
     if (wasMonitoring) {

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 5e1ddcb0-c1de-4227-bf4a-c7857d1d3c09

📥 Commits

Reviewing files that changed from the base of the PR and between f08188ca94755025dde5205f4130a8f4f98e1278 and 63a644e75b811f704605bda6f8bf9a3744b242e1.

📒 Files selected for processing (9)
  • src/au3audio/internal/au3audiodevicesprovider.cpp
  • src/au3audio/internal/au3audiodevicesprovider.h
  • src/au3audio/tests/au3audiodevicesprovider_tests.cpp
  • src/playback/internal/au3/au3player.cpp
  • src/playback/internal/playbackcontroller.cpp
  • src/playback/internal/playbackcontroller.h
  • src/playback/iplaybackcontroller.h
  • src/playback/tests/playbackcontroller_tests.cpp
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.cpp
🚧 Files skipped from review as they are similar to previous changes (4)
  • src/playback/iplaybackcontroller.h
  • src/playback/internal/playbackcontroller.h
  • src/playback/tests/playbackcontroller_tests.cpp
  • src/playback/internal/playbackcontroller.cpp

@luapmartin
luapmartin force-pushed the 11098-device-change-fixes branch from e05c17e to 5daedc1 Compare July 17, 2026 17:09

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (2)
src/playback/tests/playbackcontroller_tests.cpp (1)

1108-1114: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Require fallback playback instead of only rejecting the stale position.

These tests currently pass when togglePlay() never calls play. Assert one play(std::nullopt) call while retaining the stale-position prohibition.

  • src/playback/tests/playbackcontroller_tests.cpp#L1108-L1114: require playback of the fresh selection.
  • src/playback/tests/playbackcontroller_tests.cpp#L1147-L1154: require playback after Select All.
  • src/playback/tests/playbackcontroller_tests.cpp#L1183-L1188: require normal playback after discarding the out-of-range resume.
  • src/playback/tests/playbackcontroller_tests.cpp#L1303-L1308: require normal playback after Stop clears the pending resume.
src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp (1)

259-283: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Exercise the ASIO flag this test claims to cover.

Stage false from an applied true value to cover the important engaged-optional<bool> false path.

Proposed test addition
 TEST_F(CommonAudioApiConfigurationModelTests, Apply_NonStreamSettings_GoDirectlyToTheProvider)
 {
+    ON_CALL(*m_provider, asioUseDeviceSampleRate())
+        .WillByDefault(Return(true));
+
     m_model->defaultSampleRateValueSelected(48000);
     m_model->defaultSampleFormatSelected("16-bit");
     m_model->setAutomaticCompensationEnabled(true);
+    m_model->setAsioUseDeviceSampleRate(false);

     EXPECT_CALL(*m_provider, setDefaultSampleRate(48000)).Times(1);
     EXPECT_CALL(*m_provider, setDefaultSampleFormat("16-bit")).Times(1);
     EXPECT_CALL(*m_provider, setAutomaticCompensationEnabled(true)).Times(1);
+    EXPECT_CALL(*m_provider, setAsioUseDeviceSampleRate(false)).Times(1);

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: cb18bede-35c0-499c-a765-96f84a4d864d

📥 Commits

Reviewing files that changed from the base of the PR and between 63a644e75b811f704605bda6f8bf9a3744b242e1 and 5daedc13d4b01a27fc9f9bc938d85bbca362fffa.

📒 Files selected for processing (31)
  • CMakeLists.txt
  • src/au3audio/CMakeLists.txt
  • src/au3audio/internal/au3audiodevicesprovider.cpp
  • src/au3audio/internal/au3audiodevicesprovider.h
  • src/au3audio/internal/au3audioengine.cpp
  • src/au3audio/internal/au3audioengine.h
  • src/au3audio/tests/CMakeLists.txt
  • src/au3audio/tests/au3audiodevicesprovider_tests.cpp
  • src/au3audio/tests/mocks/audioenginemock.h
  • src/audio/iaudiodevicesprovider.h
  • src/audio/iaudioengine.h
  • src/audio/tests/mocks/audiodevicesprovidermock.h
  • src/playback/internal/au3/au3player.cpp
  • src/playback/internal/au3/au3player.h
  • src/playback/internal/playbackcontroller.cpp
  • src/playback/internal/playbackcontroller.h
  • src/playback/iplaybackcontroller.h
  • src/playback/iplayer.h
  • src/playback/tests/environment.cpp
  • src/playback/tests/mocks/playbackcontrollermock.h
  • src/playback/tests/mocks/playermock.h
  • src/playback/tests/playbackcontroller_tests.cpp
  • src/preferences/CMakeLists.txt
  • src/preferences/qml/Audacity/Preferences/AudioPreferencesPage.qml
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.cpp
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.h
  • src/preferences/qml/Audacity/Preferences/internal/AudioApiSection.qml
  • src/preferences/qml/Audacity/Preferences/internal/BufferAndLatencySection.qml
  • src/preferences/qml/Audacity/Preferences/internal/SampleRateSection.qml
  • src/preferences/tests/CMakeLists.txt
  • src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp
💤 Files with no reviewable changes (2)
  • src/preferences/qml/Audacity/Preferences/internal/SampleRateSection.qml
  • src/preferences/qml/Audacity/Preferences/internal/AudioApiSection.qml
🚧 Files skipped from review as they are similar to previous changes (14)
  • src/au3audio/internal/au3audioengine.h
  • CMakeLists.txt
  • src/au3audio/CMakeLists.txt
  • src/playback/iplayer.h
  • src/au3audio/tests/CMakeLists.txt
  • src/playback/iplaybackcontroller.h
  • src/playback/tests/mocks/playermock.h
  • src/playback/tests/environment.cpp
  • src/playback/internal/au3/au3player.h
  • src/au3audio/tests/mocks/audioenginemock.h
  • src/preferences/qml/Audacity/Preferences/internal/BufferAndLatencySection.qml
  • src/playback/internal/playbackcontroller.h
  • src/playback/internal/playbackcontroller.cpp
  • src/au3audio/internal/au3audiodevicesprovider.cpp

@luapmartin
luapmartin force-pushed the 11098-device-change-fixes branch from 5daedc1 to 0c2268f Compare July 18, 2026 10:42

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp (1)

74-76: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Reorder fixture members to ensure the system under test is destroyed before its dependencies.

In C++, class members are destroyed in the reverse order of their declaration. Currently, m_model (the system under test) is declared before its dependencies (m_provider and m_controller), meaning it will be destroyed last.

If m_model ever accesses these dependencies during its destruction (for example, to unregister event listeners or observers), it could lead to a use-after-free crash since the mocks would have already been destroyed. Declaring the dependencies first prevents this potential risk.

♻️ Proposed refactor
-    std::unique_ptr<CommonAudioApiConfigurationModel> m_model;
     std::shared_ptr<audio::AudioDevicesProviderMock> m_provider;
     std::shared_ptr<playback::PlaybackControllerMock> m_controller;
+    std::unique_ptr<CommonAudioApiConfigurationModel> m_model;

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro

Run ID: 43ca653a-ecc4-469c-943d-68f32589e282

📥 Commits

Reviewing files that changed from the base of the PR and between 5daedc13d4b01a27fc9f9bc938d85bbca362fffa and 0c2268f9624728eb2c8d78d417d17902a25a8398.

📒 Files selected for processing (6)
  • CMakeLists.txt
  • src/playback/tests/mocks/playbackcontrollermock.h
  • src/preferences/CMakeLists.txt
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.h
  • src/preferences/tests/CMakeLists.txt
  • src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp
🚧 Files skipped from review as they are similar to previous changes (5)
  • src/preferences/CMakeLists.txt
  • CMakeLists.txt
  • src/preferences/tests/CMakeLists.txt
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.h
  • src/playback/tests/mocks/playbackcontrollermock.h

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp (1)

154-169: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Assert withSingleStreamRestart(_) is skipped when nothing stream-affecting changes.

Both tests confirm individual setters aren't called but don't assert the batch scope itself is skipped. Per the design intent (non-stream settings "bypass the playback controller", no-edit apply pushes nothing), a regression that wraps these no-op paths in withSingleStreamRestart() anyway — causing a spurious, audible stream interruption — would still pass.

♻️ Proposed additions
 TEST_F(CommonAudioApiConfigurationModelTests, Apply_WithoutEdits_PushesNothing)
 {
     //! [THEN] No setter is called at all
+    EXPECT_CALL(*m_controller, withSingleStreamRestart(_)).Times(0);
     EXPECT_CALL(*m_controller, setAudioApi(_)).Times(0);
 TEST_F(CommonAudioApiConfigurationModelTests, Apply_NonStreamSettings_GoDirectlyToTheProvider)
 {
     //! [THEN] They are pushed to the provider, not through the stream restart
+    EXPECT_CALL(*m_controller, withSingleStreamRestart(_)).Times(0);
     EXPECT_CALL(*m_provider, setDefaultSampleRate(48000)).Times(1);

Also applies to: 289-307


ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI (base), Organization UI (inherited)

Review profile: CHILL

Plan: Pro Plus

Run ID: 430c9abf-6596-4d2d-b725-4997944ead6a

📥 Commits

Reviewing files that changed from the base of the PR and between 0c2268f9624728eb2c8d78d417d17902a25a8398 and 22403ff9cb469c025a96794aae12b4084b73fd3f.

📒 Files selected for processing (33)
  • CMakeLists.txt
  • src/au3audio/CMakeLists.txt
  • src/au3audio/internal/au3audiodevicesprovider.cpp
  • src/au3audio/internal/au3audiodevicesprovider.h
  • src/au3audio/internal/au3audioengine.cpp
  • src/au3audio/internal/au3audioengine.h
  • src/au3audio/tests/CMakeLists.txt
  • src/au3audio/tests/au3audiodevicesprovider_tests.cpp
  • src/au3audio/tests/mocks/audioenginemock.h
  • src/audio/iaudiodevicesprovider.h
  • src/audio/iaudioengine.h
  • src/audio/tests/mocks/audiodevicesprovidermock.h
  • src/playback/internal/au3/au3player.cpp
  • src/playback/internal/au3/au3player.h
  • src/playback/internal/playbackcontroller.cpp
  • src/playback/internal/playbackcontroller.h
  • src/playback/iplaybackcontroller.h
  • src/playback/iplayer.h
  • src/playback/playbacktypes.h
  • src/playback/tests/environment.cpp
  • src/playback/tests/mocks/playbackcontrollermock.h
  • src/playback/tests/mocks/playermock.h
  • src/playback/tests/playbackcontroller_tests.cpp
  • src/preferences/CMakeLists.txt
  • src/preferences/qml/Audacity/Preferences/AudioPreferencesPage.qml
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.cpp
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.h
  • src/preferences/qml/Audacity/Preferences/internal/AudioApiSection.qml
  • src/preferences/qml/Audacity/Preferences/internal/BufferAndLatencySection.qml
  • src/preferences/qml/Audacity/Preferences/internal/SampleRateSection.qml
  • src/preferences/tests/CMakeLists.txt
  • src/preferences/tests/commonaudioapiconfigurationmodel_tests.cpp
  • src/record/internal/au3/au3record.cpp
💤 Files with no reviewable changes (1)
  • src/preferences/qml/Audacity/Preferences/internal/AudioApiSection.qml
🚧 Files skipped from review as they are similar to previous changes (19)
  • src/au3audio/CMakeLists.txt
  • src/playback/tests/environment.cpp
  • src/preferences/qml/Audacity/Preferences/AudioPreferencesPage.qml
  • CMakeLists.txt
  • src/playback/tests/mocks/playermock.h
  • src/preferences/CMakeLists.txt
  • src/preferences/tests/CMakeLists.txt
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.h
  • src/playback/tests/mocks/playbackcontrollermock.h
  • src/audio/iaudiodevicesprovider.h
  • src/preferences/qml/Audacity/Preferences/internal/BufferAndLatencySection.qml
  • src/preferences/qml/Audacity/Preferences/internal/SampleRateSection.qml
  • src/playback/iplayer.h
  • src/audio/tests/mocks/audiodevicesprovidermock.h
  • src/playback/internal/playbackcontroller.h
  • src/playback/tests/playbackcontroller_tests.cpp
  • src/preferences/qml/Audacity/Preferences/commonaudioapiconfigurationmodel.cpp
  • src/au3audio/internal/au3audiodevicesprovider.cpp
  • src/playback/internal/playbackcontroller.cpp

Comment thread src/audio/iaudioengine.h
// Includes transitional busy/inactive stream states.
virtual std::optional<AudioStreamDescriptor> currentStream() const = 0;

struct StartStreamOptions {

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.

Nit and not for now.
It is odd for me we have a struct StartStreamOptions that has only a subset of the fields in AudioIOStartStreamOptions.
IMO we should have a struct compatible with the later.
Probably with some default constructor that already populate it just as GetDefaultOptions and some helpers to convert it to AudioIOStartStreamOptions

if (pStartTime && *pStartTime >= t1) {
// Starting at/past the region end extends to the project end.
const std::optional<double> streamStartTime = opts.streamStartTime.has_value() ? opts.streamStartTime : pStartTime;
if (streamStartTime && *streamStartTime >= t1) {

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.

Nit (personal taste). IMO using streamStartTime.value() is more intuitive than this dereference syntax

@@ -30,12 +30,8 @@ class PlaybackController : public IPlaybackController, public audio::IAudioStrea
{
public:
muse::GlobalInject<au::playback::IPlaybackConfiguration> playbackConfiguration;
<<<<<<< HEAD

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.

All other commits are completely broken (won't compile) due this thing here.
I don't know if worth it to fix this where this was inserted.

luapmartin and others added 9 commits July 28, 2026 20:34
Move the stream start override into PlayTracksOptions

Extend playback to project end when the resume position is at/past the region end

Allow starting the playback stream at an explicit position
…ller

This allows different instances to stop and resume the playback when changing audio device settings
…e iterations when device changed during active playback
Fix playback tests after rebase

Document deliberate suspension and sample-rate policies
@luapmartin
luapmartin force-pushed the 11098-device-change-fixes branch from 8581543 to 38c1c9f Compare July 28, 2026 18:34
@luapmartin
luapmartin merged commit b5f54ec into audacity:master Jul 28, 2026
10 checks passed
@luapmartin
luapmartin deleted the 11098-device-change-fixes branch July 28, 2026 18:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants