public void TestSameDistance()
        {
            var context = new SimilarityContext();

            context.AddSeeds(new Seed [] { GetSeed(s1, 1.0f) });
            Assert.AreEqual(0f, context.Distance(Scms.FromBytes(Bytes(s1))).Average());
        }
        private static object Distance(object seed_id_obj, object scms_obj)
        {
            BaseSimilarityContext context;

            if (!seeds.TryGetValue(Convert.ToInt32(seed_id_obj), out context))
            {
                throw new ArgumentException("seed_id not found", "seed_id_obj");
            }

            var scms_bytes = scms_obj as byte[];

            if (scms_bytes == null)
            {
                // TODO raise an event to notify the user (one time only) that
                // there are un-analyzed tracks?
                // notify_string
                return(Double.MaxValue);
            }

            var start = DateTime.Now;

            Scms.FromBytes(scms_bytes, context.ComparisonScms);
            total_read_ms += (DateTime.Now - start).TotalMilliseconds;

            float distance = context.Distance(context.ComparisonScms).Average();

            total_ms += (DateTime.Now - start).TotalMilliseconds;
            total_count++;

            return(distance);
        }
 private Seed GetSeed(string hex, float weight)
 {
     return(new Seed()
     {
         Scms = Scms.FromBytes(Bytes(hex)),
         Weight = weight
     });
 }
        public void TestDiscardedDistance()
        {
            // Test with different seed weight
            var distance_s1_s2_discarded = 81.65736f;
            var context = new SimilarityContext();

            context.AddSeeds(new Seed [] { GetSeed(s1, SimilarityContext.DiscardedWeight) });
            Assert.IsFalse(context.IsEmpty);
            Assert.Less(Math.Abs(distance_s1_s2_discarded - context.Distance(Scms.FromBytes(Bytes(s2))).Average()), 0.1);
        }
        public override IEnumerable <float> Distance(Scms from)
        {
            if (from == null)
            {
                yield return(float.MaxValue);

                yield break;
            }

            bool  any_seeds   = false;
            float last_weight = -99;

            foreach (var seed in seeds)
            {
                var distance = Scms.Distance(seed.Scms, from, Config);
                if (distance < 0)
                {
                    // Ignore negative distance values
                    continue;
                }

                if (distance < min_distance)
                {
                    min_distance = distance;
                    best_scms    = from;
                }
                else if (distance > max_distance)
                {
                    max_distance = distance;
                }

                float weighted_distance = distance / seed.Weight;
                if (debug)
                {
                    if (seed.Weight != last_weight)
                    {
                        last_weight = seed.Weight;
                        Console.WriteLine("    {0} seeds (weight {1,3:N1})", last_weight == ShuffledWeight ? "Shuffled" : last_weight == PlayedWeight ? "Played" : last_weight == SelectedWeight ? "Manually Selected" : "Skipped/Discarded", last_weight);
                    }

                    Console.WriteLine("      distance: {0,4:N1}, weighted: {1,4:N1} from artist_id {2,4}, uri {3}",
                                      distance, weighted_distance, seed.ArtistId, seed.Uri);
                }
                yield return(weighted_distance);

                any_seeds = true;
            }

            if (!any_seeds)
            {
                // Return the highest distance possible
                yield return(Single.MaxValue);
            }
        }
        public void TestShuffledDistances()
        {
            var distance_s1_s2 = 27.21912f;
            var context        = new SimilarityContext();

            context.AddSeeds(new Seed [] { GetSeed(s1, SimilarityContext.ShuffledWeight) });
            Assert.IsFalse(context.IsEmpty);
            Assert.Less(Math.Abs(distance_s1_s2 - context.Distance(Scms.FromBytes(Bytes(s2))).Average()), 0.1);

            // Add the same seed w/ the same weight; make sure the average is the same
            context.AddSeeds(new Seed [] { GetSeed(s1, SimilarityContext.ShuffledWeight) });
            Assert.Less(Math.Abs(distance_s1_s2 - context.Distance(Scms.FromBytes(Bytes(s2))).Average()), 0.1);
        }
Exemplo n.º 7
0
    private static void Test()
    {
        mirageaudio_initgst();

        string song1_filename = "/home/lorentz/Music/Library/Pachelbel/Johann Pachelbel - Canon And Gigue In D Major For 3 Violins And Basso Continuo.mp3";
        string song2_filename = "/home/lorentz/Music/Library/Karajan Adagios/CD 1/Pachelbel - Canon in d Major (Kanon And Gigue in d Major = d Dur) av Johann Pachelbel.mp3";
        Scms   song1          = null;
        Scms   song2          = null;

        DbgTimer t1 = new DbgTimer();

        t1.Start();
        int runs = 10;

        for (int i = 0; i < runs; i++)
        {
            Analyzer.Analyze(song1_filename);
        }
        long l1 = 0;

        t1.Stop(ref l1);
        Dbg.WriteLine("Analysis: " + runs + " times - " + l1 + "ms; " +
                      (double)l1 / (double)runs + "ms per analysis");

        song1 = Analyzer.Analyze(song1_filename);
        song2 = Analyzer.Analyze(song2_filename);

        ScmsConfiguration config = new ScmsConfiguration(Analyzer.MFCC_COEFFICIENTS);

        Console.WriteLine("Distance = " + Scms.Distance(song1, song2, config));

        DbgTimer t2 = new DbgTimer();

        t2.Start();
        runs = 100000;
        for (int i = 0; i < runs; i++)
        {
            Scms.Distance(song1, song2, config);
        }
        long l2 = 0;

        t2.Stop(ref l2);
        Dbg.WriteLine("Distance Computation: " + runs + " times - " + l2 + "ms; " +
                      (double)l2 / (double)runs + "ms per comparison");
    }
        private IEnumerable <Seed> GetSeeds(string query, DateTime after, int limit, float weight)
        {
            var reader = ServiceManager.DbConnection.Query(
                String.Format(similarity_query, query),
                Shuffler.DbId, Shuffler.DbId, after, limit // these args assume the query string has exactly one ? meant for the after date
                );

            using (reader) {
                while (reader.Read())
                {
                    yield return(new Seed()
                    {
                        TrackId = Convert.ToInt32(reader[0]),
                        Scms = Scms.FromBytes(reader[1] as byte[]),
                        Weight = weight,
                        ArtistId = Convert.ToInt32(reader[2]),
                        Uri = reader[3] as string
                    });
                }
            }
        }
 public abstract IEnumerable <float> Distance(Scms from);