// // SampleRate.cs // // Author: // Jacob Johnston (jacobj@inchoatethoughts.com) // // Copyright (C) 2009 Jacob Johnston // // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as // published by the Free Software Foundation, either version 3 of // the License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU Lesser General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . using System.IO; namespace ZenLibrary.RuleBase { /// /// This rule checks whether the MP3 has a sample rate /// greater than or equal to the user-specified minimum /// sample rate. /// class SampleRate : ConfigurableRule { private int minSampleRate = 44100; public override string Name { get { return "Minimum Sample Rate"; } } public override TestType TestType { get { return TestType.FileScan; } } public override void Configure() { SampleRateConfig sampRateConfig = new SampleRateConfig(); sampRateConfig.SampleRateCombo.SelectedItem = string.Format("{0} Hz", minSampleRate); if (sampRateConfig.ShowDialog() == true) { string selectedValue = sampRateConfig.SampleRateCombo.SelectedItem.ToString().Replace("Hz", ""); minSampleRate = int.Parse(selectedValue); } } protected override void WriteCustomConfigAttributes(System.Xml.XmlWriter writer) { writer.WriteAttributeString("MinSampleRate", minSampleRate.ToString()); base.WriteCustomConfigAttributes(writer); } protected override void ReadCustomConfigAttributes(System.Xml.XmlReader reader) { minSampleRate = int.Parse(reader["MinSampleRate"]); base.ReadCustomConfigAttributes(reader); } public override RuleTestResult RunTest(DirectoryInfo directoryInfo, FileInfo fileInfo) { TagLib.File file = TagLib.File.Create(fileInfo.FullName); int sampleRate = file.Properties.AudioSampleRate; if (sampleRate >= minSampleRate) { RuleTestResult result = new RuleTestResult(); result.TestPassed = true; return result; } else { RuleTestResult result = new RuleTestResult(); result.TestPassed = false; result.ResultPath = fileInfo.FullName; result.RuleTestFailedString = string.Format("File \"{0}\" only has a sample rate of {1} Hz", result.ResultPath, sampleRate); return result; } } } }