Skip to content

Commit

Permalink
Removed redundant direct usage of JSReferences.
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferStrube committed Aug 25, 2024
1 parent fb4cef9 commit 7419873
Show file tree
Hide file tree
Showing 21 changed files with 50 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ else
<Plot Data="timeDomainMeasurements" />
<h3>Frequency Data</h3>
<Plot Data="frequencyMeasurements" />
<!--<h3>Amplitude Data</h3>
<AmplitudePlot Analyser="analyser" />-->
<h3>Amplitude Data</h3>
<AmplitudePlot Analyser="analyser" />

@if (audioOptions.Count > 0)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@
await Pause();
}
tracks = tracks.OrderBy(x => Random.Shared.Next()).ToList();
loadedTracks = new byte[]?[4];
currentTrackLoaded = false;
currentTrack = 0;
offset = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
@using Excubo.Blazor.Canvas

<Canvas @ref="canvas" height="@Height" width="@Width" style="@canvasStyle" />
<Canvas @ref="canvas" height="@(Height * 10)" width="@(Width * 10)" style="@canvasStyle" />
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ public partial class AmplitudePlot : ComponentBase, IDisposable
public int Height { get; set; } = 200;

[Parameter]
public int Width { get; set; } = 200;
public int Width { get; set; } = 180;

protected override async Task OnAfterRenderAsync(bool _)
{
if (running || Analyser is null) return;
running = true;

int bufferLength = (int)await Analyser.GetFrequencyBinCountAsync();
int bufferLength = (int)await Analyser.GetFftSizeAsync();
await using Uint8Array timeDomainData = await Uint8Array.CreateAsync(JSRuntime, bufferLength);

while (running)
Expand All @@ -39,20 +39,27 @@ protected override async Task OnAfterRenderAsync(bool _)
{
if (!running) break;

await Analyser.GetByteFrequencyDataAsync(timeDomainData);
await Analyser.GetByteTimeDomainDataAsync(timeDomainData);

byte[] reading = await timeDomainData.GetAsArrayAsync();

double amplitude = reading.Max(r => Math.Abs(r - 128)) / 128.0;

await using (Context2D context = await canvas.GetContext2DAsync())
{
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
await context.FillRectAsync(i, 0, 1, Height);
if (i == 0)
{
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
await context.FillRectAsync(0, 0, Width * 10, Height * 10);
}

double height = reading.Sum(r => r) / (reading.Length * 255.0);
await context.FillAndStrokeStyles.FillStyleAsync($"#fff");
await context.FillRectAsync(i * 10, 0, 10, Height * 10);

await context.FillAndStrokeStyles.FillStyleAsync($"#000");
await context.FillRectAsync(i, (Height / 2.0) - (height / 2 * Height), 1, height * Height);
await context.FillRectAsync(i * 10, (Height * 10 / 2.0) - (amplitude * Height * 10), 10, amplitude * 2 * Height * 10);
}

await Task.Delay(1);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/KristofferStrube.Blazor.WebAudio/AudioBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public async Task<Float32Array> GetChannelDataAsync(ulong channel)
/// <exception cref="IndexSizeErrorException"></exception>
public async Task CopyFromChannelAsync(Float32Array destination, ulong channelNumber, ulong bufferOffset = 0)
{
await JSReference.InvokeVoidAsync("copyFromChannel", destination.JSReference, channelNumber, bufferOffset);
await JSReference.InvokeVoidAsync("copyFromChannel", destination, channelNumber, bufferOffset);
}

/// <summary>
Expand All @@ -128,6 +128,6 @@ public async Task CopyFromChannelAsync(Float32Array destination, ulong channelNu
/// <exception cref="IndexSizeErrorException"></exception>
public async Task CopyToChannelAsync(Float32Array source, ulong channelNumber, ulong bufferOffset = 0)
{
await JSReference.InvokeVoidAsync("copyToChannel", source.JSReference, channelNumber, bufferOffset);
await JSReference.InvokeVoidAsync("copyToChannel", source, channelNumber, bufferOffset);
}
}
6 changes: 3 additions & 3 deletions src/KristofferStrube.Blazor.WebAudio/AudioContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public async Task CloseAsync()
/// <returns>A new <see cref="MediaElementAudioSourceNode"/>.</returns>
public async Task<MediaElementAudioSourceNode> CreateMediaElementSourceAsync(EventTarget mediaElement)
{
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaElementSource", mediaElement.JSReference);
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaElementSource", mediaElement);
return await MediaElementAudioSourceNode.CreateAsync(JSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand All @@ -135,7 +135,7 @@ public async Task<MediaElementAudioSourceNode> CreateMediaElementSourceAsync(Eve
/// <returns>A new <see cref="MediaStreamAudioSourceNode"/>.</returns>
public async Task<MediaStreamAudioSourceNode> CreateMediaStreamSourceAsync(MediaStream mediaStream)
{
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaStreamSource", mediaStream.JSReference);
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaStreamSource", mediaStream);
return await MediaStreamAudioSourceNode.CreateAsync(JSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand All @@ -150,7 +150,7 @@ public async Task<MediaStreamAudioSourceNode> CreateMediaStreamSourceAsync(Media
/// <returns>A new <see cref="MediaStreamTrackAudioSourceNode"/>.</returns>
public async Task<MediaStreamTrackAudioSourceNode> CreateMediaStreamTrackSourceAsync(MediaStreamTrack mediaStreamTrack)
{
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaStreamTrackSource", mediaStreamTrack.JSReference);
IJSObjectReference jSInstance = await JSReference.InvokeAsync<IJSObjectReference>("createMediaStreamTrackSource", mediaStreamTrack);
return await MediaStreamTrackAudioSourceNode.CreateAsync(JSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
10 changes: 5 additions & 5 deletions src/KristofferStrube.Blazor.WebAudio/AudioNodes/AnalyserNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class AnalyserNode : AudioNode, IJSCreatable<AnalyserNode>
public static async Task<AnalyserNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, AnalyserOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructAnalyzerNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructAnalyzerNode", context, options);
return new AnalyserNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand All @@ -56,7 +56,7 @@ protected AnalyserNode(IJSRuntime jSRuntime, IJSObjectReference jSReference, Cre
/// <param name="array">This parameter is where the frequency-domain analysis data will be copied.</param>
public async Task GetFloatFrequencyDataAsync(Float32Array array)
{
await JSReference.InvokeVoidAsync("getFloatFrequencyData", array.JSReference);
await JSReference.InvokeVoidAsync("getFloatFrequencyData", array);
}

/// <summary>
Expand All @@ -71,7 +71,7 @@ public async Task GetFloatFrequencyDataAsync(Float32Array array)
/// <param name="array">This parameter is where the time-domain sample data will be copied.</param>
public async Task GetByteFrequencyDataAsync(Uint8Array array)
{
await JSReference.InvokeVoidAsync("getByteFrequencyData", array.JSReference);
await JSReference.InvokeVoidAsync("getByteFrequencyData", array);
}

/// <summary>
Expand All @@ -86,7 +86,7 @@ public async Task GetByteFrequencyDataAsync(Uint8Array array)
/// <param name="array">This parameter is where the time-domain sample data will be copied.</param>
public async Task GetFloatTimeDomainDataAsync(Float32Array array)
{
await JSReference.InvokeVoidAsync("getFloatTimeDomainData", array.JSReference);
await JSReference.InvokeVoidAsync("getFloatTimeDomainData", array);
}

/// <summary>
Expand All @@ -101,7 +101,7 @@ public async Task GetFloatTimeDomainDataAsync(Float32Array array)
/// <param name="array">This parameter is where the time-domain sample data will be copied.</param>
public async Task GetByteTimeDomainDataAsync(Uint8Array array)
{
await JSReference.InvokeVoidAsync("getByteTimeDomainData", array.JSReference);
await JSReference.InvokeVoidAsync("getByteTimeDomainData", array);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ protected AudioBufferSourceNode(IJSRuntime jSRuntime, IJSObjectReference jSRefer
public async Task SetBufferAsync(AudioBuffer? value)
{
IJSObjectReference helper = await webAudioHelperTask.Value;
await helper.InvokeVoidAsync("setAttribute", JSReference, "buffer", value?.JSReference);
await helper.InvokeVoidAsync("setAttribute", JSReference, "buffer", value);
}

/// <summary>
Expand Down
14 changes: 7 additions & 7 deletions src/KristofferStrube.Blazor.WebAudio/AudioNodes/AudioNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ protected AudioNode(IJSRuntime jSRuntime, IJSObjectReference jSReference, Creati
public async Task<AudioNode> ConnectAsync(AudioNode destinationNode, ulong output = 0, ulong input = 0)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
IJSObjectReference jSInstance = await jSReference.InvokeAsync<IJSObjectReference>("connect", destinationNode.JSReference, output, input);
IJSObjectReference jSInstance = await jSReference.InvokeAsync<IJSObjectReference>("connect", destinationNode, output, input);
return await CreateAsync(JSRuntime, jSInstance);
}

Expand All @@ -90,7 +90,7 @@ public async Task<AudioNode> ConnectAsync(AudioNode destinationNode, ulong outpu
public async Task ConnectAsync(AudioParam destinationParam, ulong output = 0)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("connect", destinationParam.JSReference, output);
await jSReference.InvokeVoidAsync("connect", destinationParam, output);
}

/// <summary>
Expand Down Expand Up @@ -126,7 +126,7 @@ public async Task DisconnectAsync(ulong output)
public async Task DisconnectAsync(AudioNode destinationNode)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("disconnect", destinationNode.JSReference);
await jSReference.InvokeVoidAsync("disconnect", destinationNode);
}

/// <summary>
Expand All @@ -143,7 +143,7 @@ public async Task DisconnectAsync(AudioNode destinationNode)
public async Task DisconnectAsync(AudioNode destinationNode, ulong output)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("disconnect", destinationNode.JSReference, output);
await jSReference.InvokeVoidAsync("disconnect", destinationNode, output);
}

/// <summary>
Expand All @@ -162,7 +162,7 @@ public async Task DisconnectAsync(AudioNode destinationNode, ulong output)
public async Task DisconnectAsync(AudioNode destinationNode, ulong output, ulong input)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("disconnect", destinationNode.JSReference, output, input);
await jSReference.InvokeVoidAsync("disconnect", destinationNode, output, input);
}

/// <summary>
Expand All @@ -178,7 +178,7 @@ public async Task DisconnectAsync(AudioNode destinationNode, ulong output, ulong
public async Task DisconnectAsync(AudioParam destinationParam)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("disconnect", destinationParam.JSReference);
await jSReference.InvokeVoidAsync("disconnect", destinationParam);
}

/// <summary>
Expand All @@ -197,7 +197,7 @@ public async Task DisconnectAsync(AudioParam destinationParam)
public async Task DisconnectAsync(AudioParam destinationParam, ulong output)
{
IJSObjectReference jSReference = errorHandlingJSReference ?? JSReference;
await jSReference.InvokeVoidAsync("disconnect", destinationParam.JSReference, output);
await jSReference.InvokeVoidAsync("disconnect", destinationParam, output);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public class BiquadFilterNode : AudioNode, IJSCreatable<BiquadFilterNode>
public static async Task<BiquadFilterNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, BiquadFilterOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructBiquadFilterNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructBiquadFilterNode", context, options);
return new BiquadFilterNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down Expand Up @@ -128,6 +128,6 @@ public async Task<AudioParam> GetGainAsync()
/// <returns></returns>
public async Task GetFrequencyResponseAsync(Float32Array frequencyHz, Float32Array magResponse, Float32Array phaseResponse)
{
await JSReference.InvokeVoidAsync("getFrequencyResponse", frequencyHz.JSReference, magResponse.JSReference, phaseResponse.JSReference);
await JSReference.InvokeVoidAsync("getFrequencyResponse", frequencyHz, magResponse, phaseResponse);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class ConstantSourceNode : AudioScheduledSourceNode, IJSCreatable<Constan
public static async Task<ConstantSourceNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, ConstantSourceOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructConstantSourceNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructConstantSourceNode", context, options);
return new ConstantSourceNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class ConvolverNode : AudioNode, IJSCreatable<ConvolverNode>
public static async Task<ConvolverNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, ConvolverOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructConvolverNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructConvolverNode", context, options);
return new ConvolverNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down Expand Up @@ -66,7 +66,7 @@ protected ConvolverNode(IJSRuntime jSRuntime, IJSObjectReference jSReference, Cr
public async Task SetBufferAsync(AudioBuffer? value)
{
IJSObjectReference helper = await webAudioHelperTask.Value;
await helper.InvokeVoidAsync("setAttribute", JSReference, "buffer", value?.JSReference);
await helper.InvokeVoidAsync("setAttribute", JSReference, "buffer", value);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class DelayNode : AudioNode, IJSCreatable<DelayNode>
public static async Task<DelayNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, DelayOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructDelayNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructDelayNode", context, options);
return new DelayNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class DynamicsCompressorNode : AudioNode, IJSCreatable<DynamicsCompressor
public static async Task<DynamicsCompressorNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, DynamicsCompressorOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructDynamicsCompressorNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructDynamicsCompressorNode", context, options);
return new DynamicsCompressorNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class GainNode : AudioNode, IJSCreatable<GainNode>
public static async Task<GainNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, GainOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructGainNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructGainNode", context, options);
return new GainNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class MediaStreamAudioSourceNode : AudioNode, IJSCreatable<MediaStreamAud
public static async Task<MediaStreamAudioSourceNode> CreateAsync(IJSRuntime jSRuntime, AudioContext context, MediaStreamAudioSourceOptions options)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructMediaStreamAudioSourceNode", context, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructMediaStreamAudioSourceNode", context, new {});
return new MediaStreamAudioSourceNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class OscillatorNode : AudioScheduledSourceNode, IJSCreatable<OscillatorN
public static async Task<OscillatorNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, OscillatorOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructOcillatorNode", context.JSReference, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructOcillatorNode", context, options);
return new OscillatorNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class AudioWorkletNode : AudioNode, IJSCreatable<AudioWorkletNode>
public static async Task<AudioWorkletNode> CreateAsync(IJSRuntime jSRuntime, BaseAudioContext context, string name, AudioWorkletNodeOptions? options = null)
{
IJSObjectReference helper = await jSRuntime.GetHelperAsync();
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructAudioWorkletNode", context.JSReference, name, options);
IJSObjectReference jSInstance = await helper.InvokeAsync<IJSObjectReference>("constructAudioWorkletNode", context, name, options);
return new AudioWorkletNode(jSRuntime, jSInstance, new() { DisposesJSReference = true });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protected MessagePortInProcess(IJSRuntime jSRuntime, IJSInProcessObjectReference

public void PostMessage(object message, ITransferable[]? transfer = null)
{
JSReference.InvokeVoid("postMessage", message, transfer?.Select(e => e.JSReference).ToArray());
JSReference.InvokeVoid("postMessage", message, transfer?.Select(e => e).ToArray());
}

/// <inheritdoc/>
Expand Down
Loading

0 comments on commit 7419873

Please sign in to comment.