////------------------------------------------------------------------------------------------------------------------------------
        /// <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();
            }
        }
 public void WritePaddingTest()
 {
     StreamBuffer target = new StreamBuffer(); // TODO: Initialize to an appropriate value
     byte paddingByte = 0; // TODO: Initialize to an appropriate value
     int length = 0; // TODO: Initialize to an appropriate value
     target.WritePadding(paddingByte, length);
     Assert.Inconclusive("A method that does not return a value cannot be verified.");
 }
Beispiel #3
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();
            }
        }
 public void SeekTest()
 {
     StreamBuffer target = new StreamBuffer();
     target.WritePadding(0x00, 1024);
     const long Offset = 100;
     const SeekOrigin Loc = new SeekOrigin();
     const long Expected = 100;
     long actual= target.Seek(Offset, Loc);
     Assert.AreEqual(Expected, actual);
 }