//
// BitRate.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 bit rate
/// greater than or equal to the user-specified minimum
/// bit rate. Or, if the user desires, if the MP3
/// was encoded with a variable bit rate.
///
class BitRate : ConfigurableRule
{
private int minBitRate = 160;
private bool allowVBR = true;
public override string Name
{
get { return "Has Minimum Bit Rate"; }
}
public override TestType TestType
{
get { return TestType.FileScan; }
}
public override void Configure()
{
BitRateConfig bitRateConfig = new BitRateConfig();
bitRateConfig.MinimumBitRateCombo.SelectedItem = string.Format("{0} kbps", minBitRate);
bitRateConfig.AllowVBRCheck.IsChecked = allowVBR;
if (bitRateConfig.ShowDialog() == true)
{
string selectedValue = bitRateConfig.MinimumBitRateCombo.SelectedItem.ToString().Replace("kbps", "");
minBitRate = int.Parse(selectedValue);
allowVBR = (bool)bitRateConfig.AllowVBRCheck.IsChecked;
}
}
protected override void WriteCustomConfigAttributes(System.Xml.XmlWriter writer)
{
writer.WriteAttributeString("MinBitRate", minBitRate.ToString());
writer.WriteAttributeString("AllowVBR", allowVBR.ToString());
base.WriteCustomConfigAttributes(writer);
}
protected override void ReadCustomConfigAttributes(System.Xml.XmlReader reader)
{
minBitRate = int.Parse(reader["MinBitRate"]);
allowVBR = bool.Parse(reader["AllowVBR"]);
base.ReadCustomConfigAttributes(reader);
}
public override RuleTestResult RunTest(DirectoryInfo directoryInfo, FileInfo fileInfo)
{
TagLib.File file = TagLib.File.Create(fileInfo.FullName);
int bitRate = file.Properties.AudioBitrate;
bool isVBR = (allowVBR && file.Properties.Description.Contains("VBR"));
if (isVBR || bitRate >= minBitRate)
{
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 bit rate of {1} kbps", result.ResultPath, bitRate);
return result;
}
}
}
}