Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 17 additions & 17 deletions plugins/AudioVisualizer/desktop/AudioVisualizerDesktop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ AudioVisualizerDesktop::~AudioVisualizerDesktop() = default;


void AudioVisualizerDesktop::render() {
std::lock_guard<std::mutex> lock(stateMutex);

ImPlot::SetCurrentContext(implotContext);

addConnectionSettings();
Expand Down Expand Up @@ -154,10 +156,6 @@ void AudioVisualizerDesktop::addDeviceSettings() {
bool isSelected = (cfg.deviceName == DEFAULT_LOOPBACK_DEVICE_NAME);
if (ImGui::Selectable((DEFAULT_LOOPBACK_DEVICE_NAME + "##default_loopback").c_str(), isSelected)) {
cfg.deviceName = DEFAULT_LOOPBACK_DEVICE_NAME;
recorder->stopRecording();
int loopbackIdx = AudioRecorder::Recorder::getDefaultOutputLoopbackIndex();
if (loopbackIdx >= 0)
recorder->startRecording(loopbackIdx);
}
if (isSelected)
ImGui::SetItemDefaultFocus();
Expand All @@ -174,8 +172,6 @@ void AudioVisualizerDesktop::addDeviceSettings() {
#endif
if (ImGui::Selectable(deviceNameWithId.c_str())) {
cfg.deviceName = device.name;
recorder->stopRecording();
recorder->startRecording(device.index);
}

if (cfg.deviceName == device.name)
Expand Down Expand Up @@ -362,6 +358,21 @@ std::optional<std::unique_ptr<UdpPacket, void (*)(UdpPacket *)> > AudioVisualize
if (sceneName != "audio_spectrum")
return std::nullopt;

std::lock_guard<std::mutex> stateLock(stateMutex);

if (cfg.deviceName != currentDeviceName) {
recorder->stopRecording();
currentDeviceName = cfg.deviceName;
}

if (cfg.deviceName == DEFAULT_LOOPBACK_DEVICE_NAME) {
int expectedLoopback = AudioRecorder::Recorder::getDefaultOutputLoopbackIndex();
if (recorder->isRecording() && expectedLoopback >= 0 && expectedLoopback != recorder->getCurrentDeviceIndex()) {
spdlog::info("Default output device changed, switching loopback to device {}", expectedLoopback);
recorder->stopRecording();
}
}

if (!recorder->isRecording())
{
int deviceIndex = -1;
Expand Down Expand Up @@ -403,17 +414,6 @@ std::optional<std::unique_ptr<UdpPacket, void (*)(UdpPacket *)> > AudioVisualize
}
recorder->startRecording(deviceIndex);
}
else if (cfg.deviceName == DEFAULT_LOOPBACK_DEVICE_NAME)
{
// Auto-switch: check if the default output device changed
int expectedLoopback = AudioRecorder::Recorder::getDefaultOutputLoopbackIndex();
if (expectedLoopback >= 0 && expectedLoopback != recorder->getCurrentDeviceIndex())
{
spdlog::info("Default output device changed, switching loopback to device {}", expectedLoopback);
recorder->stopRecording();
recorder->startRecording(expectedLoopback);
}
}

auto samplesOpt = recorder->getLastSamples();
if (!samplesOpt.has_value())
Expand Down
4 changes: 4 additions & 0 deletions plugins/AudioVisualizer/desktop/AudioVisualizerDesktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ class AudioVisualizerDesktop final : public Plugins::DesktopPlugin
std::shared_mutex lastErrorMutex;
std::string lastError;

// Concurrency protection
std::mutex stateMutex;
std::string currentDeviceName;

// Beat detection
std::unique_ptr<BeatDetector> beatDetector;
bool beat_detected;
Expand Down
4 changes: 2 additions & 2 deletions react-web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"lodash": "^4.18.1",
"lucide-react": "^0.525.0",
"react": "^19.2.6",
"react-colorful": "^5.6.1",
"react-colorful": "^5.7.0",
"react-dom": "^19.2.6",
"react-router-dom": "^7.15.0",
"sonner": "^2.0.7",
Expand All @@ -37,7 +37,7 @@
},
"devDependencies": {
"@types/lodash": "^4.17.24",
"@types/node": "^25.6.0",
"@types/node": "^25.7.0",
"@types/react": "^19.2.14",
"@types/react-dom": "^19.2.3",
"@types/uuid": "^10.0.0",
Expand Down
Loading
Loading