Ejemplo n.º 1
0
 /// <summary>
 /// Create an Album with associated metadata
 /// </summary>
 /// <param name="title"></param>
 /// <param name="artist"></param>
 /// <param name="genre"></param>
 /// <param name="releaseYear"></param>
 /// <param name="compression"></param>
 public Album(string title, Artist artist, string genre, int releaseYear, AudioCompression compression)
 {
     Title = title;
     Artist = artist;
     Genre = genre;
     ReleaseYear = releaseYear;
     CoverArt = IMPORTED_COVERART;
     Compression = compression;
 }
Ejemplo n.º 2
0
        public void UpdateAddArtist(IDBClient db, Artist toAdd)
        {
            var db4oClient = db as Db4oClient;

            // add the artist to the database
            if (db4oClient != null) db4oClient.Client.Store(toAdd);
        }
Ejemplo n.º 3
0
        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;
        }
Ejemplo n.º 4
0
        public IList<Track> RetrieveTracksByArtist(IDBClient db, Artist theArtist, Compression compression)
        {
            var db4oClient = db as Db4oClient;

            // search for the track
            if (db4oClient != null)
            {
                IEnumerable<Track> result = from Track t in db4oClient.Client
                                            where t.Album.Artist == theArtist &&
                                                  t.Album.Compression.Equals(compression)
                                            select t;

                // return the first one if anything returned
                return result.ToList();
            }

            return null;
        }
        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);
            }
        }