You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
MicroDexed/SoftenValue.hpp

107 lines
2.2 KiB

/*
MicroDexed
MicroDexed is a port of the Dexed sound engine
(https://github.com/asb2m10/dexed) for the Teensy-3.5/3.6 with audio shield.
Dexed ist heavily based on https://github.com/google/music-synthesizer-for-android
(c)2018 H. Wirtz <wirtz@parasitstudio.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software Foundation,
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SOFTEN_VALUE_H_INCLUDED
template <class T>
class SoftenValue
{
public:
SoftenValue(uint16_t steps = 10);
SoftenValue(T from, T to, uint16_t steps = 10)
{
_from = from;
_to = to;
_steps = steps;
_diff = (from - to) / _steps;
_calculate();
}
void update_to(T to)
{
_to = to;
_calculate();
}
void update_steps(uint16_t steps)
{
_steps = steps;
_calculate();
}
void tick(void)
{
if (_steps > 0)
{
_from -= _diff;
_steps--;
}
}
bool running(void)
{
if (_steps > 0)
return (true);
else
return (false);
}
uint16_t steps(void)
{
return (_steps);
}
T value(void)
{
if (_steps == 0)
return (_to);
if (std::is_same<T, float>::value)
return (_from);
else
return (round(_from));
}
protected:
float _from;
float _to;
uint16_t _steps;
float _diff;
void _calculate(void)
{
if (_from == _to || _steps <= 0)
{
_steps = 0;
_diff = 0.0;
}
else
_diff = (_from - _to) / _steps;
}
};
#endif