arduino-audio-tools
ResampleAlgorithms.h
1 #pragma once
2 namespace audio_tools {
3 
6  constexpr int getFrom() { return -getFrameCountSave(); };
7  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
8  constexpr int getFrameCountSave() { return 1; }
9  float value(float* y, float xf) {
10  int x = xf;
11  int dx = xf - x;
12  return y[x] + dx * (y[x + 1] - y[x]);
13  }
14 };
15 
18  constexpr int getFrom() { return -1; };
19  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
20  constexpr int getFrameCountSave() { return 2; }
21  float value(float* y, float xf) {
22  int x = xf;
23  int dx = xf - x;
24  // 4-point, 3rd-order B-spline (x-form)
25  float ym1py1 = y[x - 1] + y[x + 1];
26  float c0 = 1.0f / 6.0f * ym1py1 + 2.0f / 3.0f * y[x];
27  float c1 = 1.0f / 2.0f * (y[x + 1] - y[x - 1]);
28  float c2 = 1.0f / 2.0f * ym1py1 - y[x];
29  float c3 =
30  1.0f / 2.0f * (y[x] - y[x + 1]) + 1.0f / 6.0f * (y[x + 2] - y[x - 1]);
31  return ((c3 * dx + c2) * dx + c1) * dx + c0;
32  }
33 };
34 
37  constexpr int getFrom() { return -1; };
38  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
39  constexpr int getFrameCountSave() { return 2; }
40  float value(float* y, float xf) {
41  int x = xf;
42  int dx = xf - x;
43  // 4-point, 3rd-order Lagrange (x-form)
44  float c0 = y[x];
45  float c1 = y[x + 1] - 1.0f / 3.0f * y[x - 1] - 1.0f / 2.0f * y[x] -
46  1.0f / 6.0f * y[x + 2];
47  float c2 = 1.0f / 2.0f * (y[x - 1] + y[x + 1]) - y[x];
48  float c3 =
49  1.0f / 6.0f * (y[x + 2] - y[x - 1]) + 1 / 2.0 * (y[x] - y[x + 1]);
50  return ((c3 * dx + c2) * dx + c1) * dx + c0;
51  }
52 };
53 
54 // Range -1:2
56  constexpr int getFrom() { return -1; };
57  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
58  constexpr int getFrameCountSave() { return 2; }
59  float value(float* y, float xf) {
60  int x = xf;
61  int dx = xf - x;
62  // 4-point, 3rd-order Hermite (x-form)
63  float c0 = y[x];
64  float c1 = 1.0f / 2.0f * (y[x + 1] - y[x - 1]);
65  float c2 = y[x - 1] - 5.0f / 2.0f * y[x] + 2.0f * y[x + 1] -
66  1.0f / 2.0f * y[x + 2];
67  float c3 =
68  1.0f / 2.0f * (y[x + 2] - y[x - 1]) + 3.0f / 2.0f * (y[x] - y[x + 1]);
69  return ((c3 * dx + c2) * dx + c1) * dx + c0;
70  }
71 };
72 
73 // range -1:2
75  constexpr int getFrom() { return -1; };
76  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
77  constexpr int getFrameCountSave() { return 2; }
78 
79  float value(float* y, float xf) {
80  int x = xf;
81  int dx = xf - x;
82  // 4-point, 2nd-order parabolic 2x (x-form)
83  float y1mym1 = y[x + 1] - y[x - 1];
84  float c0 = 1.0f / 2.0f * y[x] + 1 / 4.0 * (y[x - 1] + y[x + 1]);
85  float c1 = 1.0 / 2.0f * y1mym1;
86  float c2 = 1.0f / 4.0f * (y[x + 2] - y[x] - y1mym1);
87  return (c2 * dx + c1) * dx + c0;
88  }
89 };
90 
91 // range 0:1
93  constexpr int getFrom() { return -getFrameCountSave(); };
94  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
95  constexpr int getFrameCountSave() { return 1; }
96 
97  float value(float* y, float xf) {
98  int x = xf;
99  int dx = xf - x;
100  // Optimal 2x (2-point, 3rd-order) (z-form)
101  float z = dx - 0.5f;
102  float even1 = y[x + 1] + y[x];
103  float odd1 = y[x + 1] - y[x];
104  float c0 = even1 * 0.50037842517188658f;
105  float c1 = odd1 * 1.00621089801788210f;
106  float c2 = even1 * -0.004541102062639801f;
107  float c3 = odd1 * -1.57015627178718420f;
108  return ((c3 * z + c2) * z + c1) * z + c0;
109  }
110 };
111 
112 // Range -1 : 2
114  constexpr int getFrom() { return -1; };
115  constexpr int getTo(int frames) { return frames - getFrameCountSave(); }
116  constexpr int getFrameCountSave() { return 2; }
117 
118  float value(float* y, float xf) {
119  int x = xf;
120  int dx = xf - x;
121 
122  // Optimal 2x (4-point, 2nd-order) (z-form)
123  float z = dx - 0.5f;
124  float even1 = y[x + 1] + y[x], odd1 = y[x + 1] - y[x];
125  float even2 = y[x + 2] + y[x - 1], odd2 = y[x + 2] - y[x - 1];
126  float c0 = even1 * 0.42334633257225274 + even2 * 0.07668732202139628;
127  float c1 = odd1 * 0.26126047291143606 + odd2 * 0.24778879018226652;
128  float c2 = even1 * -0.213439787561776841 + even2 * 0.21303593243799016;
129  return (c2 * z + c1) * z + c0;
130  }
131 };
132 
133 } // namespace audio_tools
Generic Implementation of sound input and output for desktop environments using portaudio.
Definition: AudioConfig.h:821
range 0:1
Definition: ResampleAlgorithms.h:5
Definition: ResampleAlgorithms.h:92
Definition: ResampleAlgorithms.h:113
range -1:2
Definition: ResampleAlgorithms.h:17
range -1 : 2
Definition: ResampleAlgorithms.h:36
Definition: ResampleAlgorithms.h:74
Definition: ResampleAlgorithms.h:55