160 points · by aylmao · 2y ago · 70 comments
It's the first time I use WebGPU and it's been a while since I write shaders. Feedback is very welcome!
GitHub: https://github.com/mrkev/webgpu-waveform Examples: https://aykev.dev/webgpu-waveform
I tried to make a web components here: https://lit.dev/playground/#gist=8cf935c3869bf4790653cd6fadf...
But the webgpu-waveform module isn't loading. Looks like you have a hard-coded import of "../../react@18.2.0/index.js", which isn't going to be a portable import even for the React users. For the non-React users, they shouldn't have to install React to get the module loading.
Could you make two entrypoints, one for React, the other for plain JS?
I'll find a way around this, or maybe split the package into a react version and a vanilla version. Thanks for the feedback! I think you're right, a custom element would be nice to have
Not ideal, but works.
Happy to open/share the code, just haven't had time.
PS (yours didn't work for me in Safari)
In the extreme we have the "Spotify" visualisations of vertical bars with gaps in between. I believe this is popular because it looks slightly better than a solid waveform lump with an aliased edge.
To avoid aliasing you need to use more than two pixel colours.
The aliasing in question is of the audio not the pixels, so no more colours does not help.
That's not the cause. The cause is simply that too few of the samples are plotted.
Your min/max solution succeeds by ensuring all significant samples are plotted.
Re Safari: Unfortunately WebGPU support is still WIP. I'll add a notice on the site haha. https://caniuse.com/webgpu
There are two parts: first the C++/wasm that analyses the audio and generates a bitmap that has width 30*duration and not very high. It effectively draws a line from each audio sample to the next, with alpha, but this can be optimised like crazy to a point where the time it takes is unnoticeable.
The second part is the WebGL that the bitmap (aka texture) and renders to the canvas. In principle this could be rotated, spun in 3d, mapped to a sphere or any other crazy GPU manipulations. WebGL isn't too painful and works everywhere I tried including the Android webview in my Bungee Timewarp app.
I've turned on in chrome://flags
>WebGPU Developer Features Enables web applications to access WebGPU features intended only for use during development. – Mac, Windows, Linux, ChromeOS, Android, Fuchsia, Lacros #enable-webgpu-developer-features
Chrome is up to date Version 123.0.6312.123 (Official Build) (64-bit)
RTX2060
If you're using Chrome and getting this error, do share more info.
As for justification, it was justified. Rendering waveforms is O(n) on pixels, and each can be rendered independently. I'm sure one can get creative with caching images at different resolutions to efficiently implement zooming, but otherwise it's noticeably slow.
Every vertical line is a dom element.
I was considering moving to GPU rendering as the dom approach can take a long time to re render on changes.
https://twitter.com/aykev/status/1780000204122460484
It's more code, so I said "later". I also wanted the first example to be easy to read so people wouldn't get lost in the source. But since it's such a common use-case I think I'll add it too. Thanks for the feedback!
I had a WebGPU scope demo using vanila JS here, but it's not connected to AudioContext at all.
https://stackblitz.com/edit/vitejs-vite-cuc9vs
Still, I had to use the old WebGL solution for https://glicol.org since the WebGPU support seems to be pretty slow at the moment. Perhaps you can add some kind of detection and downgrade mechanism?
In Emurse, we render waveform, and also pitch plots of audio recordings using canvas. It's pretty fast, but ours are a bit lower-resolution. The most resource-intensive part of the operation is actually processing the processing the audio.
Perhaps a Safari TP bug? I'd appreciate some browser version info so I can dig deeper.
Pretty current firefox on Linux. Oh well.
I tried to build this using WebGL before WebGPU was publicly available, but WebGL is very tailored to certain workflows and I didn't make much progress. It really felt like I was using the wrong tool for the job. Maybe I'm just too much of a noob... though I did manage to build it with WebGPU after all haha.
https://www.youtube.com/watch?v=qeUAHHPt-LY
Its a waveform shaped like a cow that sounds like a cow haha