Пример #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);
                }
Пример #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());
        }