Пример #1
0
        private bool int_FindFirstHeader(Stream i_Stream)
        {
            try
            {
                // Go to start of file
                i_Stream.Seek(0, SeekOrigin.Begin);

                if (m_FirstHeader == null)
                    m_FirstHeader = new MP3Header();

                BinaryReader Reader = new BinaryReader(i_Stream);
                if (Reader == null)
                {
                    Debugger.Trace("There was an error opening the BinaryReader to initialize the MP3File");
                    return false;
                }

                BufferedReader MyReader = new BufferedReader(Reader, 1024);
                MP3Header NextHeader = new MP3Header();

                byte[] HeaderBuffer = new byte[4];
                HeaderBuffer[0] = MyReader.ReadByte();
                HeaderBuffer[1] = MyReader.ReadByte();
                HeaderBuffer[2] = MyReader.ReadByte();
                HeaderBuffer[3] = MyReader.ReadByte();

                // Find the first valid header
                while (!m_FirstHeader.IsValid || !NextHeader.IsValid)
                {
                    // Find the next header tag
                    while ((HeaderBuffer[0] != 0xFF) || (HeaderBuffer[1] & 0xE0) != 0xE0)
                    {
                        // Get the Next Byte
                        HeaderBuffer[0] = HeaderBuffer[1];
                        HeaderBuffer[1] = HeaderBuffer[2];
                        HeaderBuffer[2] = HeaderBuffer[3];
                        HeaderBuffer[3] = MyReader.ReadByte();
                    }

                    if (m_FirstHeader.Initialize(HeaderBuffer, MyReader.Position - 4))
                    {
                        // Move to the next header to check if it is the same format
                        // make sure the reader can move that far forward
                        if (MyReader.MoveForward(m_FirstHeader.FrameSize - 4))
                        {
                            byte[] NextHeaderBuffer = new byte[4];
                            NextHeaderBuffer[0] = MyReader.ReadByte();
                            NextHeaderBuffer[1] = MyReader.ReadByte();
                            NextHeaderBuffer[2] = MyReader.ReadByte();
                            NextHeaderBuffer[3] = MyReader.ReadByte();
                            if ((NextHeaderBuffer[0] == 0xFF) && (NextHeaderBuffer[1] & 0xE0) == 0xE0)
                            {
                                if (NextHeader.Initialize(NextHeaderBuffer, MyReader.Position - 4))
                                {
                                    // If it is not the same header as the previous one then reset it
                                    if (NextHeader != m_FirstHeader)
                                        NextHeader = new MP3Header();
                                }
                            }
                            MyReader.MoveBackward(m_FirstHeader.FrameSize);
                        }
                    }

                    // Get the Next Byte
                    HeaderBuffer[0] = HeaderBuffer[1];
                    HeaderBuffer[1] = HeaderBuffer[2];
                    HeaderBuffer[2] = HeaderBuffer[3];
                    HeaderBuffer[3] = MyReader.ReadByte();
                }

                return true;
            }
            catch (Exception ex)
            {
                Debugger.Trace(ex);
                return false;
            }
        }
Пример #2
0
        public override bool Trim(TimeSpan i_Start, TimeSpan i_End, String i_FileDestination)
        {
            try
            {
                if (!m_Initialized)
                {
                    Debugger.Trace("MP3File.Trim() not initialized");
                    return false;
                }

                if (i_Start > i_End)
                {
                    Debugger.Trace("MP3File.Trim() The End time is before the Start time");
                    return false;
                }

                if (i_End > m_SongLength)
                {
                    Debugger.Trace("MP3File.Trim() The End time after the End of the song");
                    return false;
                }

                Debugger.Trace("Trimming the MP3 File");
                if (m_FirstHeader == null)
                    m_FirstHeader = new MP3Header();

                IsolatedStorageFile Store = IsolatedStorageFile.GetUserStoreForApplication();
                if (!Store.FileExists(m_FileName))
                {
                    Debugger.Trace("The mp3 file does not exist");
                    return false;
                }

                Debugger.Trace("Opening File streams");
                using (IsolatedStorageFileStream OriginalStream = Store.OpenFile(m_FileName, FileMode.Open))
                {
                    using (IsolatedStorageFileStream DestinationStream = Store.OpenFile(i_FileDestination, FileMode.Create))
                    {
                        OriginalStream.Seek(0, 0);
                        DestinationStream.Seek(0, 0);

                        BinaryReader Reader = new BinaryReader(OriginalStream);
                        if (Reader == null)
                        {
                            Debugger.Trace("There was an error opening the BinaryReader to trim the MP3File");
                            return false;
                        }

                        BinaryWriter Writer = new BinaryWriter(DestinationStream);
                        if (Writer == null)
                        {
                            Debugger.Trace("There was an error opening the BinaryWriter to trim the MP3File");
                            return false;
                        }

                        BufferedReader MyReader = new BufferedReader(Reader, 1024);

                        // Copy the Header information
                        Debugger.Trace("Copying meta data");
                        MyReader.MoveForward(m_FirstHeader.StartLocation);
                        //MyReader.Copy(Writer, m_FirstHeader.StartLocation);

                        // Skip over headers until we get to the start time
                        double CurTime = 0.0;
                        MP3Header NextHeader = new MP3Header();
                        byte[] HeaderBuffer = new byte[4];
                        HeaderBuffer[0] = MyReader.ReadByte();
                        HeaderBuffer[1] = MyReader.ReadByte();
                        HeaderBuffer[2] = MyReader.ReadByte();
                        HeaderBuffer[3] = MyReader.ReadByte();
                        while (CurTime < i_Start.TotalSeconds && NextHeader.Initialize(HeaderBuffer, MyReader.Position - 4))
                        {
                            // Update the time
                            CurTime += NextHeader.SamplesPerFrame / (double)NextHeader.SampleRate;

                            if (!MyReader.MoveForward(NextHeader.FrameSize - 4))
                            {
                                Debugger.Trace("Reached the end of the mp3 file");
                                return false;
                            }

                            HeaderBuffer[0] = MyReader.ReadByte();
                            HeaderBuffer[1] = MyReader.ReadByte();
                            HeaderBuffer[2] = MyReader.ReadByte();
                            HeaderBuffer[3] = MyReader.ReadByte();
                        }

                        // Copy Frames until we get to the end position
                        while (CurTime < i_End.TotalSeconds && NextHeader.Initialize(HeaderBuffer, MyReader.Position - 4))
                        {
                            // Update the time
                            CurTime += NextHeader.SamplesPerFrame / (double)NextHeader.SampleRate;
                            MyReader.MoveBackward(4);
                            MyReader.Copy(Writer, NextHeader.FrameSize);

                            HeaderBuffer[0] = MyReader.ReadByte();
                            HeaderBuffer[1] = MyReader.ReadByte();
                            HeaderBuffer[2] = MyReader.ReadByte();
                            HeaderBuffer[3] = MyReader.ReadByte();
                        }

                        Writer.Flush();
                        return true;
                    }
                }
            }
            catch(Exception ex)
            {
                Debugger.Trace(ex);
                return false;
            }
        }