xiaohongshu
xiaohongshu
juce-best-practices
Professional JUCE development guide covering realtime safety, threading, memory management, modern C++, and audio plugin best practices. Use when writing JUCE code, reviewing for realtime safety, implementing audio threads, managing parameters, or learning JUCE patterns and idioms.
Full skill instructions
Comprehensive guide to professional JUCE framework development with modern C++ patterns, realtime safety, thread management, and audio plugin best practices.
NEVER allocate, deallocate, lock, or block in the audio thread (processBlock).
❌ Memory Allocation
// BAD - allocates memory
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
std::vector<float> temp(buffer.getNumSamples()); // WRONG!
auto dynamicArray = new float[buffer.getNumSamples()]; // WRONG!
}
✅ Pre-allocate in prepare()
// GOOD - pre-allocate once
void prepareToPlay(double sampleRate, int maxBlockSize) {
tempBuffer.setSize(2, maxBlockSize);
workingMemory.resize(maxBlockSize);
}
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
// Use pre-allocated buffers
tempBuffer.makeCopyOf(buffer);
}
❌ Mutex Locks
// BAD - blocks audio thread
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
const ScopedLock lock(parameterLock); // WRONG!
auto value = sharedParameter;
}
✅ Use Atomics or Lock-Free Structures
// GOOD - lock-free communication
std::atomic<float> cutoffFrequency{1000.0f};
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
auto freq = cutoffFrequency.load(); // Lock-free!
filter.setCutoff(freq);
}
❌ System Calls and I/O
// BAD - system calls in audio thread
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
DBG("Processing " << buffer.getNumSamples()); // WRONG! (console I/O)
saveAudioToFile(buffer); // WRONG! (file I/O)
}
new or deletestd::vector::push_back() (may allocate)ScopedLock, std::lock_guard)std::cout, DBG())malloc or free-fno-exceptions)JUCE audio plugins operate in two separate thread contexts:
✅ Message Thread → Audio Thread
// Use atomics for simple values
std::atomic<float> gain{1.0f};
// In UI (message thread)
void sliderValueChanged(Slider* slider) {
gain.store(slider->getValue()); // Safe!
}
// In audio thread
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
auto currentGain = gain.load(); // Safe!
buffer.applyGain(currentGain);
}
✅ Audio Thread → Message Thread
// Use AsyncUpdater for async callbacks
class MyProcessor : public AudioProcessor,
private AsyncUpdater {
private:
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) override {
// Process audio...
if (needsUIUpdate) {
triggerAsyncUpdate(); // Safe!
}
}
void handleAsyncUpdate() override {
// This runs on message thread - safe to update UI
editor->updateDisplay();
}
};
✅ Complex Data with Lock-Free Queue
// For passing complex data (MIDI, analysis, etc.)
juce::AbstractFifo fifo;
std::vector<float> ringBuffer;
// Audio thread writes
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
int start1, size1, start2, size2;
fifo.prepareToWrite(buffer.getNumSamples(), start1, size1, start2, size2);
// Write to ring buffer...
fifo.finishedWrite(size1 + size2);
}
// Message thread reads
void timerCallback() {
int start1, size1, start2, size2;
fifo.prepareToRead(fifo.getNumReady(), start1, size1, start2, size2);
// Read from ring buffer...
fifo.finishedRead(size1 + size2);
}
| Action | Message Thread | Audio Thread |
|---|---|---|
| Allocate memory | ✅ OK | ❌ Never |
| File I/O | ✅ OK | ❌ Never |
| Lock mutex | ✅ OK | ❌ Never |
| Update UI | ✅ OK | ❌ Never |
| Process audio | ❌ Never | ✅ OK |
| Use atomics | ✅ OK | ✅ OK |
✅ Use RAII for Resource Management
// GOOD - automatic cleanup
class MyProcessor : public AudioProcessor {
private:
std::unique_ptr<Reverb> reverb;
std::vector<float> delayBuffer;
void prepareToPlay(double sr, int maxBlockSize) override {
reverb = std::make_unique<Reverb>(); // Auto-managed
delayBuffer.resize(sr * 2.0); // Auto-managed
}
// No manual cleanup needed - automatic destruction
};
✅ Stack Allocation is Realtime-Safe
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
// OK - stack allocation
float tempGain = 0.5f;
int sampleCount = buffer.getNumSamples();
// Process...
}
✅ Allocate Once, Reuse Many Times
class MyProcessor : public AudioProcessor {
private:
AudioBuffer<float> tempBuffer;
std::vector<float> fftData;
void prepareToPlay(double sr, int maxBlockSize) override {
// Allocate once
tempBuffer.setSize(2, maxBlockSize);
fftData.resize(2048);
}
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) override {
// Reuse pre-allocated buffers
tempBuffer.makeCopyOf(buffer);
// Process using tempBuffer...
}
};
✅ Structured Bindings (C++17)
auto [min, max] = buffer.findMinMax(0, buffer.getNumSamples());
✅ if constexpr (C++17)
template<typename SampleType>
void process(AudioBuffer<SampleType>& buffer) {
if constexpr (std::is_same_v<SampleType, float>) {
// Float-specific optimizations
} else {
// Double-specific code
}
}
✅ std::optional (C++17)
std::optional<float> tryGetParameter(const String& id) {
if (auto* param = parameters.getParameter(id))
return param->getValue();
return std::nullopt;
}
✅ Mark Non-Mutating Methods const
class Filter {
public:
float getCutoff() const { return cutoff; } // const!
float getResonance() const { return resonance; }
void setCutoff(float f) { cutoff = f; } // not const - mutates state
private:
float cutoff = 1000.0f;
float resonance = 0.707f;
};
✅ Cleaner Iteration
// OLD WAY
for (int ch = 0; ch < buffer.getNumChannels(); ++ch) {
auto* channelData = buffer.getWritePointer(ch);
for (int i = 0; i < buffer.getNumSamples(); ++i) {
channelData[i] *= gain;
}
}
// MODERN WAY
for (int ch = 0; ch < buffer.getNumChannels(); ++ch) {
auto* data = buffer.getWritePointer(ch);
for (int i = 0; i < buffer.getNumSamples(); ++i) {
data[i] *= gain;
}
}
// Or use JUCE's helpers
buffer.applyGain(gain);
✅ Use JUCE's Buffer Methods
// Apply gain
buffer.applyGain(0.5f);
// Clear buffer
buffer.clear();
// Copy buffer
AudioBuffer<float> copy;
copy.makeCopyOf(buffer);
// Add buffers
outputBuffer.addFrom(0, 0, inputBuffer, 0, 0, numSamples);
✅ Use ValueTree for Hierarchical State
ValueTree state("PluginState");
state.setProperty("version", "1.0.0", nullptr);
ValueTree parameters("Parameters");
parameters.setProperty("gain", 0.5f, nullptr);
parameters.setProperty("frequency", 1000.0f, nullptr);
state.appendChild(parameters, nullptr);
// Serialize
auto xml = state.toXmlString();
// Deserialize
auto loadedState = ValueTree::fromXml(xml);
✅ Standard Parameter Management
class MyProcessor : public AudioProcessor {
public:
MyProcessor()
: parameters(*this, nullptr, "Parameters", createParameterLayout())
{
}
private:
AudioProcessorValueTreeState parameters;
static AudioProcessorValueTreeState::ParameterLayout createParameterLayout() {
std::vector<std::unique_ptr<RangedAudioParameter>> params;
params.push_back(std::make_unique<AudioParameterFloat>(
"gain",
"Gain",
NormalisableRange<float>(0.0f, 1.0f),
0.5f
));
return { params.begin(), params.end() };
}
};
✅ Smooth Parameter Changes to Avoid Zipper Noise
class MyProcessor : public AudioProcessor {
private:
SmoothedValue<float> gainSmooth;
void prepareToPlay(double sr, int maxBlockSize) override {
gainSmooth.reset(sr, 0.05); // 50ms ramp time
}
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) override {
// Update target from parameter
auto* gainParam = parameters.getRawParameterValue("gain");
gainSmooth.setTargetValue(*gainParam);
// Apply smoothed value
for (int i = 0; i < buffer.getNumSamples(); ++i) {
auto gain = gainSmooth.getNextValue();
for (int ch = 0; ch < buffer.getNumChannels(); ++ch) {
buffer.setSample(ch, i, buffer.getSample(ch, i) * gain);
}
}
}
};
✅ Efficient Parameter Updates
void parameterChanged(const String& parameterID, float newValue) override {
if (parameterID == "cutoff") {
cutoffFrequency.store(newValue);
}
// Don't do heavy processing here - mark for update instead
}
✅ Implement getStateInformation/setStateInformation
void getStateInformation(MemoryBlock& destData) override {
auto state = parameters.copyState();
std::unique_ptr<XmlElement> xml(state.createXml());
copyXmlToBinary(*xml, destData);
}
void setStateInformation(const void* data, int sizeInBytes) override {
std::unique_ptr<XmlElement> xml(getXmlFromBinary(data, sizeInBytes));
if (xml && xml->hasTagName(parameters.state.getType())) {
parameters.replaceState(ValueTree::fromXml(*xml));
}
}
✅ Handle Backward Compatibility
void setStateInformation(const void* data, int sizeInBytes) override {
auto xml = getXmlFromBinary(data, sizeInBytes);
int version = xml->getIntAttribute("version", 1);
if (version == 1) {
// Load v1 format and migrate
migrateFromV1(xml);
} else if (version == 2) {
// Load v2 format
parameters.replaceState(ValueTree::fromXml(*xml));
}
}
✅ Calculate Once, Use Many Times
// BAD
for (int i = 0; i < buffer.getNumSamples(); ++i) {
auto coeff = std::exp(-1.0f / (sampleRate * timeConstant)); // Recalculated every sample!
}
// GOOD
auto coeff = std::exp(-1.0f / (sampleRate * timeConstant)); // Calculate once
for (int i = 0; i < buffer.getNumSamples(); ++i) {
// Use coeff
}
✅ JUCE's dsp::SIMDRegister
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
auto* data = buffer.getWritePointer(0);
auto gain = dsp::SIMDRegister<float>(0.5f);
for (int i = 0; i < buffer.getNumSamples(); i += gain.size()) {
auto samples = dsp::SIMDRegister<float>::fromRawArray(data + i);
samples *= gain;
samples.copyToRawArray(data + i);
}
}
✅ Prevent Denormals for CPU Performance
void prepareToPlay(double sr, int maxBlockSize) override {
// Enable flush-to-zero
juce::FloatVectorOperations::disableDenormalisedNumberSupport();
}
// Or add DC offset in feedback loops
float processSample(float input) {
static constexpr float denormalPrevention = 1.0e-20f;
feedbackState = input + feedbackState * 0.99f + denormalPrevention;
return feedbackState;
}
repaint() from Audio Thread// WRONG
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
// Process...
if (editor)
editor->repaint(); // BAD! UI call from audio thread
}
✅ Solution: Use AsyncUpdater
void processBlock(AudioBuffer<float>& buffer, MidiBuffer&) {
// Process...
triggerAsyncUpdate(); // Schedules UI update for message thread
}
void handleAsyncUpdate() override {
if (editor)
editor->repaint(); // GOOD! On message thread
}
// WRONG - assumes 44.1kHz
float delayTimeInSamples = 0.5f * 44100.0f;
✅ Solution: Update in prepareToPlay
void prepareToPlay(double sampleRate, int maxBlockSize) override {
delayTimeInSamples = 0.5f * sampleRate; // Correct for any sample rate
}
// WRONG
void prepareToPlay(double sr, int maxBlockSize) override {
// Forgot to call base class!
mySetup(sr, maxBlockSize);
}
✅ Solution: Always Call Base
void prepareToPlay(double sr, int maxBlockSize) override {
AudioProcessor::prepareToPlay(sr, maxBlockSize);
mySetup(sr, maxBlockSize);
}
AudioProcessorValueTreeState for parametersprepareToPlay()processBlock()DBG() or logging in processBlock()Remember: Audio plugins must be realtime-safe, thread-aware, and robust. Follow these best practices to create professional, stable plugins that work reliably across all DAWs and platforms.
xiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience