Esempio n. 1
0
        internal void SeekTo(long granulePos)
        {
            if (!_packetProvider.CanSeek)
            {
                throw new NotSupportedException();
            }

            if (granulePos < 0)
            {
                throw new ArgumentOutOfRangeException("granulePos");
            }

            DataPacket packet;

            if (granulePos > 0)
            {
                packet = _packetProvider.FindPacket(granulePos, GetPacketLength);
                //if (packet == null) throw new ArgumentOutOfRangeException("granulePos");
                if (packet == null)
                {
                    return;
                }
            }
            else
            {
                packet = _packetProvider.GetPacket(4);
            }

            lock (_seekLock)
            {
                // seek the stream
                _packetProvider.SeekToPacket(packet, 1);

                // now figure out where we are and how many samples we need to discard...
                // note that we use the granule position of the "current" packet, since it will be discarded no matter what

                // get the packet that we'll decode next
                var dataPacket = _packetProvider.PeekNextPacket();

                // now read samples until we are exactly at the granule position requested
                CurrentPosition = dataPacket.GranulePosition;
                var cnt = (int)((granulePos - CurrentPosition) * _channels);
                if (cnt > 0)
                {
                    var seekBuffer = new float[cnt];
                    while (cnt > 0)
                    {
                        var temp = ReadSamples(seekBuffer, 0, cnt);
                        if (temp == 0)
                        {
                            break;              // we're at the end...
                        }
                        cnt -= temp;
                    }
                }
            }
        }
        internal void SeekTo(long granulePos)
        {
            if (!_packetProvider.CanSeek)
            {
                throw new NotSupportedException();
            }
            if (granulePos < 0)
            {
                throw new ArgumentOutOfRangeException("granulePos");
            }
            DataPacket dataPacket;

            if (granulePos > 0)
            {
                dataPacket = _packetProvider.FindPacket(granulePos, GetPacketLength);
                if (dataPacket == null)
                {
                    throw new ArgumentOutOfRangeException("granulePos");
                }
            }
            else
            {
                dataPacket = _packetProvider.GetPacket(4);
            }
            lock (_seekLock)
            {
                _packetProvider.SeekToPacket(dataPacket, 1);
                DataPacket dataPacket2 = _packetProvider.PeekNextPacket();
                CurrentPosition = dataPacket2.GranulePosition;
                int num = (int)((granulePos - CurrentPosition) * _channels);
                if (num > 0)
                {
                    float[] buffer = new float[num];
                    while (num > 0)
                    {
                        int num2 = ReadSamples(buffer, 0, num);
                        if (num2 == 0)
                        {
                            break;
                        }
                        num -= num2;
                    }
                }
            }
        }