public void MoveToLibrary(string tempPath, string libraryLocation, Track theTrack) { // work out the new path string artistFolder = libraryLocation + "/" + theTrack.Album.Artist.Name; // create the directory if it doesn't exist if (!Directory.Exists(artistFolder)) { Directory.CreateDirectory(artistFolder); } // repeat the process for the album string albumFolder = artistFolder + "/" + theTrack.Album.Title; // create the directory if it doesn't exist if (!Directory.Exists(albumFolder)) { Directory.CreateDirectory(albumFolder); } // Propaganda only creates FLAC and MP3, so we can determine this via the compression string extension = ""; switch (theTrack.Album.Compression) { case Compression.Lossy: extension = ".mp3"; break; case Compression.Lossless: extension = ".flac"; break; } // move file the from the temporary location to its new location string newPath = albumFolder + "/" + theTrack.TrackNumber + "-" + theTrack.Title + extension; System.IO.File.Move(tempPath, newPath); // add the new path to the track theTrack.FileName = newPath; }
/// <summary> /// Add the Track to this Album /// </summary> /// <param name="newTrack"></param> public void AddTrack(Track newTrack) { // modify the track newTrack.Album = this; // if we don'a have this track on the album... if (!_tracks.Any(x => x.TrackNumber == newTrack.TrackNumber)) { // get the first track that has a higher track number var higher = _tracks.FirstOrDefault(x => x.TrackNumber > newTrack.TrackNumber); // if there isn't one... if (higher == null) // ...add it to the end _tracks.Add(newTrack); else // ...otherwise insert it just before _tracks.Insert(_tracks.IndexOf(higher)-1, newTrack); } }
public void TagFile(string filePath, Track theTrack) { // Create the file which we'll add the tag info to File toTag = File.Create(filePath); // fill out the tag information from the track toTag.Tag.Album = theTrack.Album.Title; toTag.Tag.Title = theTrack.Title; toTag.Tag.Track = (uint) theTrack.TrackNumber; toTag.Tag.Year = (uint) theTrack.Album.ReleaseYear; toTag.Tag.Genres = new[] {theTrack.Album.Genre}; var artistNames = new List<string>(); foreach (Artist artist in theTrack.Artists) { artistNames.Add(artist.Name); } toTag.Tag.AlbumArtists = artistNames.ToArray(); // save our changes in the file toTag.Save(); // now reverse engineer info for the track theTrack.TrackLength = toTag.Properties.Duration; if (toTag.MimeType == "taglib/flac") { theTrack.Album.Compression = Compression.Lossless; } else if (toTag.MimeType == "taglib/mp3") { theTrack.Album.Compression = Compression.Lossy; } }
public void UpdateAddTrack(IDBClient db, Track toAdd) { var db4oClient = db as Db4oClient; // add the track to the database if (db4oClient != null) db4oClient.Client.Store(toAdd); }
public Album GetTracks(CDDBEntry cddbResult) { // initialise the album to output Album newAlbum = null; // build up the command string var commandBuilder = new StringBuilder(); commandBuilder.Append("?"); commandBuilder.Append(FreeDBConstants.COMMAND); commandBuilder.Append(FreeDBConstants.COMMAND_READ); commandBuilder.Append("+"); commandBuilder.Append(cddbResult.Genre); commandBuilder.Append("+"); commandBuilder.Append(cddbResult.DiscID); commandBuilder.Append("&"); commandBuilder.Append(GenerateHello()); commandBuilder.Append("&"); commandBuilder.Append(GenerateProtocol()); // make the URL to use var urlBuilder = new StringBuilder(); urlBuilder.Append(FreeDBConstants.URL_FREEDB_MAIN); urlBuilder.Append(FreeDBConstants.URL_HTTP_ACCESS); // hit up FreeDB for the result IList<string> freeDBResponse = Query(urlBuilder.ToString(), commandBuilder.ToString()); // parse the query if (freeDBResponse.Count > 0) { // extract the response code ResponseCode responseCode = ExtractResponseCode(freeDBResponse[0]); switch (responseCode) { case ResponseCode.RESPONSE_CODE_DEFAULT: _log.Error("Problem querying album using command: " + commandBuilder); break; case ResponseCode.RESPONSE_CODE_200: case ResponseCode.RESPONSE_CODE_210: _log.Debug("Found track/s"); // get a hook to the DB IDBClient db = Database.RetrieveClient(); // we'll look for an artist to attach this all to Artist theArtist = Database.RetrieveArtistByName(db, cddbResult.ArtistName); // TODO Check for artists, as opposed to just one // if we don't we'll create one if (null == theArtist) { theArtist = new Artist(cddbResult.ArtistName); } db.Close(); // we'll create an album for this with invalid data, we'll fill that out after newAlbum = new Album(cddbResult.Title, theArtist, null, 1900, Compression.Undecided); // a list to keep track of the track lengths in seconds IList<int> trackLengths = new List<int>(); // keep track of the track offsets for track lengths int lastTrackOffset = 0; // break up each of the results foreach (string responseLine in freeDBResponse) { // extract the track lengths if (responseLine.Contains("#\t")) { // parse the next offset int newOffset = Int32.Parse(responseLine.Substring(2)); // stop the first one being processed if (lastTrackOffset > 0) { int seconds = (newOffset - lastTrackOffset)/75; trackLengths.Add(seconds); } // store the new offset lastTrackOffset = newOffset; } // extract the total number of seconds so we can calculate the last tracks length if (responseLine.Contains("# Disc length")) { // parse the total length of the album in seconds int totalLength = Int32.Parse(responseLine.Substring(15, 4)); int secondsToDate = lastTrackOffset/75; // extract the length of the last track from this to find out the length of the final track int lastTrackLength = totalLength - secondsToDate; // add this to the lengths trackLengths.Add(lastTrackLength); } // extract the year if (responseLine.Contains("DYEAR")) { string year = responseLine.Substring(6); newAlbum.ReleaseYear = Int32.Parse(year); } // extract a track if (responseLine.Contains("TTITLE")) { // marker for where to parse string to int indexOfSpace = responseLine.IndexOf('=', 6); string numberString = responseLine.Substring(6, (indexOfSpace - 6)); int trackNumber = Int32.Parse(numberString) + 1; string trackTitle = responseLine.Substring(indexOfSpace + 1); // create this with no path as it's not currently set var artists = new List<Artist>(); artists.Add(theArtist); var newTrack = new Track(trackNumber, artists, newAlbum, new Duration(new TimeSpan(0, 0, trackLengths[trackNumber - 1])), trackTitle, null); newAlbum.AddTrack(newTrack); } } break; default: _log.Error("Response came back we weren't expecting, handle it"); break; } } // pass back the resulting album return newAlbum; }
private void ProcessTrack(IDBClient db, string file) { // open the file using taglib try { // work out the compression type from the extension var compression = Compression.Lossy; if (AudioConstants.SUPPORTED_LOSSLESS.Contains(Path.GetExtension(file))) compression = Compression.Lossless; File musicFile = File.Create(file); // check whether or not the tag information is valid if (null == musicFile.Tag.Album || null == musicFile.Tag.Title) { _log.Error("Invalid tag information for: " + file); } else { // retrieve the album artist first if (null == _albumArtist) { // find the artist that should be for this album _albumArtist = Database.RetrieveArtistByName(db, musicFile.Tag.AlbumArtists[0]); // check if we have an existing album artist if (null == _albumArtist) { // if not, create one _albumArtist = new Artist(musicFile.Tag.AlbumArtists[0]); } } // have an album to work with if (null == _theAlbum) { // we'll attempt to find an album to add it to _theAlbum = Database.RetrieveAlbumByName(db, musicFile.Tag.Album, compression); // if there isn't an existing album if (null == _theAlbum) { // create a new album _theAlbum = new Album(musicFile.Tag.Album, _albumArtist, musicFile.Tag.FirstGenre, (int) musicFile.Tag.Year, compression); } } else { // make sure we have the right album if (_theAlbum.Title != musicFile.Tag.Album) { // we'll attempt to find an album to add it to or create a new one _theAlbum = Database.RetrieveAlbumByName(db, musicFile.Tag.Album, compression) ?? new Album(musicFile.Tag.Album, _albumArtist, musicFile.Tag.FirstGenre, (int) musicFile.Tag.Year, compression); // if there isn't an existing album } } // initialise the output track Track theTrack; var trackArtists = new List<Artist>(); // special case for tracks that have more than one artist if (musicFile.Tag.Performers.Count() > 1) { foreach (var artist in musicFile.Tag.Performers) { // we'll try with the album artist first var performer = _albumArtist; if (artist != _albumArtist.Name) performer = Database.RetrieveArtistByName(db, artist) ?? new Artist(artist); trackArtists.Add(performer); } } else { // we'll try with the album artist first if (musicFile.Tag.FirstPerformer == _albumArtist.Name) { trackArtists.Add(_albumArtist); } else { var performer = Database.RetrieveArtistByName(db, musicFile.Tag.FirstPerformer) ?? new Artist(musicFile.Tag.FirstPerformer); trackArtists.Add(performer); } // check for a track in the local object instead of hitting the DB try { // TODO not sure if this will work with the multiple artists now _theAlbum.Tracks.First( x => (x.TrackNumber == (int) musicFile.Tag.Track && x.Title == musicFile.Tag.Title)); } catch (InvalidOperationException) {} } // update the running tally foreach (var artist in trackArtists) { int result = 0; if (_tally.ContainsKey(artist)) result = _tally[artist]; if (0 == result) _tally.Add(artist, 1); else _tally[artist] = ++result; } // create a new track theTrack = new Track((int) musicFile.Tag.Track, trackArtists, _theAlbum, musicFile.Properties.Duration, musicFile.Tag.Title, file); // add the new track to the album _theAlbum.AddTrack(theTrack); // update the reference in the DB Database.UpdateAddTrack(db, theTrack); } } catch (ArgumentOutOfRangeException e) { _log.Error("Taglib had problem reading: " + file, e); return; } catch (Db4oException e) { _log.Error("DB problem when processing track: " + file, e); } catch (IOException e) { _log.Error("File IO problem when processing track: " + file, e); } }