Skip to content

Move file operations to a background thread #6

@seclorum

Description

@seclorum

Move all file operations to a background thread to prevent audio glitches during synthesis and preset management.

Relevant code:

Preset file operations (likely to cause audio glitches if blocking):
Source/PluginProcessor.cpp:269-283

// Rename a preset
void SimdSynthAudioProcessor::changeProgramName(int index, const juce::String &newName) {
    if (index >= 0 && index < presetNames.size()) {
        juce::File oldFile = juce::File::getSpecialLocation(juce::File::userApplicationDataDirectory)
                                 .getChildFile("SimdSynth/Presets")
                                 .getChildFile(presetNames[index] + ".json");
        juce::File newFile = juce::File::getSpecialLocation(juce::File::userApplicationDataDirectory)
                                 .getChildFile("SimdSynth/Presets")
                                 .getChildFile(newName + ".json");
        if (oldFile.existsAsFile()) {
            oldFile.moveFileTo(newFile);
            presetNames[index] = newName;
        }
    }
}

Main audio thread:
Source/PluginProcessor.cpp:556-572

void SimdSynthAudioProcessor::processBlock(juce::AudioBuffer<float> &buffer, juce::MidiBuffer &midiMessages) {
    juce::ScopedNoDenormals noDenormals;
    auto totalNumOutputChannels = getTotalNumOutputChannels();
    buffer.clear();
    // Prepare oversampled buffer
    juce::dsp::AudioBlock<float> block(buffer);
    auto oversampledBlock = oversampling->processSamplesUp(block);
    float sampleRate = filter.sampleRate * oversampling->getOversamplingFactor();
    double blockStartTime = currentTime;
    // Update parameters for inactive voices
    for (int i = 0; i < MAX_VOICE_POLYPHONY; ++i) {
        if (!voices[i].active) {
            voices[i].wavetableType = static_cast<int>(*wavetableTypeParam);
            voices[i].attack = *attackTimeParam;
            voices[i].decay = *decayTimeParam;
            voices[i].sustain = *sustainLevelParam;
            /*...*/
    }
    /*...*/
}

Any blocking I/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions