Skip to content

Commit

Permalink
Export spatial remapping to XML
Browse files Browse the repository at this point in the history
  • Loading branch information
VoidXH committed Nov 22, 2023
1 parent f652557 commit 654b722
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions Cavern/Channels/SpatialRemapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Text;
using System.Xml;

using Cavern.Utilities;

Expand Down Expand Up @@ -97,6 +98,39 @@ public static string ToEqualizerAPO(float[][] matrix) {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToEqualizerAPO(Channel[] playedContent) => ToEqualizerAPO(GetMatrix(playedContent));

/// <summary>
/// Convert a spatial remapping matrix to an XML file.
/// </summary>
public static string ToXML(float[][] matrix) {
StringBuilder result = new StringBuilder();
using XmlWriter writer = XmlWriter.Create(result);
writer.WriteStartElement("matrix");
for (int output = 0; output < matrix.Length; output++) {
float[] inputs = matrix[output];
writer.WriteStartElement("output");
writer.WriteAttributeString("channel", output.ToString());
for (int input = 0; input < inputs.Length; input++) {
if (inputs[input] != 0) {
writer.WriteStartElement("input");
writer.WriteAttributeString("channel", input.ToString());
writer.WriteAttributeString("gain", inputs[input].ToString(CultureInfo.InvariantCulture));
writer.WriteEndElement();
}
}
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.Flush();
return result.ToString();
}

/// <summary>
/// Create an XML file that describes a matrix mix that mixes the <paramref name="playedContent"/> to the user-set
/// <see cref="Listener.Channels"/> of the current system.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static string ToXML(Channel[] playedContent) => ToXML(GetMatrix(playedContent));

/// <summary>
/// Create a <see cref="Clip"/> that is 1 at the channel's index and 0 everywhere else.
/// </summary>
Expand Down
13 changes: 13 additions & 0 deletions Tests/Test.Cavern/Channels/SpatialRemapping_Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ public void Remap5Point1() {
TestUtils.AssertNumberOfZeros(matrix, 28);
}

/// <summary>
/// Tests if remapping 2.0 is done correctly and converted to the valid XML output.
/// </summary>
[TestMethod, Timeout(1000)]
public void RemapQuadroXML() {
const string expected = "<?xml version=\"1.0\" encoding=\"utf-16\"?><matrix><output channel=\"0\">" +
"<input channel=\"0\" gain=\"0.92387956\" /><input channel=\"1\" gain=\"0.3826834\" /></output><output channel=\"1\">" +
"<input channel=\"0\" gain=\"0.38268343\" /><input channel=\"1\" gain=\"0.92387956\" /></output></matrix>";
string result = SpatialRemapping.ToXML(SpatialRemapping.GetMatrix(ChannelPrototype.ToLayout(ChannelPrototype.ref200),
ChannelPrototype.ToLayoutAlternative(ChannelPrototype.ref200)));
Assert.AreEqual(expected, result);
}

/// <summary>
/// Tests if remapping 7.1 is done correctly to 5.1.2 and converted to the valid Equalizer APO line.
/// </summary>
Expand Down

0 comments on commit 654b722

Please sign in to comment.