public static void Main(Arguments arguments) { // 1. set up the necessary files FileInfo sourceRecording = arguments.Source; FileInfo configFile = arguments.Config.ToFileInfo(); DirectoryInfo output = arguments.Output; if (!output.Exists) { output.Create(); } if (arguments.StartOffset.HasValue ^ arguments.EndOffset.HasValue) { throw new InvalidStartOrEndException("If StartOffset or EndOffset is specified, then both must be specified"); } var offsetsProvided = arguments.StartOffset.HasValue && arguments.EndOffset.HasValue; // set default offsets - only use defaults if not provided in argments list TimeSpan?startOffset = null; TimeSpan?endOffset = null; if (offsetsProvided) { startOffset = TimeSpan.FromSeconds(arguments.StartOffset.Value); endOffset = TimeSpan.FromSeconds(arguments.EndOffset.Value); } const string title = "# MAKE FOUR SONOGRAMS FROM AUDIO RECORDING"; string date = "# DATE AND TIME: " + DateTime.Now; LoggedConsole.WriteLine(title); LoggedConsole.WriteLine(date); LoggedConsole.WriteLine("# Input audio file: " + sourceRecording.Name); // 2. get the config dictionary var configDict = GetConfigDictionary(configFile, false); configDict[ConfigKeys.Recording.Key_RecordingCallName] = sourceRecording.FullName; configDict[ConfigKeys.Recording.Key_RecordingFileName] = sourceRecording.Name; // 3: GET TEMPORARY RECORDING int resampleRate = Convert.ToInt32(configDict[AnalysisKeys.ResampleRate]); var tempAudioSegment = AudioRecording.CreateTemporaryAudioFile(sourceRecording, output, resampleRate); // 4: GET 4 sonogram images string sourceName = configDict[ConfigKeys.Recording.Key_RecordingFileName]; sourceName = Path.GetFileNameWithoutExtension(sourceName); var soxFile = new FileInfo(Path.Combine(output.FullName, sourceName + "SOX.png")); var result = GenerateFourSpectrogramImages(tempAudioSegment, soxFile, configDict, dataOnly: false, makeSoxSonogram: false); var outputImageFile = new FileInfo(Path.Combine(output.FullName, sourceName + ".FourSpectrograms.png")); result.CompositeImage.Save(outputImageFile.FullName, ImageFormat.Png); LoggedConsole.WriteLine("\n##### FINISHED FILE ###################################################\n"); }
// ######################################## AUDIO2SONOGRAM TEST METHOD BELOW HERE ###################################################### public static void TESTMETHOD_DrawFourSpectrograms() { { var sourceRecording = @"C:\SensorNetworks\SoftwareTests\TestRecordings\BAC2_20071008-085040.wav".ToFileInfo(); var output = @"C:\SensorNetworks\SoftwareTests\TestFourSonograms".ToDirectoryInfo(); var configFile = @"C:\Work\GitHub\audio-analysis\AudioAnalysis\AnalysisConfigFiles\Towsey.Sonogram.yml".ToFileInfo(); var expectedResultsDir = new DirectoryInfo(Path.Combine(output.FullName, "ExpectedTestResults")); if (!expectedResultsDir.Exists) { expectedResultsDir.Create(); } // 1. get the config dictionary var configDict = GetConfigDictionary(configFile, true); configDict[ConfigKeys.Recording.Key_RecordingCallName] = sourceRecording.FullName; configDict[ConfigKeys.Recording.Key_RecordingFileName] = sourceRecording.Name; // 2. Create temp copy of recording int resampleRate = Convert.ToInt32(configDict[AnalysisKeys.ResampleRate]); var tempAudioSegment = AudioRecording.CreateTemporaryAudioFile(sourceRecording, output, resampleRate); // 3. GET composite image of 4 sonograms var sourceName = Path.GetFileNameWithoutExtension(sourceRecording.Name); var soxImage = new FileInfo(Path.Combine(output.FullName, sourceName + ".SOX.png")); var result = GenerateFourSpectrogramImages(tempAudioSegment, soxImage, configDict, dataOnly: false, makeSoxSonogram: false); var outputImage = new FileInfo(Path.Combine(output.FullName, sourceName + ".FourSpectrograms.png")); result.CompositeImage.Save(outputImage.FullName, ImageFormat.Png); // construct output file names var fileName = sourceName + ".FourSpectrogramsImageInfo"; var pathName = Path.Combine(output.FullName, fileName); var csvFile1 = new FileInfo(pathName + ".json"); // Do my version of UNIT TESTING - This is the File Equality Test. // First construct a test result file containing image info var sb = new StringBuilder("Width,Height\n"); sb.AppendLine($"{result.CompositeImage.Width},{result.CompositeImage.Height}"); // Acoustics.Shared.Csv.Csv.WriteToCsv(csvFile1, sb); FileTools.WriteTextFile(csvFile1.FullName, sb.ToString()); // Now do the test var expectedTestFile1 = new FileInfo(Path.Combine(expectedResultsDir.FullName, "FourSpectrogramsTest.EXPECTED.json")); TestTools.FileEqualityTest("Matrix Equality", csvFile1, expectedTestFile1); Console.WriteLine("\n\n"); } }
public static void Main(Arguments arguments) { // 1. set up the necessary files var sourceRecording = arguments.Source; var configInfo = ConfigFile.Deserialize <AnalyzerConfig>(arguments.Config.ToFileInfo()); DirectoryInfo output = arguments.Output; if (!output.Exists) { output.Create(); } //if (arguments.StartOffset.HasValue ^ arguments.EndOffset.HasValue) //{ // throw new InvalidStartOrEndException("If StartOffset or EndOffset is specified, then both must be specified"); //} // set default offsets - only use defaults if not provided in arguments list // var offsetsProvided = arguments.StartOffset.HasValue && arguments.EndOffset.HasValue; //TimeSpan? startOffset; //TimeSpan? endOffset; //if (offsetsProvided) //{ // startOffset = TimeSpan.FromSeconds(arguments.StartOffset.Value); // endOffset = TimeSpan.FromSeconds(arguments.EndOffset.Value); //} const string title = "# MAKE MULTIPLE SONOGRAMS FROM AUDIO RECORDING"; string date = "# DATE AND TIME: " + DateTime.Now; LoggedConsole.WriteLine(title); LoggedConsole.WriteLine(date); LoggedConsole.WriteLine("# Input audio file: " + sourceRecording.Name); // 3: CREATE A TEMPORARY RECORDING int resampleRate = configInfo.GetIntOrNull("ResampleRate") ?? 22050; var tempAudioSegment = AudioRecording.CreateTemporaryAudioFile(sourceRecording, output, resampleRate); // 4: GENERATE SPECTROGRAM images //string sourceName = sourceRecording.FullName; string sourceName = Path.GetFileNameWithoutExtension(sourceRecording.FullName); var result = GenerateSpectrogramImages(tempAudioSegment, configInfo, sourceName); // 5: Save the image var outputImageFile = new FileInfo(Path.Combine(output.FullName, sourceName + ".Spectrograms.png")); result.CompositeImage.Save(outputImageFile.FullName, ImageFormat.Png); }