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(); } } }
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); }
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 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("--------------------"); }
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--"); } }