1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

SamplerPluginDemo: Add defensive checks in constructor to guard against missing sample resource

This commit is contained in:
reuk 2024-07-10 11:40:32 +01:00
parent 7ead20d575
commit 17fe23c95f
No known key found for this signature in database
GPG key ID: FCB43929F012EE5C

View file

@ -571,14 +571,14 @@ public:
class MemoryAudioFormatReaderFactory final : public AudioFormatReaderFactory class MemoryAudioFormatReaderFactory final : public AudioFormatReaderFactory
{ {
public: public:
MemoryAudioFormatReaderFactory (const void* sampleDataIn, size_t dataSizeIn) explicit MemoryAudioFormatReaderFactory (MemoryBlock mb)
: sampleData (sampleDataIn), : memoryBlock (std::make_shared<MemoryBlock> (std::move (mb)))
dataSize (dataSizeIn) {
{} }
std::unique_ptr<AudioFormatReader> make (AudioFormatManager& manager) const override std::unique_ptr<AudioFormatReader> make (AudioFormatManager& manager) const override
{ {
return makeAudioFormatReader (manager, sampleData, dataSize); return makeAudioFormatReader (manager, memoryBlock->getData(), memoryBlock->getSize());
} }
std::unique_ptr<AudioFormatReaderFactory> clone() const override std::unique_ptr<AudioFormatReaderFactory> clone() const override
@ -587,8 +587,7 @@ public:
} }
private: private:
const void* sampleData; std::shared_ptr<MemoryBlock> memoryBlock;
size_t dataSize;
}; };
//============================================================================== //==============================================================================
@ -2115,25 +2114,28 @@ public:
{ {
if (auto inputStream = createAssetInputStream ("cello.wav")) if (auto inputStream = createAssetInputStream ("cello.wav"))
{ {
MemoryBlock mb;
inputStream->readIntoMemoryBlock (mb); inputStream->readIntoMemoryBlock (mb);
readerFactory.reset (new MemoryAudioFormatReaderFactory (mb.getData(), mb.getSize())); readerFactory = std::make_unique<MemoryAudioFormatReaderFactory> (std::move (mb));
} }
// Set up initial sample, which we load from a binary resource if (readerFactory != nullptr)
{
AudioFormatManager manager; AudioFormatManager manager;
manager.registerBasicFormats(); manager.registerBasicFormats();
auto reader = readerFactory->make (manager);
jassert (reader != nullptr); // Failed to load resource!
auto sound = samplerSound; if (auto reader = readerFactory->make (manager))
auto sample = std::unique_ptr<Sample> (new Sample (*reader, 10.0)); {
auto sample = std::make_unique<Sample> (*reader, 10.0);
auto lengthInSeconds = sample->getLength() / sample->getSampleRate(); auto lengthInSeconds = sample->getLength() / sample->getSampleRate();
sound->setLoopPointsInSeconds ({lengthInSeconds * 0.1, lengthInSeconds * 0.9 }); samplerSound->setLoopPointsInSeconds ({ lengthInSeconds * 0.1, lengthInSeconds * 0.9 });
sound->setSample (std::move (sample)); samplerSound->setSample (std::move (sample));
}
}
// Start with the max number of voices // Start with the max number of voices
for (auto i = 0; i != maxVoices; ++i) for (auto i = 0; i != maxVoices; ++i)
synthesiser.addVoice (new MPESamplerVoice (sound)); synthesiser.addVoice (new MPESamplerVoice (samplerSound));
} }
void prepareToPlay (double sampleRate, int) override void prepareToPlay (double sampleRate, int) override
@ -2590,7 +2592,6 @@ private:
CommandFifo<SamplerAudioProcessor> commands; CommandFifo<SamplerAudioProcessor> commands;
MemoryBlock mb;
std::unique_ptr<AudioFormatReaderFactory> readerFactory; std::unique_ptr<AudioFormatReaderFactory> readerFactory;
std::shared_ptr<MPESamplerSound> samplerSound = std::make_shared<MPESamplerSound>(); std::shared_ptr<MPESamplerSound> samplerSound = std::make_shared<MPESamplerSound>();
MPESynthesiser synthesiser; MPESynthesiser synthesiser;