π π A Python library for audio.
C audioaudio-processingaudio-productionaudio-researchaudio-unitaugmentationjucemachine-learningpybind11pythonvst3vst3-host
Auto-delete output stream if constructor fails.
Peter Sobot010f90ba411c
diff --git a/pedalboard/io/AudioFile.h b/pedalboard/io/AudioFile.hindex 7f53864..03b5b1d 100644--- a/pedalboard/io/AudioFile.h+++ b/pedalboard/io/AudioFile.h@@ -473,6 +473,69 @@ int determineQualityOptionIndex(juce::AudioFormat *format,return qualityOptionIndex;}+/**+ * A tiny RAII wrapper around juce::FileOutputStream that+ * deletes the file on destruction if it was not written to.+ */+class AutoDeleteFileOutputStream : public juce::FileOutputStream {+public:+ AutoDeleteFileOutputStream(const juce::File &fileToWriteTo,+ size_t bufferSizeToUse = 16384,+ bool deleteFileOnDestruction = true)+ : juce::FileOutputStream(fileToWriteTo, bufferSizeToUse),+ deleteFileOnDestruction(deleteFileOnDestruction) {}++ static std::unique_ptr<AutoDeleteFileOutputStream>+ createOutputStream(const juce::File &fileToWriteTo,+ size_t bufferSizeToUse = 16384) {+ return std::make_unique<AutoDeleteFileOutputStream>(+ fileToWriteTo, bufferSizeToUse, !fileToWriteTo.existsAsFile());+ };++ juce::Result truncate() {+ deleteFileOnDestruction = false;+ return juce::FileOutputStream::truncate();+ }++ virtual bool setPosition(juce::int64 pos) override {+ deleteFileOnDestruction = false;+ return juce::FileOutputStream::setPosition(pos);+ }++ virtual bool write(const void *bytes, size_t len) override {+ if (!hasWrittenToFile) {+ setPosition(0);+ truncate();+ hasWrittenToFile = true;+ }++ deleteFileOnDestruction = false;+ return juce::FileOutputStream::write(bytes, len);+ }++ virtual bool writeRepeatedByte(juce::uint8 byte,+ size_t numTimesToRepeat) override {+ if (!hasWrittenToFile) {+ setPosition(0);+ truncate();+ hasWrittenToFile = true;+ }++ deleteFileOnDestruction = false;+ return juce::FileOutputStream::writeRepeatedByte(byte, numTimesToRepeat);+ }++ ~AutoDeleteFileOutputStream() override {+ if (deleteFileOnDestruction) {+ getFile().deleteFile();+ }+ }++private:+ bool deleteFileOnDestruction = false;+ bool hasWrittenToFile = false;+};+class WriteableAudioFile: public AudioFile,public std::enable_shared_from_this<WriteableAudioFile> {@@ -502,16 +565,13 @@ public:formatManager.registerBasicFormats();juce::File file(filename);- std::unique_ptr<juce::FileOutputStream> outputStream =- std::make_unique<juce::FileOutputStream>(file);+ std::unique_ptr<AutoDeleteFileOutputStream> outputStream =+ AutoDeleteFileOutputStream::createOutputStream(file);if (!outputStream->openedOk()) {throw std::domain_error("Unable to open audio file for writing: " +filename);}- outputStream->setPosition(0);- outputStream->truncate();-juce::AudioFormat *format =formatManager.findFormatForFileExtension(file.getFileExtension());diff --git a/tests/test_io.py b/tests/test_io.pyindex 62242a9..bc3e98a 100644--- a/tests/test_io.py+++ b/tests/test_io.py@@ -553,3 +553,11 @@ def test_bad_write_quality(tmp_path: pathlib.Path, extension: str, quality):filename = str(tmp_path / f"test.{extension}")with pytest.raises(ValueError):pedalboard.io.WriteableAudioFile(filename, samplerate=44100, quality=quality)+++def test_file_not_created_if_constructor_error_thrown(tmp_path: pathlib.Path):+ filename = str(tmp_path / f"test.wav")+ assert not os.path.exists(filename)+ with pytest.raises(ValueError):+ pedalboard.io.WriteableAudioFile(filename, samplerate=44100, quality="break")+ assert not os.path.exists(filename)