summaryrefslogtreecommitdiff
path: root/src/nw4r/snd/snd_Lfo.cpp
blob: 1c848667a74f0645227c9325e64d7665261c62c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include "nw4r/snd/snd_Lfo.h"

/* Original source:
 * kiwi515/ogws
 * src/nw4r/snd/snd_Lfo.cpp
 */

/*******************************************************************************
 * headers
 */

#include "common.h"

#include "nw4r/NW4RAssert.hpp"

/*******************************************************************************
 * functions
 */

namespace nw4r { namespace snd { namespace detail {

void LfoParam::Init()
{
	depth	= 0.0f;
	range	= 1;
	speed	= 6.25f;
	delay	= 0;
}

void Lfo::Reset()
{
	mCounter		= 0.0f;
	mDelayCounter	= 0;
}

void Lfo::Update(int msec)
{
	if (mDelayCounter < mParam.delay)
	{
		if (mDelayCounter + msec <= mParam.delay)
		{
			mDelayCounter += msec;
			return;
		}

		msec -= mParam.delay - mDelayCounter;
		mDelayCounter = mParam.delay;
	}

	mCounter += mParam.speed * msec / 1000;
	mCounter -= static_cast<int>(mCounter);
}

f32 Lfo::GetValue() const
{
	if (mParam.depth == 0.0f)
		return 0.0f;

	if (mDelayCounter < mParam.delay)
		return 0.0f;

	f32 value = GetSinIdx(4 * (TABLE_SIZE * mCounter))
	          / static_cast<f32>(TABLE_SIZE * 4 - 1);

	value *= mParam.depth;
	value *= mParam.range;

	return value;
}

s8 Lfo::GetSinIdx(int index)
{
	static s8 const sinTable[TABLE_SIZE + 1] =
	{
		0,   6,   12,  19,  25,  31,  37,  43,  49,  54,  60,
		65,  71,  76,  81,  85,  90,  94,  98,  102, 106, 109,
		112, 115, 117, 120, 122, 123, 125, 126, 126, 127, 127
	};

	// specifically not the source variant
	NW4RAssertHeaderClampedLValue_Line(123, index, 0, 128);

	if (index < TABLE_SIZE)
		return sinTable[index];
	else if (index < TABLE_SIZE * 2)
		return sinTable[TABLE_SIZE - (index - TABLE_SIZE)];
	else if (index < TABLE_SIZE * 3)
		return -sinTable[index - TABLE_SIZE * 2];
	else
		return -sinTable[TABLE_SIZE - (index - TABLE_SIZE * 3)];
}

}}} // namespace nw4r::snd::detail