Re:Variable duty cycle square waves with the Web Audio API
by gmueckl on Monday April 07, 2025 @01:01PM
(#43613575)
There is ome thing that I don't understand here: wouldn't implementing a custom square wave generator as an AudioWorkletProcessor be a more straightforward approach in this case?
[ Parent
| on HN ]
Re:Variable duty cycle square waves with the Web Audio API
by iamdan on Monday April 07, 2025 @04:04PM
(#43615358)
I think long term I will be moving in that direction. For this initial exploration and experimentation I've been doing with the Web Audio API, the sawtooth oscillator + step function waveshaper node have been sufficient for my use case but I will need investigate whether I can produce a more authentic sound with an AudioWorkletProcessor approach
Re:Variable duty cycle square waves with the Web Audio API
by tashian on Monday April 07, 2025 @04:58PM
(#43615872)
I did a little synth project recently that uses an AudioWorklet processor to morph between single-cycle waveforms, and it worked super well. When I tried to do this with the Web Audio API, the audio would stutter when I moved the controls. Switching to an AudioWorklet thread eliminated the stuttering issue. So, if you need real-time sound shaping controls, you may find that AudioWorklet is a better fit.
Re:Variable duty cycle square waves with the Web Audio API
by tomduncalf on Monday April 07, 2025 @05:28PM
(#43616151)
I’ve got a very simple project demonstrating how to use the newish pure C++ Emscripten Audio Worklet API if you’re interested. It’s a bit neater than the old way you’ll usually come across which also involves writing JS code, but there aren’t many docs online! https://github.com/tomduncalf/emscripten-audio-worklet-examp...
Re:Variable duty cycle square waves with the Web Audio API
by sapiogenesis on Monday April 07, 2025 @02:33PM
(#43614484)
I agree. There’s a bit of mental overhead related to working with audio worklets (you have to load them from an URL or a blob URL, etc.) but for square waves in particular the logic should be fairly straighforward, the process function just needs to output 1 or -1 for any given sample.
Re:Variable duty cycle square waves with the Web Audio API
by Blackthorn on Monday April 07, 2025 @03:31PM
(#43615004)
It needs to do a lot more than that if you want to have something that doesn't alias like crazy. And significantly more if you want it to sound like a game boy.
Re:Variable duty cycle square waves with the Web Audio API
by gmueckl on Monday April 07, 2025 @04:17PM
(#43615509)
Well, the output of the wave shaper approach in the article is exactly the same sharp digital square wave that you'd get from a trivial square wave generator.
But I overlooked the point that the GP mentions that the processor source code must be loaded from a separate JS file. That's some quite annoying overhead.
Re:Variable duty cycle square waves with the Web Audio API
by Blackthorn on Monday April 07, 2025 @04:55PM
(#43615841)
What I was trying to imply was that it's not enough to build a square wave generator to sound like a game boy. Even if that generator in the game boy was a perfect square wave generator, which I'm not sure it is.
The sound created by that generator passes through a fairly complicated filter known as The game boy's speaker. To properly create a game boy sound, you need to find or take an impulse response of that speaker and convolve it with the output of your oscillator.
Re:Variable duty cycle square waves with the Web Audio API
by gmueckl on Monday April 07, 2025 @07:07PM
(#43616886)
Well, and a single recorded impulse response is probably not enough, either, if you want to be super accurate. The directivity pattern is probably awful and shaping the sound as well.
I know all of that. But I just didn't want to get into these details in this thread.
Re:Variable duty cycle square waves with the Web Audio API
by montag on Monday April 07, 2025 @06:18PM
(#43616564)
Yes... IMO, the WebAudio synthesis primitives are not particularly useful. You are so much better off discarding these primitives and rolling your own for an audio project of any significance.