Ejemplo n.º 1
0
        /// <summary>
        /// Test the subfinger fingerprints, by trying to find a 15 second audio in 3 different bitrates.
        ///
        /// Needed are:
        /// 1. MySQL database
        /// 2. CreateAudioFingerprint (Fills the MySQL database with fingerprints)
        /// 3. CreateInversedFingerprintIndex (Create a lucene reversed search index, needed to identity a fingerprint)
        /// 4. This program to find a 15 second fragment of audio
        /// </summary>
        public void RunSubFingerTest()
        {
            Console.WriteLine("SubFinger test.");
            if (!System.IO.Directory.Exists(subFingerLookupPath))
            {
                Console.WriteLine("SubFinger path not found.");
                return;
            }

            Console.WriteLine("Opening index. With larges indexes this can take a while.");
            IndexSearcher indexSubFingerLookup = new IndexSearcher(IndexReader.Open(FSDirectory.Open(new System.IO.DirectoryInfo(subFingerLookupPath)), true));

            indexSubFingerLookup.Similarity = new CDR.Indexer.DefaultSimilarityExtended2();
            Console.WriteLine("Ready opening index.");

            SubFingerprintQuery  query   = new SubFingerprintQuery(indexSubFingerLookup);
            FingerprintSignature fsQuery = null;
            Resultset            answer  = null;

            fsQuery = CreateSubFingerprintFromAudio(@"..\..\..\Audio\Samples\JK147510-0002-224Sample-45s-60s.mp3");
            answer  = query.MatchAudioFingerprint(fsQuery);
            if (!retrieveMetadataFromMuziekweb)
            {
                ThrowIfInvalidAnswer(answer, "A5D062DE");
            }
            PrintAnswer(answer);
            Console.WriteLine();

            fsQuery = CreateSubFingerprintFromAudio(@"..\..\..\Audio\Samples\JK147510-0002-128Sample-45s-60s.mp3");
            answer  = query.MatchAudioFingerprint(fsQuery);
            if (!retrieveMetadataFromMuziekweb)
            {
                ThrowIfInvalidAnswer(answer, "A5D062DE");
            }
            PrintAnswer(answer);
            Console.WriteLine();

            fsQuery = CreateSubFingerprintFromAudio(@"..\..\..\Audio\Samples\JK147510-0002-64Sample-45s-60s.mp3");
            answer  = query.MatchAudioFingerprint(fsQuery);
            if (!retrieveMetadataFromMuziekweb)
            {
                ThrowIfInvalidAnswer(answer, "A5D062DE");
            }
            PrintAnswer(answer);
            Console.WriteLine();
        }
Ejemplo n.º 2
0
        public Message FingerprintRecognize(Stream stream)
        {
            string errorMessage;

            if (!UserAccountManager.HasAccess(UserAccountManager.CurrentAccessGroup, out errorMessage, true))
            {
                return(ServiceHelper.SendErrorMessage(ResultErrorCode.AuthorizationError, errorMessage));
            }
            UserAccountManager.GlobalNumberOfRequestsInc();

            XElement xResult = null;

            try
            {
                QueryString qs          = new QueryString(stream);
                string      fingerprint = qs["Fingerprint"];
                string      reliabilty  = qs["Reliabilities"];

                using (FingerprintSignature fsQuery = new FingerprintSignature("UNKNOWN", 0, System.Convert.FromBase64String(fingerprint), 0, true))
                {
                    fsQuery.Reliabilities = System.Convert.FromBase64String(reliabilty);

                    LuceneIndex subFingerIndex = LuceneIndexes.IndexSubFingerprint;
                    try
                    {
                        // We moeten hier al locken aangezien de audiofingerprint library geen weet heeft van onze
                        // LuceneIndex object
                        subFingerIndex.Lock();
                        using (SubFingerprintQuery query = new SubFingerprintQuery(subFingerIndex.Index))
                        {
                            Resultset answer = query.MatchAudioFingerprint(fsQuery, 2300);

                            if (answer != null)
                            {
                                xResult = ServiceHelper.CreateRoot(ResultErrorCode.OK);

                                xResult.Add(new XElement("RecognizeResult",
                                                         new XAttribute("RecognizeCode", 0),
                                                         "OK"));

                                XElement xTimeStatistics = new XElement("TimeStatistics");
                                xTimeStatistics.Add(new XElement("TotalQueryTime",
                                                                 new XAttribute("Unit", "ms"),
                                                                 (int)answer.QueryTime.TotalMilliseconds));
                                xTimeStatistics.Add(new XElement("SubFingerQueryTime",
                                                                 new XAttribute("Unit", "ms"),
                                                                 (int)answer.FingerQueryTime.TotalMilliseconds));
                                xTimeStatistics.Add(new XElement("FingerLoadTime",
                                                                 new XAttribute("Unit", "ms"),
                                                                 (int)answer.FingerLoadTime.TotalMilliseconds));
                                xTimeStatistics.Add(new XElement("MatchTime",
                                                                 new XAttribute("Unit", "ms"),
                                                                 (int)answer.MatchTime.TotalMilliseconds));
                                xResult.Add(xTimeStatistics);

                                XElement xFingerTracks = new XElement("FingerTracks",
                                                                      new XAttribute("Count", 0));
                                xResult.Add(xFingerTracks);
                                if (answer.ResultEntries.Count > 0)
                                {
                                    // Gets a NumberFormatInfo associated with the en-US culture.
                                    System.Globalization.NumberFormatInfo nfi = new System.Globalization.CultureInfo("en-US", false).NumberFormat;
                                    nfi.CurrencySymbol           = "€";
                                    nfi.CurrencyDecimalDigits    = 2;
                                    nfi.CurrencyDecimalSeparator = ".";
                                    nfi.NumberGroupSeparator     = "";
                                    nfi.NumberDecimalSeparator   = ".";

                                    Dictionary <string, ResultEntry> dict = new Dictionary <string, ResultEntry>();
                                    int count = 0;
                                    foreach (ResultEntry item in answer.ResultEntries)
                                    {
                                        XElement xFingerTrack = new XElement("FingerTrack");
                                        xFingerTracks.Add(xFingerTrack);

                                        xFingerTrack.Add(new XAttribute("BER", item.Similarity.ToString()));
                                        xFingerTrack.Add(new XElement("DetectPosition",
                                                                      new XAttribute("Unit", "ms"),
                                                                      new XAttribute("InSec", string.Format(nfi, "{0:#0.000}", (item.Time.TotalMilliseconds / 1000))),
                                                                      (int)item.Time.TotalMilliseconds));

                                        XElement xSearchStrategy = new XElement("SearchStrategy");
                                        xFingerTrack.Add(xSearchStrategy);
                                        xSearchStrategy.Add(new XElement("IndexNumberInMatchList", item.IndexNumberInMatchList));
                                        xSearchStrategy.Add(new XElement("SubFingerCountHitInFingerprint", item.SubFingerCountHitInFingerprint));
                                        xSearchStrategy.Add(new XElement("SearchName", item.SearchStrategy.ToString()));
                                        xSearchStrategy.Add(new XElement("SearchIteration", item.SearchIteration));

                                        xFingerTrack.Add(new XElement("Reference", item.Reference.ToString()));
                                        xFingerTrack.Add(new XElement("FingerTrackID", item.FingerTrackID.ToString()));
                                        count++;
                                    } //foreach
                                    xFingerTracks.Attribute("Count").Value = count.ToString();
                                }
                            }
                            else
                            {
                                // Query geeft null waarde terug. Betekent dat er iets goed fout is gegaan
                                xResult = ServiceHelper.CreateRoot(ResultErrorCode.FAILED);
                            }

                            if (xResult != null)
                            {
                                return(Message.CreateMessage(MessageVersion.None, "", new XElementBodyWriter(xResult)));
                            }

                            // Als we hier komen dan hebben we geen resultaat
                            return(ServiceHelper.SendErrorMessage(ResultErrorCode.NoResultset, "No resultset"));
                        } //using
                    }
                    finally
                    {
                        subFingerIndex.UnLock();
                    }
                } //using
            }
            catch (Exception e)
            {
                CDRLogger.Logger.LogError(e);
                return(ServiceHelper.SendErrorMessage(ResultErrorCode.Exception, e.Message));
            }
            finally
            {
                GC.Collect();
            }
        }