From 68bd06354003826dfacc41db75e923af6f0728e1 Mon Sep 17 00:00:00 2001 From: ffd8 Date: Tue, 17 Mar 2020 14:02:55 +0100 Subject: [PATCH] Fix crash when changing wavetable too quickly Hansi Raber ( @kritzikratzi ) gets credit for isolating and patching this bug that could cause minim to crash if changing the wavetable too quickly. Now it makes a local copy of the table, to avoid the possibility of its size changing while being processed. Something I ended up causing to happen regularly in my XYscope library which is constantly changing the wavetable on every draw cycle. --- src/main/java/ddf/minim/ugens/Wavetable.java | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/main/java/ddf/minim/ugens/Wavetable.java b/src/main/java/ddf/minim/ugens/Wavetable.java index 24bbe10..068c57f 100644 --- a/src/main/java/ddf/minim/ugens/Wavetable.java +++ b/src/main/java/ddf/minim/ugens/Wavetable.java @@ -128,22 +128,26 @@ public float get(int i) */ public float value(float at) { - float whichSample = lengthForValue * at; + // create local waveform for thread safe + float[] wave = this.waveform; + if(wave.length==0) return 0; + + float whichSample = wave.length * (((at%1)+1)%1); // linearly interpolate between the two samples we want. - int lowSamp = (int)whichSample; - int hiSamp = lowSamp + 1; + int lowSamp = ((int)whichSample)%wave.length; + int hiSamp = (lowSamp + 1)%wave.length; // lowSamp might be the last sample in the waveform // we need to make sure we wrap. - if ( hiSamp >= waveform.length ) + if ( hiSamp >= wave.length ) { - hiSamp -= waveform.length; + hiSamp -= wave.length; } float rem = whichSample - lowSamp; - return waveform[lowSamp] + rem - * ( waveform[hiSamp] - waveform[lowSamp] ); + return wave[lowSamp] + rem + * ( wave[hiSamp] - wave[lowSamp] ); // This was here for testing. // Causes non-interpolation, but adds max # of oscillators