math.cpp 3.77 KB
Newer Older
Erik Faye-Lund's avatar
Erik Faye-Lund committed
1
#include <gba_base.h>
Erik Faye-Lund's avatar
Erik Faye-Lund committed
2
#include <assert.h>
3
#include <stdio.h>
Erik Faye-Lund's avatar
Erik Faye-Lund committed
4
5
#include "math.h"

6
7
#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))

Erik Faye-Lund's avatar
Erik Faye-Lund committed
8
const u8 clz_lut[256] =
Erik Faye-Lund's avatar
Erik Faye-Lund committed
9
{
Erik Faye-Lund's avatar
Erik Faye-Lund committed
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
	0x8, 0x7, 0x6, 0x6, 0x5, 0x5, 0x5, 0x5, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4,
	0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3,
	0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
	0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2, 0x2,
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1, 0x1,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
	0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0
Erik Faye-Lund's avatar
Erik Faye-Lund committed
26
27
28
};


Erik Faye-Lund's avatar
Erik Faye-Lund committed
29
30
31
32
33
34
#ifndef NO_LINEAR_PERIODS
unsigned get_linear_period(int note, int fine_tune)
{
	assert(fine_tune >= -8);
	assert(fine_tune <   8);
	
35
36
37
38
	int xm_note = note - (12 * 1); // we extended our note-range with one octave.
	
	return 10 * 12 * 16 * 4 - xm_note * 16 * 4 - fine_tune / 2;
//	return 10 * 12 * 16 * 4 - note * 16 * 4 - fine_tune / 2;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
39
}
Erik Faye-Lund's avatar
Erik Faye-Lund committed
40

Erik Faye-Lund's avatar
Erik Faye-Lund committed
41
42
43
#include "linear_delta_lut.h"
unsigned get_linear_delta(unsigned period)
{
44
45
46
	unsigned p = (12 * 16 * 4 * 14) - period;
	unsigned octave        = p / (12 * 16 * 4);
	unsigned octave_period = p % (12 * 16 * 4);
Erik Faye-Lund's avatar
Erik Faye-Lund committed
47
	unsigned delta = linear_delta_lut[octave_period] << octave;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
48

Erik Faye-Lund's avatar
Erik Faye-Lund committed
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	// BEHOLD: the expression of the devil
	// this compiles to one arm-instruction
	delta = ((long long)delta * unsigned((1.0 / SAMPLERATE) * (1 << 3) * (1ULL << 32)) + (1ULL << 31)) >> 32;
	return delta;
}
#endif /* NO_LINEAR_PERIODS */

#ifndef NO_AMIGA_PERIODS
#include "amiga_period_lut.h"
unsigned get_amiga_period(int note, int fine_tune)
{
	assert(fine_tune >= -8);
	assert(fine_tune <   8);
	
63
64
	int mod_note = note - (12 * 5); // we extended our note-range with two octaves. (wtf is this 3 octave bias?)
	int index = mod_note * 8 + fine_tune + 8;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
65
	
66
67
68
69
70
71
72
73
74
75
76
	if (index < 0)
	{
		return 0;
	}

	if (index > ARRAY_SIZE(amiga_period_lut))
	{
		/* hack. */
		return amiga_period_lut[ARRAY_SIZE(amiga_period_lut) - 1];
	}

Erik Faye-Lund's avatar
Erik Faye-Lund committed
77
78
79
	// TODO: handle entries outside of the mod-note range
	// clamping will be handled on the period later anyway
	
80
	return amiga_period_lut[index];
Erik Faye-Lund's avatar
Erik Faye-Lund committed
81
}
Erik Faye-Lund's avatar
Erik Faye-Lund committed
82

Erik Faye-Lund's avatar
Erik Faye-Lund committed
83
#include "amiga_delta_lut.h"
Erik Faye-Lund's avatar
Erik Faye-Lund committed
84
85
86
87
unsigned get_amiga_delta(unsigned period)
{
	unsigned shamt = clz16(period) - 1;
	unsigned p = period << shamt;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
88
89
	unsigned p_frac = p & ((1 << AMIGA_DELTA_LUT_FRAC_BITS) - 1);
	p >>= AMIGA_DELTA_LUT_FRAC_BITS;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
90
91

	// interpolate table-entries for better result
Erik Faye-Lund's avatar
Erik Faye-Lund committed
92
93
94
	int d1 = amiga_delta_lut[p     - (AMIGA_DELTA_LUT_SIZE / 2)]; // (8363 * 1712) / float(p);
	int d2 = amiga_delta_lut[p + 1 - (AMIGA_DELTA_LUT_SIZE / 2)]; // (8363 * 1712) / float(p + 1);
	unsigned delta = (d1 << AMIGA_DELTA_LUT_FRAC_BITS) + (d2 - d1) * p_frac;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
95

Erik Faye-Lund's avatar
Erik Faye-Lund committed
96
97
	if (shamt > AMIGA_DELTA_LUT_FRAC_BITS) delta <<= shamt - AMIGA_DELTA_LUT_FRAC_BITS;
	else delta >>= AMIGA_DELTA_LUT_FRAC_BITS - shamt;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
98

Erik Faye-Lund's avatar
Erik Faye-Lund committed
99
100
101
102
	// BEHOLD: the expression of the devil 2.0
	// this compiles to one arm-instruction
	delta = ((long long)delta * unsigned(((1.0 / SAMPLERATE) * (1 << 6)) * (1LL << 32)) + (1ULL << 31)) >> 32;
	return delta;
Erik Faye-Lund's avatar
Erik Faye-Lund committed
103
}
Erik Faye-Lund's avatar
Erik Faye-Lund committed
104
105
#endif /* NO_AMIGA_PERIODS */