Exemple #1
0
            internal void PlayIntoBuffer(short[] buffer)
            {
                lock (locker)
                {
                    for (int i = 0; i < buffer.Length; i += 2)
                    {
                        if (isPlaying)
                        {
                            if (rampSampleCounter > 0)
                            {
                                speed             += rampSlope * 2;
                                rampSampleCounter -= 2;

                                if (rampSampleCounter <= 0)
                                {
                                    speed = destinationSpeed;
                                }
                            }

                            int intPos  = (int)position;
                            int intPosL = intPos & ~0x1;
                            int intPosR = intPos | 0x1;
                            var ratio   = position - intPosL; //in range 0-2 because of interleaved samples

                            //left channel
                            var samp1L = wavFile.ReadSample(intPosL);
                            var samp2L = wavFile.ReadSample(intPosL + 2);
                            var interpolatedSampleL = samp1L * (2 - ratio) + samp2L * ratio;
                            buffer[i] += Clamp(interpolatedSampleL * volume * speed);

                            //right channel
                            var samp1R = wavFile.ReadSample(intPosR);
                            var samp2R = wavFile.ReadSample(intPosR + 2);
                            var interpolatedSampleR = samp1R * (2 - ratio) + samp2R * ratio;
                            buffer[i + 1] += Clamp(interpolatedSampleR * volume * speed);

                            position += speed * 2;

                            if (speed == 0.0f)
                            {
                                isPlaying = false;
                            }

                            if (position > LastSafePosition())
                            {
                                position = autoRewind ? 0 : LastSafePosition();

                                if (!isLooped)
                                {
                                    isPlaying = false;
                                }
                            }
                        }
                    }
                }
            }