예제 #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BitStream"/> class 
        /// based on the specified byte array with an expandable capacity, using <see cref="System.IO.MemoryStream"/> as backing data.
        /// </summary>
        /// <param name="buffer">The array of unsigned bytes from which to create the current stream.</param>
        public BitStream(byte[] buffer)
        {
            if (buffer == null)
                throw new ArgumentNullException("buffer");

            _sb = new StreamBuffer(buffer.Length);
            _sb.Write(buffer, 0, buffer.Length);
            _sb.Position -= buffer.Length;
        }
 ////------------------------------------------------------------------------------------------------------------------------------
 /// <inheritdoc />
 public override byte[] ToByteArray()
 {
     using (StreamBuffer sb = new StreamBuffer())
     {
         sb.Write(base.ToByteArray());
         sb.WriteBigEndianBytes(UnencodedConstantValue, SampleSize / 8);
         return sb.ToByteArray();
     }
 }
예제 #3
0
        /// <summary>
        /// Returns the frame in a byte array.
        /// </summary>
        /// <returns>The frame in a byte array.</returns>
        public byte[] ToByteArray()
        {
            using (StreamBuffer sb = new StreamBuffer())
            {
                sb.WriteBigEndianInt32(_header);
                sb.Write(_sampleFrameNumberBytes);
                int blockSize = (_header >> 12) & 0xF;
                switch (blockSize)
                {
                    case 0x06:
                        sb.WriteByte((byte)(BlockSize - 1));
                        break;

                    case 0x07:
                        sb.WriteBigEndianInt16((short)(BlockSize - 1));
                        break;
                }
                int samplingRate = (_header >> 8) & 0xF;
                switch (samplingRate)
                {
                    case 0x0C:
                        sb.WriteByte((byte)(SamplingRate / 1000));
                        break;

                    case 0x0D:
                        sb.WriteBigEndianInt16((short)SamplingRate);
                        break;

                    case 0x0E:
                        sb.WriteBigEndianInt16((short)(SamplingRate / 10));
                        break;
                }
                sb.WriteByte((byte)_crc8);

                foreach (FlacSubFrame subFrame in SubFrames)
                    sb.Write(subFrame.ToByteArray());

                sb.WriteBigEndianInt16((short)_crc16);
                return sb.ToByteArray();
            }
        }
예제 #4
0
        /// <inheritdoc />
        public byte[] ToByteArray()
        {
            using (StreamBuffer sb = new StreamBuffer())
            {
                sb.WriteString(Identifier);
                FlacStreamInfoMetadataBlock streamInfoMetadataBlock = StreamInfoMetadataBlocks.FirstOrDefault();
                if (streamInfoMetadataBlock != null)
                    sb.Write(streamInfoMetadataBlock.ToByteArray());

                foreach (FlacMetadataBlock metadataBlock in MetadataBlocks.Where(m => !ReferenceEquals(m, streamInfoMetadataBlock)))
                    sb.Write(metadataBlock.ToByteArray());

                foreach (FlacFrame frame in Frames)
                    sb.Write(frame.ToByteArray());

                return sb.ToByteArray();
            }
        }
예제 #5
0
        /// <summary>
        /// Finds the <see cref="VbriHeader"/> header within the <paramref name="firstFrame"/>.
        /// </summary>
        /// <param name="firstFrame">The first frame.</param>
        /// <returns>The VBRI header if found; otherwise, null.</returns>
        /// <remarks>
        /// The VBRI header is located exactly 32 bytes after the end of the first MPEG audio header in the file.
        /// It will compare the first 4 bytes against the <see cref="HeaderIndicator"/> 
        /// to see if the header contains a <see cref="VbriHeader"/> or not.
        /// </remarks>
        public static new VbriHeader FindHeader(MpaFrame firstFrame)
        {
            if (firstFrame == null)
                throw new ArgumentNullException("firstFrame");

            using (StreamBuffer buffer = new StreamBuffer())
            {
                byte[] data = firstFrame.ToByteArray();
                buffer.Write(data);

                // 32 bytes = data indicating silence
                const long Offset = MpaFrame.FrameHeaderSize + SilenceDataSize;
                buffer.Seek(Offset, SeekOrigin.Begin);
                string tagName = buffer.ReadString(4, false, false);
                return String.Compare(tagName, HeaderIndicator, StringComparison.OrdinalIgnoreCase) == 0
                           ? new VbriHeader(firstFrame, buffer, Offset)
                           : null;
            }
        }
예제 #6
0
        /// <summary>
        /// Finds the <see cref="VbriHeader"/> header within the <paramref name="firstFrame"/>.
        /// </summary>
        /// <param name="firstFrame">The first frame.</param>
        /// <returns>The VBRI header if found; otherwise, null.</returns>
        /// <remarks>
        /// The XING header is located after the side information in Layer III in the first MPEG audio header in the file.
        /// It will compare the first 4 bytes against the <see cref="VbrHeaderIndicator"/> 
        /// to see if the header contains a <see cref="XingHeader"/> or not.
        /// </remarks>
        public static new XingHeader FindHeader(MpaFrame firstFrame)
        {
            if (firstFrame == null)
                throw new ArgumentNullException("firstFrame");

            long offset = MpaFrame.FrameHeaderSize + firstFrame.SideInfoSize;
            using (StreamBuffer buffer = new StreamBuffer())
            {
                byte[] data = firstFrame.ToByteArray();
                buffer.Write(data);

                buffer.Seek(offset, SeekOrigin.Begin);
                string tagName = buffer.ReadString(4, false, false);
                if ((String.Compare(tagName, VbrHeaderIndicator, StringComparison.OrdinalIgnoreCase) == 0)
                    || (String.Compare(tagName, CbrHeaderIndicator, StringComparison.OrdinalIgnoreCase) == 0))
                    return new XingHeader(firstFrame, buffer, offset);
            }
            return null;
        }
예제 #7
0
 /// <summary>
 /// Returns the frame in a byte array.
 /// </summary>
 /// <returns>The frame in a byte array.</returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer buffer = new StreamBuffer())
     {
         buffer.Write(_header);
         buffer.Write(AudioData);
         return buffer.ToByteArray();
     }
 }
예제 #8
0
        ////------------------------------------------------------------------------------------------------------------------------------
        /// <inheritdoc/>
        public byte[] ToByteArray()
        {
            using (StreamBuffer buffer = new StreamBuffer())
            {
                buffer.Write(HeaderIdentifierBytes);
                if (Lyrics != null)
                    buffer.WriteString(Lyrics, Encoding);

                buffer.Write(FooterIdentifierBytes);
                return buffer.ToByteArray();
            }
        }
예제 #9
0
 ////------------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Places the <see cref="VorbisComments"/> into a byte array.
 /// </summary>
 /// <returns>
 /// A byte array that represents the <see cref="VorbisComments"/>.
 /// </returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer buf = new StreamBuffer())
     {
         Vendor = Vendor ?? String.Empty;
         buf.WriteInt(Encoding.UTF8.GetByteCount(Vendor));
         buf.WriteString(Vendor, Encoding.UTF8);
         buf.WriteInt(Comments.Count);
         foreach (byte[] data in Comments.Where(c => c != null).Select(c => c.ToByteArray()))
         {
             buf.WriteInt(data.Length);
             buf.Write(data);
         }
         return buf.ToByteArray();
     }
 }
예제 #10
0
        private bool ReadItem(ApeVersion version, int valueSize, StreamBuffer stream, long maximumItemSize)
        {
            if (stream == null)
                throw new ArgumentNullException("stream");

            // Check if the item size indicated is really the size of the item.
            // Some files just aren't... properly written.
            byte[] data = new byte[valueSize];
            int dataBytesRead = stream.Read(data, valueSize);
            int bytesLeftInStream = (int)(maximumItemSize - dataBytesRead);
            if ((valueSize > 0) && (bytesLeftInStream > 0) && (dataBytesRead >= valueSize))
            {
                using (StreamBuffer dataBuffer = new StreamBuffer(data))
                {
                    // See if there's a next item.
                    // Try to find the start of the next item.
                    long startPositionNextItem = stream.Position;
                    long bytesUntilNextItem = GetBytesUntilNextItem(version, stream, maximumItemSize - valueSize);
                    stream.Position = startPositionNextItem;

                    // Seems that the size indicated by the item is not the total size of the item; read the extra bytes here.
                    if (bytesUntilNextItem > 0)
                    {
                        data = new byte[bytesUntilNextItem];
                        stream.Read(data, data.Length);
                        dataBuffer.Write(data);
                    }
                    data = dataBuffer.ToByteArray();
                }
            }
            Data = data;
            return true;
        }
예제 #11
0
        /// <inheritdoc />
        public byte[] ToByteArray()
        {
            using (StreamBuffer sb = new StreamBuffer())
            {
                foreach (MpaFrame frame in Frames)
                    sb.Write(frame.ToByteArray());

                return sb.ToByteArray();
            }
        }
예제 #12
0
 /// <summary>
 /// Returns the metadata block in a byte array.
 /// </summary>
 /// <returns>The frame in a byte array.</returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer buffer = new StreamBuffer())
     {
         buffer.WriteBigEndianBytes(Flags, 1);
         buffer.WriteBigEndianBytes(Data.Length, 3);
         buffer.Write(Data);
         return buffer.ToByteArray();
     }
 }
예제 #13
0
        ////------------------------------------------------------------------------------------------------------------------------------
        /// <inheritdoc />
        public byte[] ToByteArray()
        {
            using (StreamBuffer buffer = new StreamBuffer())
            {
                if (UseHeader)
                {
                    // Header
                    buffer.Write(HeaderIdentifierBytes);
                    buffer.WritePadding(NullByte, 2);
                    buffer.WriteString(XingEncoderVersion);
                    buffer.WritePadding(NullByte, 2);
                    buffer.WriteString(Version);
                    buffer.WritePadding(NullByte, 2);
                    buffer.WritePadding(PaddingByte, 226);
                }

                // Image extension
                int imageExtensionOffset = (int)buffer.Position;
                if ((Image != null) && !String.IsNullOrEmpty(Image.FileExtension))
                {
                    buffer.WriteString(Image.FileExtension);
                    buffer.WritePadding(PaddingByte, 4 - Image.FileExtension.Length);
                }
                else
                    buffer.WritePadding(PaddingByte, 4);

                // Image binary
                int imageBinaryOffset = (int)buffer.Position;
                if ((Image != null) && (Image.BinaryImage != null))
                {
                    buffer.WriteInt(Image.BinaryImage.Length);
                    buffer.Write(Image.BinaryImage);
                }
                else
                    buffer.WriteInt(0);

                // Unused
                int unusedOffset = (int)buffer.Position;
                buffer.WritePadding(NullByte, 4);

                // Version information
                int versionInfoOffset = (int)buffer.Position;
                buffer.WriteString(HeaderIdentifier);
                buffer.WritePadding(NullByte, 2);
                buffer.WriteString(XingEncoderVersion);
                buffer.WritePadding(NullByte, 2);
                buffer.WriteString(Version);
                buffer.WritePadding(NullByte, 2);
                buffer.WritePadding(PaddingByte, 226);

                // Audio meta-data
                // The audio meta-data is the heart of the MusicMatch tag.  It contains most
                // of the pertinent information found in other tagging formats (song title,
                // album title, artist, etc.) and some that are unique to this format (mood,
                // preference, situation).
                int audioMetaDataOffset = (int)buffer.Position;

                // Single-line text fields
                buffer.WriteShort((short)(SongTitle ?? String.Empty).Length);
                buffer.WriteString(SongTitle ?? String.Empty);
                buffer.WriteShort((short)(AlbumTitle ?? String.Empty).Length);
                buffer.WriteString(AlbumTitle ?? String.Empty);
                buffer.WriteShort((short)(ArtistName ?? String.Empty).Length);
                buffer.WriteString(ArtistName ?? String.Empty);
                buffer.WriteShort((short)(Genre ?? String.Empty).Length);
                buffer.WriteString(Genre ?? String.Empty);
                buffer.WriteShort((short)(Tempo ?? String.Empty).Length);
                buffer.WriteString(Tempo ?? String.Empty);
                buffer.WriteShort((short)(Mood ?? String.Empty).Length);
                buffer.WriteString(Mood ?? String.Empty);
                buffer.WriteShort((short)(Situation ?? String.Empty).Length);
                buffer.WriteString(Situation ?? String.Empty);
                buffer.WriteShort((short)(Preference ?? String.Empty).Length);
                buffer.WriteString(Preference ?? String.Empty);

                // Non-text fields
                buffer.WriteShort((short)(SongDuration ?? String.Empty).Length);
                buffer.WriteString(SongDuration ?? String.Empty);
                buffer.WriteDouble(CreationDate.ToOADate());
                buffer.WriteInt(PlayCounter);
                buffer.WriteShort((short)(OriginalFilename ?? String.Empty).Length);
                buffer.WriteString(OriginalFilename ?? String.Empty);
                buffer.WriteShort((short)(SerialNumber ?? String.Empty).Length);
                buffer.WriteString(SerialNumber ?? String.Empty);
                buffer.WriteShort(TrackNumber);

                // Multi-line text fields
                buffer.WriteShort((short)(Notes ?? String.Empty).Length);
                buffer.WriteString(Notes ?? String.Empty);
                buffer.WriteShort((short)(ArtistBio ?? String.Empty).Length);
                buffer.WriteString(ArtistBio ?? String.Empty);
                buffer.WriteShort((short)(Lyrics ?? String.Empty).Length);
                buffer.WriteString(Lyrics ?? String.Empty);

                // Internet addresses
                buffer.WriteShort((short)(ArtistUrl ?? String.Empty).Length);
                buffer.WriteString(ArtistUrl ?? String.Empty);
                buffer.WriteShort((short)(BuyCdUrl ?? String.Empty).Length);
                buffer.WriteString(BuyCdUrl ?? String.Empty);
                buffer.WriteShort((short)(ArtistEmail ?? String.Empty).Length);
                buffer.WriteString(ArtistEmail ?? String.Empty);

                // Null bytes
                buffer.WritePadding(NullByte, 16);

                // In all versions of the MusicMatch format up to and including 3.00, the
                // section is always 7868 bytes in length. All subsequent versions allowed
                // three possible lengths for this section: 7936, 8004, and 8132 bytes.  The
                // conditions under which a particular length from these three possibilities
                // was used is unknown. In all cases, this section is padded with dashes
                // ($2D) to achieve this constant size.
                double version;
                if (!Double.TryParse(Version, out version))
                    version = 3.100000;

                // The padding is calculated by taking the AudioMetaDataSize minus the total size of audio meta data fields,
                // minus the total size of the data offset fields, minus the footer size.
                long paddingSize = ((version <= 3.1) ? AudioMetaDataSizes.First() : AudioMetaDataSizes.Last()) - (buffer.Length - audioMetaDataOffset) - DataOffsetFieldsSize - FooterSize;
                buffer.WritePadding(0x2D, (int)paddingSize);

                // Data offsets
                buffer.WriteInt(imageExtensionOffset);
                buffer.WriteInt(imageBinaryOffset);
                buffer.WriteInt(unusedOffset);
                buffer.WriteInt(versionInfoOffset);
                buffer.WriteInt(audioMetaDataOffset);

                // Footer
                buffer.Write(FooterIdentifierBytes);
                buffer.WritePadding(PaddingByte, 13);
                buffer.WriteString(Version.Substring(0 ,4));
                buffer.WritePadding(PaddingByte, 12);

                return buffer.ToByteArray();
            }
        }
예제 #14
0
        ////------------------------------------------------------------------------------------------------------------------------------
        /// <inheritdoc/>
        /// <remarks>
        /// The extended tags will only be written when <see cref="UseExtendedTag"/> is set to true.
        /// </remarks>
        public byte[] ToByteArray()
        {
            using (StreamBuffer buffer = new StreamBuffer())
            {
                byte[] byteValue;
                if (UseExtendedTag)
                {
                    // TAG+
                    buffer.Write(ExtendedHeaderIdentifierBytes);

                    // Title - write the last 60 bytes
                    byteValue = _encoding.GetBytes(GetExtendedString(_trackTitle ?? String.Empty, 30, 60, true));
                    buffer.Write(byteValue);
                    buffer.WritePadding(0x00, 60 - byteValue.Length);

                    // Artist - write the last 60 bytes
                    byteValue = _encoding.GetBytes(GetExtendedString(_artist ?? String.Empty, 30, 60, true));
                    buffer.Write(byteValue);
                    buffer.WritePadding(0x00, 60 - byteValue.Length);

                    // Album Title - write the last 60 bytes
                    byteValue = _encoding.GetBytes(GetExtendedString(_albumTitle ?? String.Empty, 30, 60, true));
                    buffer.Write(byteValue);
                    buffer.WritePadding(0x00, 60 - byteValue.Length);

                    // Track Speed
                    buffer.WriteByte((byte)_trackSpeed);

                    // Extended Track Genre
                    byteValue = _encoding.GetBytes(ExtendedTrackGenre ?? String.Empty);
                    buffer.Write(byteValue);
                    buffer.WritePadding(0x00, 30 - byteValue.Length);

                    // Start-time
                    long startTimeMinutes = (StartTime.Days * 24 * 60) + (StartTime.Hours * 60) + StartTime.Minutes;
                    long startTimeSeconds = StartTime.Seconds;
                    string startTime = startTimeMinutes.ToString("000") + ":" + startTimeSeconds.ToString("00");
                    byteValue = _encoding.GetBytes(GetTruncatedEncodedString(startTime, 6));
                    buffer.Write(byteValue);

                    // End-time
                    long endTimeMinutes = (EndTime.Days * 24 * 60) + (EndTime.Hours * 60) + EndTime.Minutes;
                    long endTimeSeconds = EndTime.Seconds;
                    string endTime = endTimeMinutes.ToString("000") + ":" + endTimeSeconds.ToString("00");
                    byteValue = _encoding.GetBytes(GetTruncatedEncodedString(endTime, 6));
                    buffer.Write(byteValue);
                }

                // TAG
                buffer.Write(HeaderIdentifierBytes);

                // Track Title
                byteValue = _encoding.GetBytes(GetTruncatedEncodedString(_trackTitle ?? String.Empty, 30));
                buffer.Write(byteValue);
                buffer.WritePadding(0x00, 30 - byteValue.Length);

                // Artist
                byteValue = _encoding.GetBytes(GetTruncatedEncodedString(_artist ?? String.Empty, 30));
                buffer.Write(byteValue);
                buffer.WritePadding(0x00, 30 - byteValue.Length);

                // Album Title
                byteValue = _encoding.GetBytes(GetTruncatedEncodedString(_albumTitle ?? String.Empty, 30));
                buffer.Write(byteValue);
                buffer.WritePadding(0x00, 30 - byteValue.Length);

                // Album Year
                byteValue = _encoding.GetBytes(AlbumYear ?? String.Empty);
                buffer.Write(byteValue);
                buffer.WritePadding(0x00, 4 - byteValue.Length);

                // Track comment
                byteValue = _encoding.GetBytes(TrackComment ?? String.Empty);
                buffer.Write(byteValue);
                buffer.WritePadding(0x00, TrackCommentLength - byteValue.Length);

                // Track Number
                if (Version >= Id3v1Version.Id3v11)
                {
                    buffer.WriteByte(0x00);
                    buffer.WriteByte(TrackNumber);
                }

                // Genre
                buffer.WriteByte((byte)Genre);

                return buffer.ToByteArray();
            }
        }
예제 #15
0
 public void WriteTest2()
 {
     StreamBuffer target = new StreamBuffer(); // TODO: Initialize to an appropriate value
     byte[] buffer = null; // TODO: Initialize to an appropriate value
     target.Write(buffer);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
예제 #16
0
 ////------------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Writes the frame into a byte array.
 /// </summary>
 /// <returns>
 /// A byte array that represents the frame.
 /// </returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer sb = new StreamBuffer())
     {
         sb.WriteString(Identifier, Encoding.ASCII, FieldIdentifierLength);
         byte[] data = Data;
         if (data != null)
         {
             sb.WriteString(data.Length.ToString("D" + FieldSizeLength, CultureInfo.InvariantCulture));
             sb.Write(data);
         }
         else
         {
             sb.WriteString(0.ToString("D" + FieldSizeLength, CultureInfo.InvariantCulture));
         }
         return sb.ToByteArray();
     }
 }