Esempio n. 1
0
        private async Task<PlayableBase> GetTrack(FileInfo fileInfo, List<string> supportedExtensions)
        {
            var extension = fileInfo.Extension.Remove(0, 1);

            //--- STEP 1: Check the track ---
            //Check the extension
            if (!supportedExtensions.Any(x => string.Equals(x, extension, StringComparison.OrdinalIgnoreCase)))
                return null;

            AudioInformation audioInformation = null;
            //Check the file
            if (
                !(await
                    Task.Run(
                        () =>
                            _musicDataManager.MusicManager.AudioEngine.TestAudioFile(fileInfo.FullName,
                                out audioInformation)))) //If the audio engine can't open the track, skip
                return null;

            LocalPlayable track;
            //--- STEP 2: Get information from the file ---
            //Search if track is already in the database
            if (SearchTrack(fileInfo.FullName, out track))
                return track;

            //Create a new track with some information we already have
            track = new LocalPlayable
            {
                Extension = extension.ToUpper(),
                TrackPath = fileInfo.FullName,
                Duration = audioInformation.Duration,
                SampleRate = Math.Round(audioInformation.SampleRate / 1000d, 1)
            };

            string filenameArtistName = null;
            string tagArtistName = null;
            string internetArtistName = null;

            string albumName = null;
            string title = null;

            /*
                Information priority:
                1. Tag
                2. Internet
                3. Filename
            */

            try
            {
                //Let's have a look in the tags
                using (var tagLibInfo = File.Create(fileInfo.FullName)) //We look into the tags. Perhaps we'll find something interesting
                {
                    track.Title = tagLibInfo.Tag.Title;
                    if (!string.IsNullOrEmpty(tagLibInfo.Tag.MusicBrainzArtistId))
                        track.Artist = await GetArtistByMusicBrainzId(tagLibInfo.Tag.MusicBrainzArtistId); //Ui, that's awesome
                    else
                        tagArtistName = tagLibInfo.Tag.FirstPerformer ?? tagLibInfo.Tag.FirstAlbumArtist; //Both is okay

                    if (tagLibInfo.Tag.Pictures.Any())
                    {
                        if (tagLibInfo.Tag.Pictures.Count() > 1)
                            Debug.Print("tagLibInfo.Tag.Pictures.Length > 1");

                        track.Cover = new TagImage(fileInfo.FullName);
                    }

                    track.Bitrate = tagLibInfo.Properties.AudioBitrate;
                    albumName = tagLibInfo.Tag.Album;
                }
            }
            catch (Exception)
            {
                //Do nothing
            }

            //At the next step, the title must have a value
            if (track.Title == null || (tagArtistName == null && track.Artist == null))
            {
                var match = Regex.Match(Path.GetFileNameWithoutExtension(fileInfo.FullName), @"(?<artist>([a-zA-Z].+?)) - (?<title>(.[^\(\[-]+))");
                if (match.Success)
                {
                    title = match.Groups["title"].Value.Trim();
                    if (tagArtistName == null)
                        filenameArtistName = match.Groups["artist"].Value;
                }
                else
                {
                    title = Path.GetFileNameWithoutExtension(fileInfo.FullName);
                    if (tagArtistName == null)
                        filenameArtistName = string.Empty;
                }
            }

            //Now we search the track in the internet. If we have find something, we set all information which has to be set
            var trackInfo = await _musicDataManager.LastfmApi.GetTrackInformation(track.Title ?? title, track.Artist?.Name ?? tagArtistName ?? filenameArtistName);
            if (trackInfo != null)
            {
                if (track.Title == null)
                    track.Title = trackInfo.Name;

                if (!string.IsNullOrEmpty(trackInfo.MusicBrainzId))
                {
                    var temp = SearchTrackByMusicBrainzId(trackInfo.MusicBrainzId);
                    if (temp != null)
                        return temp;

                    //Check if we already have a track with this id
                    track.MusicBrainzId = trackInfo.MusicBrainzId;
                }

                if (track.Cover == null)
                    track.Cover = trackInfo.CoverImage;

                if (track.Artist == null)
                {
                    track.Artist =
                        await SearchArtist(tagArtistName, trackInfo.Artist, filenameArtistName, track.MusicBrainzId);
                    if (track.Artist == null)
                        internetArtistName = trackInfo.Artist;
                }
            }
            else if(track.Title == null)
            {
                track.Title = title;
            }

            if (track.Artist == null)
            {
                var name = tagArtistName ?? internetArtistName ?? filenameArtistName;
                if (!string.IsNullOrEmpty(name))
                {
                    var artist = await _musicDataManager.LastfmApi.SearchArtistOnline(name);
                    track.Artist = artist ?? new Artist(name);
                }
                else
                {
                    track.Artist = _musicDataManager.Artists.UnknownArtist;
                }
            }

            if (!_musicDataManager.Artists.ArtistDictionary.ContainsKey(track.Artist.Guid))
                await _musicDataManager.Artists.AddArtist(track.Artist);

            if (!string.IsNullOrWhiteSpace(albumName))
            {
                track.Album =
                    _musicDataManager.Albums.Collection.FirstOrDefault(
                        x =>
                            string.Equals(x.Value.Name, albumName,
                                StringComparison.OrdinalIgnoreCase)).Value;

                if (track.Album == null)
                {
                    var album = new Album
                    {
                        Name = albumName,
                        Guid = Guid.NewGuid()
                    };

                    await _musicDataManager.Albums.AddAlbum(album);
                    track.Album = album;
                }

                if (track.Artist != _musicDataManager.Artists.UnknownArtist &&
                    !track.Album.Artists.Contains(track.Artist))
                {
                    track.Album.Artists.Add(track.Artist);
                    await _musicDataManager.Albums.UpdateAlbumArtists(track.Album);
                }
            }

            await _musicDataManager.Tracks.AddTrack(track);
            return track;
        }
Esempio n. 2
0
        private bool SearchTrack(string path, out LocalPlayable playable)
        {
            foreach (var playableBase in _musicDataManager.Tracks.Tracks)
            {
                var localTrack = playableBase as LocalPlayable;
                if (localTrack != null && localTrack.TrackPath == path)
                {
                    playable = localTrack;
                    return true;
                }
            }

            playable = null;
            return false;
        }