Skip to content

Commit

Permalink
Performant CSF import
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Jul 20, 2024
1 parent d832313 commit 94d64eb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using Cavern.Channels;
using Cavern.Filters;
using Cavern.Filters.Utilities;
using Cavern.Format.ConfigurationFile.Helpers;
using Cavern.QuickEQ.Equalization;

namespace Cavern.Format.ConfigurationFile {
Expand All @@ -25,7 +26,7 @@ public CavernFilterStudioConfigurationFile(ConfigurationFile other) : base(other
/// <summary>
/// Import a Cavern Filter Studio configuration file from a <paramref name="path"/>.
/// </summary>
public CavernFilterStudioConfigurationFile(string path) : base(ParseSplitPoints(path)) { }
public CavernFilterStudioConfigurationFile(string path) : base(ParseSplitPoints(path)) => FinishLazySetup(131072);

/// <summary>
/// Create an empty file for a standard layout.
Expand Down Expand Up @@ -104,7 +105,7 @@ public CavernFilterStudioConfigurationFile(string name, params ReferenceChannel[
static Filter ParseFilter(XmlReader reader) {
switch (reader.Name) {
case nameof(GraphicEQ):
GraphicEQ graphicEQ = new GraphicEQ(new Equalizer(), Listener.DefaultSampleRate);
LazyGraphicEQ graphicEQ = new LazyGraphicEQ(new Equalizer(), Listener.DefaultSampleRate);
graphicEQ.ReadXml(reader);
return graphicEQ;
case nameof(InputChannel):
Expand All @@ -117,7 +118,7 @@ static Filter ParseFilter(XmlReader reader) {
return outputChannel;
default:
return Filter.FromXml(reader);
};
}
}

/// <summary>
Expand All @@ -137,7 +138,7 @@ public override void Export(string path) {
(FilterGraphNode node, int channel)[] exportOrder = GetExportOrder();
ValidateForExport(exportOrder);

XmlWriterSettings settings = new XmlWriterSettings() {
XmlWriterSettings settings = new XmlWriterSettings {
Indent = true
};
using XmlWriter writer = XmlWriter.Create(path, settings);
Expand All @@ -162,7 +163,7 @@ public override void Export(string path) {
return j;
}
}
throw new IndexOutOfRangeException();
throw new DataMisalignedException();
})));
writer.WriteEndElement();
}
Expand Down
37 changes: 33 additions & 4 deletions Cavern.QuickEQ.Format/ConfigurationFile/Helpers/LazyGraphicEQ.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
using Cavern.Filters;
using System.Xml;
using System.Xml.Schema;
using System.Xml.Serialization;

using Cavern.Filters;
using Cavern.QuickEQ.Equalization;
using Cavern.Utilities;

namespace Cavern.Format.ConfigurationFile.Helpers {
/// <summary>
/// Placeholder where a <see cref="GraphicEQ"/> should be created.
/// </summary>
public sealed class LazyGraphicEQ : Filter, ILazyLoadableFilter {
public sealed class LazyGraphicEQ : Filter, ILazyLoadableFilter, IXmlSerializable {
/// <summary>
/// Desired frequency response change.
/// </summary>
readonly Equalizer equalizer;
Equalizer equalizer;

/// <summary>
/// Sample rate at which this EQ is converted to a minimum-phase FIR filter.
/// </summary>
readonly int sampleRate;
int sampleRate;

/// <summary>
/// Placeholder where a <see cref="GraphicEQ"/> should be created.
Expand All @@ -38,5 +42,30 @@ public Filter CreateFilter(FFTCachePool cachePool) {

/// <inheritdoc/>
public override object Clone() => new LazyGraphicEQ(equalizer, sampleRate);

/// <inheritdoc/>
public XmlSchema GetSchema() => null;

/// <inheritdoc/>
public void ReadXml(XmlReader reader) {
while (reader.MoveToNextAttribute()) {
switch (reader.Name) {
case nameof(GraphicEQ.SampleRate):
sampleRate = int.Parse(reader.Value);
break;
case nameof(GraphicEQ.Equalizer):
equalizer = EQGenerator.FromEqualizerAPO(reader.Value);
break;
}
}
}

/// <inheritdoc/>
public void WriteXml(XmlWriter writer) {
writer.WriteStartElement(nameof(GraphicEQ));
writer.WriteAttributeString(nameof(GraphicEQ.SampleRate), sampleRate.ToString());
writer.WriteAttributeString(nameof(GraphicEQ.Equalizer), equalizer.ExportToEqualizerAPO());
writer.WriteEndElement();
}
}
}

0 comments on commit 94d64eb

Please sign in to comment.