static void BuildDataBase1() { DataBase dataBase = new DataBase(new LongHash()); dataBase.BuildDataBase(@"D:\Music\"); dataBase.Save(@"D:\Music\Avatar\DataBase.txt"); Utility.ShutDown(); }
public static void TestRandom(string dataBaseFile, string file, string correctName) { DataBase dataBase = new DataBase(new LongHash()); dataBase.Load(dataBaseFile); dataBase.RandomQuerySigleFile(file, 40, correctName); }
public static void Test(string dataBaseFile, string file, string correctName) { DataBase dataBase = new DataBase(new LongHash()); dataBase.Load(dataBaseFile); //dataBase.QuestSigleFile(file, 500, 10, 0, correctName); dataBase.QuestRandomSigleFile(file, 500, 10, 4, correctName); }
public static void Test(string dataFolder, string searchPattern, string indexFile, DataBase dataBase) { FileInfo[] fileInfoArray = Utility.GetFiles(dataFolder, searchPattern); int bSuccessCount = 0; int total = fileInfoArray.Length; using (StreamWriter sw = new StreamWriter(indexFile)) { foreach (FileInfo fileInfo in fileInfoArray) { string fileName = fileInfo.Name; string fileNameWithoutExtention = fileName.Substring(0, fileName.Length - 4); Console.Write(fileName); sw.Write(fileName); byte[] audio = Mp3ToWavConverter.ReadBytes(fileInfo.FullName); if (audio.Length < DATA_LENGTH) { Console.WriteLine(); sw.WriteLine(); total--; continue; } byte[] audioSegment = new byte[DATA_LENGTH]; for (int i = 0; i < TEST_COUNT; i++) { int startIndex = GetRandomStartIndex(audio.Length); Array.Copy(audio, startIndex, audioSegment, 0, DATA_LENGTH); int id = dataBase.GetBestHit(audioSegment, SHIFT_COUNT); string name = dataBase.GetNameByID(id); bool bSuccess = (fileNameWithoutExtention.CompareTo(name) == 0); Console.Write(bSuccess ? "\t++" : "\t--"); if (!bSuccess) { sw.Write("\t{0}", startIndex); } bSuccessCount += bSuccess ? 1 : 0; } Console.WriteLine(); sw.WriteLine(); sw.Flush(); } } Console.WriteLine("accuray rate: {0}", (double)bSuccessCount / total / TEST_COUNT); }
static void BuildDataBase(string dataFolder, IHashMaker hashMaker, string dataBaseFile, bool shutDown) { if (!Directory.Exists(dataFolder)) { WriteLog(string.Format("'{0}' doesn't exist! ", dataFolder)); return; } DataBase dataBase = new DataBase(hashMaker); dataBase.BuildDataBase(dataFolder); dataBase.SaveInBinary(dataBaseFile); if (shutDown) Utility.ShutDown(); }
static void AppendData(string indexFile, IHashMaker hashMaker, string dataFolder) { if (!Directory.Exists(dataFolder)) { WriteLog(string.Format("'{0}' doesn't exist! ", dataFolder)); return; } if (!File.Exists(indexFile)) { WriteLog(string.Format("'{0}' file doesn't exist! ", indexFile)); return; } DataBase dataBase = new DataBase(hashMaker); dataBase.CheckDuplicate = true; dataBase.Load(indexFile); dataBase.BuildDataBase(dataFolder); dataBase.Save(indexFile); }
public static void AnalyseFailure(string dataFolder, string searchPattern, string indexFile, DataBase dataBase) { dataBase.Quiet = false; using (StreamReader sw = new StreamReader(indexFile)) { while(!sw.EndOfStream) { string line = sw.ReadLine(); string[] data = line.Split('\t'); if (data.Length < 1) continue; string fileName = data[0]; Console.WriteLine(fileName); byte[] audio = Mp3ToWavConverter.ReadBytes(Path.Combine(dataFolder, fileName)); if (audio.Length < DATA_LENGTH) { Console.WriteLine(); continue; } byte[] audioSegment = new byte[DATA_LENGTH]; for (int i = 1; i < data.Length; i++ ) { int startIndex = int.Parse(data[i]); Array.Copy(audio, startIndex, audioSegment, 0, DATA_LENGTH); int id = dataBase.GetBestHit(audioSegment, SHIFT_COUNT); string name = dataBase.GetNameByID(id); Console.WriteLine("\t{0}\t{1}", startIndex, name); } Console.WriteLine(); } } }
private static void TestSingleFile(string fileName, int[] startIndices, DataBase dataBase) { byte[] audio = Mp3ToWavConverter.ReadBytesFromWav(fileName); if (audio.Length < DATA_LENGTH) { Console.WriteLine("File too small, not enough data for test."); return; } byte[] audioSegment = new byte[DATA_LENGTH]; foreach(int startIndex in startIndices) { Array.Copy(audio, startIndex, audioSegment, 0, DATA_LENGTH); int id = GetBestHit(audioSegment, dataBase, 16); string name = dataBase.GetNameByID(id); bool bSuccess = (name.CompareTo(trackName) == 0); Console.Write(bSuccess ? "\t++" : "\t--"); } }
public static void ConvertIndexFileFromTextToBinary(string textFile, string binaryFile, IHashMaker hashMaker) { DataBase dataBase = new DataBase(hashMaker); dataBase.Load(textFile); dataBase.SaveInBinary(binaryFile); }
private static void Indexing(DataBase dataBase, byte[] audio) { Console.WriteLine("Matching the song..."); dataBase.UseFilter = false; TimeInterval timeInterval = new TimeInterval(); int id = dataBase.GetBestHit(audio, 16); double intervalInSecond = timeInterval.GetDurationInSecond(); Console.WriteLine("--------------------"); Console.Write("Final Match ({0}s):\t", intervalInSecond); if (id < 0) Console.WriteLine("No match!"); else Console.WriteLine(dataBase.GetNameByID(id)); Console.WriteLine("--------------------"); }
public static DataBase Combine(DataBase first, DataBase second) { first.Combine(second); return first; }
public void Combine(DataBase other) { if(other == null) return; short count = (short)songNames.Count; songNames.AddRange(other.songNames); foreach (KeyValuePair<long, List<DataPoint>> keyValue in other.hashMap) { List<DataPoint> values = keyValue.Value; for (int i = 0; i < values.Count; i++) { values[i].SongID += count; } if (hashMap.ContainsKey(keyValue.Key)) { hashMap[keyValue.Key].AddRange(values); } else { hashMap.Add(keyValue.Key, values); } } }
public static void SimpleTest(string indexFile, IHashMaker hashMaker, bool bQuiet) { bool bTextFile = IsTextFile(indexFile); DataBase dataBase = new DataBase(hashMaker); if (bTextFile) dataBase.Load(indexFile); else dataBase.LoadFromBinary(indexFile); dataBase.Quiet = bQuiet; int seconds = 10; while (true) { Console.WriteLine("Press any key to identify a new song, press ESC to exit.\n"); ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Escape) break; byte[] audio = null; Console.WriteLine("Start recording audio from mic ...", seconds); MicRecorder recorder = new MicRecorder(); recorder.Seconds = seconds; recorder.RequestStop = false; recorder.RecStart(); audio = recorder.GetAudioData(); dataBase.UseFFTW = false; Indexing(dataBase, audio); dataBase.UseFFTW = true; Indexing(dataBase, audio); dataBase.UseSort = true; Indexing(dataBase, audio); //dataBase.UseFilter = true; //timeInterval.Reset(); //id = dataBase.GetBestHit(audio, 16); //intervalInSecond = timeInterval.GetDurationInSecond(); //Console.WriteLine("--------------------"); //Console.Write("Final Match ({0}s):\t", intervalInSecond); //if (id < 0) // Console.WriteLine("No match!"); //else // Console.WriteLine(dataBase.GetNameByID(id)); //Console.WriteLine("--------------------"); } }
public static void Test(string dataFolder, string dataBaseFile) { DataBase dataBase = new DataBase(new LongHash()); dataBase.Load(dataBaseFile); FileInfo[] fileInfoArray = Utility.GetFiles(dataFolder, "*.wav"); if(fileInfoArray.Length <= 0) { Console.WriteLine("No wav file found."); return; } int[] startIndices = new int[TEST_COUNT]; int length = Mp3ToWavConverter.ReadBytesFromWav(fileInfoArray[0].FullName).Length; //Console.Write("\t"); for(int i = 0; i < TEST_COUNT; i++) { startIndices[i] = GetRandomStartIndex(length); Console.Write("\t{0}", startIndices[i]); } Console.WriteLine(); foreach (FileInfo fileInfo in fileInfoArray) { Console.Write(fileInfo.Name); TestSingleFile(fileInfo.FullName, startIndices, dataBase); Console.WriteLine(); } }
public static void SimpleTest(string dataBaseFile) { DataBase dataBase = new DataBase(new LongHash()); dataBase.Load(dataBaseFile); int seconds = 10; while (true) { Console.WriteLine("Press any key to identify a new song, press ESC to exit.\n"); ConsoleKeyInfo keyInfo = Console.ReadKey(); if (keyInfo.Key == ConsoleKey.Escape) break; byte[] audio = null; Console.WriteLine("Start recording audio from mic ...", seconds); MicRecorder recorder = new MicRecorder(); recorder.Seconds = seconds; recorder.RequestStop = false; recorder.RecStart(); audio = recorder.GetAudioData(); Console.WriteLine("Length of audio data is {0}.", audio.Length); int id = GetBestHit(audio, dataBase, 16); Console.WriteLine("[Normal] The name of this song is : {0}", dataBase.GetNameByID(id)); int peakPercent = 95; double normPercent = 1.0; sbyte minPeak; sbyte maxPeak; sbyte peak; double ratio = 1.0; Normalize.getPeaks8(audio, out minPeak, out maxPeak); Console.WriteLine("Normal Peak : {0} {1}", minPeak, maxPeak); minPeak *= -1; peak = (minPeak > maxPeak) ? minPeak : maxPeak; ratio = 127.0 / peak * normPercent; Console.WriteLine("Ratio : {0}", ratio); byte[] newData = Normalize.Amplify(audio, ratio); id = GetBestHit(newData, dataBase, 16); Console.WriteLine("[Normal Peak] The name of this song is : {0}", dataBase.GetNameByID(id)); if (!Normalize.getSmartPeaks8(audio, peakPercent, out minPeak, out maxPeak)) Console.WriteLine("Error in Normalize.getSmartPeaks8"); else { Console.WriteLine("Smart Peak : {0} {1}", minPeak, maxPeak); minPeak *= -1; peak = (minPeak > maxPeak) ? minPeak : maxPeak; ratio = 127.0 / peak * normPercent; Console.WriteLine("Ratio : {0}", ratio); newData = Normalize.Amplify(audio, ratio); Normalize.getPeaks8(newData, out minPeak, out maxPeak); Console.WriteLine("Normal Peak After Amplify : {0} {1}", minPeak, maxPeak); id = GetBestHit(newData, dataBase, 16); Console.WriteLine("[Smart Peak] The name of this song is : {0}", dataBase.GetNameByID(id)); } } }
private static int GetBestHit(byte[] audio, DataBase dataBase, int shiftCount) { byte[] tmp = null; int max = 4096; int step = max / shiftCount; int maxScore = -1; int bestId = -1; for (int i = 0; i < max; i += step) { tmp = new byte[audio.Length - i]; Array.Copy(audio, i, tmp, 0, tmp.Length); Dictionary<int, List<int>> results = dataBase.IndexSong(tmp, 0, tmp.Length); int score = 0; int id = dataBase.GetBestHitBySpanMatch(results, ref score, false); if (score > maxScore) { maxScore = score; bestId = id; } } Console.WriteLine("Max score : {0}", maxScore); return bestId; }
static void Main(string[] args) { if (args.Length < 2) { PrintUsage(); return; } else { string firstArg = args[0].ToLower(); string secondArg = args[1]; if (firstArg.CompareTo("build") == 0) { if (args.Length < 3) { WriteLog("Not enough parameters!"); PrintUsage(); return; } string thirdArg = args[2]; bool shutDown = false; if (args.Length > 3) shutDown = bool.Parse(args[3]); BuildDataBase(secondArg, new KeyPointHash(), thirdArg, shutDown); } else if (firstArg.CompareTo("test") == 0) { string dataBaseFile = secondArg; DataBase.SimpleTest(dataBaseFile, new KeyPointHash(), true); } else if (firstArg.CompareTo("convert") == 0) { string inputFile = args[1]; string outputFile = args[2]; DataBase.ConvertIndexFileFromTextToBinary(inputFile, outputFile, new KeyPointHash()); } else if (firstArg.CompareTo("append") == 0) { string indexFile = secondArg; string dataFolder = args[2]; AppendData(indexFile, new KeyPointHash(), dataFolder); } else if (firstArg.CompareTo("combine") == 0) { if (args.Length < 3) { PrintUsage(); } else { string listFile = args[1]; string outputFile = args[2]; using (StreamReader sr = new StreamReader(listFile, Encoding.GetEncoding("GB2312"))) { DataBase finalDataBase = null; while (!sr.EndOfStream) { string indexFile = sr.ReadLine(); if (!File.Exists(indexFile)) { Console.WriteLine("File doesn't exist: {0}", indexFile); continue; } DataBase tmpDataBase = new DataBase(new KeyPointHash()); tmpDataBase.Load(indexFile); if (finalDataBase == null) finalDataBase = tmpDataBase; else { Console.WriteLine("Combining..."); finalDataBase.Combine(tmpDataBase); } } finalDataBase.Save(outputFile); } } } else { PrintUsage(); } } }
static void RunAutoTest(bool shutDown) { //DataBase dataBase = new DataBase(new LongHash()); DataBase dataBase = new DataBase(new KeyPointHash()); dataBase.Load(@"d:\Music\DataBase.txt"); dataBase.Quiet = true; AutoTest.Test(@"D:\Music\", ".mp3", @"D:\Music\Avatar\FileIndex_3_26_keyPoint.txt", dataBase); if (shutDown) Utility.ShutDown(); }
static void RunRecordAutoTest(bool shutDown) { //DataBase dataBase = new DataBase(new LongHash()); //dataBase.Load(@"D:\Music\Avatar\DataBase.txt"); DataBase dataBase = new DataBase(new KeyPointHash()); dataBase.Load(@"D:\Music\DataBase.txt"); dataBase.Quiet = true; AutoTest.TEST_COUNT = 10; AutoTest.SHIFT_COUNT = 64; //AutoTest.Test(@"D:\Music\李建.-.[音乐傲骨].专辑.(MP3)\", "*.wav", @"D:\Music\李建.-.[音乐傲骨].专辑.(MP3)\FileIndex_4_8_keyPoint.txt", dataBase); AutoTest.AnalyseFailure(@"D:\Music\李建.-.[音乐傲骨].专辑.(MP3)\", "*.wav", @"D:\Music\李建.-.[音乐傲骨].专辑.(MP3)\FileIndex_4_8_keyPoint.txt", dataBase); if (shutDown) Utility.ShutDown(); }