First of all, apologies for such an open question. I think this question comes from a lack of understanding of WASM and the nature of JSInterop in .Net.
I just wanted to reach out to anyone who has used the Superpowered Web Audio SDK with Blazor and find out how they have used it as I am struggling with JSInterop, and I feel like I am making things harder by not using a Javascript based framework.
I've setup a Blazor client web assembly project with a C# class, which acts as an interface to a very simple Javascript file to download and decode an audio file:
public class AudioTest
{
[Inject]
IJSRuntime js;
DotNetObjectReference<AudioTest> objRef;
protected override void OnInitialized()
{
objRef = DotNetObjectReference.Create(this);
}
public async void DownloadAndDecode(string url)
{
byte[] buffer = await superpoweredModule.InvokeAsync<byte[]>("downloadAndDecode", url, objRef);
Console.WriteLine(buffer.Length);
}
[JSInvokable]
public void ReceiveDecodedBuffer(string url, byte[] buffer)
{
Console.WriteLine(buffer);
}
}
When the C# method DownloadAndDecode() has been called, it passes a reference of itself to the javascript function downloadAndDecode() to be used a call back when the buffer is ready:
import { SuperpoweredTrackLoader } from '/superpowered/SuperpoweredTrackLoaderModule.js';
export async function downloadAndDecode(url, dotNetRef) {
SuperpoweredTrackLoader.downloadAndDecode(url, async (message) => {
await dotNetRef.invokeMethodAsync('ReceiveDecodedBuffer', url, message.SuperpoweredLoaded.buffer);
});
}
However, this results in a runtime error when converting the base64 buffer. I've tried converting the audio buffer from base64 before sending it (which is too slow). I also tried unmarshalled Javascript calls but they only support synchronous calls. Having to convert the buffer when passing between C#, JS and vice versa is too slow.
I planned on processing the audio buffer on both C# and JS. But it feels like I should just leave JS side and manage the buffers there. Does anyone have a suggestion for this? Or should I just leave it in the Javascript side? Or simply change my design/approach and framework to support the Superpowered library.