Files
DP44/Common/DTS.Common/.svn/pristine/ca/ca16db689ec7c990f7c179849ce3ebef85d7ebd2.svn-base
2026-04-17 14:55:32 -04:00

105 lines
4.5 KiB
Plaintext

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
{
public class StreamingFilterProfileCollection : Collection<StreamingFilterProfile>
{
private static readonly object MyLock = new object();
private static StreamingFilterProfileCollection _instance = null;
public static StreamingFilterProfileCollection GetCollection()
{
lock (MyLock)
{
if (null != _instance) { return _instance; }
WriteDefaultFileIfMissing(FILTER_PROFILES_XML_FILE);
_instance = ReadFile(FILTER_PROFILES_XML_FILE);
}
return _instance;
}
private const string FILTER_PROFILES_XML_FILE = "StreamingFilterProfiles.xml";
private StreamingFilterProfileCollection(StreamingFilterProfile[] filters)
{
foreach (var filter in filters) { Add(filter); }
}
public StreamingFilterProfileCollection() { }
public enum DefaultProfiles
{
[Display(Name ="Default (Mixed)", Description = "6 pole butterworth legacy or default", Order = DEFAULT_VALUE)]
[Scaler(double.NaN)]
Default = DEFAULT_VALUE,
[Display(Name ="Sample rate / 4", Description = "based on sample rate")]
[Scaler(4)]
Profile7 = 7,
[Display(Name = "Sample rate / 6.4", Description = "based on sample rate")]
[Scaler(6.4)]
Profile8 = 8,
[Display(Name = "Sample rate / 8", Description = "based on sample rate")]
[Scaler(8)]
Profile9 = 9,
[Display(Name = "Sample rate / 10", Description = "based on sample rate")]
[Scaler(10)]
Profile10 = 10
}
private static StreamingFilterProfileCollection CreateDefaultCollection()
{
var list = new List<StreamingFilterProfile>
{
new StreamingFilterProfile(DefaultProfiles.Default, new[] { new DASRestriction(string.Empty, -1) }),
new StreamingFilterProfile(DefaultProfiles.Profile7, new[] { new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), 50), new DASRestriction("SLICE6_AIR", 51) }),
new StreamingFilterProfile(DefaultProfiles.Profile8, new[] { new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), 50), new DASRestriction("SLICE6_AIR", 51) }),
new StreamingFilterProfile(DefaultProfiles.Profile9, new[] { new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), 50), new DASRestriction("SLICE6_AIR", 51) }),
new StreamingFilterProfile(DefaultProfiles.Profile10, new[] { new DASRestriction(HardwareTypes.SLICE6_AIR_BR.ToString(), 50), new DASRestriction("SLICE6_AIR", 51) })
};
var collection = new StreamingFilterProfileCollection(list.ToArray());
return collection;
}
private static void WriteDefaultFileIfMissing(string filePath)
{
if (File.Exists(filePath)) { return; }
var collection = CreateDefaultCollection();
var serializer = new XmlSerializer(typeof(StreamingFilterProfileCollection));
var settings = new XmlWriterSettings() { Indent = true };
using (var writer = XmlWriter.Create(filePath, settings))
{
serializer.Serialize(writer, collection);
}
}
private static StreamingFilterProfileCollection ReadFile(string filePath)
{
var deserializer = new XmlSerializer(typeof(StreamingFilterProfileCollection));
using (var fs = new FileStream(filePath, FileMode.Open))
{
var cs = (StreamingFilterProfileCollection)deserializer.Deserialize(fs);
return cs;
}
}
public const int DEFAULT_VALUE = 13;
public StreamingFilterProfile GetStreamingFilterProfile(string s)
{
var match = Items.FirstOrDefault(x => x.DisplayString == s);
if (null != match) { return match; }
match = Items.FirstOrDefault(x => x.EnumValue == DEFAULT_VALUE);
if (null != match) { return match; }
return Items[0];
}
}
}