Loading wav sample

SDK for VST 3 audio plug-in and host development.
Post Reply
humt
Posts: 9
Joined: Sun Mar 22, 2020 7:41 pm

Loading wav sample

Post by humt »

I am trying to build a sample based VST instrument.

I have my samples ready and currently I have stored the data as static array of floats in header files which are then compiled directly.
However since they are statically compiled, the samples take up memory space irrespective of wether i need them or not in a given context.

Now I want a way to load and then unload sample data from the memory at runtime depending on say some particular MIDI message event.

So what would be the way to read arrays of sample into memory and then remove them from memory at runtime.

Any example would be apreciated. Thanks a lot.

Wanderer
Posts: 24
Joined: Sun Jun 24, 2018 2:24 pm

Re: Loading wav sample

Post by Wanderer »

How you stored a sample in array of floats in header? Show code (don't need to show full sample only short part of it).

I asume that you fill static array of floats in header without function, this load it in to memory automatically. You have to create something like that:

Code: Select all

// header.h
void loadSample_Piano(std::vector<float> & wave)
{
    // fill the wave with piano sample stored in this header.h file in this function
}
I am not sure if that help, but if not, try split it in to declaration and definition (*.h and *.cpp) file.

humt
Posts: 9
Joined: Sun Mar 22, 2020 7:41 pm

Re: Loading wav sample

Post by humt »

Thanks a ton.

Here my current setup.

Each of the wav data is stored in files wav0.h to wav128.h .

Code: Select all

static std::vector<float> wav0 ={ .wav data here)

I have a file audio_data.h

Code: Select all

class AudioData
{
public:
	static std::vector<float> getAudioVector(int pitch);
};
Then I have the corresponding audio_data.cpp file which defines the function declared above as:

Code: Select all

#include "../h/audio_data.h"

#include "../audio/wav0.h"
#include "../audio/wav1.h"
..
 includes from wav0.h to wav128.h corresponding to each of 128 MIDI pitch data.
#include "../audio/wav128.h"

std::array<float> AudioData::getAudioVector(int pitch)
{
	switch(pitch)
    {
        case 0:
            return wav0 converted from array to vector;
            break;
	// handles all cases from 0 to 128 here
        case 128:
            return wav128 converted from array to vector;
            break;
        default:
            break;

    }
}

Finally on noteon event in my voice processor, I have a vector which is filled with
the corresponding vector returned by

getAudioVector(int pitch)

as defined above and is then used to populate the output.

I think this means all data gets into the memory before it is fetched in noteon.

Wanderer
Posts: 24
Joined: Sun Jun 24, 2018 2:24 pm

Re: Loading wav sample

Post by Wanderer »

This example should load wave in to memory only when Wav0Load will be called. But i am not sure if it will be working. And free memory when Wav0Unload is called. For more control over memory use float * wav0 instead std::vector;

Code: Select all

// wav0.h

static std::vector<float> wav0;

void Wav0Load()
{
    if (wav0.size() == 0)
    {
        wav0.resize(numSamples);
        // fill the wav0 with data
    }
}

void Wav0Unload()
{
    if (wav0.size() > 0)
    {
        wav0.clear();
        wav0.shrink_to_fit();
    }
}

humt
Posts: 9
Joined: Sun Mar 22, 2020 7:41 pm

Re: Loading wav sample

Post by humt »

Sorry I am a noob trrying to get around memory stuff in C++.

I have a doubt about your comment
// fill the wav0 with data
in this code.

Code: Select all

void Wav0Load()
{
    if (wav0.size() == 0)
    {
        wav0.resize(numSamples);
        // fill the wav0 with data
    }
}
How do I fill wav0 with data ?
In order to fill, the data must be and is already in memory - since the data is stored statically.
It is already occupying some memory somewhere on the stack.

How instead do I load the wav file dynamically only if needed ?

Hope I could make my query clear :)

Wanderer
Posts: 24
Joined: Sun Jun 24, 2018 2:24 pm

Re: Loading wav sample

Post by Wanderer »

Aha with your last post it is more clear what you are asking.

If you want to be sure that you load wave in to memory only when you need, just read wave file from disk (HDD) (don't store it inside *.h) and then load it in to wave.

humt
Posts: 9
Joined: Sun Mar 22, 2020 7:41 pm

Re: Loading wav sample

Post by humt »

How do I bundle the wav files with the VST plugin ?

If i save the samples in say Resources directory, how do I read the wav files from the resources directory.

Any example on how to bundle wav files and then read them from resources folder will be appreciated.

Post Reply