public void AppendID3(System.IO.FileStream Stream) { //Blank string, used to prevent substring from running out of index System.String BlankString = " "; //Getting values System.String Title = LastManager.RemoveIllegalChars(this._Track) + BlankString; System.String Artist = LastManager.RemoveIllegalChars(this._Artist) + BlankString; System.String Album = LastManager.RemoveIllegalChars(this._Album) + BlankString; System.String Year = "0000" + BlankString; System.String Comment = "Last.FM by TheLastRipper." + BlankString; //Settings max length Title = Title.Substring(0, 30); Artist = Artist.Substring(0, 30).Trim(); Album = Album.Substring(0, 30).Trim(); Year = Year.Substring(0, 4).Trim(); Comment = Comment.Substring(0, 28).Trim(); System.Byte[] TagArray = new System.Byte[128]; for (int i = 0; i < TagArray.Length; i++) { TagArray[i] = 0; } //Get encoder System.Text.Encoding Coder = new System.Text.ASCIIEncoding(); //Get the bytes System.Byte[] Buffer = Coder.GetBytes("TAG"); System.Array.Copy(Buffer, 0, TagArray, 0, Buffer.Length); Buffer = Coder.GetBytes(Title); System.Array.Copy(Buffer, 0, TagArray, 3, Buffer.Length); Buffer = Coder.GetBytes(Artist); System.Array.Copy(Buffer, 0, TagArray, 33, Buffer.Length); Buffer = Coder.GetBytes(Album); System.Array.Copy(Buffer, 0, TagArray, 63, Buffer.Length); Buffer = Coder.GetBytes(Year); System.Array.Copy(Buffer, 0, TagArray, 93, Buffer.Length); Buffer = Coder.GetBytes(Comment); System.Array.Copy(Buffer, 0, TagArray, 97, Buffer.Length); //Set Track number to 0, and genre to other TagArray[126] = System.Convert.ToByte(0); TagArray[127] = System.Convert.ToByte(12); //Write data to end of stream Stream.Seek(0, System.IO.SeekOrigin.End); Stream.Write(TagArray, 0, 128); }
///<summary> ///Returns true the track is available, and the IMetaTrack would be castable to IMetaMusic ///</summary> protected virtual System.Boolean IsAvailable(ref IMetaTrack Track) { System.String TrackPath = ""; System.String ArtistDir = this.MusicPath + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(Track.Artist); if (System.IO.Directory.Exists(ArtistDir)) { foreach (System.String Directory in System.IO.Directory.GetDirectories(ArtistDir)) { TrackPath = Directory + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(Track.Track) + ".mp3"; if (System.IO.File.Exists(TrackPath)) { Track = new MetaMusic(TrackPath); return(true); } } } return(false); }
protected void SaveSong(MetaInfo SongInfo) { System.Boolean DownloadCovers = false; System.String AlbumPath = ""; System.String NewFilePath = ""; lock (LastManager.StreamLocker) { Stream RadioStream = (Stream)this.ReadHandle.AsyncState; System.Int32 Count = RadioStream.EndRead(this.ReadHandle); if (this.SkipSave || !SongInfo.Streaming) { //Close file this.TempFile.Close(); //Create or overwrite tempfile this.TempFile = File.Create(PathSettings.TempFilePath); //Start recording agian this.ReadHandle = RadioStream.BeginRead(this.Buffer, 0, this.Buffer.Length, new System.AsyncCallback(this.TempSave), RadioStream); //Change SkipSave this.SkipSave = false; } else { //Write last data from stream this.TempFile.Write(this.Buffer, 0, Count); //Write metadata to stream as ID3v1 SongInfo.AppendID3(this.TempFile); //Write the file, and close it this.TempFile.Flush(); this.TempFile.Close(); this.TempFile.Dispose(); //Filesystem paths AlbumPath = this.MusicPath + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(SongInfo.Artist) + System.IO.Path.DirectorySeparatorChar + LastManager.RemoveIllegalChars(SongInfo.Album) + System.IO.Path.DirectorySeparatorChar; NewFilePath = AlbumPath + LastManager.RemoveIllegalChars(SongInfo.Track) + ".mp3"; //Dont overwrite file if it already exist, new rip may be bad, and we should leave it to the user to sort them manually if (File.Exists(NewFilePath)) { File.Delete(PathSettings.TempFilePath); } else { if (!Directory.Exists(AlbumPath)) { Directory.CreateDirectory(AlbumPath); } File.Move(PathSettings.TempFilePath, NewFilePath); } //Create or overwrite tempfile this.TempFile = File.Create(PathSettings.TempFilePath); //Start recording agian this.ReadHandle = RadioStream.BeginRead(this.Buffer, 0, this.Buffer.Length, new System.AsyncCallback(this.TempSave), RadioStream); //Set download covers, do this outside the lock. DownloadCovers = true; } } if (DownloadCovers) { //Download covers WebClient Client = new WebClient(); if ((!File.Exists(AlbumPath + "SmallCover.jpg")) && SongInfo.AlbumcoverSmall != null) { Client.DownloadFile(SongInfo.AlbumcoverSmall, AlbumPath + "SmallCover.jpg"); } if ((!File.Exists(AlbumPath + "MediumCover.jpg")) && SongInfo.AlbumcoverMedium != null) { Client.DownloadFile(SongInfo.AlbumcoverMedium, AlbumPath + "MediumCover.jpg"); } if ((!File.Exists(AlbumPath + "LargeCover.jpg")) && SongInfo.AlbumcoverLarge != null) { Client.DownloadFile(SongInfo.AlbumcoverLarge, AlbumPath + "LargeCover.jpg"); } } }