From 9ca60ced76295a17428432f41f3684e554272d50 Mon Sep 17 00:00:00 2001 From: Bliblank Date: Fri, 23 Jan 2026 21:53:03 -0600 Subject: [PATCH] added a spinlock on the scope --- README.md | 4 ++-- src/synth/ScopeBuffer.cpp | 3 +++ src/synth/ScopeBuffer.h | 3 +++ src/synth/Synth.cpp | 6 ++++++ src/synth/Voice.h | 1 + src/ui/widgets/Scope/Scope.cpp | 2 +- src/ui/widgets/Scope/Scope.h | 1 + 7 files changed, 17 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index f9d6cc5..c136bdb 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ This synthesizer isn't very good, but it's neat :3 responds to note-on/note-off events - [x] Make midi/keyboard control cross-platform. Use case will mostly be Midi -> linux and Keyboard -> windows though -- [ ] Create oscillator class where the actual tone generation occurs. Multiple +- [x] Create oscillator class where the actual tone generation occurs. Multiple oscillators increase the sound complexity considerably - [x] Create a UI scope to visualize the synthesized composite waveform - [ ] Create wavetables for more complex tone generation. Needs to be selectable from ui @@ -60,7 +60,7 @@ Build. The script will build and install dependencies automatically On Windows (MSVC): ```PowerShell -.\scripts\build.ps1 +.\scripts\build.ps1 # builds in build/Debug/ ``` On Linux (GCC): diff --git a/src/synth/ScopeBuffer.cpp b/src/synth/ScopeBuffer.cpp index 8e9263f..d94d452 100644 --- a/src/synth/ScopeBuffer.cpp +++ b/src/synth/ScopeBuffer.cpp @@ -14,6 +14,9 @@ void ScopeBuffer::push(float sample) { // TODO: needs a mutex to prevent flickering // outputs value from the scope buffer, called by the scope widget void ScopeBuffer::read(std::vector& out) const { + + while(!spinLock_) { int x = 1 + 1; } + size_t w = writeIndex_.load(std::memory_order_relaxed); for (size_t i = 0; i < out.size(); i++) { size_t idx = (w + trigger_ + i * wavelength_ / out.size()) % buffer_.size(); diff --git a/src/synth/ScopeBuffer.h b/src/synth/ScopeBuffer.h index 571b9f5..9ddae93 100644 --- a/src/synth/ScopeBuffer.h +++ b/src/synth/ScopeBuffer.h @@ -21,6 +21,7 @@ public: void setWavelength(int32_t wavelength) { wavelength_ = wavelength; } int32_t trigger() { return trigger_; } int32_t wavelength() { return wavelength_; } + void spinlock(bool lock) { spinLock_ = lock; }; // NOTE: there are limits to the wavelengths that the scope can show cleanly due to the size of the audio buffer // at a buffer size of 256 at 44100hz the min visible steady frequency is ~172hz @@ -33,4 +34,6 @@ private: int32_t trigger_ = 0; // units in array indices int32_t wavelength_ = 400; + bool spinLock_ = false; + }; diff --git a/src/synth/Synth.cpp b/src/synth/Synth.cpp index 0e10cb6..b9b5929 100644 --- a/src/synth/Synth.cpp +++ b/src/synth/Synth.cpp @@ -92,6 +92,9 @@ void Synth::process(float* out, uint32_t nFrames, uint32_t sampleRate) { } } + // lock the scope from the buffer + scope_->spinlock(true); + for (uint32_t i = 0; i < nFrames; i++) { // updates internal buffered parameters for smoothing @@ -133,4 +136,7 @@ void Synth::process(float* out, uint32_t nFrames, uint32_t sampleRate) { } } + // unlock the scope from the buffer + scope_->spinlock(false); + } \ No newline at end of file diff --git a/src/synth/Voice.h b/src/synth/Voice.h index fa65795..79f12e5 100644 --- a/src/synth/Voice.h +++ b/src/synth/Voice.h @@ -63,6 +63,7 @@ private: // filters Filter filter1_; Filter filter2_; + // TODO: I think the filter's state being uninitialized is what's causing popping when a voice starts for the first time // paramstore pointer SmoothedParam* params_; diff --git a/src/ui/widgets/Scope/Scope.cpp b/src/ui/widgets/Scope/Scope.cpp index 44b7765..1769903 100644 --- a/src/ui/widgets/Scope/Scope.cpp +++ b/src/ui/widgets/Scope/Scope.cpp @@ -24,7 +24,7 @@ void Scope::setScopeBuffer(ScopeBuffer* buffer) { } void Scope::paintEvent(QPaintEvent*) { - if (!buffer_) return; + if(!buffer_) return; int32_t wavelength = buffer_->wavelength(); int32_t trigger = buffer_->trigger(); diff --git a/src/ui/widgets/Scope/Scope.h b/src/ui/widgets/Scope/Scope.h index 0689df9..0b6ad7e 100644 --- a/src/ui/widgets/Scope/Scope.h +++ b/src/ui/widgets/Scope/Scope.h @@ -32,4 +32,5 @@ private: ScopeBuffer* buffer_ = nullptr; std::vector samples_; QTimer timer_; + };