/// <summary> /// Generate the permutations according to a greedy random algorithm /// </summary> /// <returns>String to be save into the file</returns> private static string GeneratePermutations(PermutationGeneratorService permutationGeneratorService) { // Original perm.csv file has 100 rows with 255 values in it seperated by comma int hashTables = fingerprintingConfigCreation.NumberOfHashTables; int keysPerTable = fingerprintingConfigCreation.NumberOfKeys; int startIndex = fingerprintingConfigCreation.StartFingerprintIndex; int endIndex = fingerprintingConfigCreation.EndFingerprintIndex; StringBuilder final = new StringBuilder(); Dictionary<int, int[]> perms = null; perms = permutationGeneratorService.GenerateRandomPermutationsUsingUniqueIndexes(hashTables, keysPerTable, startIndex, endIndex); if (perms != null) foreach (KeyValuePair<int, int[]> perm in perms) { StringBuilder permutation = new StringBuilder(); foreach (int t in perm.Value) permutation.Append(t + ","); final.AppendLine(permutation.ToString()); } return final.ToString(); }
/// <summary> /// Generate the permutations according to a greedy random algorithm and save to the outputfile /// </summary> /// <param name="outputFilePath">output file, e.g. 'perms-new.csv'</param> /// <example> /// Analyzer.GenerateAndSavePermutations("perms-new.csv"); /// </example> public static void GenerateAndSavePermutations(PermutationGeneratorService permutationGeneratorService, string outputFilePath) { string permutations = GeneratePermutations(permutationGeneratorService); using (StreamWriter writer = new StreamWriter(outputFilePath)) { writer.Write(permutations); } }
public static void Main(string[] args) { Analyzer.AnalysisMethod analysisMethod = Analyzer.AnalysisMethod.SCMS; //Analyzer.AnalysisMethod analysisMethod = Analyzer.AnalysisMethod.MandelEllis; string scanPath = ""; double skipDurationAboveSeconds = -1; // less than zero disables this string queryPath = ""; int queryId = -1; int numToTake = 20; double percentage = 0.4; // percentage below and above when querying bool resetdb = false; bool silent = false; AudioFeature.DistanceType distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence; // Command line parsing Arguments CommandLine = new Arguments(args); if(CommandLine["match"] != null) { queryPath = CommandLine["match"]; } if(CommandLine["matchid"] != null) { string matchId = CommandLine["matchid"]; queryId = int.Parse(matchId); } if(CommandLine["scandir"] != null) { scanPath = CommandLine["scandir"]; } if(CommandLine["skipduration"] != null) { double.TryParse(CommandLine["skipduration"], NumberStyles.Number,CultureInfo.InvariantCulture, out skipDurationAboveSeconds); } if(CommandLine["num"] != null) { string num = CommandLine["num"]; numToTake = int.Parse(num); } if(CommandLine["percentage"] != null) { double.TryParse(CommandLine["percentage"], NumberStyles.Number,CultureInfo.InvariantCulture, out percentage); } if(CommandLine["type"] != null) { string type = CommandLine["type"]; if (type.Equals("kl", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence; } else if (type.StartsWith("dtw", StringComparison.InvariantCultureIgnoreCase)) { if (type.Equals("dtwe", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.Dtw_Euclidean; } else if (type.Equals("dtwe2", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.Dtw_SquaredEuclidean; } else if (type.Equals("dtwman", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.Dtw_Manhattan; } else if (type.Equals("dtwmax", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.Dtw_Maximum; } else if (type.Equals("ucrdtw", StringComparison.InvariantCultureIgnoreCase)) { distanceType = AudioFeature.DistanceType.UCR_Dtw; } else { distanceType = AudioFeature.DistanceType.Dtw_Euclidean; } } } if(CommandLine["dtw"] != null || CommandLine["dtwe"] != null) { distanceType = AudioFeature.DistanceType.Dtw_Euclidean; } if(CommandLine["dtwe2"] != null) { distanceType = AudioFeature.DistanceType.Dtw_SquaredEuclidean; } if(CommandLine["dtwman"] != null) { distanceType = AudioFeature.DistanceType.Dtw_Manhattan; } if(CommandLine["dtwmax"] != null) { distanceType = AudioFeature.DistanceType.Dtw_Maximum; } if(CommandLine["kl"] != null) { distanceType = AudioFeature.DistanceType.KullbackLeiblerDivergence; } if(CommandLine["ucrdtw"] != null) { distanceType = AudioFeature.DistanceType.UCR_Dtw; } if(CommandLine["resetdb"] != null) { resetdb = true; } if(CommandLine["silent"] != null) { silent = true; } if(CommandLine["permutations"] != null) { Console.WriteLine("Generating hash permutations for used by the Soundfingerprinting methods."); Console.WriteLine("Saving to file: {0}", "Soundfingerprinting\\perms-new.csv"); Console.WriteLine(); PermutationGeneratorService permutationGeneratorService = new PermutationGeneratorService(); Analyzer.GenerateAndSavePermutations(permutationGeneratorService, "Soundfingerprinting\\perms-new.csv"); return; } if(CommandLine["?"] != null) { PrintUsage(); return; } if(CommandLine["help"] != null) { PrintUsage(); return; } if(CommandLine["gui"] != null) { StartGUI(); return; } if (queryPath == "" && queryId == -1 && scanPath == "") { PrintUsage(); return; } // Get database Db mandelEllisScmsDatabase = new Db(); // For MandelEllis and SCMS // Instansiate soundfingerprinting Repository FingerprintService fingerprintService = Analyzer.GetSoundfingerprintingService(); DatabaseService databaseService = DatabaseService.Instance; // For AudioFingerprinting IPermutations permutations = new LocalPermutations("Soundfingerprinting\\perms.csv", ","); //IPermutations permutations = new LocalPermutations("Soundfingerprinting\\perms-new.csv", ","); Repository repository = new Repository(permutations, databaseService, fingerprintService); if (scanPath != "") { if (IOUtils.IsDirectory(scanPath)) { if (resetdb) { // For MandelEllis and Scms mandelEllisScmsDatabase.RemoveTable(); mandelEllisScmsDatabase.AddTable(); // For AudioFingerprinting databaseService.RemoveFingerprintTable(); databaseService.AddFingerprintTable(); databaseService.RemoveHashBinTable(); databaseService.AddHashBinTable(); databaseService.RemoveTrackTable(); databaseService.AddTrackTable(); } Console.WriteLine("FindSimilar. Version {0}.", VERSION); ScanDirectory(scanPath, mandelEllisScmsDatabase, repository, skipDurationAboveSeconds, silent); } else { Console.Out.WriteLine("No directory found {0}!", scanPath); } } if (queryPath != "") { FileInfo fi = new FileInfo(queryPath); if (fi.Exists) { FindSimilar(queryPath, mandelEllisScmsDatabase, analysisMethod, numToTake, percentage, distanceType); } else { Console.Out.WriteLine("No file found {0}!", queryPath); } } if (queryId != -1) { FindSimilar(new int[] { queryId }, mandelEllisScmsDatabase, analysisMethod, numToTake, percentage, distanceType); } System.Console.ReadLine(); }