static void updateaudiostream() {
        bytesCopiedToStream = 0;
        short[] data = stream_cache_data;
        int stereo = stream_cache_stereo;
        int len = stream_cache_len;
        int buflen;
        int start, end;

        if (stream_playing == 0) {
            return;	/* error */
        }

        buflen = audio_buffer_length;
        start = voice_pos;
        end = voice_pos + len;
        if (end > buflen) {
            end -= buflen; // IS this intended to wrap-around ?
        }

        if (stereo!=0) {
            /*int p = start;
            int bi = 0;
            int di = 0;
            while (p != end) {
                if (p >= buflen) {
                    p -= buflen;
                }
                soundInstance.WriteSample(2 * bi, (short) ((data[p++] * master_volume / 256)));
                bi += 2;
                soundInstance.WriteSample(2 * bi + 1, (short) ((data[p++] * master_volume / 256)));
                bi += 2;
                bytesCopiedToStream += 4;
            }*/
            int p = start;
            int d=0;
            while (p != end)
            {
                if (p >= buflen) p -= buflen;
                soundInstance.WriteSample(2*d, (short)(data[p] * master_volume / 256));
                soundInstance.WriteSample(2*d+1, (short)(data[p+1] * master_volume / 256));
                p++;// ???
                d+=2;
                bytesCopiedToStream += 4;
            }

        } else {
            int p = start;
            int bi = 0;
            while (p != end) {
                if (p >= buflen) {
                    p -= buflen;
                }
                soundInstance.WriteSample(bi, (short) ((data[p++] * master_volume / 256)));
                bytesCopiedToStream += 2;
                bi += 2;
            }
        }

        voice_pos = end;
        if (voice_pos == buflen) {
            voice_pos = 0;
        }
    }