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:
parent
7ead20d575
commit
17fe23c95f
1 changed files with 21 additions and 20 deletions
|
|
@ -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;
|
{
|
||||||
manager.registerBasicFormats();
|
AudioFormatManager manager;
|
||||||
auto reader = readerFactory->make (manager);
|
manager.registerBasicFormats();
|
||||||
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 lengthInSeconds = sample->getLength() / sample->getSampleRate();
|
auto sample = std::make_unique<Sample> (*reader, 10.0);
|
||||||
sound->setLoopPointsInSeconds ({lengthInSeconds * 0.1, lengthInSeconds * 0.9 });
|
auto lengthInSeconds = sample->getLength() / sample->getSampleRate();
|
||||||
sound->setSample (std::move (sample));
|
samplerSound->setLoopPointsInSeconds ({ lengthInSeconds * 0.1, lengthInSeconds * 0.9 });
|
||||||
|
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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue