Skip to content

Commit ffce264

Browse files
Added tests for ConvolverNode.
1 parent debfd49 commit ffce264

File tree

3 files changed

+130
-4
lines changed

3 files changed

+130
-4
lines changed

src/KristofferStrube.Blazor.WebAudio/AudioNodes/ConvolverNode.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,14 @@ protected ConvolverNode(IJSRuntime jSRuntime, IJSObjectReference jSReference, Cr
5050
/// </summary>
5151
public async Task<AudioBuffer?> GetBufferAsync()
5252
{
53+
await using ValueReference bufferAttribute = new(JSRuntime, JSReference, "buffer");
54+
if (await bufferAttribute.GetTypeNameAsync() is "null")
55+
{
56+
return null;
57+
}
58+
5359
IJSObjectReference helper = await webAudioHelperTask.Value;
54-
IJSObjectReference? jSInstance = await helper.InvokeAsync<IJSObjectReference?>("getAttribute", JSReference, "buffer");
60+
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("getAttribute", JSReference, "buffer");
5561
return jSInstance is null ? null : await AudioBuffer.CreateAsync(JSRuntime, jSInstance, new() { DisposesJSReference = true });
5662
}
5763

@@ -70,7 +76,7 @@ public async Task SetBufferAsync(AudioBuffer? value)
7076
}
7177

7278
/// <summary>
73-
/// Gets the flag for whether the impulse response from the buffer will be scaled by an equal-power normalization when the buffer atttribute is set.<br />
79+
/// Gets the flag for whether the impulse response from the buffer will be scaled by an equal-power normalization when the buffer attribute is set.<br />
7480
/// </summary>
7581
/// <remarks>
7682
/// Its default value is <see langword="true"/> in order to achieve a more uniform output level from the convolver when loaded with diverse impulse responses.
@@ -87,7 +93,7 @@ public async Task<bool> GetNormalizeAsync()
8793
}
8894

8995
/// <summary>
90-
/// Sets the flag for whether the impulse response from the buffer will be scaled by an equal-power normalization when the buffer atttribute is set.<br />
96+
/// Sets the flag for whether the impulse response from the buffer will be scaled by an equal-power normalization when the buffer attribute is set.<br />
9197
/// </summary>
9298
/// <remarks>
9399
/// Its default value is <see langword="true"/> in order to achieve a more uniform output level from the convolver when loaded with diverse impulse responses.

src/KristofferStrube.Blazor.WebAudio/Options/ConvolverOptions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ public class ConvolverOptions : AudioNodeOptions
1717
/// <summary>
1818
/// The opposite of the desired initial value for <see cref="ConvolverNode.GetNormalizeAsync"/>.
1919
/// </summary>
20+
/// <remarks>
21+
/// The default value is <see langword="false"/>.
22+
/// </remarks>
2023
[JsonPropertyName("disableNormalization")]
2124
public bool DisableNormalization { get; set; } = false;
2225

tests/IntegrationTests/AudioNodeTests/ConvolverNodeTest.cs

Lines changed: 118 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using KristofferStrube.Blazor.WebIDL.Exceptions;
1+
using FluentAssertions;
2+
using KristofferStrube.Blazor.WebIDL.Exceptions;
23
using Microsoft.JSInterop;
4+
using System;
35

46
namespace IntegrationTests.AudioNodeTests;
57

@@ -12,4 +14,119 @@ public override async Task<ConvolverNode> CreateAsync(IJSRuntime jSRuntime, Audi
1214
{
1315
[ChannelCountMode.Max] = typeof(NotSupportedErrorException)
1416
};
17+
18+
[Test]
19+
public async Task GetBufferAsync_ShouldReturnNull_WhenItHasNoBuffer()
20+
{
21+
// Arrange
22+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext);
23+
24+
// Act
25+
AudioBuffer? buffer = await node.GetBufferAsync();
26+
27+
// Assert
28+
_ = buffer.Should().BeNull();
29+
}
30+
31+
[Test]
32+
public async Task GetBufferAsync_ShouldRetrieveBuffer_WhenItHasBuffer()
33+
{
34+
// Arrange
35+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
36+
{
37+
Length = 1,
38+
SampleRate = await AudioContext.GetSampleRateAsync()
39+
});
40+
41+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext, new()
42+
{
43+
Buffer = buffer
44+
});
45+
46+
// Act
47+
AudioBuffer? readBuffer = await node.GetBufferAsync();
48+
49+
// Assert
50+
_ = readBuffer.Should().NotBeNull();
51+
}
52+
53+
[Test]
54+
public async Task SetBufferAsync_ShouldUpdateTheBuffer()
55+
{
56+
// Arrange
57+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext);
58+
59+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
60+
{
61+
Length = 1,
62+
SampleRate = await AudioContext.GetSampleRateAsync()
63+
});
64+
65+
// Act
66+
await node.SetBufferAsync(buffer);
67+
68+
// Assert
69+
AudioBuffer? readBuffer = await node.GetBufferAsync();
70+
_ = readBuffer.Should().NotBeNull();
71+
}
72+
73+
[Test]
74+
public async Task SetBufferAsync_WithNullArgument_ShouldClearBuffer()
75+
{
76+
// Arrange
77+
await using AudioBuffer buffer = await AudioBuffer.CreateAsync(JSRuntime, new AudioBufferOptions()
78+
{
79+
Length = 1,
80+
SampleRate = await AudioContext.GetSampleRateAsync()
81+
});
82+
83+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext, new()
84+
{
85+
Buffer = buffer
86+
});
87+
88+
// Act
89+
await node.SetBufferAsync(null);
90+
91+
// Assert
92+
AudioBuffer? readBuffer = await node.GetBufferAsync();
93+
_ = readBuffer.Should().BeNull();
94+
}
95+
96+
[Test]
97+
[TestCase(false)]
98+
[TestCase(true)]
99+
public async Task GetNormalizeAsync_ShouldRetrieveNormalize(bool normalize)
100+
{
101+
// Arrange
102+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext, new()
103+
{
104+
DisableNormalization = !normalize
105+
});
106+
107+
// Act
108+
bool readNormalize = await node.GetNormalizeAsync();
109+
110+
// Assert
111+
_ = readNormalize.Should().Be(normalize);
112+
}
113+
114+
[Test]
115+
[TestCase(false)]
116+
[TestCase(true)]
117+
public async Task SetNormalizeAsync_ShouldRetrieveNormalize(bool normalize)
118+
{
119+
// Arrange
120+
await using ConvolverNode node = await ConvolverNode.CreateAsync(JSRuntime, AudioContext, new()
121+
{
122+
DisableNormalization = normalize // this is the inverse as the options parameter is whether normalization should be disabled.
123+
});
124+
125+
// Act
126+
await node.SetNormalizeAsync(normalize);
127+
128+
// Assert
129+
bool readNormalize = await node.GetNormalizeAsync();
130+
_ = readNormalize.Should().Be(normalize);
131+
}
15132
}

0 commit comments

Comments
 (0)