e-zero
August 6, 2024, 5:20pm
1
I have a binary ir.wav file (loaded with Projucer) and I load that file with the following code:
auto* binaryData = BinaryData::ir_wav;
auto binaryDataSize = BinaryData::ir_wavSize;
juce::MemoryInputStream memoryInputStream(binaryData, binaryDataSize, false);
I then try to load the ‘memoryInputStream’ into an AudioFormatReader with the following code:
juce::WavAudioFormat wavFormat;
std::unique_ptr<juce::AudioFormatReader> reader(wavFormat.createReaderFor(memoryInputStream, true));
but doing that gives me the following build/syntax error:
Error (active) E0413 no suitable conversion function from "juce::MemoryInputStream" to "juce::InputStream *" exists ... PluginProcessor.cpp
I also tried:
std::unique_ptr<juce::AudioFormatReader> reader(wavFormat.createReaderFor(&memoryInputStream, true));
and:
std::unique_ptr<juce::AudioFormatReader> reader(wavFormat.createReaderFor(std::move(&memoryInputStream), true));
but those two approaches resulted in a run-time error faulting to the ‘delete_scalar.cpp’ file. The error displayed is as follows:
A breakpoint instruction (__debugbreak() statement or a similar call) was executed…
I’m not sure what else I can try. Any ideas how to fix this, or why this may be happening?
Thanks.
The MemoryInputStream needs to be dynamically allocated, as the reader will delete it when it the reader id deleted.
From the createReaderFor documentation
sourceStream : the stream to read from - the AudioFormatReader object that is returned will delete this stream when it no longer needs it.
e-zero
August 7, 2024, 1:16am
3
I tried this code to ‘dynamically load’ the MemoryInputStream:
auto* binaryData = BinaryData::ir_wav;
auto binaryDataSize = BinaryData::ir_wavSize;
// Step 1: Dynamically create and initialize MemoryBlock
auto memoryBlock = std::make_unique<juce::MemoryBlock>(binaryData, static_cast<size_t>(binaryDataSize));
// Step 2: Dynamically create a MemoryInputStream
std::unique_ptr<juce::MemoryInputStream> memoryInputStream =
std::make_unique<juce::MemoryInputStream>(*memoryBlock, false);
// Step 3: Create a WavAudioFormat instance
juce::WavAudioFormat wavFormat;
// Step 4: Create an AudioFormatReader for the WAV data
std::unique_ptr<juce::AudioFormatReader> reader(
wavFormat.createReaderFor(std::move(memoryInputStream.get()), true)
);
However, now I get an Access Violation Error. Any help with what I am doing wrong?
I believe you want to get rid of the move and use release instead of get.
e-zero
August 7, 2024, 1:40am
5
Thanks that worked! I simplified my code and it seems to work great:
auto* binaryData = BinaryData::ir_wav;
auto binaryDataSize = BinaryData::ir_wavSize;
auto memoryInputStream = std::make_unique<juce::MemoryInputStream>(binaryData, binaryDataSize, false);
// Load the WAV data into an AudioFormatReader
juce::WavAudioFormat wavFormat;
std::unique_ptr<juce::AudioFormatReader> reader(wavFormat.createReaderFor(memoryInputStream.release(), true));
2 Likes