Example #1
0
        /// <summary>
        /// Seeks the stream for a valid packet at the specified granule position.
        /// </summary>
        /// <param name="granulePosition">The granule position.</param>
        private void SeekToGranulePosition(long granulePosition)
        {
            if (!CanSeek)
            {
                throw new InvalidOperationException("Stream is not seekable.");
            }

            if (granulePosition < 0 || granulePosition > GranuleCount)
            {
                throw new ArgumentOutOfRangeException(nameof(granulePosition));
            }

            // Find a packet based on offset and return 1 in the callback if the packet is valid
            var foundPacket = _packetProvider.FindPacket(granulePosition, GetPacketLength);

            // Check of the found packet is valid
            if (foundPacket == null || foundPacket.IsEndOfStream)
            {
                _endOfStream    = true;
                _nextDataPacket = null;
                return;
            }

            // Just seek to this found packet and get the previous packet (preRoll = 1)
            _packetProvider.SeekToPacket(foundPacket, 1);

            // Update the PageGranulePosition to the position from this next packet which will be retrieved by the next QueueNextPacket call
            PageGranulePosition = _packetProvider.PeekNextPacket().PageGranulePosition;

            // Reset the state from the decoder to start processing a fresh stream
            _decoder.ResetState();
        }
Example #2
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;
                    }
                }
            }
        }
Example #3
0
        internal void SeekTo(long granulePos)
        {
            if (!_packetProvider.CanSeek)
            {
                throw new NotSupportedException();
            }

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

            var targetPacketIndex = 3;

            if (granulePos > 0)
            {
                var idx = _packetProvider.FindPacket(granulePos, GetPacketLength);
                if (idx == -1)
                {
                    throw new ArgumentOutOfRangeException("granulePos");
                }
                targetPacketIndex = idx - 1;  // move to the previous packet to prime the decoder
            }

            // seek the stream
            _packetProvider.SeekToPacket(targetPacketIndex);

            // 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;
                    }
                }
            }
        }