Beispiel #1
0
        /// <summary>
        /// 同时根据专辑信息以及歌曲信息获取网易云音乐上的歌曲
        /// </summary>
        /// <param name="tag"></param>
        /// <param name="track"></param>
        /// <param name="album"></param>
        /// <returns></returns>
        private static async Task <TrackInfo> SearchTrackAsync(Tag tag, Track track, Album album)
        {
            TrackInfo trackInfo;
            int       trackId;
            NcmTrack  ncmTrack;
            bool      byUser;

            trackInfo = _database.TrackInfos.Match(track, album);
            if (!(trackInfo is null))
            {
                return(trackInfo);
            }
            // 先尝试从数据库获取歌曲
            if (The163KeyHelper.TryGetTrackId(tag, out trackId))
            {
                // 尝试从163Key获取ID成功
                ncmTrack = new NcmTrack(track, trackId);
            }
            else
            {
                // 不存在163Key
                AlbumInfo albumInfo;

                albumInfo = album is null ? null : await SearchAlbumAsync(album);

                // 尝试获取专辑信息
                if (!(albumInfo is null))
                {
                    // 网易云音乐收录了歌曲所在专辑
                    NcmTrack[] ncmTracks;

                    ncmTracks = (await GetAlbumTracksAsync(albumInfo)).Where(t => ComputeSimilarity(t.Name, track.Name, false) != 0).ToArray();
                    // 获取网易云音乐上专辑收录的歌曲
                    ncmTrack = MatchByUser(ncmTracks, track);
                }
Beispiel #2
0
        private static AudioInfo[] LoadAllAudioInfos(string directory)
        {
            return(Directory.EnumerateFiles(directory, "*", SearchOption.AllDirectories).Where(audioPath => {
                string lrcPath;

                lrcPath = Path.ChangeExtension(audioPath, ".lrc");
                return !CanSkip(audioPath, lrcPath);
            }).AsParallel().AsOrdered().Select(audioPath => {
                TagLib.File audioFile;
                Tag tag;
                AudioInfo audioInfo;
                TrackInfo trackInfo;
                int trackId;

                audioFile = null;
                audioInfo = new AudioInfo {
                    Path = audioPath
                };
                try {
                    audioFile = TagLib.File.Create(audioPath);
                    tag = audioFile.Tag;
                    if (Album.HasAlbumInfo(tag))
                    {
                        audioInfo.Album = new Album(tag, true);
                    }
                    audioInfo.Track = new Track(tag);
                }
                catch (Exception ex) {
                    FastConsole.WriteError("无效音频文件!");
                    FastConsole.WriteException(ex);
                    return null;
                }
                finally {
                    audioFile?.Dispose();
                }
                trackInfo = _database.TrackInfos.Match(audioInfo.Album, audioInfo.Track);
                if (!(trackInfo is null))
                {
                    audioInfo.TrackInfo = trackInfo;
                    return audioInfo;
                }
                // 尝试从数据库获取歌曲
                if (The163KeyHelper.TryGetTrackId(tag, out trackId))
                {
                    audioInfo.TrackInfo = new TrackInfo(audioInfo.Track, audioInfo.Album, trackId);
                    return audioInfo;
                }
                // 尝试从163Key获取ID
                return audioInfo;
            }).Where(t => !(t is null)).ToArray());
        }
Beispiel #3
0
        /// <summary>
        /// 同时根据专辑信息以及歌曲信息获取网易云音乐上的歌曲
        /// </summary>
        /// <param name="track"></param>
        /// <param name="album"></param>
        /// <param name="audioPath"></param>
        /// <returns></returns>
        private static async Task <NcmTrack> MapToAsync(Track track, Album album, string audioPath)
        {
            if (track == null)
            {
                throw new ArgumentNullException(nameof(track));
            }
            if (audioPath == null)
            {
                throw new ArgumentNullException(nameof(audioPath));
            }

            string     fileName;
            TrackCache trackCache;
            int        trackId;
            NcmTrack   ncmTrack;

            fileName   = Path.GetFileName(audioPath);
            trackCache = album == null?_allCaches.TrackCaches.Match(track, fileName) : _allCaches.TrackCaches.Match(track, album);

            // 有专辑信息就用专辑信息,没有专辑信息就用文件名
            if (trackCache != null)
            {
                return(new NcmTrack(track, trackCache.Id));
            }
            // 先尝试从缓存获取歌曲
            if (The163KeyHelper.TryGetMusicId(audioPath, out trackId))
            {
                // 尝试从163Key获取ID
                ncmTrack = new NcmTrack(track, trackId);
            }
            else
            {
                NcmAlbum ncmAlbum;

                ncmAlbum = null;
                if (album != null)
                {
                    // 存在专辑信息,尝试获取网易云音乐上对应的专辑
                    AlbumCache albumCache;

                    albumCache = _allCaches.AlbumCaches.Match(album);
                    if (albumCache != null)
                    {
                        ncmAlbum = new NcmAlbum(album, albumCache.Id);
                    }
                    // 先尝试从缓存获取专辑
                    if (ncmAlbum == null)
                    {
                        ncmAlbum = await MapToAsync(album);

                        if (ncmAlbum != null)
                        {
                            UpdateCache(album, ncmAlbum.Id);
                        }
                    }
                }
                if (ncmAlbum == null)
                {
                    // 没有对应的专辑信息,使用无专辑匹配
                    ncmTrack = await MapToAsync(track);
                }
                else
                {
                    // 网易云音乐收录了歌曲所在专辑
                    NcmTrack[] ncmTracks;

                    ncmTracks = (await GetTracksAsync(ncmAlbum)).Where(t => ComputeSimilarity(t.Name, track.Name, false) != 0).ToArray();
                    // 获取网易云音乐上专辑收录的歌曲
                    ncmTrack = MatchByUser(ncmTracks, track);
                    if (ncmTrack == null)
                    {
                        // 网易云音乐上的专辑可能没收录这个歌曲,不清楚为什么,但是确实存在这个情况,比如专辑id:3094396
                        ncmTrack = await MapToAsync(track);
                    }
                }
            }
            if (ncmTrack == null)
            {
                Logger.Instance.LogWarning("歌曲匹配失败!");
            }
            else
            {
                Logger.Instance.LogInfo("歌曲匹配成功!");
                if (album == null)
                {
                    UpdateCache(track, fileName, ncmTrack.Id);
                }
                else
                {
                    UpdateCache(track, album, ncmTrack.Id);
                }
            }
            return(ncmTrack);
        }