Пример #1
0
        public static SongViewModel ToSongViewModel(Song song)
        {
            Mapper.CreateMap<Song, SongViewModel>();
            var result = Mapper.Map<Song, SongViewModel>(song);
            if (result != null && result.Duration != default(TimeSpan))
            {
                result.DurationFormatted = result.Duration.ToString(@"mm\:ss");

            }

            return result;
        }
Пример #2
0
        public void AddSong(Song song, string userId)
        {
            using (var db = new ApplicationDbContext())
            {
                song.AddBy = userId;
                song.AddDate = DateTime.Now;
                db.Songs.Add(song);

                CreateUpdateDefaultPlaylist(song.SongId, userId, db);

                db.SaveChanges();
            }
        }
Пример #3
0
        public void AddToVkPlayList(string userId, Song song)
        {
            using (var db = new ApplicationDbContext())
            {
                var user = db.Users.FirstOrDefault(m => m.Id == userId);
                var myPlaylist = db.Playlist.FirstOrDefault(m => m.PlaylistId == userId + "VK");
                song.AddDate = DateTime.Now;
                // song.SongId = Guid.NewGuid().ToString();

                var songExists = db.Songs.FirstOrDefault(m => m.SongId == song.SongId);

                if (songExists == null)
                {
                    db.Songs.Add(song);
                }
                if (myPlaylist == null)
                {
                    if (user != null)
                    {
                        var playlist = new Playlist
                        {
                            AddDate = DateTime.Now,
                            Note = "Vk плэйлист",
                            PlayListName = "Добавлено из вк",
                            PlaylistId = userId + "VK",

                        };

                        db.Playlist.Add(playlist);
                        user.Playlist.Add(playlist);

                        AddSongItemToPlayList(song, playlist, db);
                    }

                }
                else
                {
                    AddSongItemToPlayList(song, myPlaylist, db);
                }

                db.SaveChanges();
            }
        }
Пример #4
0
        //public static Song ToSongFromTagModel(File audioFile, string songId, string songPath, string songAlbumCoverPath, string albumInfoContent, string lyrics, string fileName, SongInfo songInfoFromVk)
        //{
        //    return new Song
        //    {
        //        Year = audioFile.Tag.Year.ToString(),
        //        Artist = string.IsNullOrEmpty(ConvertStringArrayToString(audioFile.Tag.Artists).ToUtf8()) ? songInfoFromVk.Artist : ConvertStringArrayToString(audioFile.Tag.Artists).ToUtf8(),
        //        Genre = string.IsNullOrEmpty(ConvertStringArrayToString(audioFile.Tag.Genres).ToUtf8()) ? songInfoFromVk.Genre : ConvertStringArrayToString(audioFile.Tag.Genres).ToUtf8(),
        //        Album = audioFile.Tag.Album.ToUtf8(),
        //        AddDate = DateTime.Now,
        //        SongId = songId,
        //        BitRate = audioFile.Properties.AudioBitrate,
        //        Duration = audioFile.Properties.Duration,
        //        Title = string.IsNullOrEmpty(audioFile.Tag.Title.ToUtf8()) ? songInfoFromVk.Title.ToUtf8() : audioFile.Tag.Title.ToUtf8(),
        //        SongPath = songPath,
        //        SongAlbumCoverPath = songAlbumCoverPath,
        //        AlbumAndTrackInfo = albumInfoContent,
        //        Copyright = audioFile.Tag.Copyright,
        //        DiscCount = (int)audioFile.Tag.DiscCount,
        //        Composers = ConvertStringArrayToString(audioFile.Tag.Composers).ToUtf8(),
        //        Lyrics = lyrics.ToUtf8(),
        //        Disc = (int)audioFile.Tag.Disc,
        //        Performers = ConvertStringArrayToString(audioFile.Tag.Performers).ToUtf8(),
        //        FileName = fileName,
        //    };
        //}
        public Song Recognise(string pathSong, Song song)
        {
            double totalDuration = 0;
            if (song.Duration.HasValue)
            {
                totalDuration = song.Duration.Value.TotalSeconds;
            }
            if (string.IsNullOrEmpty(pathSong) == false)
            {
                var resultJson = _recognitionService.Recognise(pathSong, (int)totalDuration / 2, 20);
                if (resultJson != null)
                {
                    var songData = resultJson.metadata.music.FirstOrDefault();
                    if (songData != null)
                    {
                        if (songData.artists != null)
                        {
                            song.Artist = string.Join(",", songData.artists.Select(m => m.Name));
                        }

                        if (songData.album != null)
                        {
                            song.Album = songData.album.Name;
                        }

                        if (songData.genres != null)
                        {
                            song.Genre = string.Join(",", songData.genres.Select(m => m.Name));
                        }

                        song.Title = songData.title;
                        song.Copyright = songData.label;
                        song.Duration = TimeSpan.FromMilliseconds(Convert.ToDouble(songData.duration_ms));
                    }
                }
            }

            return song;
        }
Пример #5
0
        private bool CheckRecogniseFailed(Song song)
        {
            bool recogniseNeeded =
                string.IsNullOrEmpty(song.Artist)
                || string.IsNullOrEmpty(song.Genre)
                || string.IsNullOrEmpty(song.Title)
                || string.IsNullOrEmpty(song.Album);

            return recogniseNeeded;
        }
Пример #6
0
        public void UploadSong(string fileExtension, string fileName, string pathSong, string songId, string absoluteSongCoverPath, string userId)
        {
            if (string.IsNullOrEmpty(fileName) == false)
            {
                fileName = fileName.Replace("\\p{Cntrl}", "");
            }

            var songPathToDb = FilePathContainer.SongVirtualPath + songId + fileExtension;
            var songAlbumPicturePathToDb = FilePathContainer.SongAlbumCoverPathRelative + songId + FilePathContainer.SongAlbumCoverFileFormat;
            var song = new Song
            {
                AddDate = DateTime.Now,
                SongId = songId,
                SongPath = songPathToDb,
                FileName = fileName,
            };
            var audioFile = TagLib.File.Create(pathSong);

            song.Year = audioFile.Tag.Year.ToString();
            song.BitRate = audioFile.Properties.AudioBitrate;
            song.Duration = audioFile.Properties.Duration;
            song.DiscCount = (int)audioFile.Tag.DiscCount;
            song.Composers = audioFile.Tag.Composers.ConvertStringArrayToString().ToUtf8();
            song.Disc = (int)audioFile.Tag.Disc;
            song.Performers = audioFile.Tag.Performers.ConvertStringArrayToString().ToUtf8();

            song = Recognise(pathSong, song);
            bool recogniseFailed = CheckRecogniseFailed(song);
            if (recogniseFailed)
            {
                song.Artist = string.Join(",", audioFile.Tag.AlbumArtists);
                song.Genre = string.Join(",", audioFile.Tag.Genres);
                song.Album = audioFile.Tag.Album.ToUtf8();
                song.Title = audioFile.Tag.Title.ToUtf8();
            }

            if (audioFile.Tag.Pictures.Any())
            {
                song.SongAlbumCoverPath = songAlbumPicturePathToDb;
                SongPictureGetter.GetAndSavePictureByTag(audioFile.Tag, absoluteSongCoverPath, songId);
            }
            else
            {
                var pictureInfo = SongPictureGetter.CheckContent(song.Artist, song.Album, song.Title);
                if (pictureInfo != null)
                {
                    song.SongAlbumCoverPath = pictureInfo.PicturePath;
                }
            }

            var vkaudioService = new VkAudioService();
            song.Lyrics = vkaudioService.GetSongLyrics(song.Title, song.Artist);

            //var saveSongCoverPath = absoluteSongCoverPath;

            //var title = audioFile.Tag.Title.ToUtf8();
            //var artist = audioFile.Tag.AlbumArtists.ConvertStringArrayToString().ToUtf8();

            //if (string.IsNullOrEmpty(title) || string.IsNullOrEmpty(artist))
            //{

            //}

            //string lyrics = string.Empty;
            //string titleFromVk = string.Empty;
            //string artistVk = string.Empty;

            //if (songInfoFromVk != null)
            //{
            //    lyrics = songInfoFromVk.Lyrics.ToUtf8();
            //    titleFromVk = songInfoFromVk.Title.ToUtf8();
            //    artistVk = songInfoFromVk.Title.ToUtf8();
            //}

            //songAlbumPicturePathToDb = SongAlbumPicturePathToDb(audioFile, saveSongCoverPath, songId, songAlbumPicturePathToDb, titleFromVk, artistVk, fileName, ref content);
            //GetSongTags(audioFile.Tag, titleFromVk, artistVk);

            //    ModelConverters.ToSongFromTagModel(audioFile, songId, songPathToDb, songAlbumPicturePathToDb);

            _musicRepository.AddSong(song, userId);
        }
Пример #7
0
 private static void AddSongItemToPlayList(Song song, Playlist playlist, ApplicationDbContext db)
 {
     var newRecord = new PlaylistItem
     {
         SongId = song.SongId,
         AddDate = DateTime.Now,
         PlaylistItemId = Guid.NewGuid().ToString(),
         PlaylistId = playlist.PlaylistId,
     };
     db.PlaylistItem.Add(newRecord);
 }