/// <summary>
        ///   Draw the fingerprints of an audio file
        /// </summary>
        private void BtnDrawFingerprintsClick(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(_tbPathToFile.Text))
            {
                MessageBox.Show(Resources.SelectAPathToBeDrawn, Resources.SelectFile, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (!File.Exists(Path.GetFullPath(_tbPathToFile.Text)))
            {
                MessageBox.Show(Resources.NoSuchFile, Resources.NoSuchFile, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            if (_lbImageTypes.SelectedIndex == 0)
            {
                string fileName = Path.GetFileNameWithoutExtension(_tbPathToFile.Text);
                SaveFileDialog sfd =
                    new SaveFileDialog
                    {
                        FileName = fileName + "_fingerprints_" + ".jpg",
                        Filter = Resources.FileFilterJPeg
                    };
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string path = Path.GetFullPath(sfd.FileName);
                    FadeControls(false);
                    Action action =
                        () =>
                        {
                            using (IAudio proxy = new BassProxy())
                            {
                                FingerprintManager manager = new FingerprintManager();
                                StaticStride stride = new StaticStride((int) _nudStride.Value);
                                int totalFingerprints = 0;
                                List<bool[]> fingerprints = manager.CreateFingerprints(proxy, Path.GetFullPath(_tbPathToFile.Text), stride);
                                int width = manager.FingerprintLength;
                                int height = manager.LogBins;
                                Bitmap image = Imaging.GetFingerprintsImage(fingerprints, width, height);
                                image.Save(path);
                                image.Dispose();
                            }
                        };
                    action.BeginInvoke((result) =>
                                       {
                                           FadeControls(true);
                                           MessageBox.Show(Resources.ImageIsDrawn, Resources.Finished, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                           action.EndInvoke(result);
                                       }, action);
                }
            }
            else if (_lbImageTypes.SelectedIndex == 1)
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    string path = fbd.SelectedPath;
                    string fileName = Path.GetFileName(_tbPathToFile.Text);
                    FadeControls(false);
                    Action action = () =>
                                    {
                                        using (IAudio proxy = new BassProxy())
                                        {
                                            FingerprintManager manager = new FingerprintManager();
                                            StaticStride stride = new StaticStride((int) _nudStride.Value);
                                            List<bool[]> result = manager.CreateFingerprints(proxy, Path.GetFullPath(_tbPathToFile.Text), stride);
                                            int i = -1;
                                            int width = manager.FingerprintLength;
                                            int height = manager.LogBins;
                                            foreach (bool[] item in result)
                                            {
                                                Image image = Imaging.GetFingerprintImage(item, width, height);
                                                image.Save(path + "\\" + fileName + i++ + ".jpg", ImageFormat.Jpeg);
                                            }
                                        }
                                    };
                    action.BeginInvoke((result) =>
                                       {
                                           FadeControls(true);
                                           MessageBox.Show(Resources.ImageIsDrawn, Resources.Finished, MessageBoxButtons.OK, MessageBoxIcon.Information);
                                           action.EndInvoke(result);
                                       }
                        , action);
                }
            }
        }
        /// <summary>
        ///   Create fingerprints/min-hash signatures/LSH buckets and insert them in underlying storage
        /// </summary>
        /// <param name = "files">Files to be processed</param>
        /// <param name = "audioProxy">Audio proxy used in processing</param>
        /// <param name = "queryStride">Stride between 2 consecutive fingerprints on query</param>
        /// <param name = "creationalStride">Stride between 2 consecutive fingerprints on creation</param>
        /// <param name = "mintracklen">Minimum track length</param>
        /// <param name = "maxtracklen">Maximum track length</param>
        /// <param name = "milliSecondsToProcess">Number of milliseconds to process</param>
        /// <param name = "startMillisecond">Start processing at a specific millisecond</param>
        /// <param name = "hashTables">Number of hash-tables used in LSH decomposition</param>
        /// <param name = "hashKeys">Number of Min Hash keys per LSH table</param>
        /// <param name = "trackProcessed">Invoked once the track is processed</param>
        /// <returns>List of processed tracks</returns>
        public List <Track> ProcessTracks(List <string> files, BassProxy audioProxy, IStride queryStride, IStride creationalStride,
                                          int mintracklen, int maxtracklen,
                                          int milliSecondsToProcess, int startMillisecond, int hashTables, int hashKeys, Action <Track> trackProcessed)
        {
            List <Track> tracks = new List <Track>();

            _threadCounts++;

            foreach (string file in files)             //filter files that can be processed
            {
                if (_aborted)
                {
                    break;
                }

                Track track = GetTrack(mintracklen, maxtracklen, file, audioProxy);
                if (track == null)
                {
                    continue;                     /*track is not eligible because of min/max parameters*/
                }
                //create spectrogram of the file
                float[][] logSpectrum = null;
                try
                {
                    //try creating the spectrum from a file
                    logSpectrum = _manager.CreateLogSpectrogram(audioProxy, track.Path, milliSecondsToProcess, startMillisecond);
                }
                catch (Exception ex)
                {
                    if (ex is ThreadAbortException)
                    {
                        throw;
                    }
                    /*the file might be corrupted or missing*/
                    continue;                     /*Continue processing even if creation of the spectrogram failed*/
                }
                _storage.InsertTrack(track);      /*Insert track into the storage*/
                /*Create fingerprints that will be used as initial fingerprints to be queried*/
                List <bool[]> dbFingers = _manager.CreateFingerprints(logSpectrum, creationalStride);
                /*Get fingerprint's hash signature, and associate it to a specific track*/
                List <HashSignature> creationalsignatures = GetSignatures(dbFingers, track, hashTables, hashKeys);
                foreach (HashSignature hash in creationalsignatures)
                {
                    _storage.InsertHash(hash, HashType.Creational);
                    /*Set this hashes as also the query hashes*/
                    _storage.InsertHash(hash, HashType.Query);
                }
                /*Create fingerprints for query*/
                List <bool[]>        queryFingers    = _manager.CreateFingerprints(logSpectrum, queryStride);      /*Create fingerprints*/
                List <HashSignature> querysignatures = GetSignatures(queryFingers, track, hashTables, hashKeys);

                // ***** PIN TODO: CHANGE THIS
                object[][] arr   = new object[querysignatures.Count][];
                int        count = 0;
                foreach (HashSignature hash in querysignatures)
                {
                    _storage.InsertHash(hash, HashType.Query);                     /*Insert hash-buckets into hash-tables*/

                    String signatureText = "{";
                    for (int s = 0; s < hash.Signature.Length; s++)
                    {
                        signatureText += hash.Signature[s];
                        signatureText += " ";
                    }
                    signatureText += "}";

                    arr[count++] = new object[5] {
                        hash.Id,
                        hash.Track.Title,
                        hash.Track.Artist,
                        hash.Track.TrackLength,
                        signatureText
                    };
                }
                String    filenameToSave = String.Format("C:\\{0}-hash-buckets.txt", System.IO.Path.GetFileNameWithoutExtension(file));
                CSVWriter csv            = new CSVWriter(filenameToSave);
                csv.Write(arr);
                // ***** end PIN

                if (trackProcessed != null)
                {
                    trackProcessed.Invoke(track);
                }
                tracks.Add(track);
            }
            _threadCounts--;
            return(tracks);
        }
Exemple #3
0
        /// <summary>
        ///   Get hash similarity of one song
        /// </summary>
        /// <param name = "manager">Fingerprint manager</param>
        /// <param name = "dbstride">Database stride between fingerprints</param>
        /// <param name = "queryStride">Query stride between fingerprints</param>
        /// <param name = "numberOfFingerprintsToAnalyze">Number of fingerprints to analyze</param>
        /// <param name = "hashTables">Number of hash tables in the LSH transformation</param>
        /// <param name = "hashKeys">Number of hash keys per table in the LSH transformation</param>
        /// <param name = "proxy">Audio proxy</param>
        /// <param name = "path">Path to analyzed file</param>
        /// <param name = "results">Results object to be filled with the appropriate data</param>
        private static void GetHashSimilarity(FingerprintManager manager, IStride dbstride, IStride queryStride, int numberOfFingerprintsToAnalyze, int hashTables, int hashKeys, IAudio proxy, string path, DumpResults results)
        {
            double sum = 0;
            int hashesCount = 0;
            int startindex = 0;

            List<bool[]> listDb = manager.CreateFingerprints(proxy, path, dbstride);
            List<bool[]> listQuery = manager.CreateFingerprints(proxy, path, queryStride);
            IPermutations perms = new DbPermutations(ConfigurationManager.ConnectionStrings["FingerprintConnectionString"].ConnectionString);
            MinHash minHash = new MinHash(perms);
            List<int[]> minHashDb = listDb.Select(minHash.ComputeMinHashSignature).ToList();
            List<int[]> minHashQuery = listQuery.Select(minHash.ComputeMinHashSignature).ToList();

            /*Calculate Min Hash signature similarity by comparing 2 consecutive signatures*/
            int countDb = minHashDb.Count;
            int countQuery = minHashQuery.Count;
            int minHashSignatureLen = minHashDb[0].Length;
            int similarMinHashValues = 0;
            for (int i = 0; i < countDb; i++)
            {
                for (int j = 0; j < countQuery; j++)
                {
                    for (int k = 0; k < minHashSignatureLen; k++)
                        if (minHashDb[i][k] == minHashQuery[j][k])
                            similarMinHashValues++;
                }
            }
            results.Results.SumIdenticalMinHash = similarMinHashValues;
            results.Results.AverageIdenticalMinHash = (double) similarMinHashValues/(countDb*countQuery*minHashSignatureLen);

            /*Group min hash signatures into LSH Buckets*/
            List<Dictionary<int, long>> lshBucketsDb =
                minHashDb.Select(item => minHash.GroupMinHashToLSHBuckets(item, hashTables, hashKeys)).ToList();

            List<Dictionary<int, long>> lshBucketsQuery =
                minHashQuery.Select(item => minHash.GroupMinHashToLSHBuckets(item, hashTables, hashKeys)).ToList();

            int countSignatures = lshBucketsDb.Count;
            sum = 0;
            foreach (Dictionary<int, long> a in lshBucketsDb)
            {
                Dictionary<int, long>.ValueCollection aValues = a.Values;
                foreach (Dictionary<int, long> b in lshBucketsQuery)
                {
                    Dictionary<int, long>.ValueCollection bValues = b.Values;
                    hashesCount += aValues.Intersect(bValues).Count();
                }
            }

            results.Results.SumJaqLSHBucketSimilarity = -1;
            results.Results.AverageJaqLSHBucketSimilarity = -1;
            results.Results.TotalIdenticalLSHBuckets = hashesCount;
        }
Exemple #4
0
        /// <summary>
        ///   Get fingerprint similarity of one song
        /// </summary>
        /// <param name = "manager">Fingerprint manager used in file decomposition</param>
        /// <param name = "dbstride">Database creation stride</param>
        /// <param name = "queryStride">Query stride</param>
        /// <param name = "numberOfItemsToCompare">Number of subsequent elements to compare with</param>
        /// <param name = "proxy">Proxy</param>
        /// <param name = "path">Path to first file</param>
        /// <param name = "results">Results object to be filled with the corresponding data</param>
        private static void GetFingerprintSimilarity(FingerprintManager manager, IStride dbstride, IStride queryStride, int numberOfItemsToCompare, IAudio proxy, string path, DumpResults results)
        {
            int startindex = 0;
            int count = 0;
            double sum = 0;

            List<bool[]> list = manager.CreateFingerprints(proxy, path, dbstride);
            List<bool[]> listToCompare = manager.CreateFingerprints(proxy, path, queryStride);

            count = list.Count;
            int toCompare = listToCompare.Count;

            double max = double.MinValue;

            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < toCompare; j++)
                {
                    double value = MinHash.CalculateSimilarity(list[i], listToCompare[j]);
                    if (value > max)
                        max = value;
                    sum += value;
                }
            }

            results.Results.SumJaqFingerprintsSimilarity = sum;
            results.Results.AverageJaqFingerprintSimilarity = sum/(count*toCompare);
            results.Results.MaxJaqFingerprintSimilarity = max;
        }
Exemple #5
0
        /// <summary>
        ///   Get fingerprint similarity between 2 different songs.
        /// </summary>
        /// <param name = "manager">Fingerprint manager used in file decomposition</param>
        /// <param name = "stride">Stride object parameter</param>
        /// <param name = "proxy">Proxy to the audio object</param>
        /// <param name = "path">Path to first file</param>
        /// <param name = "differentPath">Path to different file</param>
        /// <param name = "results">Results object to be filled with the corresponding data</param>
        private static void GetFingerprintSimilarity(FingerprintManager manager, IStride stride, IAudio proxy, string path, string differentPath, DumpResults results)
        {
            int startindex = 0;
            int count = 0;
            double sum = 0;

            List<bool[]> imglista = manager.CreateFingerprints(proxy, path, stride);
            List<bool[]> imglistb = manager.CreateFingerprints(proxy, differentPath, stride);


            count = imglista.Count > imglistb.Count ? imglistb.Count : imglista.Count;
            double max = double.MinValue;
            for (int i = 0; i < count; i++)
            {
                int j = i;
                double value = MinHash.CalculateSimilarity(imglista[i], imglistb[j]);
                if (value > max)
                    max = value;
                sum += value;
            }

            results.SumJaqFingerprintSimilarityBetweenDiffertSongs = sum;
            results.AverageJaqFingerprintsSimilarityBetweenDifferentSongs = sum/count;
            results.MaxJaqFingerprintsSimilarityBetweenDifferentSongs = max;
        }
Exemple #6
0
        static void Process(string path)
        {
            Console.WriteLine(path);
            var proxy = new BassProxy();
            proxy.RecodeTheFile(path + ".mp3",  path + ".wav", 5512);
            var data = File.CreateText(path + ".txt");

            FingerprintManager manager = new FingerprintManager();
            manager.FingerprintLength = Length;
            manager.TopWavelets = 150;
            manager.MaxFrequency = 2048;
            manager.MinFrequency = 512;
            float[][] spec = manager.CreateSpectrogram(proxy, path + ".wav", 0, 0);

            var StaticStride = ( path == "7" ) ?  new StaticStride(Milliseconds) : new StaticStride(0);

            var fingerprint = manager.CreateFingerprints(proxy, path + ".wav", StaticStride);
            if (path == "4") { sfinger = fingerprint; }

            foreach (var finger in fingerprint)
            {
                int[] bits = finger.Select(f => f ? 1 : 0).ToArray();
                data.WriteLine(string.Join(";", bits));
            }
            data.Close();

            if(File.Exists( path + ".jpg") )
                File.Delete(path + ".jpg");
            if (spec.Length > 0)
            {
                Bitmap image = Imaging.GetSpectrogramImage(spec, 800, 600);
                image.Save(path + ".jpg", ImageFormat.Jpeg);
            }
            

            if (path == "7")
            {
                var hasMin = new MinHash(new LocalPermutations("2.txt", ";"));
                var result = File.CreateText("r.txt");
                
                Dictionary<int, int> count = new Dictionary<int, int>();

                foreach (var finger in sfinger)
                {
                    //var t = fingerprint.Select(p => MinHash.CalculateSimilarity(finger, p)).OrderByDescending(p => p).ToArray();
                    //var t = fingerprint.Select(p => MinHash.CalculateHammingDistance (finger, p)).OrderByDescending(p => p).ToArray();
                    //var t = sfinger.Select(p => MinHash.CalculateSimilarity(finger, p)).OrderByDescending(p => p).ToArray();

                    for (int i = 0; i < fingerprint.Count; i++)
                    {
                        var similarity = MinHash.CalculateSimilarity(finger, fingerprint[i]);
                        if (similarity > 0.5f)
                        {
                            if (!count.ContainsKey(i))
                                count.Add(i, 0);
                            count[i]++;
                            result.Write("{1}:{0};", similarity.ToString("#.000"), i, i * 11.6f * Block, (i + 1) * 11.6f * Block);
                        }
                    }
                    result.WriteLine();
                }
                foreach (var c in count.OrderByDescending(p => p.Value))
                    result.Write("{0}:{1};", c.Key, c.Value);

                result.WriteLine("-------------------");
                foreach (var c in count.OrderBy(p => p.Key))
                    result.WriteLine("{0}:{1} Time: {2} ms;", c.Key, c.Value, c.Key * 11.6f * Block);

                result.Close();
            }
        }
Exemple #7
0
        static void Process(Files.File file)
        {
            var audio = new Files.AudioFile
            {
                ID = file.ID,
                ContentType = file.ContentType,
                Name = file.Name,
                Size = file.Size,
                Url = file.Url,
                FullPath = file.FullPath
            };

            audio.AlterPath = file.FullPath.ToLower().Replace("mp3", "wav");
            var proxy = new BassProxy();

            if (!File.Exists(audio.AlterPath))
                proxy.RecodeTheFile(file.FullPath, audio.AlterPath, 5512);
            
            FingerprintManager manager = new FingerprintManager();
            manager.FingerprintLength = Length;
            manager.TopWavelets = 150;
            manager.MaxFrequency = 2048;
            manager.MinFrequency = 512;

            //float[][] spec = manager.CreateSpectrogram(proxy, audio.AlterPath, 0, 0);

            var StaticStride = new StaticStride(0);

            audio.Finger = manager.CreateFingerprints(proxy, audio.AlterPath, StaticStride).ToArray();
            
            FileProvider.Save(audio);
        }