using DTS.Common.Enums.Hardware;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Serialization;
namespace DTS.Common.Classes.DSP
{
///
/// collection for all dsp filters
///
[System.Diagnostics.CodeAnalysis.SuppressMessage("Minor Code Smell", "S101:Types should be named in PascalCase", Justification = "Acronym")]
public class DSPFilterCollection : Collection
{
private static readonly object MyLock = new object();
private static DSPFilterCollection _instance = null;
public static DSPFilterCollection GetDSPFilterCollection()
{
lock (MyLock)
{
if (null != _instance) { return _instance; }
WriteDefaultFileIfMissing(DSP_FILTER_XML_FILE);
_instance = ReadFile(DSP_FILTER_XML_FILE);
}
return _instance;
}
private const string DSP_FILTER_XML_FILE = "DSPFilters.xml";
private DSPFilterCollection(DSPFilterType[] filters)
{
foreach (var filter in filters) { Add(filter); }
}
public DSPFilterCollection() { }
public const int BUTTERWORTH = 13;
public const int FIR45TAP = 14;
public const int NONE = 0;
public enum DSPFilterDefaults
{
[Display(Name ="None", Description = "Default and legacy setting for streaming")]
[Scaler(double.NaN)]
None = NONE,
[Display(Name = "6th IIR Butterworth", Description = "6 - pole IIR Butterworth Low Pass")]
[Scaler(double.NaN)]
Butterworth = BUTTERWORTH,
[Display(Name = "45-Tap FIR", Description = "finite impulse response filter with 45 taps")]
[Scaler(double.NaN)]
FIR = FIR45TAP
}
private static DSPFilterCollection CreateDefaultCollection()
{
var list = new List
{
new DSPFilterType(DSPFilterDefaults.None, new DASRestriction[] { new DASRestriction() }),
new DSPFilterType(DSPFilterDefaults.Butterworth, new DASRestriction[] { new DASRestriction(HardwareTypes.SLICE6_AIR.ToString(), 28), new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), -1) }),
new DSPFilterType(DSPFilterDefaults.FIR, new DASRestriction[] { new DASRestriction(HardwareTypes.SLICE6_AIR.ToString(), 28), new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), -1) })
};
var collection = new DSPFilterCollection(list.ToArray());
return collection;
}
private static void WriteDefaultFileIfMissing(string filePath)
{
if (File.Exists(filePath)) { return; }
var collection = CreateDefaultCollection();
var serializer = new XmlSerializer(typeof(DSPFilterCollection));
var settings = new XmlWriterSettings() { Indent = true };
using (var writer = XmlWriter.Create(filePath, settings))
{
serializer.Serialize(writer, collection);
}
}
private static DSPFilterCollection ReadFile(string filePath)
{
var deserializer = new XmlSerializer(typeof(DSPFilterCollection));
using (var fs = new FileStream(filePath, FileMode.Open))
{
var cs = (DSPFilterCollection)deserializer.Deserialize(fs);
return cs;
}
}
///
/// returns the matching filter, or None if not found, or if none isn't found then the first entry
///
///
///
public DSPFilterType GetFilter(string s)
{
var match = Items.FirstOrDefault(x => x.DisplayString == s);
if (null != match) { return match; }
match = Items.FirstOrDefault(x => x.EnumValue == 0);
if (null != match) { return match; }
return Items[0];
}
}
}