Exemplo n.º 1
0
        public override long Seek(long offset, SeekOrigin origin)
        {
            MemoryStream ms = WAVIrrKlangFileFactory.PrepareStream(Name, ID);
            long         p  = ms.Seek(offset, origin);

            WAVIrrKlangFileFactory.SavePos(Name, ID);
            return(p);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Overriden Read, it handles basic info reading, also emulate loop
        /// </summary>
        /// <param name="array">Where to read bytes</param>
        /// <param name="offset">Offset in array</param>
        /// <param name="count">Number of bytes to read</param>
        /// <returns></returns>
        public override int Read(byte[] array, int offset, int count)
        {
            _ms = WAVIrrKlangFileFactory.PrepareStream(Name, ID);
            int rdb = 0;

            byte[] preread = new byte[4];

            try
            {
                // Check the overread case, if trying to read after the second marker or the end of the file
                if (LoopedEndPosition != 0 && (_ms.Position + count > LoopedEndPosition))
                {
                    // Just for sure, reverse check, if already overrun
                    if (LoopedEndPosition - _ms.Position > 0)
                    {
                        rdb = _ms.Read(array, offset, (int)(LoopedEndPosition - _ms.Position));
                    }
                }
                else
                {
                    if (count < 4)
                    {
#if DEBUGSCR
                        Console.WriteLine("(Now using preread on file {0})", this.Name.Substring(this.Name.LastIndexOf('\\')));
#endif
                        rdb = _ms.Read(preread, 0, 4);
                        _ms.Seek(4 - count, SeekOrigin.Current);
                        for (int i = 0; i < count; i++)
                        {
                            array[i] = preread[i];
                        }
                    }
                    else
                    {
                        // May read without problems
                        rdb = _ms.Read(array, offset, count);
                        for (int i = 0; i < (rdb >= 4 ? 4 : count); i++)
                        {
                            preread[i] = array[i];
                        }
                    }
                }

                // Length operation functions and CUE read
                #region Length and CUE operations
                // Previously set flag if the length of the wav will be the next info
                if (_isNextLength)
                {
#if DEBUGSCR
                    Console.WriteLine("Using file: " + Name.Substring(Name.LastIndexOf('\\')));
#endif
                    // Store the original legth
                    _InternalLength = FromArray(array);

                    // Set the provided length to an enough high number - it's more than six hours
                    array[0] = 0;
                    array[1] = 0;
                    array[2] = 0;
                    array[3] = 0xF0;
                    // Reset flag and store the positions
                    // Also set markers to default
                    _isNextLength          = false;
                    _AbsoluteBeginPosition = _ms.Position;
                    _Marker1Position       = 0;
                    _Marker2Position       = _InternalLength;

                    // Load cue info
                    FindCUE();
                }
                // data, nex is length
                //else if (count == 4 && array[0] == 100 && array[1] == 97 && array[2] == 116 && array[3] == 97)
                else if (preread[0] == 100 && preread[1] == 97 && preread[2] == 116 && preread[3] == 97)
                {
                    _isNextLength = true;
                }
                // fmt, may read the BPS
                else if (preread[0] == 102 && preread[1] == 109 && preread[2] == 116)
                {
                    long pos = _ms.Position;
                    _ms.Seek(6, SeekOrigin.Current);
                    _SPS = (int)FromReadArray(2);
                    _ms.Seek(10, SeekOrigin.Current);
                    _BPS = (int)FromReadArray(2) / 8;
                    _ms.Seek(pos, SeekOrigin.Begin);
                }
                #endregion

                // Check if loop, if is, read the rest to the buffer from the begining
                // If not, it is the end of the loop, so no more data
                while (count > rdb && !_isShouldFinish)
                {
                    LoopCount++;
                    _ms.Seek(BeginPosition, SeekOrigin.Begin);
                    //rdb += base.Read(array, offset + rdb, count - rdb);
                    // Check the overread case, if trying to read after the second marker or the end of the file
                    if (LoopedEndPosition != 0 && (_ms.Position + (count - rdb) > LoopedEndPosition))
                    {
                        // Just for sure, reverse check, if already overrun
                        if (LoopedEndPosition - _ms.Position > 0)
                        {
                            rdb += _ms.Read(array, offset + rdb, (int)(LoopedEndPosition - _ms.Position));
                        }
                    }
                    else
                    {
                        // May read without problems
                        rdb += _ms.Read(array, offset + rdb, count - rdb);
                    }
                }

                if (count > rdb && _isShouldFinish && _ms.Position < LoopedEndPosition)
                {
                    rdb += _ms.Read(array, offset + rdb, (int)(LoopedEndPosition - _ms.Position));
                }

                isPlaying = rdb != 0;

                // return the read bytes number, if less than expected, it will indicate the end of the stream
                return(rdb);
            }
#if DEBUGSCR
            catch (Exception e)
            {
                Console.WriteLine("Exception caught on file {0}.", this.Name.Substring(this.Name.LastIndexOf('\\')));
                Console.WriteLine(e.Message);
                Console.WriteLine(e.StackTrace);
                return(rdb);
            }
#else
            catch (Exception)
            {
                return(rdb);
            }
#endif
            finally
            {
                WAVIrrKlangFileFactory.SavePos(Name, ID);
            }
        }