How to embed .wav files in a executable

Hey guys i am trying to build a MIDI sampler. I am quite new to JUCE and to understand it i am trying to run the examples (examples/Audio/AudioSynthesizerDemo.h), i see this project has a functionality to either play from a sampled sound or a pure sine wave. The sampled sound is located in examples/Assets/cello.wav. Now i have a few wav samples i want to “relate/assign” to different keys. How could it so when built the samples are already included? Any tips or guidance is more than welcome! Thanks!

If you’re using Projucer, you can drag the cello.wav file into the Resources/ section of the file explorer (or right-click on Resources and “add existing file”), and then ensure that file is included in “Binary Resources” by setting the flag in the right-hand panel.

Then, when you save and rebuild the project, BinaryData.h/.cpp will be re-generated with the included .wav file contents as a binary asset which you can access like this:

const void* data = BinaryData::cello_wav; // Pointer to the binary data
int dataSize = BinaryData::cell_wavSize; // Size of the data in bytes

If you’re using CMake instead (which you should, really), then include your .wav file in either Assets/ or Resources/ folder, and then use the following CMake code:

# Define a target for the binary data
juce_add_binary_data(BinaryData
    SOURCES
        path/to/your/assets/cello.wav
)

# Link the BinaryData target to your main target
target_link_libraries(MyAppTarget PRIVATE BinaryData)

… which you can later use, thus:

// Same idea as above:
const void* data = BinaryData::cello_wav; // Pointer to the binary data
int dataSize = BinaryData::cell_wavSize; // Size of the data in bytes

// Use it, somehow ..
juce::MemoryInputStream inputStream(data, static_cast<size_t>(dataSize), false);
juce::WavAudioFormat wavFormat;
std::unique_ptr<juce::AudioFormatReader> reader(wavFormat.createReaderFor(&inputStream, true));

if (reader != nullptr)
{
    // Use the reader to process the WAV data
}
2 Likes