-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
143 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using System; | ||
|
||
using Cavern.Utilities; | ||
|
||
namespace Cavern.Channels { | ||
/// <summary> | ||
/// Multiple ways of getting a mixing matrix to simulate one channel layout on a different one. While Cavern can play any standard | ||
/// content on any layout, getting the matrix is useful for applying this feature in calibrations. | ||
/// </summary> | ||
public static class SpatialRemapping { | ||
/// <summary> | ||
/// Get a mixing matrix that maps the <paramref name="playedContent"/> to a <paramref name="usedLayout"/>. The result is a set of | ||
/// multipliers for each output (playback) channel, with which the input (content) channels should be multiplied and mixed to that | ||
/// specific channel. The dimensions are [output channels][input channels]. | ||
/// </summary> | ||
public static float[][] GetMatrix(Channel[] playedContent, Channel[] usedLayout) { | ||
Channel[] oldChannels = Listener.Channels; | ||
Listener.ReplaceChannels(usedLayout); | ||
int inputs = playedContent.Length, | ||
outputs = usedLayout.Length; | ||
|
||
// Create simulation | ||
Listener simulator = new Listener() { | ||
UpdateRate = Math.Max(inputs, 16), | ||
LFESeparation = true, | ||
DirectLFE = true | ||
}; | ||
for (int i = 0; i < inputs; i++) { | ||
simulator.AttachSource(new Source() { | ||
Clip = GetClipForChannel(i, inputs, simulator.SampleRate), | ||
Position = playedContent[i].SpatialPos * Listener.EnvironmentSize, | ||
LFE = playedContent[i].LFE, | ||
VolumeRolloff = Rolloffs.Disabled | ||
}); | ||
} | ||
|
||
// Simulate and format | ||
float[] result = simulator.Render(); | ||
Listener.ReplaceChannels(oldChannels); | ||
int expectedLength = inputs * outputs; | ||
if (result.Length > expectedLength) { | ||
Array.Resize(ref result, expectedLength); | ||
} | ||
float[][] output = new float[outputs][]; | ||
for (int i = 0; i < outputs; i++) { | ||
output[i] = new float[inputs]; | ||
} | ||
WaveformUtils.InterlacedToMultichannel(result, output); | ||
return output; | ||
} | ||
|
||
/// <summary> | ||
/// Create a <see cref="Clip"/> that is 1 at the channel's index and 0 everywhere else. | ||
/// </summary> | ||
static Clip GetClipForChannel(int channel, int channels, int sampleRate) { | ||
float[] data = new float[channels]; | ||
data[channel] = 1; | ||
return new Clip(data, 1, sampleRate); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Tests/Test.Cavern/ChannelPrototype_Tests.cs → ...Cavern/Channels/ChannelPrototype_Tests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Cavern; | ||
using Cavern.Channels; | ||
|
||
namespace Test.Cavern.Channels { | ||
/// <summary> | ||
/// Tests the <see cref="SpatialRemapping"/> functions. | ||
/// </summary> | ||
[TestClass] | ||
public class SpatialRemapping_Tests { | ||
/// <summary> | ||
/// Tests if remapping the alternative 5.1 is done correctly to average 5.1 placement. | ||
/// </summary> | ||
[TestMethod, Timeout(1000)] | ||
public void Remap5Point1() { | ||
Channel[] content = ChannelPrototype.ToLayoutAlternative(ChannelPrototype.GetStandardMatrix(6)), | ||
playback = ChannelPrototype.ToLayout(ChannelPrototype.GetStandardMatrix(6)); | ||
float[][] matrix = SpatialRemapping.GetMatrix(content, playback); | ||
Assert.AreEqual(1, matrix[0][0]); // FL | ||
Assert.AreEqual(1, matrix[1][1]); // FR | ||
Assert.AreEqual(1, matrix[2][2]); // C | ||
Assert.AreEqual(1, matrix[3][3]); // LFE | ||
Assert.AreEqual(.570968032f, matrix[0][4]); // SL front mix | ||
Assert.AreEqual(.570968032f, matrix[1][5]); // SR front mix | ||
Assert.AreEqual(.820972264f, matrix[4][4]); // SL side mix | ||
Assert.AreEqual(.820972264f, matrix[5][5]); // SR side mix | ||
TestUtils.AssertNumberOfZeros(matrix, 28); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Test.Cavern { | ||
/// <summary> | ||
/// Common utilities used in testing like assertions. | ||
/// </summary> | ||
internal static class TestUtils { | ||
/// <summary> | ||
/// Test if the number of zeros in a jagged <paramref name="array"/> match an expected <paramref name="count"/>. | ||
/// </summary> | ||
public static void AssertNumberOfZeros(float[][] array, int count) { | ||
int zeros = 0; | ||
for (int i = 0; i < array.Length; i++) { | ||
float[] subarray = array[i]; | ||
for (int j = 0; j < subarray.Length; j++) { | ||
if (subarray[j] == 0) { | ||
zeros++; | ||
} | ||
} | ||
} | ||
Assert.AreEqual(count, zeros); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters