////------------------------------------------------------------------------------------------------------------------------------
 /// <inheritdoc />
 public override byte[] ToByteArray()
 {
     using (StreamBuffer sb = new StreamBuffer())
     {
         sb.Write(base.ToByteArray());
         sb.WriteBigEndianBytes(UnencodedConstantValue, SampleSize / 8);
         return sb.ToByteArray();
     }
 }
Пример #2
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();
     }
 }
Пример #3
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();
            }
        }
Пример #4
0
 /// <summary>
 /// Places the <see cref="VorbisComment"/> into a byte array.
 /// </summary>
 /// <returns>
 /// A byte array that represents the <see cref="VorbisComment"/>.
 /// </returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer buf = new StreamBuffer())
     {
         string val = (Name ?? String.Empty) + Delimiter + Value;
         buf.WriteInt(Encoding.UTF8.GetByteCount(val));
         buf.WriteString(val, Encoding.UTF8);
         return buf.ToByteArray();
     }
 }
Пример #5
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();
     }
 }
Пример #6
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();
     }
 }
Пример #7
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();
     }
 }
Пример #8
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();
            }
        }
Пример #9
0
 ////------------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Writes the <see cref="Id3v2ReplayGain"/> into a byte array.
 /// </summary>
 /// <returns>
 /// A byte array that represents the <see cref="Id3v2ReplayGain"/> instance.
 /// </returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer stream = new StreamBuffer())
     {
         int value = ((int)NameCode & NameCodeMask) << 13;
         value &= ((int)OriginatorCode & OriginatorCodeMask) << 10;
         value &= ((int)Sign & SignMask) << 9;
         value &= Adjustment & AdjustmentMask;
         stream.WriteShort((short)value);
         return stream.ToByteArray();
     }
 }
Пример #10
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();
            }
        }
Пример #11
0
 /// <summary>
 /// Returns the frame in a byte array.
 /// </summary>
 /// <returns>The frame in a byte array.</returns>
 public virtual byte[] ToByteArray()
 {
     using (StreamBuffer sb = new StreamBuffer())
     {
         sb.WriteBigEndianInt32(Header);
         sb.WriteUnaryInt(WastedBits);
         return sb.ToByteArray();
     }
 }
Пример #12
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();
            }
        }
Пример #13
0
 public void ToByteArrayTest()
 {
     byte[] value = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A };
     StreamBuffer target = new StreamBuffer(value);
     byte[] expected = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A };
     byte[] actual = target.ToByteArray();
     Assert.IsTrue(actual.SequenceEqual(expected));
 }
Пример #14
0
        /// <inheritdoc/>
        public override byte[] ToByteArray()
        {
            using (StreamBuffer buf = new StreamBuffer())
            {
                buf.WriteString(Name);
                buf.WriteBigEndianInt32(Flags);

                if ((Flags & XingHeaderFlags.FrameCountFlag) != 0)
                    buf.WriteBigEndianInt32(FrameCount);

                if ((Flags & XingHeaderFlags.FileSizeFlag) != 0)
                    buf.WriteBigEndianInt32(FileSize);

                // Extract TOC (Table of Contents) for more accurate seeking
                if ((Flags & XingHeaderFlags.TocFlag) != 0)
                {
                    for (int i = 0; i < 100; i++)
                        buf.WriteByte((byte)Toc[i]);
                }

                if ((Flags & XingHeaderFlags.VbrScaleFlag) != 0)
                    buf.WriteBigEndianInt32(Quality);

                return buf.ToByteArray();
            }
        }
Пример #15
0
        /// <inheritdoc />
        public byte[] ToByteArray()
        {
            using (StreamBuffer sb = new StreamBuffer())
            {
                foreach (MpaFrame frame in Frames)
                    sb.Write(frame.ToByteArray());

                return sb.ToByteArray();
            }
        }
Пример #16
0
        /// <inheritdoc/>
        public override byte[] ToByteArray()
        {
            using (StreamBuffer buffer = new StreamBuffer())
            {
                buffer.WriteString(Name);
                buffer.WriteBigEndianInt16(Version);
                buffer.WriteBigEndianInt16((short)_delay);
                buffer.WriteBigEndianInt16((short)Quality);
                buffer.WriteBigEndianInt32(FileSize);
                buffer.WriteBigEndianInt32(FrameCount);
                buffer.WriteBigEndianInt16(TableEntries);
                buffer.WriteBigEndianInt16(TableScale);
                buffer.WriteBigEndianInt16(TableEntrySize);
                buffer.WriteBigEndianInt16(FramesPerTableEntry);
                for (int i = 0; i <= TableEntries; i++)
                    buffer.WriteBigEndianBytes(Toc[i] / TableScale, TableEntrySize);

                return buffer.ToByteArray();
            }
        }
Пример #17
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;
        }
Пример #18
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();
            }
        }
Пример #19
0
 ////------------------------------------------------------------------------------------------------------------------------------
 /// <summary>
 /// Places the <see cref="LameTag"/> into a byte array.
 /// </summary> 
 /// <returns>A byte array that represents the <see cref="LameTag"/> instance.</returns>
 public byte[] ToByteArray()
 {
     using (StreamBuffer buffer = new StreamBuffer())
     {
         if (Version < 3.09f)
         {
             buffer.WriteString(EncoderVersion, Encoding.ASCII, 20);
         }
         else
         {
             buffer.WriteString(EncoderVersion, Encoding.ASCII, 9);
             buffer.WriteByte((byte)((InfoTagRevision & 0xF0) | (VbrMethod & 0x0F)));
             buffer.WriteByte((byte)LowpassFilterValue);
             buffer.WriteFloat(PeakSignalAmplitude);
             buffer.WriteShort(RadioReplayGain);
             buffer.WriteShort(AudiophileReplayGain);
             buffer.WriteByte((byte)EncodingFlags);
             buffer.WriteByte((byte)BitRate);
             buffer.WriteBytes(EncoderDelays, 3);
             buffer.WriteByte((byte)Misc);
             buffer.WriteByte((byte)Mp3Gain);
             buffer.WriteShort(PresetSurroundInfo);
             buffer.WriteBigEndianInt32(MusicLength);
             buffer.WriteBigEndianInt16(_musicCrc);
             buffer.WriteBigEndianInt16(_infoTagCrc);
         }
         return buffer.ToByteArray();
     }
 }