Based on this java code: package com.github.natanbc.lavadsp.vibrato; import…
- User
Based on this java code:
package com.github.natanbc.lavadsp.vibrato;
import com.github.natanbc.lavadsp.Converter;
public class VibratoConverter implements Converter { private static final int ADDITIONAL_DELAY = 3; private static final float BASE_DELAY_SEC = 0.002f; // 2 ms private static final float VIBRATO_FREQUENCY_DEFAULT_HZ = 2; private static final float VIBRATO_FREQUENCY_MAX_HZ = 14; private static final float VIBRATO_DEPTH_DEFAULT_PERCENT = 50;
private final float sampleRate; private final Lfo lfo; private final RingBuffer buffer; private float depth = VIBRATO_DEPTH_DEFAULT_PERCENT / 100; public VibratoConverter(int sampleRate) { if(sampleRate < 1) { throw new IllegalArgumentException("Sample rate < 1"); } this.sampleRate = sampleRate; this.lfo = new Lfo(sampleRate); this.buffer = new RingBuffer((int)(BASE_DELAY_SEC * sampleRate * 2)); } public void setDepth(float depth) { if(depth <= 0) { throw new IllegalArgumentException("Depth <= 0"); } if(depth > 1) { throw new IllegalArgumentException("Depth > 1"); } this.depth = depth; } public void setFrequency(float frequency) { if(frequency <= 0) { throw new IllegalArgumentException("Frequency <= 0"); } if(frequency > VIBRATO_FREQUENCY_MAX_HZ) { throw new IllegalArgumentException("Frequency > max (" + VIBRATO_FREQUENCY_MAX_HZ + ")"); } lfo.frequency = frequency; } @Override public void process(float[] input, int inputOffset, float[] output, int outputOffset, int samples) { for(int i = 0; i < samples; i++) { float lfoValue = lfo.getValue(); int maxDelay = (int)(BASE_DELAY_SEC * sampleRate); float delay = lfoValue * depth * maxDelay + ADDITIONAL_DELAY; output[outputOffset + i] = buffer.getHermiteAt(delay); buffer.writeMargined(input[inputOffset + i]); } } private static class Lfo { private final float sampleRate; float frequency = VIBRATO_FREQUENCY_DEFAULT_HZ; float phase; Lfo(float sampleRate) { this.sampleRate = sampleRate; } float getValue() { float dp = 2 * (float)Math.PI * frequency / sampleRate; float value = (float)((Math.sin(phase) + 1) * 0.5); phase += dp; while(phase > 2 * Math.PI) { phase -= 2 * Math.PI; } return value; } } private static class RingBuffer { private static final int INTERPOLATOR_MARGIN = 3; private final float[] buffer; private final int size; private int writeIndex; RingBuffer(int size) { this.buffer = new float[size + INTERPOLATOR_MARGIN]; this.size = size; } void writeMargined(float sample) { buffer[writeIndex] = sample; if(writeIndex < INTERPOLATOR_MARGIN) { buffer[size + writeIndex] = sample; } writeIndex++; if(writeIndex == size){ writeIndex = 0; } } float getHermiteAt(float delay) { float fReadIndex = writeIndex - 1 - delay; while(fReadIndex < 0) { fReadIndex += size; } while(fReadIndex >= size) { fReadIndex -= size; } int iPart = (int)fReadIndex; // integer part of the delay float fPart = fReadIndex - iPart; // fractional part of the delay return getSampleHermite4p3o(fPart, buffer, iPart); } // Hermite polynomial interpolation // 4-point, 3rd-order Hermite (x-form) private static float getSampleHermite4p3o(float x, float[] buffer, int offset) { float y0 = buffer[offset]; float y1 = buffer[offset + 1]; float y2 = buffer[offset + 2]; float y3 = buffer[offset + 3]; //c0 = y[1]; //c1 = (1.0/2.0)*(y[2]-y[0]); float c1 = (1f / 2f) * (y2 - y0); //c2 = (y[0] - (5.0/2.0)*y[1]) + (2.0*y[2] - (1.0/2.0)*y[3]); float c2 = (y0 - (5f / 2f) * y1) + (2f * y2 - (1f / 2f) * y3); //c3 = (1.0/2.0)*(y[3]-y[0]) + (3.0/2.0)*(y[1]-y[2]); float c3 = (1f / 2f) * (y3 - y0) + (3f / 2f) * (y1 - y2); return ((c3 * x + c2) * x + c1) * x + y1; } } - Assistant
- User
Please, implement it correctly in my ChannelProcessor class, following the current code
- Assistant
- User
Could you now implement hermite?
- Assistant
- User
- Assistant
- User
Please revise that as it's not working like the java code, even with proper constants and clamp16Bit defined
- Assistant
- User
Could you revise it again? The filter still doesn't match, the js one is way stronger, but that's because how it's calculated it's wrong
- Assistant
- User
- Assistant
- User
Simplify that and fix it, still doesn't work properly, please re-check the function that creates the effect
- Assistant
- User
Better, but still wrong, revise the part of applying the filter, and the quantity
- Assistant
- User
Still wrong. Java code doesn't make the voice of the music I'm testing robotized, but this js code does, it's like it's stronger, hard to define
- Assistant
- User
And the wrap function?
- Assistant
- User
Please don't create those unecessary functions like setFrequency and setDepth, and please don't use percentage for depth as it's already from the range of 0-1. Anyway, the voice is still robotized
- Assistant
- User
Still with robotized voice, please, do it.
- Assistant
- User
First, it's only outputting to the right channel. The input values are correct as they work in the java code. And the left channel is with some weird noise, not sure what it is, like the vibrato effect went to it.
- Assistant
- User
file:///home/system/Downloads/Projects/Nodelink/src/filters.js:139 const xm1 = this.buffer[wrapIndex(index - 1)]; ^
TypeError: Cannot read properties of undefined (reading 'NaN') at ChannelProcessor.hermiteInterpolate (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:139:28) at ChannelProcessor.applyVibratoToSample (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:126:32) at ChannelProcessor.process (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:190:41) at Filtering.process (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:217:18) at Filtering._transform (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:221:10) at Transform._write (node:internal/streams/transform:171:8) at writeOrBuffer (node:internal/streams/writable:564:12) at _write (node:internal/streams/writable:493:10) at Writable.write (node:internal/streams/writable:502:10) at VolumeTransformer.ondata (node:internal/streams/readable:1007:22)
- Assistant
The error message you're seeing indicates that the 'wrapIndex' function isn't working as expected because either is or is not defined and thus, doesn't exist in the current context when it's called.
- User
If it's NaN it's because there's a mistake in the code, fix it
- Assistant
- User
node:internal/buffer:74 throw new ERR_OUT_OF_RANGE('value', range, value); ^
RangeError [ERR_OUT_OF_RANGE]: The value of "value" is out of range. It must be >= -32768 and <= 32767. Received 32809.16785886483 at checkInt (node:internal/buffer:74:11) at writeU_Int16LE (node:internal/buffer:724:3) at Buffer.writeInt16LE (node:internal/buffer:867:10) at ChannelProcessor.process (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:195:15) at Filtering.process (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:218:18) at Filtering._transform (file:///home/system/Downloads/Projects/Nodelink/src/filters.js:222:10) at Transform._write (node:internal/streams/transform:171:8) at writeOrBuffer (node:internal/streams/writable:564:12) at _write (node:internal/streams/writable:493:10) at Writable.write (node:internal/streams/writable:502:10) { code: 'ERR_OUT_OF_RANGE' }
Node.js v21.2.0
- Assistant
The error message you're seeing ("
RangeError: The value of 'value' is out of range") indicates that the vibrato processing is creating sample values outside the valid range for 16-bit PCM audio data, which should be between -32768 and 32767. We need to ensure that the output of the method is clamped appropriately. - User
now it is stereo, hoever it doesnt match java code effect
- Assistant