@@ -49,7 +49,7 @@ public void setFrequency(float frequency) {
49
49
public void process (float [] input , int inputOffset , float [] output , int outputOffset , int samples ) {
50
50
for (int i = 0 ; i < samples ; i ++) {
51
51
float lfoValue = lfo .getValue ();
52
- float maxDelay = BASE_DELAY_SEC * sampleRate ;
52
+ int maxDelay = ( int )( BASE_DELAY_SEC * sampleRate ) ;
53
53
54
54
float delay = lfoValue * depth * maxDelay + ADDITIONAL_DELAY ;
55
55
@@ -69,36 +69,33 @@ private static class Lfo {
69
69
}
70
70
71
71
float getValue () {
72
- float dp = (float )(2 * Math .PI * frequency / sampleRate ); // phase step
73
-
74
- float value = (float )Math .sin (phase );
75
- value = (value + 1f ) * 0.5f ; // transform from [-1; 1] to [0; 1]
76
-
72
+ float dp = 2 * (float )Math .PI * frequency / sampleRate ;
73
+ float value = (float )((Math .sin (phase ) + 1 ) * 0.5 );
77
74
phase += dp ;
78
- while (phase > 2 * Math .PI )
75
+ while (phase > 2 * Math .PI ) {
79
76
phase -= 2 * Math .PI ;
80
-
77
+ }
81
78
return value ;
82
79
}
83
80
}
84
81
85
82
private static class RingBuffer {
86
83
private static final int INTERPOLATOR_MARGIN = 3 ;
87
84
88
- private final FloatBuffer buffer ;
85
+ private final float [] buffer ;
89
86
private final int size ;
90
87
private int writeIndex ;
91
88
92
89
RingBuffer (int size ) {
93
- this .buffer = FloatBuffer . allocate ( size + INTERPOLATOR_MARGIN ) ;
90
+ this .buffer = new float [ size + INTERPOLATOR_MARGIN ] ;
94
91
this .size = size ;
95
92
}
96
93
97
94
void writeMargined (float sample ) {
98
- buffer . put ( writeIndex , sample ) ;
95
+ buffer [ writeIndex ] = sample ;
99
96
100
97
if (writeIndex < INTERPOLATOR_MARGIN ) {
101
- buffer . put ( size + writeIndex , sample ) ;
98
+ buffer [ size + writeIndex ] = sample ;
102
99
}
103
100
104
101
writeIndex ++;
@@ -109,21 +106,35 @@ void writeMargined(float sample) {
109
106
110
107
float getHermiteAt (float delay ) {
111
108
float fReadIndex = writeIndex - 1 - delay ;
112
- while (fReadIndex < 0 )
113
- fReadIndex += buffer .capacity ();
114
- while (fReadIndex >= buffer .capacity ())
115
- fReadIndex -= buffer .capacity ();
109
+ while (fReadIndex < 0 ) {
110
+ fReadIndex += size ;
111
+ }
112
+ while (fReadIndex >= size ) {
113
+ fReadIndex -= size ;
114
+ }
116
115
117
116
int iPart = (int )fReadIndex ; // integer part of the delay
118
117
float fPart = fReadIndex - iPart ; // fractional part of the delay
119
118
120
- // Hermite polynomial interpolation
121
- // 4-point, 3rd-order Hermite (x-form)
122
- float c0 = buffer .get (iPart + 1 );
123
- float c1 = 0.5f * (buffer .get (iPart + 2 ) - buffer .get (iPart ));
124
- float c2 = (buffer .get (iPart ) - 2.5f * buffer .get (iPart + 1 )) + (2.0f *buffer .get (iPart + 2 ) - 0.5f * buffer .get (iPart + 3 ));
125
- float c3 = 0.5f * (buffer .get (iPart + 3 ) - buffer .get (iPart )) + 1.5f * (buffer .get (iPart + 1 ) - buffer .get (iPart + 2 ));
126
- return ((c3 * fPart + c2 ) * fPart + c1 ) * fPart + c0 ;
119
+ return getSampleHermite4p3o (fPart , buffer , iPart );
120
+ }
121
+
122
+ // Hermite polynomial interpolation
123
+ // 4-point, 3rd-order Hermite (x-form)
124
+ private static float getSampleHermite4p3o (float x , float [] buffer , int offset ) {
125
+ float y0 = buffer [offset ];
126
+ float y1 = buffer [offset + 1 ];
127
+ float y2 = buffer [offset + 2 ];
128
+ float y3 = buffer [offset + 3 ];
129
+
130
+ //c0 = y[1];
131
+ //c1 = (1.0/2.0)*(y[2]-y[0]);
132
+ float c1 = (1f / 2f ) * (y2 - y0 );
133
+ //c2 = (y[0] - (5.0/2.0)*y[1]) + (2.0*y[2] - (1.0/2.0)*y[3]);
134
+ float c2 = (y0 - (5f / 2f ) * y1 ) + (2f * y2 - (1f / 2f ) * y3 );
135
+ //c3 = (1.0/2.0)*(y[3]-y[0]) + (3.0/2.0)*(y[1]-y[2]);
136
+ float c3 = (1f / 2f ) * (y3 - y0 ) + (3f / 2f ) * (y1 - y2 );
137
+ return ((c3 * x + c2 ) * x + c1 ) * x + y1 ;
127
138
}
128
139
}
129
140
}
0 commit comments