Skip to content
This repository was archived by the owner on Apr 10, 2023. It is now read-only.

Commit f925068

Browse files
committed
fix wrong fReadIndex calculation for the vibrato effect, clean up code a bit (and make it closer to the original)
1 parent f9cbaa9 commit f925068

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

src/main/java/com/github/natanbc/lavadsp/vibrato/VibratoConverter.java

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void setFrequency(float frequency) {
4949
public void process(float[] input, int inputOffset, float[] output, int outputOffset, int samples) {
5050
for(int i = 0; i < samples; i++) {
5151
float lfoValue = lfo.getValue();
52-
float maxDelay = BASE_DELAY_SEC * sampleRate;
52+
int maxDelay = (int)(BASE_DELAY_SEC * sampleRate);
5353

5454
float delay = lfoValue * depth * maxDelay + ADDITIONAL_DELAY;
5555

@@ -69,36 +69,33 @@ private static class Lfo {
6969
}
7070

7171
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);
7774
phase += dp;
78-
while(phase > 2 * Math.PI)
75+
while(phase > 2 * Math.PI) {
7976
phase -= 2 * Math.PI;
80-
77+
}
8178
return value;
8279
}
8380
}
8481

8582
private static class RingBuffer {
8683
private static final int INTERPOLATOR_MARGIN = 3;
8784

88-
private final FloatBuffer buffer;
85+
private final float[] buffer;
8986
private final int size;
9087
private int writeIndex;
9188

9289
RingBuffer(int size) {
93-
this.buffer = FloatBuffer.allocate(size + INTERPOLATOR_MARGIN);
90+
this.buffer = new float[size + INTERPOLATOR_MARGIN];
9491
this.size = size;
9592
}
9693

9794
void writeMargined(float sample) {
98-
buffer.put(writeIndex, sample);
95+
buffer[writeIndex] = sample;
9996

10097
if(writeIndex < INTERPOLATOR_MARGIN) {
101-
buffer.put(size + writeIndex, sample);
98+
buffer[size + writeIndex] = sample;
10299
}
103100

104101
writeIndex++;
@@ -109,21 +106,35 @@ void writeMargined(float sample) {
109106

110107
float getHermiteAt(float delay) {
111108
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+
}
116115

117116
int iPart = (int)fReadIndex; // integer part of the delay
118117
float fPart = fReadIndex - iPart; // fractional part of the delay
119118

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;
127138
}
128139
}
129140
}

src/main/java/com/github/natanbc/lavadsp/vibrato/VibratoPcmAudioFilter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
/**
2424
* <a href="https://en.wikipedia.org/wiki/Vibrato">Vibrato</a> filter implementation.
25+
*
26+
* Ported from <a href="https://github.com/Bershov/Vibrato-effect">BerVibrato</a>
2527
*/
2628
public class VibratoPcmAudioFilter extends ConverterPcmAudioFilter<VibratoConverter> {
2729
//values taken from BerVibrato.h

0 commit comments

Comments
 (0)