Browse code

For in_snsf, replaced the B-spline interpolation with the 6-point, 5th-order version, and replaced the Optimal interpolation with 6-point, 5th-order 2nd-order Osculating.

Naram Qashat authored on 2013/04/12 13:36:31
Showing 1 changed files
1 1
deleted file mode 100644
... ...
@@ -1,125 +0,0 @@
1
-/* Simple resampler based on bsnes's ruby audio library */
2
-
3
-#ifndef __OPTIMAL_RESAMPLER_H
4
-#define __OPTIMAL_RESAMPLER_H
5
-
6
-#include <cmath>
7
-#include "resampler.h"
8
-
9
-#undef CLAMP
10
-#undef SHORT_CLAMP
11
-//template<typename T1, typename T2> static inline T1 CLAMP(T1 x, T2 low, T2 high) { return x > high ? high : (x < low ? low : x); }
12
-//template<typename T> static inline short SHORT_CLAMP(T n) { return static_cast<short>(CLAMP(n, -32768, 32767)); }
13
-
14
-class OptimalResampler : public Resampler
15
-{
16
-protected:
17
-	double r_step;
18
-	double r_frac;
19
-	int r_left[4], r_right[4];
20
-
21
-	double optimal(double x, double a, double b, double c, double d)
22
-	{
23
-		double z = x - 0.5;
24
-		double even1 = c + b, odd1 = c - b;
25
-		double even2 = d + a, odd2 = d - a;
26
-		double c0 = even1 * 0.46822774170144532 + even2 * 0.03177225758005808;
27
-		double c1 = odd1 * 0.55890365706150436 + odd2 * 0.14703258836343669;
28
-		double c2 = even1 * -0.250153411893796031 + even2 * 0.25015343462990891;
29
-		double c3 = odd1 * -0.49800710906733769 + odd2 * 0.16600005174304033;
30
-		double c4 = even1 * 0.00064264050033187 + even2 * -0.00064273459469381;
31
-		return (((c4 * z + c3) * z + c2) * z + c1) * z + c0;
32
-	}
33
-
34
-public:
35
-	OptimalResampler(int num_samples) : Resampler(num_samples)
36
-	{
37
-		this->clear();
38
-	}
39
-
40
-	void time_ratio(double ratio)
41
-	{
42
-		this->r_step = ratio;
43
-		this->clear();
44
-	}
45
-
46
-	void clear()
47
-	{
48
-		ring_buffer::clear ();
49
-		this->r_frac = 1.0;
50
-		this->r_left[0] = this->r_left[1] = this->r_left[2] = this->r_left[3] = 0;
51
-		this->r_right[0] = this->r_right[1] = this->r_right[2] = this->r_right[3] = 0;
52
-	}
53
-
54
-	void read(short *data, int num_samples)
55
-	{
56
-		int i_position = this->start >> 1;
57
-		short *internal_buffer = reinterpret_cast<short *>(this->buffer);
58
-		int o_position = 0;
59
-		int consumed = 0;
60
-
61
-		while (o_position < num_samples && consumed < this->buffer_size)
62
-		{
63
-			int s_left = internal_buffer[i_position];
64
-			int s_right = internal_buffer[i_position + 1];
65
-			int max_samples = this->buffer_size >> 1;
66
-			const double margin_of_error = 1.0e-10;
67
-
68
-			if (std::abs(this->r_step - 1.0) < margin_of_error)
69
-			{
70
-				data[o_position] = static_cast<short>(s_left);
71
-				data[o_position + 1] = static_cast<short>(s_right);
72
-
73
-				o_position += 2;
74
-				i_position += 2;
75
-				if (i_position >= max_samples)
76
-					i_position -= max_samples;
77
-				consumed += 2;
78
-
79
-				continue;
80
-			}
81
-
82
-			while (this->r_frac <= 1.0 && o_position < num_samples)
83
-			{
84
-				data[o_position] = SHORT_CLAMP(optimal(this->r_frac, this->r_left[0], this->r_left[1], this->r_left[2], this->r_left[3]));
85
-				data[o_position + 1] = SHORT_CLAMP(optimal(this->r_frac, this->r_right[0], this->r_right[1], this->r_right[2], this->r_right[3]));
86
-
87
-				o_position += 2;
88
-
89
-				this->r_frac += this->r_step;
90
-			}
91
-
92
-			if (this->r_frac > 1.0)
93
-			{
94
-				this->r_left[0] = this->r_left[1];
95
-				this->r_left[1] = this->r_left[2];
96
-				this->r_left[2] = this->r_left[3];
97
-				this->r_left[3] = s_left;
98
-
99
-				this->r_right[0] = this->r_right[1];
100
-				this->r_right[1] = this->r_right[2];
101
-				this->r_right[2] = this->r_right[3];
102
-				this->r_right[3] = s_right;
103
-
104
-				this->r_frac -= 1.0;
105
-
106
-				i_position += 2;
107
-				if (i_position >= max_samples)
108
-					i_position -= max_samples;
109
-				consumed += 2;
110
-			}
111
-		}
112
-
113
-		this->size -= consumed << 1;
114
-		this->start += consumed << 1;
115
-		if (this->start >= this->buffer_size)
116
-			this->start -= this->buffer_size;
117
-	}
118
-
119
-	inline int avail()
120
-	{
121
-		return static_cast<int>(std::floor(((this->size >> 2) - this->r_frac) / this->r_step) * 2);
122
-	}
123
-};
124
-
125
-#endif /* __OPTIMAL_RESAMPLER_H */
Browse code

Added B-spline and Optimal interpolations to in_snsf, but I'll probably be changing this later.

Naram Qashat authored on 2013/04/12 10:24:45
Showing 1 changed files
1 1
new file mode 100644
... ...
@@ -0,0 +1,125 @@
1
+/* Simple resampler based on bsnes's ruby audio library */
2
+
3
+#ifndef __OPTIMAL_RESAMPLER_H
4
+#define __OPTIMAL_RESAMPLER_H
5
+
6
+#include <cmath>
7
+#include "resampler.h"
8
+
9
+#undef CLAMP
10
+#undef SHORT_CLAMP
11
+//template<typename T1, typename T2> static inline T1 CLAMP(T1 x, T2 low, T2 high) { return x > high ? high : (x < low ? low : x); }
12
+//template<typename T> static inline short SHORT_CLAMP(T n) { return static_cast<short>(CLAMP(n, -32768, 32767)); }
13
+
14
+class OptimalResampler : public Resampler
15
+{
16
+protected:
17
+	double r_step;
18
+	double r_frac;
19
+	int r_left[4], r_right[4];
20
+
21
+	double optimal(double x, double a, double b, double c, double d)
22
+	{
23
+		double z = x - 0.5;
24
+		double even1 = c + b, odd1 = c - b;
25
+		double even2 = d + a, odd2 = d - a;
26
+		double c0 = even1 * 0.46822774170144532 + even2 * 0.03177225758005808;
27
+		double c1 = odd1 * 0.55890365706150436 + odd2 * 0.14703258836343669;
28
+		double c2 = even1 * -0.250153411893796031 + even2 * 0.25015343462990891;
29
+		double c3 = odd1 * -0.49800710906733769 + odd2 * 0.16600005174304033;
30
+		double c4 = even1 * 0.00064264050033187 + even2 * -0.00064273459469381;
31
+		return (((c4 * z + c3) * z + c2) * z + c1) * z + c0;
32
+	}
33
+
34
+public:
35
+	OptimalResampler(int num_samples) : Resampler(num_samples)
36
+	{
37
+		this->clear();
38
+	}
39
+
40
+	void time_ratio(double ratio)
41
+	{
42
+		this->r_step = ratio;
43
+		this->clear();
44
+	}
45
+
46
+	void clear()
47
+	{
48
+		ring_buffer::clear ();
49
+		this->r_frac = 1.0;
50
+		this->r_left[0] = this->r_left[1] = this->r_left[2] = this->r_left[3] = 0;
51
+		this->r_right[0] = this->r_right[1] = this->r_right[2] = this->r_right[3] = 0;
52
+	}
53
+
54
+	void read(short *data, int num_samples)
55
+	{
56
+		int i_position = this->start >> 1;
57
+		short *internal_buffer = reinterpret_cast<short *>(this->buffer);
58
+		int o_position = 0;
59
+		int consumed = 0;
60
+
61
+		while (o_position < num_samples && consumed < this->buffer_size)
62
+		{
63
+			int s_left = internal_buffer[i_position];
64
+			int s_right = internal_buffer[i_position + 1];
65
+			int max_samples = this->buffer_size >> 1;
66
+			const double margin_of_error = 1.0e-10;
67
+
68
+			if (std::abs(this->r_step - 1.0) < margin_of_error)
69
+			{
70
+				data[o_position] = static_cast<short>(s_left);
71
+				data[o_position + 1] = static_cast<short>(s_right);
72
+
73
+				o_position += 2;
74
+				i_position += 2;
75
+				if (i_position >= max_samples)
76
+					i_position -= max_samples;
77
+				consumed += 2;
78
+
79
+				continue;
80
+			}
81
+
82
+			while (this->r_frac <= 1.0 && o_position < num_samples)
83
+			{
84
+				data[o_position] = SHORT_CLAMP(optimal(this->r_frac, this->r_left[0], this->r_left[1], this->r_left[2], this->r_left[3]));
85
+				data[o_position + 1] = SHORT_CLAMP(optimal(this->r_frac, this->r_right[0], this->r_right[1], this->r_right[2], this->r_right[3]));
86
+
87
+				o_position += 2;
88
+
89
+				this->r_frac += this->r_step;
90
+			}
91
+
92
+			if (this->r_frac > 1.0)
93
+			{
94
+				this->r_left[0] = this->r_left[1];
95
+				this->r_left[1] = this->r_left[2];
96
+				this->r_left[2] = this->r_left[3];
97
+				this->r_left[3] = s_left;
98
+
99
+				this->r_right[0] = this->r_right[1];
100
+				this->r_right[1] = this->r_right[2];
101
+				this->r_right[2] = this->r_right[3];
102
+				this->r_right[3] = s_right;
103
+
104
+				this->r_frac -= 1.0;
105
+
106
+				i_position += 2;
107
+				if (i_position >= max_samples)
108
+					i_position -= max_samples;
109
+				consumed += 2;
110
+			}
111
+		}
112
+
113
+		this->size -= consumed << 1;
114
+		this->start += consumed << 1;
115
+		if (this->start >= this->buffer_size)
116
+			this->start -= this->buffer_size;
117
+	}
118
+
119
+	inline int avail()
120
+	{
121
+		return static_cast<int>(std::floor(((this->size >> 2) - this->r_frac) / this->r_step) * 2);
122
+	}
123
+};
124
+
125
+#endif /* __OPTIMAL_RESAMPLER_H */