Saving processed audio as a .wav file

Hello I have code that is meant to read an audio file, process it, and then save the processed audio as a .wav file. I am having an issue with that last part. I’m getting an error that says “Call to implicitly-deleted copy constructor of ‘std::unique_ptrjuce::AudioFormatWriter’” and I haven’t been able to find a way around it.

Here is where I initialize the process to save the audio file. This is in the PluginProcesser.h file:

void saveWavFile (juce::File, juce::AudioBuffer<float>, std::unique_ptr<juce::AudioFormatWriter>, juce::WavAudioFormat);

Here is the matching process in the PluginProcessor.cpp file. This is where I’m getting the error.

void BasicFilterAudioProcessor::prepareToPlay (double sampleRate, int samplesPerBlock)
{
    //Declaring variables for the process block.
    biquad.setFs(sampleRate);
    biquad2.setFs(sampleRate);
    //File path of source audio (NEED TO CHANGE TO YOUR LOCAL FILEPATH)
    maxiSample.load("/FILEPATH/");
    juce::AudioBuffer<float> buffer;
    //Delete any file that currently exists to ensure nothing is over-written.
    file.deleteFile();
    file.create();
    //Wav audio format variables
    juce::WavAudioFormat format;
    std::unique_ptr<juce::AudioFormatWriter> writer;
    saveWavFile(file, buffer, writer, format);
    
}

The saveWavFile process works perfectly and doesn’t have any errors. Unsure why I’m getting the implicitly-deleted copy constructor error. Any help would be appreciated! Been stuck on this for a while now.

Your saveWavFile function takes the std::unique_ptr argument “by copy”, but every std::unique_ptr disables copy, because it cannot be owned at different places.

The solution is either passing in by reference:

void saveWavFile (juce::File, juce::AudioBuffer<float>, std::unique_ptr<juce::AudioFormatWriter>&, juce::WavAudioFormat);

which keeps ownership at the callers.
Other solution is to move it into the function (invalidates the original):

saveWavFile (file, buffer, std::move (writer), format);

The whole function signature seems me a bit random. Either you pass in a writer object, in which case there is no need for the WavAudioFormat and the File. Or (preferred) you create the writer in the function:

void saveWavFile (juce::File file, juce::AudioBuffer<flaot>& buffer)
{
    juce::WavAudioFormat format;
    auto writer = format.createWriterFor (file, /* ... */);
    // ...
}

Last note: Have a look at AudioFormatManager.