I downloaded a sourde code from github that converts images into wavetables, it’s from 3 years ago so i believe something change in the frame work… I’m getting this error:
‘initializing’: cannot convert from ‘std::unique_ptr<juce::FileOutputStream,std::default_deletejuce::FileOutputStream>’ to ‘juce::FileOutputStream *’
This is because a in the meantime a lot of functions that returned pointers that the caller must take ownership of changed their signature to return a std::unique_ptr instead of the raw pointer. This is the modern C++ way of managing object lifetime, as you cannot forget to call delete on such objects and create a memory leak this way. Looking at the code above I can see some leaks, which shows that unique ptrs are a good idea, especially for beginners The code block above rewritten with those modern approaches would look something like, I’m fan of using auto a lot to avoid long type names
if(fileDialog.browseForFileToSave(true))
{
// Pipe audio data to a file
auto outputFile = fileDialog.getResult();
auto size = static_cast<int> (AudioOutBuffer[0].size());
// There is no real reason to allocate an audio buffer with new, its a container itself which can safely live on the stack
AudioSampleBuffer buffer (2, size);
buffer.clear();
// buffer was a pointer before, so dereferencing it here. Better: Modify CopyAudioToBuffer to take a reference instead of a pointer
CopyAudioToBuffer (&buffer);
// Same as with the buffer above, just keep it on the stack
WavAudioFormat wavFile;
// File::createOutputStream also changed to return a unique pointer
auto outputStream = outputFile.createOutputStream();
// WavAudioFormat::createWriterFor has not been updated yet and returns a raw pointer. We should wrap it in a unique pointer
// One special thing here: it this function will take ownership of the stream passed in in case it could create a writer.
// As there is no error checking if creation worked at all this code assumes that it will always create a writer and will always release the stream
std::unique_ptr<AudioFormatWriter> streamWriter (wavFile.createWriterFor(outputStream.release(), 44100, 2, 16, nullptr, 0));
streamWriter->writeFromAudioSampleBuffer (buffer, 0, size);
// When using unique pointers they will delete their content automatically here without calling delete
}
}