/// <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 #2
0
        /// <summary>
        ///   Gets the spectrum of the wavelet decomposition before extracting top wavelets and binary transformation
        /// </summary>
        /// <param name = "pathToFile">Path to file to be drawn</param>
        /// <param name = "stride">Stride within the fingerprint creation</param>
        /// <param name = "proxy">Proxy manager</param>
        /// <param name = "manager">Fingerprint manager</param>
        /// <returns>Image to be saved</returns>
        public static Image GetWaveletSpectralImage(string pathToFile,
                                                    IStride stride,
                                                    IAudio proxy,
                                                    FingerprintManager manager)
        {
            List<float[][]> wavelets = new List<float[][]>();
            float[][] spectrum = manager.CreateLogSpectrogram(proxy, pathToFile, 0, 0);
            int specLen = spectrum.GetLength(0);
            int start = stride.GetFirstStride()/manager.Overlap;
            int logbins = manager.LogBins;
            int fingerprintLength = manager.FingerprintLength;
            int overlap = manager.Overlap;
            while (start + fingerprintLength < specLen)
            {
                float[][] frames = new float[fingerprintLength][];
                for (int i = 0; i < fingerprintLength; i++)
                {
                    frames[i] = new float[logbins];
                    Array.Copy(spectrum[start + i], frames[i], logbins);
                }
                start += fingerprintLength + stride.GetStride()/overlap;
                wavelets.Add(frames);
            }

            const int imagesPerRow = 5; /*5 bitmap images per line*/
            const int spaceBetweenImages = 10; /*10 pixel space between images*/
            int width = wavelets[0].GetLength(0);
            int height = wavelets[0][0].Length;
            int fingersCount = wavelets.Count;
            int rowCount = (int) Math.Ceiling((float) fingersCount/imagesPerRow);

            int imageWidth = imagesPerRow*(width + spaceBetweenImages) + spaceBetweenImages;
            int imageHeight = rowCount*(height + spaceBetweenImages) + spaceBetweenImages;

            Bitmap image = new Bitmap(imageWidth, imageHeight, PixelFormat.Format16bppRgb565);
            /*Change the background of the bitmap*/
            for (int i = 0; i < imageWidth; i++)
                for (int j = 0; j < imageHeight; j++)
                    image.SetPixel(i, j, Color.White);

            double maxValue = wavelets.Max((wavelet) => (wavelet.Max((column) => column.Max())));
            int verticalOffset = spaceBetweenImages;
            int horizontalOffset = spaceBetweenImages;
            int count = 0;
            double max = wavelets.Max(wav => wav.Max(w => w.Max(v => Math.Abs(v))));
            foreach (float[][] wavelet in wavelets)
            {
                for (int i = 0; i < width /*128*/; i++)
                {
                    for (int j = 0; j < height /*32*/; j++)
                    {
                        Color color = ValueToBlackWhiteColor(wavelet[i][j], max/4);
                        image.SetPixel(i + horizontalOffset, j + verticalOffset, color);
                    }
                }
                count++;
                if (count%imagesPerRow == 0)
                {
                    verticalOffset += height + spaceBetweenImages;
                    horizontalOffset = spaceBetweenImages;
                }
                else
                    horizontalOffset += width + spaceBetweenImages;
            }
            return image;
        }