public static void PlayAndRecord(Record_Spotify toRecord) { SpotifyWebAPI spotify = SPOTIFY_INST; if (spotify.AccessToken == null) { spotify = GetInstanceAuth(); } ErrorResponse error = spotify.PausePlayback(); string spotifyID = toRecord.SpotifyURL.Substring(31, 22); FullTrack track = spotify.GetTrack(spotifyID); if (track.Uri == null) { MyRequests.GetTrack(spotifyID); } var config = new MapperConfiguration(cfg => cfg.CreateMap <FullTrack, ExtTrack>()); var mapper = config.CreateMapper(); ExtTrack extTrack = mapper.Map <ExtTrack>(track); extTrack.AddProps(toRecord.TrackTitle, toRecord.ArtistName, toRecord.TrackID); outputFilePathWav = savePathWav + CleanString(extTrack.Filename) + ".wav"; outputFilePathMP3 = savePathMP3 + CleanString(extTrack.Filename) + ".mp3"; error = spotify.ResumePlayback("", "", new List <string> { track.Uri.ToString() }, 0); Record_Start(outputFilePathWav); System.Threading.Thread.Sleep(track.DurationMs + 1000); //System.Threading.Thread.Sleep(10000); error = spotify.PausePlayback(); Record_Stop(); WaveToMP3(outputFilePathWav, outputFilePathMP3, extTrack); }
private static void DiscogCall(ExtTrack extTrack) { string url = "https://api.discogs.com/database/search?" + System.Web.HttpUtility.UrlEncode(extTrack.OCName); if (extTrack.OCArtist != "") { url += "&artist=" + HttpUtility.UrlEncode(extTrack.OCArtist); } url += "&per_page=100&type=master"; HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(url); myReq.Headers["Authorization"] = "Discogs key=" + DISCOGS_CONSUMERKEY + ", secret=" + DISCOGS_CONSUMERSECRET; myReq.UserAgent = "MyGenres/1.0 +http://martinharran.com"; WebResponse wr = myReq.GetResponse(); Stream receiveStream = wr.GetResponseStream(); StreamReader reader = new StreamReader(receiveStream, Encoding.UTF8); string content = reader.ReadToEnd(); result = DiscogsResult.FromJson(content); if (result != null) { genres = new List <string>(); if (result.Results != null) { for (int i = 0; i < result.Results.Count(); i++) { Result r = result.Results[i]; if (r.Country != null) { if (r.Country.ToUpper() == "UK" || r.Country.ToUpper() == "US" || r.Country.ToUpper() == "Europe" ) { if (r.Style != null) { for (int g = 0; g < r.Style.Count(); g++) { string style = r.Style[g]; //string style = r.Style[g].ToString().Replace(" ", "_"); var match = genres .FirstOrDefault(stringToCheck => stringToCheck.Contains(style)); if (match == null) { genres.Add(style); } } } } } if (r.Year != null) { if (extTrack.Year == null) { extTrack.Year = (int)r.Year; } else if (r.Year < trackYear) { extTrack.Year = (int)r.Year; } } } } } }
private static void WaveToMP3(string waveFileName, string mp3FileName, ExtTrack extTrack, int bitRate = 320) { //get genres genres = new List <string>(); DiscogCall(extTrack); string directory = savePathMP3 + "\\" + CleanString(extTrack.TagArtists[0]) + "\\" + CleanString(extTrack.Album.Name); string fullPathMP3 = directory + "\\" + CleanString(extTrack.Filename) + ".mp3"; Directory.CreateDirectory(directory); // LASTFM - genres dodgy? // extTrack.Year = LastFMCall(extTrack.OCName, extTrack.OCArtist); extTrack.TagGenres = genres.ToArray(); // temporary workaround for problems with genres extTrack.TagGenres = new string[] { string.Join(",", extTrack.TagGenres) }; //Stopwatch timer = new Stopwatch(); //timer.Start(); using (var reader = new AudioFileReader(waveFileName)) using (var writer = new LameMP3FileWriter(fullPathMP3, reader.WaveFormat, bitRate)) reader.CopyTo(writer); //timer.Stop(); //long elapsedSecs = timer.ElapsedMilliseconds / 1000; //tag wav file TagLib.File wav = TagLib.File.Create(waveFileName); wav.Tag.Title = extTrack.Filename; wav.Tag.Performers = extTrack.TagArtists; wav.Tag.Album = extTrack.Album.Name; wav.Tag.AlbumArtists = extTrack.TagArtists; wav.Tag.Genres = extTrack.TagGenres; //wav.Tag.Comment = "my comments, maybe some info about track"; if (extTrack.Year != null) { wav.Tag.Year = (uint)extTrack.Year; } wav.Save(); wav.Dispose(); //tag mp3 file TagLib.File mp3 = TagLib.File.Create(fullPathMP3); mp3.Tag.Title = extTrack.Filename; mp3.Tag.Performers = extTrack.TagArtists; mp3.Tag.Album = extTrack.Album.Name; mp3.Tag.AlbumArtists = extTrack.TagArtists; mp3.Tag.Genres = extTrack.TagGenres; //mp3.Tag.Comment = "my comments, maybe some info about track"; if (extTrack.Year != null) { mp3.Tag.Year = (uint)extTrack.Year; } mp3.Save(); mp3.Dispose(); // update table with filepath of mp3. OfficialChartsEntities db2 = new OfficialChartsEntities(); var record = db2.Tracks.SingleOrDefault(b => b.TrackID == extTrack.OCID); if (record != null) { record.PathOnDisc = fullPathMP3; db2.SaveChanges(); } // CheckFileTags(extTrack.Filename); }