Wav assets compiling along with the plugin

I’m relatively new to JUCE and C++ I was struggling for a little while loading in a file for use as a SamplerSound in a synth that I am creating until I realised I had to use an absolute path. This is a little bit of a pain as I can’t really predict where people will install my plugins and will have to change the path for every operating system I support.

So I was wondering if there was a way for me to load in all the wavs I will be using as assets and compile them into the plugin itself to bypass all of these issues?

1 Like

Of course - just add them to the file list in the projucer, and they’ll be embedded as binary literals in the BinaryData class.

2 Likes

Ah that’s great, thank you. So can that BinaryData be cast as a file and passed into the audio format manager ?

This is the code I have at the moment, I’,m just wondering how dramatically this will need to change and if so what it might need to change to:

File* file = new File(“path/to/file.wav”);

ScopedPointer reader = audioFormatManager.createReaderFor(*file);

The AudioFormatManager also takes an InputStream as well: AudioFormatManager::createReaderFor (InputStream* audioFileStream), and the memory can be turned into a MemoryInputStream.

IIRC:

MemoryInputStream* input = new MemoryInputStream (BinaryData::fille_wav, BinaryData::file_wav_size, false);
auto reader = audioFormatManager.createReaderFor (input);

HTH

EDIT: Fixed code after @jules reply…

1 Like

oh, careful there - createReaderFor takes ownership of the pointer you give it.

1 Like

Ah cool, I was wondering, but couldn’t figure out immediately…
(Reading it again now, it’s said in the docs… sorry)

1 Like

Thanks guys, works perfectly, exactly what I was trying to achieve!

Trying to get started with some simple playback of a short sample for a game sfx. I added a wav file via Projucer as a binary resource. The function playTestSound() works fine, but no sound is heard though my below simple example, so what am I doing wrong?

	MemoryInputStream* file = new MemoryInputStream(BinaryData::BrickHit_wav, BinaryData::BrickHit_wavSize, false);
	auto reader = formatManager.createReaderFor(file);
	MemoryInputStream(BinaryData::BrickHit_wav, BinaryData::BrickHit_wavSize, false));

	if (reader != nullptr)
	{
		std::unique_ptr<AudioFormatReaderSource> newSource(new AudioFormatReaderSource(reader, true));
		transportSource.setSource(newSource.get(), 0, nullptr, reader->sampleRate);
		transportSource.start();
	}

Your AudioFormatReaderSource gets deleted as soon as you leave the if (reader != nullptr) scope.

Does that has something to do with this; readerSource.reset(newSource.release());

What is the simplest way I can playback my wav file, beyond the nullptr scop, later in my code, whenever it is needed?

Ok I got it to work by adding the release and a few lines above what I had before, so it now looks like this;

	formatManager.registerBasicFormats();
	transportSource.addChangeListener(this);

	MemoryInputStream* file = new MemoryInputStream(BinaryData::BrickHit_wav, BinaryData::BrickHit_wavSize, false);
	auto reader = formatManager.createReaderFor(file);

	if (reader != nullptr)
	{
		std::unique_ptr<AudioFormatReaderSource> newSource(new AudioFormatReaderSource(reader, true));
		transportSource.setSource(newSource.get(), 0, nullptr, reader->sampleRate);
		readerSource.reset(newSource.release());
		transportSource.start();
	}

With that I can now play it any time it is need in my game! Thanks anyways.