Just a small code that I use for some swapable variables, such as audio or other things that I want to mess with while everything is running. EG: load a new audio while the previous audio is still playing, no interruptions.
Please, if you find this stupid be nice and explain why so I can learn.
Iām a very old āguruā programmer, but I suffer from mental problems, so sometimes I miss the little thingsā¦
Hereās an example on how I would use this for audio. I declare:
WusikSwapable originalAudio;
Now I get get the current playiung one with
originalAudio.getCurrent();
And the other one with
originalAudio.getOther();
When Iām ready to swamp each other I call
originalAudio.readyToSwap();
And in the processor block, before anything else I call
originalAudio.check();
This way I can load something to the next buffer without disrupting the audio and while keeping a copy of the previous buffer at all times and also while having just one code to handle how the audio plays.
template <class WType>
class WusikSwapable
{
public:
WusikSwapable()
{
current = 0;
swap = false;
};
//
void readyToSwap() { swap = true; }
//
WType& getCurrent()
{
return variables[current];
}
//
WType& getOther()
{
return variables[1 - current];
}
//
bool check()
{
if (swap)
{
current = 1 - current;
swap = false;
return true;
}
//
return false;
}
//
bool getSwap() { return swap; }
//
private:
WType variables[2];
int current;
bool swap;
};
