2022-03-25 05:00:26 -04:00
|
|
|
//
|
|
|
|
// FILE: WaveMix.cpp
|
|
|
|
// AUTHOR: Rob Tillaart
|
2022-03-27 04:26:59 -04:00
|
|
|
// VERSION: 0.1.2
|
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-03-26 06:33:27 -04:00
|
|
|
void WaveMix::setWeight(float weight1, float weight2)
|
2022-03-25 05:00:26 -04:00
|
|
|
{
|
2022-03-26 06:33:27 -04:00
|
|
|
float factor = 1.0 / (weight1 + weight2);
|
|
|
|
_weight[0] = weight1 * factor;
|
|
|
|
_weight[1] = weight2 * factor;
|
2022-03-25 05:00:26 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-03-26 06:33:27 -04:00
|
|
|
void WaveMix::setPercentage(float percentage)
|
2022-03-25 05:00:26 -04:00
|
|
|
{
|
2022-03-26 06:33:27 -04:00
|
|
|
_weight[0] = percentage * 0.01;
|
|
|
|
_weight[1] = 1.0 - _weight[0];
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -- END OF FILE --
|
2022-03-26 06:33:27 -04:00
|
|
|
|