2022-03-25 05:00:26 -04:00
|
|
|
//
|
|
|
|
// FILE: WaveMix.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-11-27 04:36:01 -05:00
|
|
|
// VERSION: 0.1.3
|
2022-03-26 06:33:27 -04:00
|
|
|
// PURPOSE: Arduino library to mix two signals (A and B) with an adaptive weight.
|
2022-03-25 05:00:26 -04:00
|
|
|
// URL: https://github.com/RobTillaart/WaveMix
|
|
|
|
|
|
|
|
|
|
|
|
#include "WaveMix.h"
|
|
|
|
|
|
|
|
|
|
|
|
WaveMix::WaveMix()
|
|
|
|
{
|
2022-03-26 06:33:27 -04:00
|
|
|
_weight[0] = 0.5;
|
|
|
|
_weight[1] = 0.5;
|
2022-03-27 04:26:59 -04:00
|
|
|
_gain = 1.0;
|
|
|
|
_offset = 0.0;
|
2022-03-25 05:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 04:36:01 -05:00
|
|
|
bool WaveMix::setWeight(float weight1, float weight2)
|
2022-03-25 05:00:26 -04:00
|
|
|
{
|
2022-11-27 04:36:01 -05:00
|
|
|
if ((weight1 + weight2) == 0) return false; // test for <= 0 ?
|
2022-03-26 06:33:27 -04:00
|
|
|
float factor = 1.0 / (weight1 + weight2);
|
|
|
|
_weight[0] = weight1 * factor;
|
|
|
|
_weight[1] = weight2 * factor;
|
2022-11-27 04:36:01 -05:00
|
|
|
return true;
|
2022-03-25 05:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 04:36:01 -05:00
|
|
|
bool WaveMix::setPercentage(float percentage)
|
2022-03-25 05:00:26 -04:00
|
|
|
{
|
2022-11-27 04:36:01 -05:00
|
|
|
if ((percentage < 0) || (percentage > 100)) return false;
|
2022-03-26 06:33:27 -04:00
|
|
|
_weight[0] = percentage * 0.01;
|
|
|
|
_weight[1] = 1.0 - _weight[0];
|
2022-11-27 04:36:01 -05:00
|
|
|
return true;
|
2022-03-25 05:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 04:36:01 -05:00
|
|
|
// do we need a value check here
|
|
|
|
void WaveMix::setGain(float gain)
|
|
|
|
{
|
|
|
|
_gain = gain;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float WaveMix::getGain()
|
|
|
|
{
|
|
|
|
return _gain;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// do we need a value check here
|
|
|
|
void WaveMix::setOffset(float offset)
|
|
|
|
{
|
|
|
|
_offset = offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
float WaveMix::getOffset()
|
|
|
|
{
|
|
|
|
return _offset;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2022-03-25 05:00:26 -04:00
|
|
|
float WaveMix::mix(float s1, float s2)
|
|
|
|
{
|
2022-03-27 04:26:59 -04:00
|
|
|
float total = 0;
|
2022-03-26 06:33:27 -04:00
|
|
|
if (_gain == 0) return 0;
|
2022-03-27 04:26:59 -04:00
|
|
|
if (_weight[0] != 0) total += (_weight[0] * s1);
|
|
|
|
if (_weight[1] != 0) total += (_weight[1] * s2);
|
|
|
|
total *= _gain;
|
|
|
|
if (_offset != 0) total += _offset;
|
|
|
|
return total;
|
2022-03-25 05:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-27 04:36:01 -05:00
|
|
|
// -- END OF FILE --
|
2022-03-26 06:33:27 -04:00
|
|
|
|