Buggy code:
private Player player = null ; private PlayerChannel chn = null ; private boolean mixing = false ; private boolean active = false ; private int offs = 0 ; private int suboffs = 0 ; private boolean looping = false ; private boolean pingpong = false ; private boolean reverse = false ; private Handle env_vol = null ; private Handle env_pan = null ; private Handle env_per = null ; private int curlplen = 0 ; private int curlpbeg = 0 ; private float vol_import = 1.0F ; private int vol_env = 64 ; private int vol_fadeout = 1024 ; private float vol_calc = 1.0F ; private int pan_import = 32 ; private int pan_env = 32 ; private int per_import = 0 ; private int per_env = 32 ; private int filt_import = 127 ; private int filt_env = 64 ; private int filt_res = 0 ; private float filt_a = 1.0F ; private float filt_b = 0.0F ; private float filt_c = 0.0F ; private boolean filt_needs_calc = false ; private float filt_k1l = 0.0F ; private float filt_k2l = 0.0F ; private float filt_k1r = 0.0F ; private float filt_k2r = 0.0F ; private boolean note_off = false ; private boolean note_fade = false ; private boolean foreground = false ; public VirtualChannel ( Player player ) { } public void reset ( ) { } public void enslave ( PlayerChannel chn , VirtualChannel other ) { } public void detach ( ) { } public void mix ( float [ ] [ ] buf , int offs , int len ) { } public void updateEnvelopes ( ) { } private void calculateVol ( ) { } public float getCalculatedVol ( ) { } private void calcFilter ( ) { } private void doMix ( float [ ] [ ] buf , int boffs , int blen ) { if ( ( csmp ) == null ) return ; calculateVol ( ) ; float base_vol = getCalculatedVol ( ) ; int base_spd_c = player . calcSlide2 ( per_import , ( ( per_env ) - 32 ) ) ; float base_spd_x = ( ( float ) ( base_spd_c ) ) // ( ( float ) ( player . getFreq ( ) ) ) ; if ( base_spd_x > ( ( csmp . getC5Speed ( ) ) << 5 ) ) base_spd_x = ( csmp . getC5Speed ( ) ) << 5 ; int base_spd = ( ( int ) ( base_spd_x * ( ( float ) ( 1 << 13 ) ) ) ) ; float vol1 = base_vol ; float vol2 = base_vol ; if ( player . hasStereo ( ) ) { int base_pan_c = ( ( pan_import ) == 100 ? 32 : pan_import ) - 32 ; base_pan_c = base_pan_c + ( ( ( 32 - ( Math . abs ( base_pan_c ) ) ) * ( ( pan_env ) - 32 ) ) > > 5 ) ; if ( base_pan_c < 0 ) vol2 *= ( 32 + base_pan_c ) // 32.0F ; else vol1 *= ( 32 - base_pan_c ) // 32.0F ; if ( ( pan_import ) == 100 ) vol2 = - vol2 ; } calcFilter ( ) ; float [ ] [ ] data = ( note_off ) ? csmp . getDataLoop ( ) : csmp . getDataSustain ( ) ; if ( data == null ) return ; float [ ] dl = data [ 0 ] ; float [ ] dr = data [ 1 ] ; <START_BUG> int length = data [ 0 ] . length ; <END_BUG> int xcurlplen = ( pingpong ) ? ( ( curlplen ) * 2 ) - 1 : curlplen ; int lpend = ( curlpbeg ) + xcurlplen ; int end = boffs + blen ; float [ ] bufl = buf [ 0 ] ; float [ ] bufr = buf [ 1 ] ; if ( ( curlplen ) <= 0 ) looping = false ; float fa = filt_a ; float fb = filt_b ; float fc = filt_c ; try { if ( base_vol == 0.0F ) { suboffs += base_spd * blen ; offs += ( suboffs ) > > 13 ; suboffs &= ( 1 << 13 ) - 1 ; } else if ( ( VirtualChannel . NO_FILTERS ) || ( ( ( ( ! ( VirtualChannel . DEBUG_TORTURE_CPU ) ) && ( fa == 1.0F ) ) && ( fb == 0.0F ) ) && ( fc == 0.0F ) ) ) { for ( int i = boffs ; i < end ; i ++ ) { bufl [ i ] += ( dl [ offs ] ) * vol1 ; bufr [ i ] += ( dr [ offs ] ) * vol2 ; suboffs += base_spd ; offs += ( suboffs ) > > 13 ; suboffs &= ( 1 << 13 ) - 1 ; } } else if ( ( VirtualChannel . FILTER_MIX_MONOONLY ) || ( ( ! ( VirtualChannel . FILTER_MIX_STEREOONLY ) ) && ( ( ( csmp . getFlags ( ) ) & ( SessionSample . SFLG_STEREO ) ) == 0 ) ) ) { int endx = end - ( ( end - boffs ) % 3 ) ; float k0 = 0.0F ; float k1 = filt_k1l ; float k2 = filt_k2l ; for ( int i = boffs ; i < endx ; ) { k0 = ( ( ( dl [ offs ] ) * fa ) + ( k1 * fb ) ) + ( k2 * fc ) ; suboffs += base_spd ; offs += ( suboffs ) > > 13 ; suboffs &= ( 1 << 13 ) - 1 ; k2 = ( ( ( dl [ offs ] ) * fa ) + ( k0 * fb ) ) + ( k1 * fc ) ;

Fix:
int length = csmp . getLength ( ) ;