Exemple #1
0
        public bool Init(IStorageFile stream, bool readTags, string stateJson = null)
        {
            if (stream == null)
            {
                throw new ArgumentNullException($"Null argument {nameof(stream)} !");
            }

            _readTags   = readTags;
            _fileStream = stream;
            bsbuf       = bsspace_1;

            _fileStream.UseReadBuffer(2048);

            bool stateIsRestored = false;

            if (!string.IsNullOrEmpty(stateJson))
            {
                var state = JsonConvert.DeserializeObject <Mp3EncoderState>(stateJson);
                if (state != null)
                {
                    Args      = state.Args;
                    bsnum     = state.bsnum;
                    firsthead = state.firsthead;
                    fr        = state.frame;
                    framesize = state.framesize;
                    fsize     = state.fsize;
                    fsizeold  = state.fsizeold;
                    oldhead   = state.oldhead;
                    ssize     = state.ssize;
                    syncword  = state.syncword;

                    _frameOffsets = (state.Args.FramesFileOffsets?.ToList() ?? null);
                    // Restore full offset instead of relative offset value.
                    if (_frameOffsets != null)
                    {
                        for (int i = 1; i < _frameOffsets.Count; i++)
                        {
                            _frameOffsets[i] += _frameOffsets[i - 1];
                        }
                    }

                    // Seek to the beggining of the current frame.
                    _fileStream.Seek(state.FileOffset, SeekOrigin.Begin);

                    // Read current frame data.
                    //ReadFrame();

                    stateIsRestored = true;
                }
            }

            if (!stateIsRestored)
            {
                ReadFrame();
            }
            return(true);
        }
Exemple #2
0
        public bool ReadTags(IStorageFile file, int headerDataSize)
        {
            //TALB - album
            //TCOM - composer
            //TIT2 - song name.
            //TYER - year
            //TCON - genre
            //TRCK - track number
            //TPE1 - lead performer
            //TPE2 - band
            //APIC - Attached picture

            HashSet <string>            setFrameIds = new HashSet <string>(new[] { "TALB", "TCOM", "TIT2", "TYER", "TCON", "TRCK", "TPE1", "TPE2", "APIC" });
            Dictionary <string, string> dicAttributes = new Dictionary <string, string>();
            List <AttachedPicture>      attachedPictures = new List <AttachedPicture>();
            int offset = 0, offsetEnd = 0, numread = 0;

            byte[] tbuf = new byte[10];

            while (offset <= headerDataSize)
            {
                // Read frame id(4 bytes), size(4 bytes) and flags (2 bytes).
                numread = file.Read(tbuf, 0, 10);
                if (numread != 10)
                {
                    return(false);
                }

                var attrName = ReadAsString(tbuf, 0, 4, out offsetEnd);
                if (string.IsNullOrEmpty(attrName))
                {
                    break;
                }

                int size = (int)(BigEndianRead(tbuf, 4) & 0x7FFFFFFF);
                if (size == 0)
                {
                    break;
                }

                offset += (10 + size);

                if (setFrameIds.Contains(attrName))
                {
                    if (attrName == "APIC") // Attached picture.
                    {
                        var pict = ReadAttachedPicture(file, size);
                        if (pict != null)
                        {
                            attachedPictures.Add(pict);
                        }
                        else
                        {
                            break;
                        }
                        // _fileStream.Seek(size, SeekOrigin.Current); // Next attribute.
                    }
                    else
                    {
                        byte[] frameData = new byte[size];
                        numread = file.Read(frameData, 0, size);
                        if (numread != size)
                        {
                            return(false);
                        }
                        dicAttributes[attrName] = ReadAsString(frameData, 1, size - 1, out offsetEnd, frameData[0]);
                    }
                }
                else
                {
                    file.Seek(size, SeekOrigin.Current); // Next attribute.
                }
            }

            _attachedPictures = attachedPictures; // Attached pictures.
            _id3v2Tags        = dicAttributes;    // Set ID3v2 attributes.
            return(true);
        }
Exemple #3
0
        public bool Seek_StreamByPos(long pos /*packet position*/, bool readFrame = true)
        {
            long Posi = pos;
            long OldPosi, XPosi;

            if (Posi >= 0 && Posi <= MaxFrames)
            {
                if (((long)Args.CurrentPos) != pos) // Check current position.
                {
                    OldPosi = _fileStream.Seek(0, SeekOrigin.Current);

                    if (this._frameOffsets != null && this._frameOffsets.Count > pos)
                    {
                        XPosi = this._frameOffsets[(int)pos];
                    }
                    else
                    {
                        if (Args.IsVBR)
                        {
                            int index = (int)((pos * 100.0) / Args.NumFrames);
                            XPosi = (long)(((Args.TOC[index] * 1.0) / 256.0) * Args.FileLength) + Args.FirstFrameOffset;
                        }
                        else
                        {
                            XPosi = ((Framesize + 4) * Posi) + Args.FirstFrameOffset;
                        }
                    }

                    try
                    {
                        _fileStream.Seek(XPosi, SeekOrigin.Begin);
                    }
                    catch (IOException)
                    {
                        _fileStream.Seek(OldPosi, SeekOrigin.Begin);
                        return(false);
                    }

                    Args.CurrentPos = (ulong)Posi;
                }

                if (readFrame)
                {
                    int Res;
read_again:
                    Res = ReadFrame();
                    if (Res != 0)
                    {
                        Args.CurrentPos++;
                    }
                    if (Res == 0 && Res != -2)
                    {
                        goto read_again;
                    }
                }
                else
                {
                    framebuffIsEmpty = true; // Read frame of current packet.
                }

                return(true);
            }

            return(false);
        }