Saving a wav file from a buffer using Filechooser

Hi, I want to create a wav file from a buffer using filechooser in order to let select the user the location of the file. My code is this:

auto soundChooser = std::make_unique<juce::FileChooser>("Please save your IR");
    soundChooser->launchAsync(juce::FileBrowserComponent::openMode | juce::FileBrowserComponent::canSelectFiles, [this](const juce::FileChooser& chooser)
    {
        auto file = chooser.getResult();
        if (file.exists())
        {
            auto outputFile = chooser.getResult();
            std::unique_ptr<juce::FileOutputStream> outStream;
            outStream = outputFile.createOutputStream();

            juce::WavAudioFormat format;
            std::unique_ptr<juce::AudioFormatWriter> writer;
            writer.reset (format.createWriterFor (outStream.get(),
                                                  44100,
                                                  audioSampleBufferOut.getNumChannels(),
                                                  24,
                                                  {},
                                                  0));
            if (writer != nullptr)
                writer->writeFromAudioSampleBuffer (audioSampleBufferOut, 0, audioSampleBufferOut.getNumSamples());

            outStream.release();
        }

    });

My problem is that CMAKE is showing me the message: Variable ‘audioSampleBufferOut’ cannot be implicitly captured in a lambda with no capture-default specified.
I’m implementing this function in a subsection called by AudioProcessor (only is triggered once when you click button designed to save a file).

Thank you in advance!

The FileChooser is launched asynchronously.
So you have 2 things to note:

  • The instance you create (soundChooser) must live outside of the scope of the code you shared, until the asynchronous call ends.
  • The lambda you create must capture what it needs. Instead of [this](const juce::FileChooser& chooser) you need [&audioSampleBufferOut](const juce::FileChooser& chooser). Note that I added & before audioSampleBufferOut so it is captured by reference to avoid copying it. This assumes that, as well as soundChooser, audioSampleBufferOut will live as long as it needs to.

Learn more about lambda expressions here.

1 Like

Thanks! this helped me a lot, I got it!