internal static void Fingerprint(Database.Song row) { NAudioDecoder decoder = new NAudioDecoder(); string filename = row.Path + "\\" + row.Filename; if (System.IO.File.Exists(filename)) { decoder.Load(filename); string duration; int bits = decoder.SourceBitDepth; int channels = decoder.SourceChannels; if (decoder.Ready) { duration = decoder.Duration.ToString(); ChromaContext context = new ChromaContext(); context.Start(decoder.SampleRate, decoder.Channels); decoder.Decode(context.Consumer, 120); context.Finish(); // Release audio file handle. decoder.Dispose(); if (String.IsNullOrEmpty(AcoustID.Configuration.ApiKey)) { // TODO: display a prompt for api key. AcoustID.Configuration.ApiKey = "8XaBELgH"; } LookupService service = new LookupService(); List<Recording> recs = new List<Recording>(); List<LookupResult> results = service.Get(context.GetFingerprint(), decoder.Duration, new string[] { "recordings", "compress" }); foreach (LookupResult res in results) { foreach (Recording rec in res.Recordings) { recs.Add(rec); } } string _lastFmQueryAddress = RockBox.Properties.Resources.LastFmQueryAddress + "?"; string _lastFmApiKey = RockBox.Properties.Resources.LastFmApiKey; LastFmApi.Session s = new LastFmApi.Session(_lastFmApiKey, _lastFmQueryAddress); if (recs.Count > 0) { Recording li = recs[0]; string artist = ""; string title = ""; if (li.Artists.Count > 0) { artist = li.Artists[0].Name; } if (li.Title != null) { title = li.Title; } // we kinda have to do it this way because last.fm returns slightly different schema // depending on what data it has (Tags being a name value pair as opposed to a list of pairs, for example) try { LastFmApi.Query.Track.GetInfo q = new LastFmApi.Query.Track.GetInfo(artist, title); LastFmApi.DataTypes.Track.RootObject o = q.Get(s); ProcessTrack(o, row); Console.WriteLine("Processing " + row.Filename); } catch (Exception e) { LastFmApi.Query.Track2.GetInfo q = new LastFmApi.Query.Track2.GetInfo(artist, title); LastFmApi.DataTypes.Track2.RootObject o = q.Get(s); ProcessTrack2(o, row); Console.WriteLine("Processing " + row.Filename); } } else { Console.WriteLine("Couldn't find a recording for " + row.Filename); } } } }
private void Lookup(string fingerprint, int duration) { btnFingerprint.IsEnabled = false; LookupService service = new LookupService(); service.GetAsync((results, e) => { btnOpen.IsEnabled = true; if (e != null) { System.Windows.MessageBox.Show(e.Message, "Webservice error"); return; } if (results.Count == 0) { if (String.IsNullOrEmpty(service.Error)) { System.Windows.MessageBox.Show("No results for given fingerprint."); } else System.Windows.MessageBox.Show(service.Error, "Webservice error"); return; } foreach (var result in results) { var li = new AcoustIdItem(); li.AcoustId = result.Id; li.Score = result.Score.ToString(); li.Recordings = result.Recordings; lvResults.Items.Add(li); } }, fingerprint, duration, new string[] { "recordings", "compress" }); }
private void Lookup(string fingerprint, int duration) { btnOpen.Enabled = false; btnFingerPrint.Enabled = false; btnRequest.Enabled = false; LookupService service = new LookupService(); var context = TaskScheduler.FromCurrentSynchronizationContext(); var task = service.GetAsync(fingerprint, duration, new string[] { "recordings", "compress" }); // Error handling: task.ContinueWith(t => { foreach (var e in t.Exception.InnerExceptions) { MessageBox.Show(e.Message, "Webservice error"); } }, CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, context); // On success: task.ContinueWith(t => { btnOpen.Enabled = true; var response = t.Result; if (!string.IsNullOrEmpty(response.ErrorMessage)) { MessageBox.Show(response.ErrorMessage, "Webservice error"); return; } if (response.Results.Count == 0) { MessageBox.Show("No results for given fingerprint."); return; } foreach (var result in response.Results) { var item = new ListViewItem(new string[] { result.Id, result.Score.ToString(CultureInfo.InvariantCulture), result.Recordings.Count.ToString() }); item.Tag = result.Recordings; listView1.Items.Add(item); } }, CancellationToken.None, TaskContinuationOptions.OnlyOnRanToCompletion, context); }
private void Lookup(string fingerprint, int duration) { btnOpen.Enabled = false; btnFingerPrint.Enabled = false; btnRequest.Enabled = false; LookupService service = new LookupService(); service.GetAsync((results) => { btnOpen.Enabled = true; if (results.Count == 0) { if (String.IsNullOrEmpty(service.Error)) { MessageBox.Show("No results for given fingerprint."); } else MessageBox.Show(service.Error, "Webservice error"); return; } foreach (var result in results) { var item = new ListViewItem(new string[] { result.Id, result.Score.ToString(CultureInfo.InvariantCulture), result.Recordings.Count.ToString() }); item.Tag = result.Recordings; listView1.Items.Add(item); } }, fingerprint, duration, new string[] { "recordings", "compress" }); }